AngularJS + Webpack 适用于开发模式但不适用于生产模式
AngularJS + Webpack works on development mode but not on production mode
我正在 运行ning webpack-dev-server 开发中,它可以毫无问题地为我的项目服务。如果我在 webpack 配置中使用 mode: "development"
构建项目,则完全没有任何问题。
但是当我 运行 带有 mode: "production"
的 webpack 时,它不起作用。
这是我的 webpack 配置(通用、开发和生产):
// webpack.common.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');
function resolvePath(dir) {
return path.join(__dirname, '../', dir);
}
const env = process.env.NODE_ENV || 'development';
module.exports = {
target: "web",
entry: {
vendors: './src/assets/vendors.js',
bundle: './src/bin/www'
},
plugins: [
new HtmlWebpackPlugin({
filename: './index.html',
template: './src/index.html'
}),
new CleanWebpackPlugin()
],
output: {
path: resolvePath('dist'),
filename: '[name].js'
},
externals: {
jquery: "jQuery",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(html)$/,
exclude: /node_modules/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src']
}
}
},
{
test: /\.(woff(2)?|ttf|eot|svg|jpg|jpeg|png|jpg|gif)$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'media/[name].[ext]',
},
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
'style-loader',
'css-loader'
]
},
],
},
};
// webpack.dev.js
const merge = require('webpack-merge');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
contentBase: './dist'
}
});
// webpack.prod.js
const merge = require('webpack-merge');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'production'
});
构建工作正常,但是当我尝试 运行 浏览器中的代码时,使用 http-server,我可能会遇到下一个错误 - 仅当构建用于生产时,而不是开发! (我正在使用 AngularJS 1.7.2):
at angular.js:99
at angular.js:5027
at q (angular.js:387)
at g (angular.js:4987)
at hb (angular.js:4904)
at c (angular.js:1936)
at Vc (angular.js:1957)
at ye (angular.js:1842)
at HTMLDocument.<anonymous> (angular.js:35431)
at l (jquery.min.js:2)
我从 webpack 收到警告说我的包太大了。也许这就是问题所在?
您遇到的问题是缩小过程正在缩小 angular 对象的 DI 名称。当发生这种情况并且 angularjs 尝试加载注入的对象时,它无法追踪依赖项,因为名称已被破坏。
为了避免这种情况,您应该使用 babel-plugin-angularjs-annotate 插件。此插件可确保您的 ngInject
语句不会被破坏。
我正在 运行ning webpack-dev-server 开发中,它可以毫无问题地为我的项目服务。如果我在 webpack 配置中使用 mode: "development"
构建项目,则完全没有任何问题。
但是当我 运行 带有 mode: "production"
的 webpack 时,它不起作用。
这是我的 webpack 配置(通用、开发和生产):
// webpack.common.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');
function resolvePath(dir) {
return path.join(__dirname, '../', dir);
}
const env = process.env.NODE_ENV || 'development';
module.exports = {
target: "web",
entry: {
vendors: './src/assets/vendors.js',
bundle: './src/bin/www'
},
plugins: [
new HtmlWebpackPlugin({
filename: './index.html',
template: './src/index.html'
}),
new CleanWebpackPlugin()
],
output: {
path: resolvePath('dist'),
filename: '[name].js'
},
externals: {
jquery: "jQuery",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(html)$/,
exclude: /node_modules/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src']
}
}
},
{
test: /\.(woff(2)?|ttf|eot|svg|jpg|jpeg|png|jpg|gif)$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'media/[name].[ext]',
},
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
'style-loader',
'css-loader'
]
},
],
},
};
// webpack.dev.js
const merge = require('webpack-merge');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
contentBase: './dist'
}
});
// webpack.prod.js
const merge = require('webpack-merge');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'production'
});
构建工作正常,但是当我尝试 运行 浏览器中的代码时,使用 http-server,我可能会遇到下一个错误 - 仅当构建用于生产时,而不是开发! (我正在使用 AngularJS 1.7.2):
at angular.js:99 at angular.js:5027 at q (angular.js:387) at g (angular.js:4987) at hb (angular.js:4904) at c (angular.js:1936) at Vc (angular.js:1957) at ye (angular.js:1842) at HTMLDocument.<anonymous> (angular.js:35431) at l (jquery.min.js:2)
我从 webpack 收到警告说我的包太大了。也许这就是问题所在?
您遇到的问题是缩小过程正在缩小 angular 对象的 DI 名称。当发生这种情况并且 angularjs 尝试加载注入的对象时,它无法追踪依赖项,因为名称已被破坏。
为了避免这种情况,您应该使用 babel-plugin-angularjs-annotate 插件。此插件可确保您的 ngInject
语句不会被破坏。