Forked from matthewma7/ExternalTemplateRemotesPlugin.js
Created
January 23, 2023 03:19
-
-
Save jame-earnin/32308c17168d9c051a6139a534c4c72d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /*! | |
| * ExternalTemplateRemotesPlugin | |
| * License: MIT (https://mit-license.org/) | |
| */ | |
| const extractUrlAndGlobal = require('webpack/lib/util/extractUrlAndGlobal'); | |
| const { RawSource } = require('webpack-sources'); | |
| const PLUGIN_NAME = 'ExternalTemplateRemotesPlugin'; | |
| class ExternalTemplateRemotesPlugin { | |
| apply(compiler) { | |
| compiler.hooks.make.tap(PLUGIN_NAME, compilation => { | |
| const scriptExternalModules = []; | |
| compilation.hooks.buildModule.tap(PLUGIN_NAME, module => { | |
| if (module.constructor.name === 'ExternalModule' && module.externalType === 'script') { | |
| scriptExternalModules.push(module); | |
| } | |
| }); | |
| compilation.hooks.afterCodeGeneration.tap(PLUGIN_NAME, function() { | |
| scriptExternalModules.map(module => { | |
| const urlTemplate = extractUrlAndGlobal(module.request)[0]; | |
| const urlExpression = toExpression(urlTemplate); | |
| const sourceMap = compilation.codeGenerationResults.get(module).sources; | |
| const rawSource = sourceMap.get('javascript'); | |
| sourceMap.set( | |
| 'javascript', | |
| new RawSource(rawSource.source().replace(`"${urlTemplate}"`, urlExpression)) | |
| ); | |
| }); | |
| }); | |
| }); | |
| } | |
| } | |
| function toExpression(templateUrl) { | |
| const result = []; | |
| const current = []; | |
| let isExpression = false; | |
| let invalid = false; | |
| for (const c of templateUrl) { | |
| if (c === '[') { | |
| if (isExpression) { | |
| invalid = true; | |
| break; | |
| } | |
| isExpression = true; | |
| if (current.length) { | |
| result.push(`"${current.join('')}"`); | |
| current.length = 0; | |
| } | |
| } else if (c === ']') { | |
| if (!isExpression) { | |
| invalid = true; | |
| break; | |
| } | |
| isExpression = false; | |
| if (current.length) { | |
| result.push(`${current.join('')}`); | |
| current.length = 0; | |
| } | |
| current.length = 0; | |
| } else { | |
| current.push(c); | |
| } | |
| } | |
| if (isExpression || invalid) { | |
| throw new Error(`Invalid template URL "${templateUrl}"`); | |
| } | |
| if (current.length) { | |
| result.push(`"${current.join('')}"`); | |
| } | |
| return result.join(' + '); | |
| } | |
| module.exports = ExternalTemplateRemotesPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment