【发布时间】:2017-03-02 00:46:58
【问题描述】:
我使用 Webpack 2.x 来编译我的 Angular 2 应用程序。对于生产代码,我结合使用ngc 和@ngtools/webpack 的AotPlugin 进行编译。对于测试,我使用awesome-typescript-loader。我的代码的生产版本工作得很好。但是,当我执行npm test 时,发生了一些非常奇怪的事情......awesome-typescript-loader 抱怨它无法编译代码,但 Karma 无论如何都会运行测试并且它们都通过了。
bash-3.2$ npm test
> btc2017@1.0.0 test /Users/tomb/Projects/brandontom.com/wp-content/themes/btc2017
> NODE_ENV=test node ./node_modules/.bin/karma start --single-run=true
webpack: wait until bundle finished:
[at-loader] Using typescript@2.2.1 from typescript and "tsconfig.json" from /Users/tomb/Projects/brandontom.com/wp-content/themes/b
tc2017/tsconfig.json.
[at-loader] Checking started in a separate process...
[at-loader] Ok, 1.054 sec.
ERROR in ./ngfactory/src/app/app.module.ngfactory.ts
Module parse failed: /Users/tomb/Projects/brandontom.com/wp-content/themes/btc2017/ngfactory/src/app/app.module.ngfactory.ts Unexpe
cted token (65:56)
You may need an appropriate loader to handle this file type.
| import * as import55 from '@angular/router/src/router_config_loader';
| import * as import56 from '@angular/router/src/router_state';
| class AppModuleInjector extends import0.NgModuleInjector<import1.AppModule> {
| _CommonModule_0:import2.CommonModule;
| _ApplicationModule_1:import3.ApplicationModule;
@ ./bootstrap/main.aot.ts 1:0-78
webpack: Failed to compile.
Chrome 56.0.2924 (Mac OS X 10.12.3): Executed 4 of 4 SUCCESS (1.41 secs / 0.758 secs)
这是一个巧妙的技巧,但我宁愿跳过尝试解析该文件,尤其是因为该文件已生成。
我的第一个想法是忽略生成文件的目录。它在名为ngfactory 的文件夹中构建。这是我在webpack.config.js 中添加以下规则的规则:
{
test: /\.ts$/,
loaders: ['awesome-typescript-loader', 'angular2-template-loader'],
exclude: [/(node_modules|bootstrap|prebuild|ngfactory)/]
}
但是awesome-typescript-loader 一直在尝试解析这个文件,尽管exclude。接下来,我认为可能是 Karma 将其引入,因此我在 karma.conf.js 中添加了排除规则。
module.exports = function (config) {
var _config = {
basePath: '',
frameworks: ['jasmine'],
files: [
{ pattern: './karma-shim.js', watched: false }
],
exclude: [
'bootstrap',
'dist',
'ngfactory', // <--- here's where that file lives
'node_modules',
'prebuild'
],
...
...
当我删除工件文件夹(prebuild 和 ngfactory)时,我意识到引导文件(位于名为 bootstrap 的目录中)正在被解析并且该文件正在尝试导入 app.module.ngfactory.ts 我添加bootstrap 到各种exclude 属性(包括我的tsconfig.json)。尽管如此,没有什么能阻止这个问题的出现。如果有人见过这样的事情,我很想知道你是如何解决这个问题的。
这是我完整的webpack.config.js:
var neat = require('node-neat')
var path = require('path')
var webpack = require('webpack')
var AotPlugin = require('@ngtools/webpack').AotPlugin
var sassPaths = neat.includePaths.map(function (path) {
return 'includePaths[]=' + path
}).join('&')
module.exports = (function (nodeEnv) {
var config = {
entry: {
globals: [ 'core-js/client/shim.min', 'reflect-metadata', 'zone.js' ],
main: path.resolve(__dirname, 'bootstrap') + '/main.aot.ts'
},
devServer: {
inline: true
},
module: {
rules: [
{ test: /\.html$/, loader: 'raw-loader' },
{ test: /\.scss$/, loader: 'raw-loader!css-loader!sass-loader?' + sassPaths, exclude: /node_modules/ }
]
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: ['.js', '.ts']
},
plugins: [
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
__dirname
),
new webpack.DefinePlugin({
process: { env: { NODE_ENV: JSON.stringify(nodeEnv) } }
})
]
}
if (nodeEnv !== 'test') {
config.module.rules.push({
test: /\.ts$/,
loader: '@ngtools/webpack'
})
config.plugins.push(new AotPlugin({
tsConfigPath: './tsconfig.json'
}))
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
mangle: { screw_ie8: true, keep_fnames: true },
compress: { screw_ie8: true, warnings: false },
comments: false
}))
} else {
config.module.rules.push({
test: /\.ts$/,
loaders: ['awesome-typescript-loader', 'angular2-template-loader'],
exclude: [/(node_modules|bootstrap|prebuild|ngfactory)/]
})
}
return config
})(process.env.NODE_ENV)
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noEmitHelpers": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"outDir": "./prebuild",
"lib": [
"es2015",
"dom"
],
"types": [
"jasmine",
"node"
]
},
"compileOnSave": false,
"buildOnSave": false,
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"angularCompilerOptions": {
"genDir": "./ngfactory",
"entryModule": "src/app/app.module#AppModule"
}
}
为了更好的衡量,这也是我的karma.conf.js:
var webpackConfig = require('./webpack.config')
module.exports = function (config) {
var _config = {
basePath: '',
frameworks: ['jasmine'],
files: [
{ pattern: './karma-shim.js', watched: false }
],
exclude: [
'bootstrap',
'dist',
'ngfactory',
'node_modules',
'prebuild'
],
preprocessors: {
'./karma-shim.js': ['webpack']
},
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only'
},
webpackServer: {
noInfo: true
},
progress: ['progress'],
port: 9876,
color: true,
logLevel: config.LOG_ERROR,
browsers: ['Chrome']
}
config.set(_config)
}
【问题讨论】:
标签: angular typescript webpack karma-jasmine angular2-aot