Skip to content

Commit b32afd9

Browse files
committed
fix: html plugin v4
fixes #63
1 parent 75f2e62 commit b32afd9

File tree

4 files changed

+169
-112
lines changed

4 files changed

+169
-112
lines changed

module.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const HtmlWebpackPlugin = require('html-webpack-plugin');
23
const createSri = require('sri-create');
34

45
const empty = '';
@@ -50,7 +51,7 @@ class WebpackCdnPlugin {
5051
const getArgs = [this.url, this.prefix, this.prod, output.publicPath];
5152

5253
compiler.hooks.compilation.tap('WebpackCdnPlugin', (compilation) => {
53-
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync(
54+
WebpackCdnPlugin._getHtmlHook(compilation, 'beforeAssetTagGeneration', 'htmlWebpackPluginBeforeHtmlGeneration').tapAsync(
5455
'WebpackCdnPlugin',
5556
(data, callback) => {
5657
const moduleId = data.plugin.options.cdnModule;
@@ -66,6 +67,12 @@ class WebpackCdnPlugin {
6667

6768
modules = modules.filter((module) => module.version);
6869

70+
if (output.publicPath === empty) {
71+
data.assets.js.forEach((item, i) => {
72+
data.assets.js[i] = data.assets.js[i].replace(data.assets.publicPath, empty);
73+
});
74+
}
75+
6976
data.assets.js = WebpackCdnPlugin._getJs(modules, ...getArgs).concat(data.assets.js);
7077
data.assets.css = WebpackCdnPlugin._getCss(modules, ...getArgs).concat(
7178
data.assets.css,
@@ -93,7 +100,7 @@ class WebpackCdnPlugin {
93100
compiler.hooks.afterPlugins.tap('WebpackCdnPlugin', () => {
94101
compiler.hooks.thisCompilation.tap('WebpackCdnPlugin', () => {
95102
compiler.hooks.compilation.tap('HtmlWebpackPluginHooks', (compilation) => {
96-
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapPromise(
103+
WebpackCdnPlugin._getHtmlHook(compilation, 'alterAssetTags', 'htmlWebpackPluginAlterAssetTags').tapPromise(
97104
'WebpackCdnPlugin',
98105
this.alterAssetTags.bind(this),
99106
);
@@ -138,8 +145,16 @@ class WebpackCdnPlugin {
138145
}
139146
}
140147
};
141-
await Promise.all(pluginArgs.head.filter(filterTag).map(processTag));
142-
await Promise.all(pluginArgs.body.filter(filterTag).map(processTag));
148+
149+
/* istanbul ignore if */
150+
/* istanbul ignore else */
151+
if (pluginArgs.assetTags) {
152+
await Promise.all(pluginArgs.assetTags.scripts.filter(filterTag).map(processTag));
153+
await Promise.all(pluginArgs.assetTags.styles.filter(filterTag).map(processTag));
154+
} else {
155+
await Promise.all(pluginArgs.head.filter(filterTag).map(processTag));
156+
await Promise.all(pluginArgs.body.filter(filterTag).map(processTag));
157+
}
143158
}
144159

145160
/**
@@ -265,6 +280,16 @@ class WebpackCdnPlugin {
265280

266281
return files;
267282
}
283+
284+
static _getHtmlHook(compilation, v4Name, v3Name) {
285+
try {
286+
/* istanbul ignore next */
287+
return HtmlWebpackPlugin.getHooks(compilation)[v4Name] || compilation.hooks[v3Name];
288+
} catch (e) {
289+
/* istanbul ignore next */
290+
return compilation.hooks[v3Name];
291+
}
292+
}
268293
}
269294

270295
module.exports = WebpackCdnPlugin;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"eslint": "6.8.0",
5858
"eslint-config-airbnb-base": "14.1.0",
5959
"eslint-plugin-import": "^2.20.2",
60-
"html-webpack-plugin": "^3.2.0",
60+
"html-webpack-plugin": "^4.0.4",
6161
"husky": "^4.2.3",
6262
"jasmine": "^3.5.0",
6363
"jasmine-spec-reporter": "^5.0.1",

spec/webpack.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ const path = require('path');
22
const webpack = require('webpack');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const WebpackCdnPlugin = require('../module');
5-
const createSri = require('sri-create');
65

7-
const cssMatcher = /<link href="(.+?)" rel="stylesheet"( crossorigin="anonymous")?( integrity="sha.+")?>/g;
8-
const jsMatcher = /<script type="text\/javascript" src="([^"]+?)"( crossorigin="anonymous")?( integrity="sha[^"]+")?>/g;
6+
const cssMatcher = /<link href="([^"]+?)" rel="stylesheet"( crossorigin="anonymous")?( integrity="sha.[^"]+?")?>/g;
7+
const jsMatcher = /<script(?: type="text\/javascript")? src="([^"]+?)"( crossorigin="anonymous")?( integrity="sha[^"]+?")?>/g;
98

109
let cssAssets;
1110
let jsAssets;
@@ -52,7 +51,9 @@ function runWebpack(callback, config) {
5251
const html = stats.compilation.assets['../index.html'].source();
5352
const html2 = stats.compilation.assets['../index2.html'].source();
5453

55-
let matches, sriMatches;
54+
let matches;
55+
let sriMatches;
56+
5657
while ((matches = cssMatcher.exec(html))) {
5758
cssAssets.push(matches[1]);
5859
cssCrossOrigin.push(/crossorigin="anonymous"/.test(matches[2]));
@@ -109,7 +110,7 @@ function getConfig({
109110
output.publicPath = publicPath2;
110111
}
111112

112-
var modules = [
113+
let modules = [
113114
{ name: 'jasmine-spec-reporter', path: 'index.js' },
114115
{
115116
name: 'nyc',
@@ -123,7 +124,7 @@ function getConfig({
123124
if (sri === 'jasmine') {
124125
if (moduleProdUrl) {
125126
modules = [
126-
{ name: 'jasmine', path: 'lib/jasmine.js' }
127+
{ name: 'jasmine', path: 'lib/jasmine.js' },
127128
];
128129
} else {
129130
modules = [
@@ -138,10 +139,10 @@ function getConfig({
138139
}
139140
}
140141
if (moduleProdUrl) {
141-
modules[modules.length-1].prodUrl = moduleProdUrl;
142+
modules[modules.length - 1].prodUrl = moduleProdUrl;
142143
}
143144
if (moduleDevUrl) {
144-
modules[modules.length-1].devUrl = moduleDevUrl;
145+
modules[modules.length - 1].devUrl = moduleDevUrl;
145146
}
146147
if (multiple) {
147148
modules = {
@@ -429,7 +430,6 @@ describe('Webpack Integration', () => {
429430
expect(jsSri).toEqual([]);
430431
});
431432
});
432-
433433
});
434434

435435
describe('When `prod` is false', () => {
@@ -521,7 +521,7 @@ describe('Webpack Integration', () => {
521521
beforeAll((done) => {
522522
runWebpack(done, getConfig({
523523
prod: false,
524-
moduleDevUrl: ":name/dist/:path",
524+
moduleDevUrl: ':name/dist/:path',
525525
}));
526526
});
527527

0 commit comments

Comments
 (0)