@rushstack/set-webpack-public-path-plugin 5.2.15 → 5.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +35 -0
- package/CHANGELOG.md +13 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib-esm/SetPublicPathCurrentScriptPlugin.js +84 -0
- package/lib-esm/SetPublicPathCurrentScriptPlugin.js.map +1 -0
- package/lib-esm/SetPublicPathPlugin.js +99 -0
- package/lib-esm/SetPublicPathPlugin.js.map +1 -0
- package/lib-esm/SetPublicPathPluginBase.js +49 -0
- package/lib-esm/SetPublicPathPluginBase.js.map +1 -0
- package/lib-esm/codeGenerator.js +51 -0
- package/lib-esm/codeGenerator.js.map +1 -0
- package/lib-esm/index.js +6 -0
- package/lib-esm/index.js.map +1 -0
- package/package.json +30 -8
- /package/{lib → lib-commonjs}/SetPublicPathCurrentScriptPlugin.js +0 -0
- /package/{lib → lib-commonjs}/SetPublicPathCurrentScriptPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/SetPublicPathPlugin.js +0 -0
- /package/{lib → lib-commonjs}/SetPublicPathPlugin.js.map +0 -0
- /package/{lib → lib-commonjs}/SetPublicPathPluginBase.js +0 -0
- /package/{lib → lib-commonjs}/SetPublicPathPluginBase.js.map +0 -0
- /package/{lib → lib-commonjs}/codeGenerator.js +0 -0
- /package/{lib → lib-commonjs}/codeGenerator.js.map +0 -0
- /package/{lib → lib-commonjs}/index.js +0 -0
- /package/{lib → lib-commonjs}/index.js.map +0 -0
- /package/{lib → lib-dts}/SetPublicPathCurrentScriptPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/SetPublicPathCurrentScriptPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/SetPublicPathPlugin.d.ts +0 -0
- /package/{lib → lib-dts}/SetPublicPathPlugin.d.ts.map +0 -0
- /package/{lib → lib-dts}/SetPublicPathPluginBase.d.ts +0 -0
- /package/{lib → lib-dts}/SetPublicPathPluginBase.d.ts.map +0 -0
- /package/{lib → lib-dts}/codeGenerator.d.ts +0 -0
- /package/{lib → lib-dts}/codeGenerator.d.ts.map +0 -0
- /package/{lib → lib-dts}/index.d.ts +0 -0
- /package/{lib → lib-dts}/index.d.ts.map +0 -0
package/CHANGELOG.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/set-webpack-public-path-plugin",
|
|
3
3
|
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"version": "5.3.1",
|
|
6
|
+
"tag": "@rushstack/set-webpack-public-path-plugin_v5.3.1",
|
|
7
|
+
"date": "Thu, 19 Feb 2026 01:30:06 GMT",
|
|
8
|
+
"comments": {
|
|
9
|
+
"dependency": [
|
|
10
|
+
{
|
|
11
|
+
"comment": "Updating dependency \"@rushstack/webpack-plugin-utilities\" to `0.6.1`"
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"version": "5.3.0",
|
|
18
|
+
"tag": "@rushstack/set-webpack-public-path-plugin_v5.3.0",
|
|
19
|
+
"date": "Thu, 19 Feb 2026 00:04:53 GMT",
|
|
20
|
+
"comments": {
|
|
21
|
+
"minor": [
|
|
22
|
+
{
|
|
23
|
+
"comment": "Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `\"exports\"` field in `package.json`."
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
"dependency": [
|
|
27
|
+
{
|
|
28
|
+
"comment": "Updating dependency \"@rushstack/node-core-library\" to `5.20.0`"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"comment": "Updating dependency \"@rushstack/webpack-plugin-utilities\" to `0.6.0`"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"comment": "Updating dependency \"@rushstack/heft\" to `1.2.0`"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
},
|
|
4
39
|
{
|
|
5
40
|
"version": "5.2.15",
|
|
6
41
|
"tag": "@rushstack/set-webpack-public-path-plugin_v5.2.15",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
# Change Log - @rushstack/set-webpack-public-path-plugin
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Thu, 19 Feb 2026 01:30:06 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 5.3.1
|
|
6
|
+
Thu, 19 Feb 2026 01:30:06 GMT
|
|
7
|
+
|
|
8
|
+
_Version update only_
|
|
9
|
+
|
|
10
|
+
## 5.3.0
|
|
11
|
+
Thu, 19 Feb 2026 00:04:53 GMT
|
|
12
|
+
|
|
13
|
+
### Minor changes
|
|
14
|
+
|
|
15
|
+
- Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `"exports"` field in `package.json`.
|
|
4
16
|
|
|
5
17
|
## 5.2.15
|
|
6
18
|
Sat, 07 Feb 2026 01:13:26 GMT
|
package/dist/tsdoc-metadata.json
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
|
|
4
|
+
const PLUGIN_NAME = 'set-webpack-public-path-current-script-plugin';
|
|
5
|
+
const CURRENT_SCRIPT_VARIABLE_NAME = '__RUSHSTACK_CURRENT_SCRIPT__';
|
|
6
|
+
const PUBLIC_PATH_VARIABLE_NAME = '_publicPath';
|
|
7
|
+
/**
|
|
8
|
+
* This simple plugin wraps the webpack bundle in an IIFE that sets a the `document.currentScript` value to a variable
|
|
9
|
+
* that is then used to populate the `__webpack_public_path__` variable.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export class SetPublicPathCurrentScriptPlugin extends SetPublicPathPluginBase {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(PLUGIN_NAME);
|
|
16
|
+
}
|
|
17
|
+
_applyCompilation(thisWebpack, compilation) {
|
|
18
|
+
var _a;
|
|
19
|
+
const outputLibraryType = (_a = compilation.options.output.library) === null || _a === void 0 ? void 0 : _a.type;
|
|
20
|
+
class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
|
|
21
|
+
constructor() {
|
|
22
|
+
super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
|
|
23
|
+
}
|
|
24
|
+
generate() {
|
|
25
|
+
return [
|
|
26
|
+
`var ${PUBLIC_PATH_VARIABLE_NAME} = ${CURRENT_SCRIPT_VARIABLE_NAME} ? ${CURRENT_SCRIPT_VARIABLE_NAME}.src : '';`,
|
|
27
|
+
`${thisWebpack.RuntimeGlobals.publicPath} = ${PUBLIC_PATH_VARIABLE_NAME}.slice(0, ${PUBLIC_PATH_VARIABLE_NAME}.lastIndexOf('/') + 1);`
|
|
28
|
+
].join('\n');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const runtimeModule = new SetPublicPathRuntimeModule();
|
|
32
|
+
function appliesToChunk(chunk, codeGenerationResults) {
|
|
33
|
+
return chunk.hasRuntime() && codeGenerationResults.has(runtimeModule, chunk.runtime);
|
|
34
|
+
}
|
|
35
|
+
compilation.hooks.runtimeRequirementInTree
|
|
36
|
+
.for(thisWebpack.RuntimeGlobals.publicPath)
|
|
37
|
+
.tap(PLUGIN_NAME, (chunk, set) => {
|
|
38
|
+
compilation.addRuntimeModule(chunk, runtimeModule);
|
|
39
|
+
});
|
|
40
|
+
const javascriptModulesPluginHooks = thisWebpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation);
|
|
41
|
+
javascriptModulesPluginHooks.render.tap({ name: PLUGIN_NAME, stage: Number.MAX_SAFE_INTEGER }, (source, { codeGenerationResults, chunk }) => {
|
|
42
|
+
if (appliesToChunk(chunk, codeGenerationResults)) {
|
|
43
|
+
return new thisWebpack.sources.ConcatSource(`(()=>{ var ${CURRENT_SCRIPT_VARIABLE_NAME} = document.currentScript; `, source, '})();');
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return source;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
javascriptModulesPluginHooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash, { codeGenerationResults }) => {
|
|
50
|
+
hash.update(PLUGIN_NAME);
|
|
51
|
+
if (appliesToChunk(chunk, codeGenerationResults)) {
|
|
52
|
+
hash.update('set-public-path');
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {
|
|
56
|
+
let hasProblematicLibraryType = false;
|
|
57
|
+
switch (outputLibraryType) {
|
|
58
|
+
case 'var':
|
|
59
|
+
case 'module':
|
|
60
|
+
hasProblematicLibraryType = true;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
if (hasProblematicLibraryType) {
|
|
64
|
+
const codeGenerationResults = compilation.codeGenerationResults;
|
|
65
|
+
if (!codeGenerationResults) {
|
|
66
|
+
compilation.errors.push(new thisWebpack.WebpackError(`${PLUGIN_NAME}: codeGenerationResults is undefined in afterSeal. This is unexpected and may indicate a misconfiguration or bug.`));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
let appliesToAnyChunk = false;
|
|
70
|
+
for (const chunk of compilation.chunks) {
|
|
71
|
+
if (appliesToChunk(chunk, codeGenerationResults)) {
|
|
72
|
+
appliesToAnyChunk = true;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (appliesToAnyChunk) {
|
|
77
|
+
compilation.errors.push(new thisWebpack.WebpackError(`The "${outputLibraryType}" output.library.type is not supported by the ${SetPublicPathCurrentScriptPlugin.name}` +
|
|
78
|
+
' plugin. Including this plugin with produce unexpected or invalid results.'));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=SetPublicPathCurrentScriptPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetPublicPathCurrentScriptPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathCurrentScriptPlugin.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAI3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE,MAAM,WAAW,GAAW,+CAA+C,CAAC;AAE5E,MAAM,4BAA4B,GAAW,8BAA8B,CAAC;AAC5E,MAAM,yBAAyB,GAAW,aAAa,CAAC;AASxD;;;;;GAKG;AACH,MAAM,OAAO,gCAAiC,SAAQ,uBAAuB;IAC3E;QACE,KAAK,CAAC,WAAW,CAAC,CAAC;IACrB,CAAC;IAES,iBAAiB,CAAC,WAA2B,EAAE,WAAgC;;QACvF,MAAM,iBAAiB,GAAuB,MAAA,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,0CAAE,IAAI,CAAC;QAEvF,MAAM,0BAA2B,SAAQ,WAAW,CAAC,aAAa;YAChE;gBACE,KAAK,CAAC,YAAY,EAAE,WAAW,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;YAC7D,CAAC;YAEM,QAAQ;gBACb,OAAO;oBACL,OAAO,yBAAyB,MAAM,4BAA4B,MAAM,4BAA4B,YAAY;oBAChH,GAAG,WAAW,CAAC,cAAc,CAAC,UAAU,MAAM,yBAAyB,aAAa,yBAAyB,yBAAyB;iBACvI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACf,CAAC;SACF;QAED,MAAM,aAAa,GAA+B,IAAI,0BAA0B,EAAE,CAAC;QAEnF,SAAS,cAAc,CAAC,KAAoB,EAAE,qBAA4C;YACxF,OAAO,KAAK,CAAC,UAAU,EAAE,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GAAG,CAAC,WAAW,EAAE,GAAG,EAAE;YAChD,IAAI,yBAAyB,GAAY,KAAK,CAAC;YAC/C,QAAQ,iBAAiB,EAAE,CAAC;gBAC1B,KAAK,KAAK,CAAC;gBACX,KAAK,QAAQ;oBACX,yBAAyB,GAAG,IAAI,CAAC;oBACjC,MAAM;YACV,CAAC;YAED,IAAI,yBAAyB,EAAE,CAAC;gBAC9B,MAAM,qBAAqB,GAAsC,WAAW,CAAC,qBAAqB,CAAC;gBACnG,IAAI,CAAC,qBAAqB,EAAE,CAAC;oBAC3B,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,IAAI,WAAW,CAAC,YAAY,CAC1B,GAAG,WAAW,mHAAmH,CAClI,CACF,CAAC;oBACF,OAAO;gBACT,CAAC;gBACD,IAAI,iBAAiB,GAAY,KAAK,CAAC;gBACvC,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;oBACvC,IAAI,cAAc,CAAC,KAAK,EAAE,qBAAqB,CAAC,EAAE,CAAC;wBACjD,iBAAiB,GAAG,IAAI,CAAC;wBACzB,MAAM;oBACR,CAAC;gBACH,CAAC;gBAED,IAAI,iBAAiB,EAAE,CAAC;oBACtB,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,IAAI,WAAW,CAAC,YAAY,CAC1B,QAAQ,iBAAiB,iDAAiD,gCAAgC,CAAC,IAAI,EAAE;wBAC/G,4EAA4E,CAC/E,CACF,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type webpack from 'webpack';\n\nimport { SetPublicPathPluginBase } from './SetPublicPathPluginBase';\n\nconst PLUGIN_NAME: string = 'set-webpack-public-path-current-script-plugin';\n\nconst CURRENT_SCRIPT_VARIABLE_NAME: string = '__RUSHSTACK_CURRENT_SCRIPT__';\nconst PUBLIC_PATH_VARIABLE_NAME: string = '_publicPath';\n\ntype JavascriptModulesPluginHooks = ReturnType<\n typeof webpack.javascript.JavascriptModulesPlugin.getCompilationHooks\n>;\ntype CodeGenerationResults = Parameters<\n Parameters<JavascriptModulesPluginHooks['render']['tap']>[1]\n>[1]['codeGenerationResults'];\n\n/**\n * This simple plugin wraps the webpack bundle in an IIFE that sets a the `document.currentScript` value to a variable\n * that is then used to populate the `__webpack_public_path__` variable.\n *\n * @public\n */\nexport class SetPublicPathCurrentScriptPlugin extends SetPublicPathPluginBase {\n public constructor() {\n super(PLUGIN_NAME);\n }\n\n protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void {\n const outputLibraryType: string | undefined = compilation.options.output.library?.type;\n\n class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {\n public constructor() {\n super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);\n }\n\n public generate(): string {\n return [\n `var ${PUBLIC_PATH_VARIABLE_NAME} = ${CURRENT_SCRIPT_VARIABLE_NAME} ? ${CURRENT_SCRIPT_VARIABLE_NAME}.src : '';`,\n `${thisWebpack.RuntimeGlobals.publicPath} = ${PUBLIC_PATH_VARIABLE_NAME}.slice(0, ${PUBLIC_PATH_VARIABLE_NAME}.lastIndexOf('/') + 1);`\n ].join('\\n');\n }\n }\n\n const runtimeModule: SetPublicPathRuntimeModule = new SetPublicPathRuntimeModule();\n\n function appliesToChunk(chunk: webpack.Chunk, codeGenerationResults: CodeGenerationResults): boolean {\n return chunk.hasRuntime() && codeGenerationResults.has(runtimeModule, chunk.runtime);\n }\n\n compilation.hooks.runtimeRequirementInTree\n .for(thisWebpack.RuntimeGlobals.publicPath)\n .tap(PLUGIN_NAME, (chunk: webpack.Chunk, set: Set<string>) => {\n compilation.addRuntimeModule(chunk, runtimeModule);\n });\n\n const javascriptModulesPluginHooks: JavascriptModulesPluginHooks =\n thisWebpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation);\n\n javascriptModulesPluginHooks.render.tap(\n { name: PLUGIN_NAME, stage: Number.MAX_SAFE_INTEGER },\n (source, { codeGenerationResults, chunk }) => {\n if (appliesToChunk(chunk, codeGenerationResults)) {\n return new thisWebpack.sources.ConcatSource(\n `(()=>{ var ${CURRENT_SCRIPT_VARIABLE_NAME} = document.currentScript; `,\n source,\n '})();'\n );\n } else {\n return source;\n }\n }\n );\n\n javascriptModulesPluginHooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash, { codeGenerationResults }) => {\n hash.update(PLUGIN_NAME);\n if (appliesToChunk(chunk, codeGenerationResults)) {\n hash.update('set-public-path');\n }\n });\n\n compilation.hooks.afterSeal.tap(PLUGIN_NAME, () => {\n let hasProblematicLibraryType: boolean = false;\n switch (outputLibraryType) {\n case 'var':\n case 'module':\n hasProblematicLibraryType = true;\n break;\n }\n\n if (hasProblematicLibraryType) {\n const codeGenerationResults: CodeGenerationResults | undefined = compilation.codeGenerationResults;\n if (!codeGenerationResults) {\n compilation.errors.push(\n new thisWebpack.WebpackError(\n `${PLUGIN_NAME}: codeGenerationResults is undefined in afterSeal. This is unexpected and may indicate a misconfiguration or bug.`\n )\n );\n return;\n }\n let appliesToAnyChunk: boolean = false;\n for (const chunk of compilation.chunks) {\n if (appliesToChunk(chunk, codeGenerationResults)) {\n appliesToAnyChunk = true;\n break;\n }\n }\n\n if (appliesToAnyChunk) {\n compilation.errors.push(\n new thisWebpack.WebpackError(\n `The \"${outputLibraryType}\" output.library.type is not supported by the ${SetPublicPathCurrentScriptPlugin.name}` +\n ' plugin. Including this plugin with produce unexpected or invalid results.'\n )\n );\n }\n }\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { Text } from '@rushstack/node-core-library';
|
|
4
|
+
import { getSetPublicPathCode } from './codeGenerator';
|
|
5
|
+
import { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
|
|
6
|
+
const SHOULD_REPLACE_ASSET_NAME_TOKEN = Symbol('set-public-path-plugin-should-replace-asset-name');
|
|
7
|
+
const PLUGIN_NAME = 'set-webpack-public-path';
|
|
8
|
+
const ASSET_NAME_TOKEN = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';
|
|
9
|
+
/**
|
|
10
|
+
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export class SetPublicPathPlugin extends SetPublicPathPluginBase {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super(PLUGIN_NAME);
|
|
17
|
+
this.options = options;
|
|
18
|
+
const scriptNameOptions = options.scriptName;
|
|
19
|
+
if (scriptNameOptions.useAssetName && scriptNameOptions.name) {
|
|
20
|
+
throw new Error('scriptName.userAssetName and scriptName.name must not be used together');
|
|
21
|
+
}
|
|
22
|
+
else if (scriptNameOptions.isTokenized && !scriptNameOptions.name) {
|
|
23
|
+
throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
_applyCompilation(thisWebpack, compilation) {
|
|
27
|
+
class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
|
|
28
|
+
constructor(pluginOptions) {
|
|
29
|
+
super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
|
|
30
|
+
this._pluginOptions = pluginOptions;
|
|
31
|
+
}
|
|
32
|
+
generate() {
|
|
33
|
+
const { name: regexpName, isTokenized: regexpIsTokenized, useAssetName } = this._pluginOptions.scriptName;
|
|
34
|
+
const { chunk } = this;
|
|
35
|
+
if (!chunk) {
|
|
36
|
+
throw new Error(`Chunk is not defined`);
|
|
37
|
+
}
|
|
38
|
+
let regexName;
|
|
39
|
+
if (regexpName) {
|
|
40
|
+
regexName = regexpName;
|
|
41
|
+
if (regexpIsTokenized) {
|
|
42
|
+
regexName = regexName
|
|
43
|
+
.replace(/\[name\]/g, Text.escapeRegExp(`${chunk.name}`))
|
|
44
|
+
.replace(/\[hash\]/g, chunk.renderedHash || '');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (useAssetName) {
|
|
48
|
+
chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;
|
|
49
|
+
regexName = ASSET_NAME_TOKEN;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error('scriptName.name or scriptName.useAssetName must be set');
|
|
53
|
+
}
|
|
54
|
+
const moduleOptions = {
|
|
55
|
+
webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath,
|
|
56
|
+
regexName,
|
|
57
|
+
...this._pluginOptions
|
|
58
|
+
};
|
|
59
|
+
return getSetPublicPathCode(moduleOptions);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
compilation.hooks.runtimeRequirementInTree
|
|
63
|
+
.for(thisWebpack.RuntimeGlobals.publicPath)
|
|
64
|
+
.tap(PLUGIN_NAME, (chunk, set) => {
|
|
65
|
+
compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));
|
|
66
|
+
});
|
|
67
|
+
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
|
|
68
|
+
for (const chunkGroup of compilation.chunkGroups) {
|
|
69
|
+
for (const chunk of chunkGroup.chunks) {
|
|
70
|
+
if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
|
|
71
|
+
for (const assetFilename of chunk.files) {
|
|
72
|
+
let escapedAssetFilename;
|
|
73
|
+
if (assetFilename.match(/\.map$/)) {
|
|
74
|
+
// Trim the ".map" extension
|
|
75
|
+
escapedAssetFilename = assetFilename.slice(0, -4 /* '.map'.length */);
|
|
76
|
+
escapedAssetFilename = Text.escapeRegExp(escapedAssetFilename);
|
|
77
|
+
// source in sourcemaps is JSON-encoded
|
|
78
|
+
escapedAssetFilename = JSON.stringify(escapedAssetFilename);
|
|
79
|
+
// Trim the quotes from the JSON encoding
|
|
80
|
+
escapedAssetFilename = escapedAssetFilename.slice(1, -1);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
escapedAssetFilename = Text.escapeRegExp(assetFilename);
|
|
84
|
+
}
|
|
85
|
+
const asset = assets[assetFilename];
|
|
86
|
+
const newAsset = new thisWebpack.sources.ReplaceSource(asset);
|
|
87
|
+
const sourceString = asset.source().toString();
|
|
88
|
+
for (let index = sourceString.lastIndexOf(ASSET_NAME_TOKEN); index >= 0; index = sourceString.lastIndexOf(ASSET_NAME_TOKEN, index - 1)) {
|
|
89
|
+
newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);
|
|
90
|
+
}
|
|
91
|
+
assets[assetFilename] = newAsset;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=SetPublicPathPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAI3D,OAAO,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAEpD,OAAO,EAAyB,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAmFpE,MAAM,+BAA+B,GAAkB,MAAM,CAC3D,kDAAkD,CACnD,CAAC;AAMF,MAAM,WAAW,GAAW,yBAAyB,CAAC;AAEtD,MAAM,gBAAgB,GAAW,kDAAkD,CAAC;AAEpF;;;;GAIG;AACH,MAAM,OAAO,mBAAoB,SAAQ,uBAAuB;IAG9D,YAAmB,OAA2C;QAC5D,KAAK,CAAC,WAAW,CAAC,CAAC;QACnB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAE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,YAAY,CAAC,oBAAoB,CAAC,CAAC;gCAC/D,uCAAuC;gCACvC,oBAAoB,GAAG,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC,CAAC;gCAC5D,yCAAyC;gCACzC,oBAAoB,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;4BAC3D,CAAC;iCAAM,CAAC;gCACN,oBAAoB,GAAG,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;4BAC1D,CAAC;4BAED,MAAM,KAAK,GAA2B,MAAM,CAAC,aAAa,CAAC,CAAC;4BAE5D,MAAM,QAAQ,GAAkC,IAAI,WAAW,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;4BAC7F,MAAM,YAAY,GAAW,KAAK,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC;4BACvD,KACE,IAAI,KAAK,GAAW,YAAY,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAC9D,KAAK,IAAI,CAAC,EACV,KAAK,GAAG,YAAY,CAAC,WAAW,CAAC,gBAAgB,EAAE,KAAK,GAAG,CAAC,CAAC,EAC7D,CAAC;gCACD,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,oBAAoB,CAAC,CAAC;4BACrF,CAAC;4BAED,MAAM,CAAC,aAAa,CAAC,GAAG,QAAQ,CAAC;wBACnC,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type webpack from 'webpack';\n\nimport { Text } from '@rushstack/node-core-library';\n\nimport { type IInternalOptions, getSetPublicPathCode } from './codeGenerator';\nimport { SetPublicPathPluginBase } from './SetPublicPathPluginBase';\n\n/**\n * The base options for setting the webpack public path at runtime.\n *\n * @public\n */\nexport interface ISetWebpackPublicPathOptions {\n /**\n * Check for a variable with this name on the page and use its value as a regular expression against script paths to\n * the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable\n * called foo during initialization, and if a foo variable is found, use its value as a regular expression to detect\n * the bundle's script.\n *\n * See the README for more information.\n */\n regexVariable?: string;\n\n /**\n * A function that returns a snippet of code that manipulates the variable with the name that's specified in the\n * parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified\n * in-place - the processed value should not be returned.\n *\n * See the README for more information.\n */\n getPostProcessScript?: (varName: string) => string;\n\n /**\n * If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.\n * This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.\n */\n preferLastFoundScript?: boolean;\n}\n\n/**\n * @public\n */\nexport interface IScriptNameAssetNameOptions {\n /**\n * If set to true, use the webpack generated asset's name. This option is not compatible with\n * andy other scriptName options.\n */\n useAssetName: true;\n}\n\n/**\n * @public\n */\nexport interface IScriptNameRegexOptions {\n /**\n * A regular expression expressed as a string to be applied to all script paths on the page.\n */\n name: string;\n\n /**\n * If true, the name property is tokenized.\n *\n * See the README for more information.\n */\n isTokenized?: boolean;\n}\n\n/**\n * @public\n */\nexport type IScriptNameOptions = IScriptNameAssetNameOptions | IScriptNameRegexOptions;\n\ntype IScriptNameInternalOptions =\n | (IScriptNameAssetNameOptions & { [key in keyof IScriptNameRegexOptions]?: never })\n | (IScriptNameRegexOptions & { [key in keyof IScriptNameAssetNameOptions]?: never });\n\n/**\n * Options for the set-webpack-public-path plugin.\n *\n * @public\n */\nexport interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPathOptions {\n /**\n * An object that describes how the public path should be discovered.\n */\n scriptName: IScriptNameOptions;\n}\n\nconst SHOULD_REPLACE_ASSET_NAME_TOKEN: unique symbol = Symbol(\n 'set-public-path-plugin-should-replace-asset-name'\n);\n\ninterface IExtendedChunk extends webpack.Chunk {\n [SHOULD_REPLACE_ASSET_NAME_TOKEN]?: boolean;\n}\n\nconst PLUGIN_NAME: string = 'set-webpack-public-path';\n\nconst ASSET_NAME_TOKEN: string = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';\n\n/**\n * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.\n *\n * @public\n */\nexport class SetPublicPathPlugin extends SetPublicPathPluginBase {\n public readonly options: ISetWebpackPublicPathPluginOptions;\n\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\n super(PLUGIN_NAME);\n this.options = options;\n\n const scriptNameOptions: IScriptNameInternalOptions = options.scriptName;\n if (scriptNameOptions.useAssetName && scriptNameOptions.name) {\n throw new Error('scriptName.userAssetName and scriptName.name must not be used together');\n } else if (scriptNameOptions.isTokenized && !scriptNameOptions.name) {\n throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');\n }\n }\n\n protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void {\n class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {\n private readonly _pluginOptions: ISetWebpackPublicPathPluginOptions;\n\n public constructor(pluginOptions: ISetWebpackPublicPathPluginOptions) {\n super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);\n this._pluginOptions = pluginOptions;\n }\n\n public generate(): string {\n const {\n name: regexpName,\n isTokenized: regexpIsTokenized,\n useAssetName\n } = this._pluginOptions.scriptName as IScriptNameInternalOptions;\n\n const { chunk } = this;\n if (!chunk) {\n throw new Error(`Chunk is not defined`);\n }\n\n let regexName: string;\n if (regexpName) {\n regexName = regexpName;\n if (regexpIsTokenized) {\n regexName = regexName\n .replace(/\\[name\\]/g, Text.escapeRegExp(`${chunk.name}`))\n .replace(/\\[hash\\]/g, chunk.renderedHash || '');\n }\n } else if (useAssetName) {\n (chunk as IExtendedChunk)[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;\n\n regexName = ASSET_NAME_TOKEN;\n } else {\n throw new Error('scriptName.name or scriptName.useAssetName must be set');\n }\n\n const moduleOptions: IInternalOptions = {\n webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath,\n regexName,\n ...this._pluginOptions\n };\n\n return getSetPublicPathCode(moduleOptions);\n }\n }\n\n compilation.hooks.runtimeRequirementInTree\n .for(thisWebpack.RuntimeGlobals.publicPath)\n .tap(PLUGIN_NAME, (chunk: webpack.Chunk, set: Set<string>) => {\n compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));\n });\n\n compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {\n for (const chunkGroup of compilation.chunkGroups) {\n for (const chunk of chunkGroup.chunks) {\n if ((chunk as IExtendedChunk)[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {\n for (const assetFilename of chunk.files) {\n let escapedAssetFilename: string;\n if (assetFilename.match(/\\.map$/)) {\n // Trim the \".map\" extension\n escapedAssetFilename = assetFilename.slice(0, -4 /* '.map'.length */);\n escapedAssetFilename = Text.escapeRegExp(escapedAssetFilename);\n // source in sourcemaps is JSON-encoded\n escapedAssetFilename = JSON.stringify(escapedAssetFilename);\n // Trim the quotes from the JSON encoding\n escapedAssetFilename = escapedAssetFilename.slice(1, -1);\n } else {\n escapedAssetFilename = Text.escapeRegExp(assetFilename);\n }\n\n const asset: webpack.sources.Source = assets[assetFilename];\n\n const newAsset: webpack.sources.ReplaceSource = new thisWebpack.sources.ReplaceSource(asset);\n const sourceString: string = asset.source().toString();\n for (\n let index: number = sourceString.lastIndexOf(ASSET_NAME_TOKEN);\n index >= 0;\n index = sourceString.lastIndexOf(ASSET_NAME_TOKEN, index - 1)\n ) {\n newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);\n }\n\n assets[assetFilename] = newAsset;\n }\n }\n }\n }\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { VersionDetection } from '@rushstack/webpack-plugin-utilities';
|
|
4
|
+
import { PackageJsonLookup } from '@rushstack/node-core-library';
|
|
5
|
+
/**
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export class SetPublicPathPluginBase {
|
|
9
|
+
constructor(pluginName) {
|
|
10
|
+
this._pluginName = pluginName;
|
|
11
|
+
}
|
|
12
|
+
apply(compiler) {
|
|
13
|
+
if (!VersionDetection.isWebpack5(compiler)) {
|
|
14
|
+
const thisPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);
|
|
15
|
+
throw new Error(`The ${this.constructor.name} plugin requires Webpack 5. Use major version 4 of ` +
|
|
16
|
+
`${thisPackageJson.name} for Webpack 4 support.`);
|
|
17
|
+
}
|
|
18
|
+
const thisWebpack = compiler.webpack;
|
|
19
|
+
const initialOutputPublicPathSetting = compiler.options.output.publicPath;
|
|
20
|
+
compiler.hooks.thisCompilation.tap(this._pluginName, (compilation) => {
|
|
21
|
+
if (initialOutputPublicPathSetting) {
|
|
22
|
+
compilation.warnings.push(new compiler.webpack.WebpackError(`The "output.publicPath" option is set in the Webpack configuration. The ${this.constructor.name} ` +
|
|
23
|
+
'plugin may produce unexpected results. It is recommended that the "output.publicPath" configuration option ' +
|
|
24
|
+
'be unset when using this plugin.'));
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({
|
|
28
|
+
name: this._pluginName,
|
|
29
|
+
register: (tap) => {
|
|
30
|
+
if (tap.name === 'RuntimePlugin') {
|
|
31
|
+
// Disable the default public path runtime plugin
|
|
32
|
+
return {
|
|
33
|
+
...tap,
|
|
34
|
+
fn: () => {
|
|
35
|
+
/* noop */
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return tap;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
this._applyCompilation(thisWebpack, compilation);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=SetPublicPathPluginBase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SetPublicPathPluginBase.js","sourceRoot":"","sources":["../src/SetPublicPathPluginBase.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAI3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAqB,MAAM,8BAA8B,CAAC;AAEpF;;GAEG;AACH,MAAM,OAAgB,uBAAuB;IAG3C,YAAmB,UAAkB;QACnC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;IAChC,CAAC;IAEM,KAAK,CAAC,QAA0B;QACrC,IAAI,CAAC,gBAAgB,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3C,MAAM,eAAe,GAAiB,iBAAiB,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;YACtF,MAAM,IAAI,KAAK,CACb,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,qDAAqD;gBAC/E,GAAG,eAAe,CAAC,IAAI,yBAAyB,CACnD,CAAC;QACJ,CAAC;QAED,MAAM,WAAW,GAAmB,QAAQ,CAAC,OAAO,CAAC;QAErD,MAAM,8BAA8B,GAClC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC;QAErC,QAAQ,CAAC,KAAK,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,WAAgC,EAAE,EAAE;YACxF,IAAI,8BAA8B,EAAE,CAAC;gBACnC,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,QAAQ,CAAC,OAAO,CAAC,YAAY,CAC/B,2EAA2E,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG;oBACjG,6GAA6G;oBAC7G,kCAAkC,CACrC,CACF,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,WAAW,CAAC,KAAK,CAAC,wBAAwB,CAAC,GAAG,CAAC,WAAW,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,SAAS,CAAC;oBAC9F,IAAI,EAAE,IAAI,CAAC,WAAW;oBACtB,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE;wBAChB,IAAI,GAAG,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;4BACjC,iDAAiD;4BACjD,OAAO;gCACL,GAAG,GAAG;gCACN,EAAE,EAAE,GAAG,EAAE;oCACP,UAAU;gCACZ,CAAC;6BACF,CAAC;wBACJ,CAAC;6BAAM,CAAC;4BACN,OAAO,GAAG,CAAC;wBACb,CAAC;oBACH,CAAC;iBACF,CAAC,CAAC;YACL,CAAC;YAED,IAAI,CAAC,iBAAiB,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;QACnD,CAAC,CAAC,CAAC;IACL,CAAC;CAGF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type webpack from 'webpack';\n\nimport { VersionDetection } from '@rushstack/webpack-plugin-utilities';\nimport { PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\n\n/**\n * @public\n */\nexport abstract class SetPublicPathPluginBase implements webpack.WebpackPluginInstance {\n private readonly _pluginName: string;\n\n public constructor(pluginName: string) {\n this._pluginName = pluginName;\n }\n\n public apply(compiler: webpack.Compiler): void {\n if (!VersionDetection.isWebpack5(compiler)) {\n const thisPackageJson: IPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);\n throw new Error(\n `The ${this.constructor.name} plugin requires Webpack 5. Use major version 4 of ` +\n `${thisPackageJson.name} for Webpack 4 support.`\n );\n }\n\n const thisWebpack: typeof webpack = compiler.webpack;\n\n const initialOutputPublicPathSetting: typeof compiler.options.output.publicPath =\n compiler.options.output.publicPath;\n\n compiler.hooks.thisCompilation.tap(this._pluginName, (compilation: webpack.Compilation) => {\n if (initialOutputPublicPathSetting) {\n compilation.warnings.push(\n new compiler.webpack.WebpackError(\n `The \"output.publicPath\" option is set in the Webpack configuration. The ${this.constructor.name} ` +\n 'plugin may produce unexpected results. It is recommended that the \"output.publicPath\" configuration option ' +\n 'be unset when using this plugin.'\n )\n );\n } else {\n compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({\n name: this._pluginName,\n register: (tap) => {\n if (tap.name === 'RuntimePlugin') {\n // Disable the default public path runtime plugin\n return {\n ...tap,\n fn: () => {\n /* noop */\n }\n };\n } else {\n return tap;\n }\n }\n });\n }\n\n this._applyCompilation(thisWebpack, compilation);\n });\n }\n\n protected abstract _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;\n}\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
const VAR_NAME = 'publicPath';
|
|
4
|
+
function joinLines(lines, linePrefix) {
|
|
5
|
+
return lines
|
|
6
|
+
.map((line) => {
|
|
7
|
+
if (line) {
|
|
8
|
+
return `${linePrefix || ''}${line}`;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return line;
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
.join('\n')
|
|
15
|
+
.replace(/\n\n+/g, '\n\n');
|
|
16
|
+
}
|
|
17
|
+
export function getSetPublicPathCode({ regexName, regexVariable, preferLastFoundScript, webpackPublicPathVariable, getPostProcessScript, linePrefix }) {
|
|
18
|
+
let lines = [];
|
|
19
|
+
lines = [`var scripts = document.getElementsByTagName('script');`];
|
|
20
|
+
const regexInitializationSnippet = `/${regexName}/i`;
|
|
21
|
+
const regexVarName = regexVariable;
|
|
22
|
+
if (regexVariable) {
|
|
23
|
+
lines.push(...[
|
|
24
|
+
`var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
lines.push(...[`var regex = ${regexInitializationSnippet};`]);
|
|
29
|
+
}
|
|
30
|
+
lines.push(...[
|
|
31
|
+
`var ${VAR_NAME};`,
|
|
32
|
+
'',
|
|
33
|
+
'if (scripts && scripts.length) {',
|
|
34
|
+
' for (var i = 0; i < scripts.length; i++) {',
|
|
35
|
+
' if (!scripts[i]) continue;',
|
|
36
|
+
` var path = scripts[i].getAttribute('src');`,
|
|
37
|
+
' if (path && path.match(regex)) {',
|
|
38
|
+
` ${VAR_NAME} = path.substring(0, path.lastIndexOf('/') + 1);`,
|
|
39
|
+
...(preferLastFoundScript ? [] : [' break;']),
|
|
40
|
+
' }',
|
|
41
|
+
' }',
|
|
42
|
+
'}',
|
|
43
|
+
''
|
|
44
|
+
]);
|
|
45
|
+
if (getPostProcessScript) {
|
|
46
|
+
lines.push(...['', `if (${VAR_NAME}) {`, ` ${getPostProcessScript(VAR_NAME)};`, '}', '']);
|
|
47
|
+
}
|
|
48
|
+
lines.push(`${webpackPublicPathVariable} = ${VAR_NAME};`);
|
|
49
|
+
return joinLines(lines, linePrefix);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=codeGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codeGenerator.js","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAU3D,MAAM,QAAQ,GAAW,YAAY,CAAC;AAEtC,SAAS,SAAS,CAAC,KAAe,EAAE,UAAmB;IACrD,OAAO,KAAK;SACT,GAAG,CAAC,CAAC,IAAY,EAAE,EAAE;QACpB,IAAI,IAAI,EAAE,CAAC;YACT,OAAO,GAAG,UAAU,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;QACtC,CAAC;aAAM,CAAC;YACN,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC,CAA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;IAE1D,OAAO,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACtC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { ISetWebpackPublicPathOptions } from './SetPublicPathPlugin';\n\nexport interface IInternalOptions extends ISetWebpackPublicPathOptions {\n webpackPublicPathVariable: string;\n regexName: string;\n linePrefix?: string;\n}\n\nconst VAR_NAME: string = 'publicPath';\n\nfunction joinLines(lines: string[], linePrefix?: string): string {\n return lines\n .map((line: string) => {\n if (line) {\n return `${linePrefix || ''}${line}`;\n } else {\n return line;\n }\n })\n .join('\\n')\n .replace(/\\n\\n+/g, '\\n\\n');\n}\n\nexport function getSetPublicPathCode({\n regexName,\n regexVariable,\n preferLastFoundScript,\n webpackPublicPathVariable,\n getPostProcessScript,\n linePrefix\n}: IInternalOptions): string {\n let lines: string[] = [];\n lines = [`var scripts = document.getElementsByTagName('script');`];\n\n const regexInitializationSnippet: string = `/${regexName}/i`;\n const regexVarName: string | undefined = regexVariable;\n if (regexVariable) {\n lines.push(\n ...[\n `var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`\n ]\n );\n } else {\n lines.push(...[`var regex = ${regexInitializationSnippet};`]);\n }\n\n lines.push(\n ...[\n `var ${VAR_NAME};`,\n '',\n 'if (scripts && scripts.length) {',\n ' for (var i = 0; i < scripts.length; i++) {',\n ' if (!scripts[i]) continue;',\n ` var path = scripts[i].getAttribute('src');`,\n ' if (path && path.match(regex)) {',\n ` ${VAR_NAME} = path.substring(0, path.lastIndexOf('/') + 1);`,\n ...(preferLastFoundScript ? [] : [' break;']),\n ' }',\n ' }',\n '}',\n ''\n ]\n );\n\n if (getPostProcessScript) {\n lines.push(...['', `if (${VAR_NAME}) {`, ` ${getPostProcessScript(VAR_NAME)};`, '}', '']);\n }\n\n lines.push(`${webpackPublicPathVariable} = ${VAR_NAME};`);\n\n return joinLines(lines, linePrefix);\n}\n"]}
|
package/lib-esm/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
export { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
|
|
4
|
+
export { SetPublicPathPlugin } from './SetPublicPathPlugin';
|
|
5
|
+
export { SetPublicPathCurrentScriptPlugin } from './SetPublicPathCurrentScriptPlugin';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EACL,mBAAmB,EAMpB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gCAAgC,EAAE,MAAM,oCAAoC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nexport { SetPublicPathPluginBase } from './SetPublicPathPluginBase';\nexport {\n SetPublicPathPlugin,\n type ISetWebpackPublicPathOptions,\n type ISetWebpackPublicPathPluginOptions,\n type IScriptNameAssetNameOptions,\n type IScriptNameOptions,\n type IScriptNameRegexOptions\n} from './SetPublicPathPlugin';\nexport { SetPublicPathCurrentScriptPlugin } from './SetPublicPathCurrentScriptPlugin';\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/set-webpack-public-path-plugin",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.1",
|
|
4
4
|
"description": "This plugin sets the webpack public path at runtime.",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "./lib-commonjs/index.js",
|
|
6
|
+
"module": "./lib-esm/index.js",
|
|
7
|
+
"types": "./dist/set-webpack-public-path-plugin.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/set-webpack-public-path-plugin.d.ts",
|
|
11
|
+
"import": "./lib-esm/index.js",
|
|
12
|
+
"require": "./lib-commonjs/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./lib/*": {
|
|
15
|
+
"types": "./lib-dts/*.d.ts",
|
|
16
|
+
"import": "./lib-esm/*.js",
|
|
17
|
+
"require": "./lib-commonjs/*.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
"*": {
|
|
23
|
+
"lib/*": [
|
|
24
|
+
"lib-dts/*"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
7
28
|
"license": "MIT",
|
|
8
29
|
"repository": {
|
|
9
30
|
"type": "git",
|
|
@@ -20,17 +41,18 @@
|
|
|
20
41
|
}
|
|
21
42
|
},
|
|
22
43
|
"dependencies": {
|
|
23
|
-
"@rushstack/node-core-library": "5.
|
|
24
|
-
"@rushstack/webpack-plugin-utilities": "0.
|
|
44
|
+
"@rushstack/node-core-library": "5.20.0",
|
|
45
|
+
"@rushstack/webpack-plugin-utilities": "0.6.1"
|
|
25
46
|
},
|
|
26
47
|
"devDependencies": {
|
|
27
48
|
"@types/node": "20.17.19",
|
|
28
49
|
"eslint": "~9.37.0",
|
|
29
50
|
"memfs": "4.12.0",
|
|
30
|
-
"webpack": "~5.105.
|
|
31
|
-
"
|
|
32
|
-
"
|
|
51
|
+
"webpack": "~5.105.2",
|
|
52
|
+
"local-node-rig": "1.0.0",
|
|
53
|
+
"@rushstack/heft": "1.2.0"
|
|
33
54
|
},
|
|
55
|
+
"sideEffects": false,
|
|
34
56
|
"scripts": {
|
|
35
57
|
"build": "heft build --clean",
|
|
36
58
|
"_phase:build": "heft run --only build -- --clean"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|