-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implementing SystemJSPublicPathWebpackPlugin. Resolves #12. * Self review * Documenting Resource Query approach
- Loading branch information
1 parent
cecbb99
commit 78308fc
Showing
6 changed files
with
4,116 additions
and
3,748 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const webpack = require("webpack"); | ||
|
||
const isWebpack5 = webpack.version && webpack.version.startsWith("5."); | ||
|
||
class SystemJSPublicPathWebpackPlugin { | ||
constructor(options) { | ||
this.options = options || {}; | ||
if (!isWebpack5 && !this.options.systemjsModuleName) { | ||
throw Error( | ||
`SystemJSPublicPathWebpackPlugin: When using webpack@<5, you must provide 'systemjsModuleName' as an option.` | ||
); | ||
} | ||
} | ||
apply(compiler) { | ||
const additionalEntries = []; | ||
|
||
if (isWebpack5) { | ||
additionalEntries.push( | ||
`systemjs-webpack-interop/auto-public-path/${this.options | ||
.rootDirectoryLevel || 1}` | ||
); | ||
} else { | ||
additionalEntries.push( | ||
`systemjs-webpack-interop/resource-query-public-path?systemjsModuleName=${this.options.systemjsModuleName}` | ||
); | ||
} | ||
|
||
compiler.options.entry = prependEntry( | ||
compiler.options.entry, | ||
additionalEntries | ||
); | ||
} | ||
} | ||
|
||
// This function was copied from https://github.com/webpack/webpack-dev-server/blob/b0161e9852cdf41730e82aa43efe7e88f44a4f9d/lib/utils/DevServerPlugin.js#L72 | ||
function prependEntry(originalEntry, additionalEntries) { | ||
if (typeof originalEntry === "function") { | ||
return () => | ||
Promise.resolve(originalEntry()).then(entry => | ||
prependEntry(entry, additionalEntries) | ||
); | ||
} | ||
|
||
if (typeof originalEntry === "object" && !Array.isArray(originalEntry)) { | ||
/** @type {Object<string,string>} */ | ||
const clone = {}; | ||
|
||
Object.keys(originalEntry).forEach(key => { | ||
// entry[key] should be a string here | ||
const entryDescription = originalEntry[key]; | ||
clone[key] = prependEntry(entryDescription, additionalEntries); | ||
}); | ||
|
||
return clone; | ||
} | ||
|
||
// in this case, entry is a string or an array. | ||
// make sure that we do not add duplicates. | ||
/** @type {Entry} */ | ||
const entriesClone = additionalEntries.slice(0); | ||
[].concat(originalEntry).forEach(newEntry => { | ||
if (!entriesClone.includes(newEntry)) { | ||
entriesClone.push(newEntry); | ||
} | ||
}); | ||
return entriesClone; | ||
} | ||
|
||
module.exports = SystemJSPublicPathWebpackPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.