最近我开始研究简单的 HTML5 canvas 游戏 + angular2 用于路由、显示高分等。由于我没有接触 angular2 核心,我不想重新编译所有内容并将它们捆绑到一个大文件中。我更希望将 angular2 核心 + http + 路由器和我的应用程序放在单独的文件中。
现在我得到了将近 5MB 的包,甚至更大的 .map
在浏览器中加载时暂停了我的电脑一段时间(鼠标卡住,音乐停止播放了一会儿,就像半sec) 这很烦人(我想,这是因为包的大小)。我可以使用多个入口点来完成这项工作吗?
这是我的 webpack 配置:
module.exports = {
devtool: 'source-map',
entry: './src/app/bootstrap',
output: {
path: __dirname + '/dist', publicPath: 'dist/', filename: 'bundle.js'
},
resolve: {
extensions: ['', '.js', '.ts']
},
module: {
loaders: [
{
test: /\.ts/, loaders: ['ts-loader'], exclude: /node_modules/
}
]
}
};
我正在使用 angular 2.0.0-beta.0 和 webpack 1.12.2
最佳答案
更新:考虑 cache option .
您的问题的答案是:是的。您可以使用多个入口点。
这是我正在使用的示例。
var path = require('path');
var webpack = require('webpack');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
var ProvidePlugin = webpack.ProvidePlugin;
//var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
module.exports = {
devtool: 'source-map',
debug: true, // set false in production
cache: true,
entry: {
'vendor': './src/vendor.ts', // third party dependencies
'app': './src/app/app.ts' // our app
},
output: {
path: root('dist'),
filename: '[name].js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js',
pathinfo: true
},
resolve: {
extensions: ['', '.ts', '.js', '.json', '.css', '.html']
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader',
query: {
'ignoreDiagnostics': [
2403, // 2403 -> Subsequent variable declarations
2300, // 2300 -> Duplicate identifier
2374, // 2374 -> Duplicate number index signature
2375 // 2375 -> Duplicate string index signature
]
},
exclude: [/\.(spec|e2e)\.ts$/, /node_modules\/(?!(ng2-.+))/]
},
// Support for *.json files.
{test: /\.json$/, loader: 'json-loader'},
// support for .css
{test: /\.css$/, loaders: ['style', 'css']},
],
noParse: [/angular2-polyfills/]
},
plugins: [
new CommonsChunkPlugin({name: 'vendor', filename: 'vendor.js', minChunks: Infinity}),
new CommonsChunkPlugin({name: 'common', filename: 'common.js', minChunks: 2, chunks: ['app', 'vendor']}),
new ProvidePlugin({
$: "jquery",
jQuery: "jquery",
Cookies: "js-cookie"
})
// new UglifyJsPlugin() // use for production
],
// Other module loader config
tslint: {
emitErrors: true,
failOnHint: false
},
// our Webpack Development Server config
devServer: {
contentBase: 'src',
publicPath: '/__build__',
colors: true,
progress: true,
port: 3000,
displayCached: true,
displayErrorDetails: true,
inline: true
}
};
// Helper functions
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}
function rootNode(args) {
args = Array.prototype.slice.call(arguments, 0);
return root.apply(path, ['node_modules'].concat(args));
}
这个配置比你的稍微复杂一点。它来自这个Angular 2 / Bootstrap 4 / OAuth2 Github project使用 Webpack。
这会将 Angular 的东西(以及 RxJS 和其他任何东西)放在一个“vendor ”包中,但是你必须制作一个 vendor.ts
文件来调用你需要的东西。
vendor .ts:
require('./css/bootstrap.css');
require('./css/main.css');
import 'angular2/bundles/angular2-polyfills';
import 'angular2/platform/browser';
import 'angular2/core';
import 'angular2/http';
import 'angular2/router';
然后将以下代码添加到您的 index.html
文件的底部。
<script src="dist/common.js"></script>
<script src="dist/vendor.js"></script>
<script src="dist/app.js"></script>
您可能需要调整一些路径才能正确连接,具体取决于您的 index.html 文件相对于其他文件夹的位置。
但是,我认为,这将为您完成。查看 Github 项目以查看其运行情况。
关于webpack - 我如何设置 webpack 不每次都重新编译 Angular ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556425/