问题是 index.html 没有被 Webpack 监视。它只监视代码中某处“需要”或“导入”的文件,并且加载程序正在测试。
解决方案分为两部分。
首先需要您的入口点中的 index.html 文件。从技术上讲,您可以在应用程序的任何地方使用它,但这非常方便。如果您在 html-webpack-plugin 中使用模板,我相信您也可以只需要您的模板。
我在我的 index.js 文件中需要我的 index.html,这是我的入口点:
require('./index.html')
const angular = require('angular')
const app = angular.module('app', [])
//...so on and so forth
最后,安装 raw-loader 和所有其他加载器,并将其添加到 Webpack 配置文件中。因此:
{
test: /\.html$/,
loader: "raw-loader"
}
原始加载器会将几乎所有“需要”的文件转换为文本字符串,然后,Webpack 会为您监视它并在您每次进行更改时刷新开发服务器。
无论是 Webpack 本身还是您的程序都不会在加载 index.html 文件(或模板)的阶段对其进行任何实际操作。这对于您的生产或测试环境来说完全没有必要,所以为了更好的衡量,我只在运行开发服务器时添加它:
/* eslint no-undef: 0 */
if (process.env.NODE_ENV === 'development') {
require('./index.html')
}
const angular = require('angular')
const app = angular.module('app', [])
//...so on and so forth
理论上,您可以“要求”一堆您希望它观看的其他静态 html 文件。 ...或文本文件。我自己将 raw-loader 用于 Angular 指令模板,但我不必将它们添加到我的入口点的开头。我可以只在指令模板属性中要求,如下所示:
module.exports = function(app) {
app.directive('myDirective', function(aListItem) {
return {
template: require('./myTemplate.html'),
restrict: 'E',
controller: function($scope) {
$scope.someThingThatGoesInMyTemplate = 'I love raw-loader!'
}
}
})
}