如果我想要构建一个使用TypeScript编写的Angular2项目,但是我不想将所有内容捆绑成一个包。我希望将供应商捆绑到vendor.bundle.js中,并将其他内容保留在与项目结构相同的形状中。
基本结构:
我们想要的结构是:
在index.html文件中,我们进行了以下操作:
基本结构:
app/
main-module/
main-module.ts
main-module.html
main-module.css
page-module/
page-module.ts
page-module.html
page-module.css
main.ts
vendor.ts
我们想要的结构是:
app/
main-module/
main-module.js
main-module.html
main-module.css
page-module/
page-module.js
page-module.html
page-module.css
main.js
vendor.bundle.js
在index.html文件中,我们进行了以下操作:
<script src="vendor.bundle.js"></script>
<script src="main-module.js"></script>
<script src="page-module.js"></script>
我的webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
context: __dirname + '/app',
entry: {
'vendor': './vendor',
'main': './main'
},
stats: {
colors: true,
reasons: true
},
output: {
path: path.join(__dirname, '/js'),
publicPath: 'js/',
filename: '[name].bundle.js',
sourceMapFilename: '[name].[chunkhash].bundle.map',
chunkFilename: '[id].chunk.js'
},
resolve: {
extensions: ['', '.ts', '.js'],
modulesDirectories: ['node_modules']
},
module: {
loaders: [
{
test: /\.ts$/, loader: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loaders: ['to-string-loader', 'css-loader']
},
{
test: /\.html$/,
loader: 'raw-loader'
// exclude: [helpers.root('src/index.html')]
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['vendor']
})
]
};