我构建了一个 Angular 2 应用程序并将其与 webpack 捆绑在一起。目前,我的应用程序仍然很小,但 webpack 任务已经花费了大约 10 秒。是否可以优化我的 webpack 配置或 TypeSript 编译选项以提高编译和打包持续时间?
这是我使用的 webpack 配置:
var webpack = require('webpack');
var LiveReloadPlugin = require('webpack-livereload-plugin');
module.exports = {
entry: __dirname + '/assets/app/app.ts',
output: {
filename: 'myApp.bundle.js',
path: __dirname + '/build/'
},
// Turn on sourcemaps
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.js']
},
plugins: [
new LiveReloadPlugin({
appendScriptTag: true
}),
// Fixes angular 2 warning
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
__dirname
)
],
module: {
rules: [{
enforce: 'pre',
test: /\.js$/,
loader: "source-map-loader"
},
{
enforce: 'pre',
test: /\.tsx?$/,
use: "ts-loader"
}
]
}
}
和 tsconfig :
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"pretty": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": false,
"removeComments": true,
"skipLibCheck": true,
"strictNullChecks": false,
"baseUrl": "./src",
"typeRoots": ["node_modules/@types"],
"types": [
"core-js",
"systemjs"
],
"outDir": "./build"
},
"exclude": [
"node_modules"
]
}
更新 (请参阅我对固定 webpack.config 的回答)
我通过在单独的构建中编译 Angular 来尝试@jpwiddy 建议的 DLL webpack 插件,以便在开发过程中只需要重新构建应用程序代码并获得大量编译时间。
但是查看了输出的js,文件大小还是一样的,里面还是有角码的。
这是 Angular 资源的新 webpack 配置文件:
var webpack = require('webpack');
module.exports = {
entry: {
angular:[
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/core',
'@angular/common',
'@angular/compiler',
'@angular/http',
'@angular/router',
'@angular/forms'
]
},
output: {
filename: 'ng2.dll.js',
path: __dirname + '/build/',
library: 'ng2'
},
plugins: [
// Fixes angular 2 warning
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
__dirname
),
new webpack.DllPlugin({
name: 'ng2',
path: __dirname + '/build/ng2.json'
})
]
}
以及应用程序的更新 webpack 配置:
var webpack = require('webpack');
var LiveReloadPlugin = require('webpack-livereload-plugin');
module.exports = {
entry: __dirname + '/assets/app/app.ts',
output: {
filename: 'myApp.bundle.js',
path: __dirname + '/build/'
},
// Turn on sourcemaps
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.js']
},
plugins: [
new LiveReloadPlugin({
appendScriptTag: true
}),
// Fixes angular 2 warning
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
__dirname
),
new webpack.DllReferencePlugin({
context: __dirname + '/build/',
manifest: require(__dirname + '/build/ng2.json')
})
],
module: {
rules: [{
enforce: 'pre',
test: /\.js$/,
loader: "source-map-loader"
},
{
enforce: 'pre',
test: /\.tsx?$/,
use: "ts-loader"
}
]
}
}
这是我在我的应用程序 JS 输出中找到的角度代码之一:
_TsEmitterVisitor.prototype.visitBuiltintType = function (type, ctx) {
var typeStr;
switch (type.name) {
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].Bool:
typeStr = 'boolean';
break;
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].Dynamic:
typeStr = 'any';
break;
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].Function:
typeStr = 'Function';
break;
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].Number:
typeStr = 'number';
break;
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].Int:
typeStr = 'number';
break;
case __WEBPACK_IMPORTED_MODULE_2__output_ast__["R" /* BuiltinTypeName */].String:
typeStr = 'string';
break;
default:
throw new Error("Unsupported builtin type " + type.name);
}
ctx.print(typeStr);
return null;
};
我是否错过了新配置中的某些内容以防止 webpack 在输出中包含角度源?
谢谢