简体   繁体   English

使用 webpack 配置 Typescript 项目

[英]Configuring Typescript project with webpack

Hello I would like to configure Typescript with composite config and webpack (the code was working well when using a single tsconfig.json ).您好,我想使用复合配置和 webpack 配置 Typescript(使用单个tsconfig.json时代码运行良好)。 I precise that I am TypeScript newbie and rusted in javascript.我准确地说我是 TypeScript 新手并且在 javascript 中生锈了。 I have difficulties to obtain my outputs json files.我很难获得我的输出 json 文件。

EDIT : I added a bit more configuration and changed the directory structure for something better I believe编辑:我添加了更多配置并更改了目录结构以获得更好的东西,我相信

My project structure:我的项目结构:

    tsconfig.base.json
    tsconfig.json
    tsconfig.base.json
    webpack.config.json
    |--model
       |--tsconfig.json
       |--src
         |--foo
            |--x.ts
            |--...
         |--bar
            |--y.ts
            |--...
            |--...
   |--control
       |--tsconfig.json
       |--src
            |--eventHandlerA.ts
            |--eventHandlerB.ts
            |--...
   |--app1
       |--tsconfig.json
       |--src
            |--app1.ts
   |--app1
       |--tsconfig.json
       |--src
            |--app2.ts

My desired output are 2 files "app1.js" and "app2.js".我想要的 output 是 2 个文件“app1.js”和“app2.js”。 Both of them rely on a "controller" package which relies on a "model" package.它们都依赖于“控制器”package,它依赖于“模型”package。 I would like to run at least the following commands:我想至少运行以下命令:

  • npm run build #builds the javascript (can be reused for unit testing and package) npm run build #builds javascript(可重复用于单元测试和打包)
  • npm run package #builds and copy the output app1.js and app2.js in a delivery directory. npm 运行 package #builds 并将 output app1.js 和 app2.js 复制到交付目录中。 I must be able to include only those js files to run my application in a webpage.我必须能够仅包含那些 js 文件才能在网页中运行我的应用程序。

Content of ./tsconfig.js : ./tsconfig.js的内容:

{
    "references": [
        {
            "path": "app1"
        },
        {
            "path": "app2"
        }
    ],
    "include": []
}

Content of ./tsconfig.base.js : ./tsconfig.base.js的内容:

{
    "compileOnSave": true,
    "compilerOptions": {
        "composite": true
        "declaration": true,
        "moduleResolution": "node",
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react",
    }
}

Content of /tsconfig.js : /tsconfig.js的内容:

{
   "files": [],
    "include": [],
    "references": [
        {
            "path": "app1"
        },

        {
            "path": "app2"
        }
    ]
}

Content of ./webpack.conf.js ./webpack.conf.js的内容

module.exports = {
    mode: "production",
    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".ts", ".tsx"],
    },

    module: {
        rules: [
            {
                test: /\.ts(x?)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "ts-loader"
                    }
                ]
            },
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            {
                enforce: "pre",
                test: /\.js$/,
                loader: "source-map-loader"
            }
        ]
    },

    // When importing a module whose path matches one of the following, just
    // assume a corresponding global variable exists and use that instead.
    // This is important because it allows us to avoid bundling all of our
    // dependencies, which allows browsers to cache those libraries between builds.
    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    }
};

content of package.json package.json的内容

{
  "name": "abc",
  "version": "1.0.0-SNAPSHOT"
  "scripts": {
    "test": "npm run build && jest", 
    "build": "tsc -b",
    "clean": "tsc -b --clean",
    "rebuild": "npm run clean && npm run build"
  },
  "keywords": [],
  "author": "toto",
  "license": "ISC",
  "devDependencies": {
    ...
  },
  "dependencies": {
    ...
  }
}

If I run npm run build, I have import failures.如果我运行 npm 运行构建,我会遇到导入失败。 To analyze it I ran / tsc -b control /:为了分析它,我运行了 / tsc -b control /:

error TS2307: Cannot find module 'x' or its corresponding type declarations.

4 import * as x from 'x'

I tried to modify my import in several ways but I always have a failure on the import.我尝试以多种方式修改我的导入,但导入时总是失败。 How to import a class (which is exported) from a composite "project" to another using absolute path?如何使用绝对路径将复合“项目”中的 class(已导出)导入另一个?

Thank you for helping感谢您的帮助

To use absolute paths try to configure your tsconfig.json file with baseUrl and paths :要使用绝对路径,请尝试使用baseUrlpaths配置您的tsconfig.json文件:

{
    "compileOnSave": true,
    "compilerOptions": {
        "declaration": true,
        "moduleResolution": "node",
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react",
        "baseUrl": "../../", /// <--- baseUrl gives a path from the tsconfig to the root
        "paths": {
            "src/*": [ "../../src/*" ] /// <--- paths specify src/* is inside the src folder, which is ../../ relative to the tsconfig
        }
    },
    "include": [
        "./**/*.ts",
        "./**/*.tsx"
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 webpack 编译 typescript 项目 - Compiling a typescript project with webpack 最大听众问题:typescript 项目和无服务器 webpack - max listeners problem : typescript project and serverless webpack Webpack 无法解析 JS 模块(在 Typescript 项目中) - Webpack unable to resolve JS module (in Typescript project) 使用最新的设置种子项目:Angular,Node / Express,Webpack,Typescript - Setting Seed Project with Latest: Angular, Node/Express, Webpack, Typescript 在 electron 项目中使用带有 typescript 而没有 webpack 的 monaco-editor - Using monaco-editor with typescript without webpack in an electron project 在Visual Studio代码中使用TypeScript和Webpack调试Node.js项目的正确方法 - The right way to debug nodejs project with typescript and webpack in visual studio code 如何使这个Typescript Webpack项目正确编译? - How do I get this Typescript Webpack project to compile without error? 语法错误:带有 Typescript 和 Webpack 的 Nodejs 项目中的意外令牌“导出” - SyntaxError: Unexpected token 'export' in Nodejs project with Typescript and Webpack Webpack 4配置入口点和输出(每个项目文件夹中生成的dist文件夹) - Webpack 4 configuring entry points and outputs (dist folder generated in each project folder) Node.js + Webpack + TypeScript:使用源文件访问项目路径(不是使用项目) - Node.js + Webpack + TypeScript: access to path of project with source files (not usage project)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM