【发布时间】:2018-02-17 23:04:25
【问题描述】:
我正在处理一个项目 - 用 Typescript (2.4) 编写,与 Webpack (3.5) 捆绑在一起,以及一些节点依赖项。它有一个客户端和服务器部分,每个部分都在它自己的子文件夹中。我很快意识到这些共享大量代码,并添加了另一个顶级“共享”文件夹 - 在 tsconfig.json 文件中,我添加了:
...
"include": [
"*.ts",
"../shared/*.ts",
]
...
所有这些工作都很好 - 但很快这些共享文件开始拥有自己的依赖项。我认为由于共享文件并不是真正建立在自己的 - 依赖项的所有权将属于服务器和客户端目录。像这样的:
project
| server
| | node_modules
| | | @types
| | | | some-npm-library
| | | some-npm-library
| | main.ts
| | SomeServerClass.ts (inherits ../shared/BaseClass)
| | tsconfig.json
| | webpack.config.js
| client
| | node_modules
| | | @types
| | | | some-npm-library
| | | some-npm-library
| | main.ts
| | SomeClientClass.ts (inherits ../shared/BaseClass)
| | tsconfig.json
| | webpack.config.js
| shared
| | BaseClass.ts (Has an dependency on some npm thing)
| | otherstuff.ts
当我运行tsc - 它编译没有错误。但是 webpack 似乎永远找不到 node_modules 依赖项,Error: Can't resolve 'some-npm-library'。
客户端tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"baseUrl": "./",
"sourceMap": true,
"outDir": "./build",
"typeRoots": ["./node_modules/@types"],
"types": ["some-npm-library"],
"lib": [ "es6", "dom", "es2017.object", "es2016.array.include" ]
},
"include": [
"*.ts",
"..shared/**.ts"
]
}
客户端 webpack.config.js
const path = require('path');
module.exports = {
entry: "./main.ts",
target: "web",
output: {
filename: "app.js",
path: __dirname + "/bin"
},
watch: true,
devtool: "source-map",
resolve: {
extensions: [".ts", ".js", ".json"],
},
module: {
rules: [
{ test: /\.ts$/, loader: "ts-loader" },
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
}
};
【问题讨论】:
标签: typescript webpack dependencies node-modules resolve