Angular 角度2+;用于生产部署的网页包
我想在我的生产部署中使用webpack缩小我所有的js和CSS&当我开始在开发模式下工作时,它应该变得漂亮,我是webpack的新手&不确定我是如何做到这一点的,下面是我的webpack配置,我如何修改它以在开发和生产环境中单独工作Angular 角度2+;用于生产部署的网页包,angular,webpack,Angular,Webpack,我想在我的生产部署中使用webpack缩小我所有的js和CSS&当我开始在开发模式下工作时,它应该变得漂亮,我是webpack的新手&不确定我是如何做到这一点的,下面是我的webpack配置,我如何修改它以在开发和生产环境中单独工作 var webpack = require('webpack'); var path = require('path'); // Webpack Config var webpackConfig = { entry: { 'polyfills': '.
var webpack = require('webpack');
var path = require('path');
// Webpack Config
var webpackConfig = {
entry: {
'polyfills': './src/polyfills.browser.ts',
'vendor': './src/vendor.browser.ts',
'main': './src/main.browser.ts',
},
output: {
path: './dist',
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(true),
new webpack.optimize.CommonsChunkPlugin({ name: ['main', 'vendor', 'polyfills'], minChunks: Infinity }),
],
module: {
loaders: [
// .ts files for TypeScript
{ test: /\.ts$/, loaders: ['awesome-typescript-loader', 'angular2-template-loader'] },
{ test: /\.css$/, loaders: ['to-string-loader', 'css-loader'] },
{ test: /\.html$/, loader: 'raw-loader' },
{ test: /\.json$/, loader: 'json-loader' },
]
}
};
// Our Webpack Defaults
var defaultConfig = {
devtool: 'cheap-module-source-map',
cache: true,
debug: true,
output: {
filename: '[name].bundle.js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js'
},
module: {
preLoaders: [
{
test: /\.js$/,
loader: 'source-map-loader',
exclude: [
// these packages have problems with their sourcemaps
path.join(__dirname, 'node_modules', 'rxjs'),
path.join(__dirname, 'node_modules', '@angular2-material'),
path.join(__dirname, 'node_modules', '@angular'),
]
}
],
noParse: [
path.join(__dirname, 'node_modules', 'zone.js', 'dist'),
path.join(__dirname, 'node_modules', 'angular2', 'bundles')
]
},
resolve: {
root: [ path.join(__dirname, 'src') ],
extensions: ['', '.ts', '.js', '.json']
},
devServer: {
historyApiFallback: true,
watchOptions: { aggregateTimeout: 300, poll: 1000 }
},
node: {
global: 1,
crypto: 'empty',
module: 0,
Buffer: 0,
clearImmediate: 0,
setImmediate: 0
},
}
var webpackMerge = require('webpack-merge');
module.exports = webpackMerge(defaultConfig, webpackConfig);
因此,当我启用Angular 2
enableProduction
webpack时,也应该调用生产代码并缩小JS&CSS,因为现在我是如何缩小它以用于生产的,这是一项手动工作,但至少现在是这样。在config中添加了运行watcher时要放入的minimize
参数,即webpack--watch--minimize
,下面是代码:
...
var path = require('path');
var minimize = process.argv.indexOf('--minimize') !== -1;
...
...
...
if (minimize) {
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin());
}
var webpackMerge = require('webpack-merge');
module.exports = webpackMerge(defaultConfig, webpackConfig);
您可以创建webpack.config.production.js,然后在主webpack.config.js中导入生产配置,如下所示: var environment=process.env.NODE\u env?process.env.NODE_env:“本地”; var envspectconfig=require('./webpack.config.'+environment) 其中,通过gulp、构建服务器或其他方式将环境设置为环境变量 因此,原则上,所有的公共配置都将进入webpack.config.js,而生产内容将进入webpack.config.production.js 此处的配置示例(首先是webpack.config.js): 然后在webpack.config.production.js中
var webpack = require('webpack');
var path = require( 'path' );
module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
minimize: true,
mangle: false // Due to https://github.com/angular/angular/issues/6678
})
]
};
通过这种方式,您可以进行任意数量的特定于环境的配置,只需将环境变量设置为与配置文件名匹配。使用webpack和webpack.DefinePlugin设置不同的环境模式
最佳做法是为每个环境提供不同的文件,以便易于维护,并根据环境的不同进行不同的配置。我已经创建了一个angular2网页包种子,在配置文件夹中可以找到网页包文件
- 如果该值是一个字符串,它将用作代码片段
- 如果值不是字符串,它将被字符串化(包括函数)
- 如果值是对象,则所有关键点的定义方式相同
- 如果将typeof作为键的前缀,则它仅为typeof调用定义
为什么不将angular cli与Web包一起使用?有没有办法读取angular应用程序中定义的常量?如果是这样,这是如何工作的?通过将属性嵌套在
process.env
键下,可以:
var webpack = require('webpack');
var path = require( 'path' );
module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
minimize: true,
mangle: false // Due to https://github.com/angular/angular/issues/6678
})
]
};
new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(true),
VERSION: JSON.stringify("5fa3b9"),
BROWSER_SUPPORTS_HTML5: true,
TWO: "1+1",
"typeof window": JSON.stringify("object")
})
if (!PRODUCTION)
console.log('Debug info')
if (PRODUCTION)
console.log('Production log')