这是我原来的 webpack 配置文件,由两种不同的配置组成:
const ExtractTextPlugin = require('extract-text-webpack-plugin')
module.exports = [{
entry: __dirname + '/src/browser/main',
output: {
path: __dirname + '/dist/browser',
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.js', '.ts'] // '' is needed to find modules like "jquery"
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
}
]
}
}, {
entry: ['babel-polyfill', __dirname + '/src/app/browser/app'],
output: {
path: __dirname + '/dist/app/browser',
filename: 'bundle.js'
},
resolve: {
root: ['./node_modules', './src'],
extensions: ['', '.js', '.ts'] // '' is needed to find modules like "jquery"
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css!sass')
}
]
},
plugins: [
new ExtractTextPlugin('bundle.css')
]
}]
即使我所有的导入都被例如 Atom(带有 TypeScript 插件的文本编辑器)接受,但目前只有以下替代工作:
const ExtractTextPlugin = require('extract-text-webpack-plugin')
module.exports = [{
entry: __dirname + '/src/browser/main',
output: {
path: __dirname + '/dist/browser',
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.js', '.ts'] // '' is needed to find modules like "jquery"
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
}
]
}
}, {
entry: ['babel-polyfill', __dirname + '/src/app/browser/app'],
output: {
path: __dirname + '/dist/app/browser',
filename: 'bundle.js'
},
resolve: {
root: ['./node_modules', './src'],
extensions: ['', '.js', '.ts'] // '' is needed to find modules like "jquery"
},
module: {
loaders: [
{
test: /\.ts$/,
loader: require.resolve('ts-loader')
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css!sass')
}
]
},
plugins: [
new ExtractTextPlugin('bundle.css')
]
}]
(注意require.resolve('ts-loader')
)
如您所见,在第一个配置'ts'
中仍然存在(因为它仍然有效),而在第二个配置中我需要将其替换为require.resolve('ts-loader')
. 现在我试图找到这个要求的原因。我记得我也被迫require.resolve
在其他项目中使用,但我从来不知道原因。
产生差异的不显眼的代码行(第二个配置):
import * as dateTime from 'lib/date-time/date-time'
如果我评论它,它会再次使用'ts'
.
在我的文件系统上,路径 lib/date-time/date-time.ts 位于 ./src 的正下方(在 resolve/root 数组中提到)。它不是第一次导入,具体取决于 resolve/root 数组。
带有错误消息的 Webpack 输出:
> webpack
ts-loader: Using typescript@2.1.4 and /.../src/app/browser/tsconfig.json
ts-loader: Using typescript@2.1.4 and /.../src/browser/tsconfig.json
Hash: 5058e7029f1c0243a269161aa4ddda242a3d33a0
Version: webpack 1.14.0
Child
Hash: 5058e7029f1c0243a269
Version: webpack 1.14.0
Time: 3165ms
Asset Size Chunks Chunk Names
bundle.js 283 kB 0 [emitted] main
+ 4 hidden modules
Child
Hash: 161aa4ddda242a3d33a0
Version: webpack 1.14.0
Time: 3670ms
Asset Size Chunks Chunk Names
bundle.js 617 kB 0 [emitted] main
bundle.css 25 kB 0 [emitted] main
[0] multi main 40 bytes {0} [built]
+ 329 hidden modules
ERROR in Cannot find module './node_modules/ts-loader/index.js'
@ ./src/records/departure/departure.ts 2:15-49
Child extract-text-webpack-plugin:
+ 2 hidden modules
Child extract-text-webpack-plugin:
+ 2 hidden modules
Child extract-text-webpack-plugin:
+ 2 hidden modules
Child extract-text-webpack-plugin:
+ 2 hidden modules