[英]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:我想至少运行以下命令:
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
:要使用绝对路径,请尝试使用baseUrl
和paths
配置您的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.