Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 85 additions & 4 deletions module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,38 @@ class WebpackCdnPlugin {
},
);
});
const externals = compiler.options.externals || {};

// I would like to make this plugin having an option for
// forcing updating externals.
// like this
//
// code:
// if (moduleEntry.var) {
// Reflect.ownKeys(this.modules).forEach((key) => {
// const mods = this.modules[key];
// mods
// .filter((m) => !m.cssOnly)
// .forEach((p) => {
// this.updateExternalsIfNot(externals, p);
// });
// });
// README.md
// `var`:`string` (optional)
// A variable that will be assigned to the module in global scope, webpack requires this. This option updates webpack externals configuration, if supplied.



let externals = compiler.options.externals || {};
Reflect.ownKeys(this.modules).forEach((key) => {
const mods = this.modules[key];
mods
.filter((m) => !m.cssOnly)
.forEach((p) => {
externals[p.name] = p.var || p.name;
externals = this.updateExternalsIfNot(externals, p);
});
});

compiler.options.externals = externals;


if (this.prod && (this.crossOrigin || this.sri)) {
compiler.hooks.afterPlugins.tap('WebpackCdnPlugin', () => {
compiler.hooks.thisCompilation.tap('WebpackCdnPlugin', () => {
Expand Down Expand Up @@ -158,6 +177,68 @@ class WebpackCdnPlugin {
}
}

/**
* update webpack externals option
*/
updateExternalsIfNot(externals, moduleEntry) {
let updateExternal = false
if (Array.isArray(externals)) {
const self = this;
const extEntry = externals.find(
elm => self.matchExternal(elm, moduleEntry));
updateExternal = typeof extEntry === 'undefined'
} else if (typeof externals === 'function') {
// you should not update external option
updateExternal = false;
} else if (typeof externals === 'string') {
if (!this.matchExternal(externals, moduleEntry)) {
externals = [ externals ];
updateExternal = true;
}
} else if (externals instanceof RegExp) {
if (!this.matchExternal(externals, moduleEntry)) {
externals = [ externals ];
updateExternal = true;
}
} else {
const keys = Reflect.ownKeys(externals);
const idx = keys.findIndex(key => key == moduleEntry.name);
updateExternal = idx < 0;
}
if (updateExternal) {
const modName = moduleEntry.var || moduleEntry.name;
if (Array.isArray(externals)) {
const extEntry = {};
extEntry[moduleEntry.name] = modName;
externals.push(extEntry);
} else if (typeof externals === 'object') {
externals[moduleEntry.name] = modName;
}
}
return externals;
}

/**
* you get true if extEntry has moduleEntry already.
*/
matchExternal(extEntry, moduleEntry) {
let result = false
if (extEntry instanceof RegExp) {
result = moduleEntry.name.match(extEntry)
} else if (typeof extEntry === 'function') {
// You should not append external entry
result = true
} else if (typeof extEntry === 'string') {
result = extEntry == moduleEntry.name;
} else if (typeof extEntry === 'object') {
result = moduleEntry.name in extEntry;
}
return result

}



/**
* Returns the version of a package in the root of the `node_modules` folder.
*
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-cdn-plugin",
"version": "3.3.1",
"version": "3.3.2",
"description": "A webpack plugin that use externals of CDN urls for production and local node_modules for development",
"main": "module.js",
"module": "module.js",
Expand Down Expand Up @@ -54,17 +54,17 @@
"devDependencies": {
"archy": "^1.0.0",
"bootstrap-css-only": "^4.4.1",
"eslint": "6.8.0",
"eslint-config-airbnb-base": "14.1.0",
"eslint-plugin-import": "^2.20.2",
"html-webpack-plugin": "^4.0.4",
"husky": "^4.2.3",
"jasmine": "^3.5.0",
"jasmine-spec-reporter": "^5.0.1",
"nyc": "^15.0.1",
"eslint": "8.4.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "^2.25.3",
"html-webpack-plugin": "^5.5.0",
"husky": "^7.0.4",
"jasmine": "^3.10.0",
"jasmine-spec-reporter": "^7.0.0",
"nyc": "^15.1.0",
"validate-commit-msg": "^2.14.0",
"webpack": "4.42.1",
"webpack-cli": "^3.3.4"
"webpack": "5.64.4",
"webpack-cli": "^4.9.1"
},
"peerDependencies": {
"html-webpack-plugin": ">=3"
Expand Down