Javascript AngularJS+;如何散列模板htmls的网页包
在使用gulp之前,我已经将AngularJS应用程序迁移到webpack。在gulp版本中,我使用rev-plugin对所有文件(css、js和html)进行了rev-plugin,但是在webpack模式下,我找不到向html模板添加哈希的方法,这会导致浏览器为旧html文件服务时出现问题。怎么能修好呢?下面是由网页包配置文件Javascript AngularJS+;如何散列模板htmls的网页包,javascript,angularjs,webpack,Javascript,Angularjs,Webpack,在使用gulp之前,我已经将AngularJS应用程序迁移到webpack。在gulp版本中,我使用rev-plugin对所有文件(css、js和html)进行了rev-plugin,但是在webpack模式下,我找不到向html模板添加哈希的方法,这会导致浏览器为旧html文件服务时出现问题。怎么能修好呢?下面是由网页包配置文件 const webpack = require('webpack'); const path = require('path'); const HtmlWebpack
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const RemoteServer = process.env.REMOTE_SERVER;
const appEnv = process.env.NODE_ENV || 'development';
const isProduction = appEnv === 'production';
const patterns = require('../server/src/main/resources/regex.json');
const appPath = path.join(__dirname, 'app');
const buildPath = path.join(__dirname, 'artifacts');
const config = {
entry: [path.join(appPath, 'index.js')],
output: {
path: buildPath,
filename: '[name].[hash].js',
chunkFilename: '[name].[hash].js'
},
resolve: {
modules: ['node_modules', appPath],
alias: {
'ui-select-css': path.resolve('./node_modules/ui-select/dist/select.css'),
fonts: path.resolve(__dirname, 'assets/fonts')
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
emitWarning: true,
quiet: true
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'resolve-url-loader'
]
},
{
test: /\.less$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader', options: {
url: false,
sourceMap: true
}
}, {
loader: 'less-loader', options: {
relativeUrls: false,
sourceMap: true
}
}]
},
{
test: /\.(jpe?g|png|gif)(\?.*)?$/i,
use: [{
loader: 'file-loader',
options: {name: '[path][name].[hash].[ext]'}
}]
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {limit: 10000, mimetype: 'application/octet-stream'}
}
]
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader'
}
]
},
{
test: /\.svg$/i,
loader: 'raw-loader'
},
{
test: require.resolve('angular'),
use: [
{loader: 'expose-loader', options: 'angular'},
]
},
{
test: require.resolve('jquery'),
use: [
{loader: 'expose-loader', options: '$'},
{loader: 'expose-loader', options: 'jQuery'},
]
},
{
test: require.resolve('lodash'),
use: [
{loader: 'expose-loader', options: '_'},
]
},
{
test: require.resolve('moment'),
use: [
{loader: 'expose-loader', options: 'moment'},
]
},
{
test: /\.html$/,
use: [{
loader: 'raw-loader',
options: {name: '[path][name].[hash].[ext]'}
}]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(appPath, 'index.html')
}),
new webpack.DefinePlugin({
INJECT_REGEX_HERE: JSON.stringify(patterns)
}),
new CopyWebpackPlugin([
{from: 'app/images', to: 'assets/images'},
{from: 'app/fonts', to: 'assets/fonts'},
{from: 'app/templates', to: 'assets/templates'},
{from: 'app/silent-callback.html', to: 'silent-callback.html'},
{from: 'node_modules/font-awesome/css', to: 'assets/font-awesome/css'},
{from: 'node_modules/font-awesome/fonts', to: 'assets/font-awesome/fonts'},
{from: 'node_modules/angular-ui-grid/fonts', to: 'assets/fonts'},
{from: 'node_modules/d3/d3.min.js', to: 'assets/d3'}
]),
new MiniCssExtractPlugin({
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css'
}),
new OpenBrowserPlugin({url: 'http://localhost:1337'})
],
devtool: isProduction ? 'source-map' : 'inline-source-map',
devServer: {
port: 1337
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
}
})
几年前我做了一次类似的迁移,我不需要那种类型的解决方案。我的目标是将每个组件封装为一个模块,然后尽可能地延迟加载
// webpack config
{
test: /\.html$/,
use: ['html-loader'],
},
然后在每个组件中,我只需要样式和模板,如:
require('./_proposal-page.scss');
(function() {
'use strict';
angular.module('component.myComponent', []).component('myComponent', {
template: require('./proposal-page.html'),
controller: MyController,
});
/** @ngInject */
function MyController($log) {
const $ctrl = this;
$ctrl.$onInit = function() {
$log.log('$onInit myComponent');
}
}
})();
if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.exports === exports) {
module.exports = 'component.myComponent';
}
Webpack检测所需文件并将每个
.html
文件导出为一个模块,它工作顺利。使用Webpack的主要好处之一是减少浏览器为呈现应用程序而必须执行的请求数量,并使应用程序启动更快。
为了实现这一点,它将相关资源分组在“块”中,并在一个请求中一起加载。单独加载所需的单个文件,如HTML模板(没有特定原因),在这里可能被认为是一种反模式
最佳实践是将所有相关的JS、HTML和CSS代码组合在一个大的捆绑包中,一次加载一次,有时(对于更大的应用程序)会为来自node_模块的代码提供第二个“供应商”捆绑包,从而加快开发速度,因为这一块不会经常更改
替代解决方案:
因此,在您的情况下,如果没有具体的理由将事情分开(您没有写过),我宁愿建议将HTML与控制代码一起放在一个块中,而不是单独加载HTML文件
一个好的简单的起点是只构建两个块。用以下代码替换整个优化块:
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all'
}
}
}
},
这将构建两个块:一个用于所有JS和HTML文件的主块,另一个用于node_modules文件夹中的所有内容
这样,您就不必再担心HTML文件的浏览器缓存了,因为它们是在您的主块中构建的,而且,作为一个好处,您的应用程序启动速度会更快。您找到解决方案了吗?我正在寻找同样的东西,作为一个选项,您可以将所有html模板内联到js中。这就是我们在应用程序中所做的。@PetrAveryanov您如何处理nginclude(如果您有)?我们只有“文件本地”ng include,没有问题。(我们将全局模板直接放入templateCache)基本要点是,webpack将自动为您处理此问题,您不需要自己构建哈希。
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all'
}
}
}
},