@rushstack/set-webpack-public-path-plugin 5.0.1 → 5.1.0

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/README.md CHANGED
@@ -4,13 +4,48 @@
4
4
 
5
5
  `npm install @rushstack/set-webpack-public-path-plugin --save-dev`
6
6
 
7
- ## Overview
7
+ ## Mode 1: Using the `document.currentScript` API
8
+
9
+ ### Overview
10
+
11
+ This plugin wraps the entire webpack bundle in an immediately executed function expression (IIFE) that sets a variable
12
+ to the value of `document.currentScript` and then injects code that extracts the current script's base path from
13
+ the `src` attribute when setting the `__webpack_public_path__` variable.
14
+
15
+ This is similar to the `output.publicPath = 'auto'` option, but differs in two important ways:
16
+
17
+ 1. It does not contain any fallback logic to look at `<script />` elements
18
+ 2. It stores the `document.currentScript` value immediately when the bundle is executed, not when
19
+ the runtime is executed. This is important when the bundle's factory function is called by another script, like
20
+ when an AMD output target is produced.
21
+
22
+ ### Plugin
23
+
24
+ To use the plugin, add it to the `plugins` array of your Webpack config. For example:
25
+
26
+ ```JavaScript
27
+ import { SetPublicPathCurrentScriptPlugin } from '@rushstack/set-webpack-public-path-plugin';
28
+
29
+ {
30
+ plugins: [
31
+ new SetPublicPathCurrentScriptPlugin()
32
+ ]
33
+ }
34
+ ```
35
+
36
+ ### Options
37
+
38
+ This plugin has no options.
39
+
40
+ ## Mode 2: Automatic public path detection via regular expression
41
+
42
+ ### Overview
8
43
 
9
44
  This simple plugin uses a specified regular expression or the emitted asset name to set the `__webpack_public_path__`
10
45
  variable. This is useful for scenarios where the Webpack automatic public path detection does not work. For example,
11
46
  when emitting AMD-style assets that are initialized by a callback.
12
47
 
13
- # Plugin
48
+ ### Plugin
14
49
 
15
50
  To use the plugin, add it to the `plugins` array of your Webpack config. For example:
16
51
 
@@ -24,7 +59,7 @@ import { SetPublicPathPlugin } from '@rushstack/set-webpack-public-path-plugin';
24
59
  }
25
60
  ```
26
61
 
27
- ## Options
62
+ ### Options
28
63
 
29
64
  #### `scriptName = { }`
30
65
 
@@ -49,7 +84,7 @@ property. `useAssetName` is exclusive to `name` and `isTokenized`.
49
84
 
50
85
  This option is exclusive to other options. If it is set, `systemJs`, `publicPath`, and `urlPrefix` will be ignored.
51
86
 
52
- #### `regexVariable = '...'`
87
+ ##### `regexVariable = '...'`
53
88
 
54
89
  Check for a variable with name `...` on the page and use its value as a regular expression against script paths to
55
90
  the bundle's script. If a value `foo` is passed into `regexVariable`, the produced bundle will look for a variable
@@ -59,7 +94,7 @@ detect the bundle's script.
59
94
  For example, if the `regexVariable` option is set to `scriptRegex` and `scriptName` is set to `{ name: 'myscript' }`,
60
95
  consider two cases:
61
96
 
62
- ##### Case 1
97
+ ###### Case 1
63
98
 
64
99
  ```HTML
65
100
  <html>
@@ -77,7 +112,7 @@ consider two cases:
77
112
  In this case, because there is a `scriptRegex` variable defined on the page, the bundle will use its value
78
113
  (`/thescript/i`) to find the script.
79
114
 
80
- ##### Case 2
115
+ ###### Case 2
81
116
 
82
117
  ```HTML
83
118
  <html>
@@ -92,7 +127,7 @@ In this case, because there is a `scriptRegex` variable defined on the page, the
92
127
  In this case, because there is not a `scriptRegex` variable defined on the page, the bundle will use the value
93
128
  passed into the `scriptName` option to find the script.
94
129
 
95
- #### `getPostProcessScript = (variableName) => { ... }`
130
+ ##### `getPostProcessScript = (variableName) => { ... }`
96
131
 
97
132
  A function that returns a snippet of code that manipulates the variable with the name that's specified in the
98
133
  parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified
@@ -111,7 +146,7 @@ the public path variable will have `/assets/` appended to the found path.
111
146
 
112
147
  Note that the existing value of the variable already ends in a slash (`/`).
113
148
 
114
- #### `preferLastFoundScript = false`
149
+ ##### `preferLastFoundScript = false`
115
150
 
116
151
  If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.
117
152
  This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
@@ -1,9 +1,3 @@
1
- /**
2
- * This simple plugin sets the `__webpack_public_path__` variable to
3
- * a value specified in the arguments.
4
- * @packageDocumentation
5
- */
6
-
7
1
  import type webpack from 'webpack';
8
2
 
9
3
  /**
@@ -80,15 +74,36 @@ export declare interface ISetWebpackPublicPathPluginOptions extends ISetWebpackP
80
74
  scriptName: IScriptNameOptions;
81
75
  }
82
76
 
77
+ /**
78
+ * This simple plugin wraps the webpack bundle in an IIFE that sets a the `document.currentScript` value to a variable
79
+ * that is then used to populate the `__webpack_public_path__` variable.
80
+ *
81
+ * @public
82
+ */
83
+ export declare class SetPublicPathCurrentScriptPlugin extends SetPublicPathPluginBase {
84
+ constructor();
85
+ protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
86
+ }
87
+
83
88
  /**
84
89
  * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.
85
90
  *
86
91
  * @public
87
92
  */
88
- export declare class SetPublicPathPlugin implements webpack.WebpackPluginInstance {
93
+ export declare class SetPublicPathPlugin extends SetPublicPathPluginBase {
89
94
  readonly options: ISetWebpackPublicPathPluginOptions;
90
95
  constructor(options: ISetWebpackPublicPathPluginOptions);
96
+ protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
97
+ }
98
+
99
+ /**
100
+ * @public
101
+ */
102
+ export declare abstract class SetPublicPathPluginBase implements webpack.WebpackPluginInstance {
103
+ private readonly _pluginName;
104
+ constructor(pluginName: string);
91
105
  apply(compiler: webpack.Compiler): void;
106
+ protected abstract _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
92
107
  }
93
108
 
94
109
  export { }
@@ -0,0 +1,13 @@
1
+ import type webpack from 'webpack';
2
+ import { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
3
+ /**
4
+ * This simple plugin wraps the webpack bundle in an IIFE that sets a the `document.currentScript` value to a variable
5
+ * that is then used to populate the `__webpack_public_path__` variable.
6
+ *
7
+ * @public
8
+ */
9
+ export declare class SetPublicPathCurrentScriptPlugin extends SetPublicPathPluginBase {
10
+ constructor();
11
+ protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
12
+ }
13
+ //# sourceMappingURL=SetPublicPathCurrentScriptPlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetPublicPathCurrentScriptPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathCurrentScriptPlugin.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAcpE;;;;;GAKG;AACH,qBAAa,gCAAiC,SAAQ,uBAAuB;;IAK3E,SAAS,CAAC,iBAAiB,CAAC,WAAW,EAAE,OAAO,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,GAAG,IAAI;CAgEjG"}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.SetPublicPathCurrentScriptPlugin = void 0;
6
+ const SetPublicPathPluginBase_1 = require("./SetPublicPathPluginBase");
7
+ const PLUGIN_NAME = 'set-webpack-public-path-current-script-plugin';
8
+ const CURRENT_SCRIPT_VARIABLE_NAME = '__RUSHSTACK_CURRENT_SCRIPT__';
9
+ const PUBLIC_PATH_VARIABLE_NAME = '_publicPath';
10
+ /**
11
+ * This simple plugin wraps the webpack bundle in an IIFE that sets a the `document.currentScript` value to a variable
12
+ * that is then used to populate the `__webpack_public_path__` variable.
13
+ *
14
+ * @public
15
+ */
16
+ class SetPublicPathCurrentScriptPlugin extends SetPublicPathPluginBase_1.SetPublicPathPluginBase {
17
+ constructor() {
18
+ super(PLUGIN_NAME);
19
+ }
20
+ _applyCompilation(thisWebpack, compilation) {
21
+ var _a;
22
+ const outputLibraryType = (_a = compilation.options.output.library) === null || _a === void 0 ? void 0 : _a.type;
23
+ switch (outputLibraryType) {
24
+ case 'var':
25
+ case 'module':
26
+ compilation.errors.push(new thisWebpack.WebpackError(`The "${outputLibraryType}" output.library.type is not supported by the ${SetPublicPathCurrentScriptPlugin.name}` +
27
+ ' plugin. Including this plugin with produce unexpected or invalid results.'));
28
+ break;
29
+ }
30
+ class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
31
+ constructor() {
32
+ super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
33
+ }
34
+ generate() {
35
+ return [
36
+ `var ${PUBLIC_PATH_VARIABLE_NAME} = ${CURRENT_SCRIPT_VARIABLE_NAME} ? ${CURRENT_SCRIPT_VARIABLE_NAME}.src : '';`,
37
+ `${thisWebpack.RuntimeGlobals.publicPath} = ${PUBLIC_PATH_VARIABLE_NAME}.slice(0, ${PUBLIC_PATH_VARIABLE_NAME}.lastIndexOf('/') + 1);`
38
+ ].join('\n');
39
+ }
40
+ }
41
+ const runtimeModule = new SetPublicPathRuntimeModule();
42
+ function appliesToChunk(chunk, codeGenerationResults) {
43
+ return chunk.hasRuntime() && codeGenerationResults.has(runtimeModule, chunk.runtime);
44
+ }
45
+ compilation.hooks.runtimeRequirementInTree
46
+ .for(thisWebpack.RuntimeGlobals.publicPath)
47
+ .tap(PLUGIN_NAME, (chunk, set) => {
48
+ compilation.addRuntimeModule(chunk, runtimeModule);
49
+ });
50
+ const javascriptModulesPluginHooks = thisWebpack.javascript.JavascriptModulesPlugin.getCompilationHooks(compilation);
51
+ javascriptModulesPluginHooks.render.tap({ name: PLUGIN_NAME, stage: Number.MAX_SAFE_INTEGER }, (source, { codeGenerationResults, chunk }) => {
52
+ if (appliesToChunk(chunk, codeGenerationResults)) {
53
+ return new thisWebpack.sources.ConcatSource(`(()=>{ var ${CURRENT_SCRIPT_VARIABLE_NAME} = document.currentScript; `, source, '})();');
54
+ }
55
+ else {
56
+ return source;
57
+ }
58
+ });
59
+ javascriptModulesPluginHooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash, { codeGenerationResults }) => {
60
+ hash.update(PLUGIN_NAME);
61
+ if (appliesToChunk(chunk, codeGenerationResults)) {
62
+ hash.update('set-public-path');
63
+ }
64
+ });
65
+ }
66
+ }
67
+ exports.SetPublicPathCurrentScriptPlugin = SetPublicPathCurrentScriptPlugin;
68
+ //# sourceMappingURL=SetPublicPathCurrentScriptPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetPublicPathCurrentScriptPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathCurrentScriptPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAG3D,uEAAoE;AAEpE,MAAM,WAAW,GAAW,+CAA+C,CAAC;AAE5E,MAAM,4BAA4B,GAAW,8BAA8B,CAAC;AAC5E,MAAM,yBAAyB,GAAW,aAAa,CAAC;AASxD;;;;;GAKG;AACH,MAAa,gCAAiC,SAAQ,iDAAuB;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;QACvF,QAAQ,iBAAiB,EAAE,CAAC;YAC1B,KAAK,KAAK,CAAC;YACX,KAAK,QAAQ;gBACX,WAAW,CAAC,MAAM,CAAC,IAAI,CACrB,IAAI,WAAW,CAAC,YAAY,CAC1B,QAAQ,iBAAiB,iDAAiD,gCAAgC,CAAC,IAAI,EAAE;oBAC/G,4EAA4E,CAC/E,CACF,CAAC;gBACF,MAAM;QACV,CAAC;QAED,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,CA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EAAE,EAAE;YACjG,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YACzB,IAAI,cAAc,CAAC,KAAK,EAAE,qBAAqB,CAAC,EAAE,CAAC;gBACjD,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;YACjC,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AArED,4EAqEC","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';\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 switch (outputLibraryType) {\n case 'var':\n case 'module':\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 break;\n }\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}\n"]}
@@ -1,4 +1,5 @@
1
1
  import type webpack from 'webpack';
2
+ import { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
2
3
  /**
3
4
  * The base options for setting the webpack public path at runtime.
4
5
  *
@@ -73,9 +74,9 @@ export interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPat
73
74
  *
74
75
  * @public
75
76
  */
76
- export declare class SetPublicPathPlugin implements webpack.WebpackPluginInstance {
77
+ export declare class SetPublicPathPlugin extends SetPublicPathPluginBase {
77
78
  readonly options: ISetWebpackPublicPathPluginOptions;
78
79
  constructor(options: ISetWebpackPublicPathPluginOptions);
79
- apply(compiler: webpack.Compiler): void;
80
+ protected _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
80
81
  }
81
82
  //# sourceMappingURL=SetPublicPathPlugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SetPublicPathPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAInC;;;;GAIG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAEnD;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,YAAY,EAAE,IAAI,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,2BAA2B,GAAG,uBAAuB,CAAC;AAMvF;;;;GAIG;AACH,MAAM,WAAW,kCAAmC,SAAQ,4BAA4B;IACtF;;OAEG;IACH,UAAU,EAAE,kBAAkB,CAAC;CAChC;AAcD;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,OAAO,CAAC,qBAAqB;IACvE,SAAgB,OAAO,EAAE,kCAAkC,CAAC;gBAEzC,OAAO,EAAE,kCAAkC;IAWvD,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;CAkI/C"}
1
+ {"version":3,"file":"SetPublicPathPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAGnC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAEpE;;;;GAIG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAEnD;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,YAAY,EAAE,IAAI,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,2BAA2B,GAAG,uBAAuB,CAAC;AAMvF;;;;GAIG;AACH,MAAM,WAAW,kCAAmC,SAAQ,4BAA4B;IACtF;;OAEG;IACH,UAAU,EAAE,kBAAkB,CAAC;CAChC;AAcD;;;;GAIG;AACH,qBAAa,mBAAoB,SAAQ,uBAAuB;IAC9D,SAAgB,OAAO,EAAE,kCAAkC,CAAC;gBAEzC,OAAO,EAAE,kCAAkC;IAY9D,SAAS,CAAC,iBAAiB,CAAC,WAAW,EAAE,OAAO,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,GAAG,IAAI;CAqFjG"}
@@ -3,9 +3,9 @@
3
3
  // See LICENSE in the project root for license information.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.SetPublicPathPlugin = void 0;
6
- const webpack_plugin_utilities_1 = require("@rushstack/webpack-plugin-utilities");
7
6
  const node_core_library_1 = require("@rushstack/node-core-library");
8
7
  const codeGenerator_1 = require("./codeGenerator");
8
+ const SetPublicPathPluginBase_1 = require("./SetPublicPathPluginBase");
9
9
  const SHOULD_REPLACE_ASSET_NAME_TOKEN = Symbol('set-public-path-plugin-should-replace-asset-name');
10
10
  const PLUGIN_NAME = 'set-webpack-public-path';
11
11
  const ASSET_NAME_TOKEN = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';
@@ -14,8 +14,9 @@ const ASSET_NAME_TOKEN = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';
14
14
  *
15
15
  * @public
16
16
  */
17
- class SetPublicPathPlugin {
17
+ class SetPublicPathPlugin extends SetPublicPathPluginBase_1.SetPublicPathPluginBase {
18
18
  constructor(options) {
19
+ super(PLUGIN_NAME);
19
20
  this.options = options;
20
21
  const scriptNameOptions = options.scriptName;
21
22
  if (scriptNameOptions.useAssetName && scriptNameOptions.name) {
@@ -25,98 +26,68 @@ class SetPublicPathPlugin {
25
26
  throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');
26
27
  }
27
28
  }
28
- apply(compiler) {
29
- if (!webpack_plugin_utilities_1.VersionDetection.isWebpack5(compiler)) {
30
- const thisPackageJson = node_core_library_1.PackageJsonLookup.loadOwnPackageJson(__dirname);
31
- throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 5. Use major version 4 of ` +
32
- `${thisPackageJson.name} for Webpack 4 support.`);
33
- }
34
- const thisWebpack = compiler.webpack;
35
- const initialOutputPublicPathSetting = compiler.options.output.publicPath;
36
- compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
37
- if (initialOutputPublicPathSetting) {
38
- compilation.warnings.push(new compiler.webpack.WebpackError(`The "output.publicPath" option is set in the Webpack configuration. The ${SetPublicPathPlugin.name} ` +
39
- 'plugin may produce unexpected results. It is recommended that the "output.publicPath" configuration option ' +
40
- 'be unset when using this plugin.'));
29
+ _applyCompilation(thisWebpack, compilation) {
30
+ class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
31
+ constructor(pluginOptions) {
32
+ super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
33
+ this._pluginOptions = pluginOptions;
41
34
  }
42
- else {
43
- compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({
44
- name: PLUGIN_NAME,
45
- register: (tap) => {
46
- if (tap.name === 'RuntimePlugin') {
47
- // Disable the default public path runtime plugin
48
- return Object.assign(Object.assign({}, tap), { fn: () => {
49
- /* noop */
50
- } });
51
- }
52
- else {
53
- return tap;
54
- }
35
+ generate() {
36
+ const { name: regexpName, isTokenized: regexpIsTokenized, useAssetName } = this._pluginOptions.scriptName;
37
+ let regexName;
38
+ if (regexpName) {
39
+ regexName = regexpName;
40
+ if (regexpIsTokenized) {
41
+ regexName = regexName
42
+ .replace(/\[name\]/g, node_core_library_1.Text.escapeRegExp(this.chunk.name))
43
+ .replace(/\[hash\]/g, this.chunk.renderedHash || '');
55
44
  }
56
- });
57
- }
58
- class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
59
- constructor(pluginOptions) {
60
- super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
61
- this._pluginOptions = pluginOptions;
62
45
  }
63
- generate() {
64
- const { name: regexpName, isTokenized: regexpIsTokenized, useAssetName } = this._pluginOptions.scriptName;
65
- let regexName;
66
- if (regexpName) {
67
- regexName = regexpName;
68
- if (regexpIsTokenized) {
69
- regexName = regexName
70
- .replace(/\[name\]/g, node_core_library_1.Text.escapeRegExp(this.chunk.name))
71
- .replace(/\[hash\]/g, this.chunk.renderedHash || '');
72
- }
73
- }
74
- else if (useAssetName) {
75
- this.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;
76
- regexName = ASSET_NAME_TOKEN;
77
- }
78
- else {
79
- throw new Error('scriptName.name or scriptName.useAssetName must be set');
80
- }
81
- const moduleOptions = Object.assign({ webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath, regexName }, this._pluginOptions);
82
- return (0, codeGenerator_1.getSetPublicPathCode)(moduleOptions);
46
+ else if (useAssetName) {
47
+ this.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;
48
+ regexName = ASSET_NAME_TOKEN;
49
+ }
50
+ else {
51
+ throw new Error('scriptName.name or scriptName.useAssetName must be set');
83
52
  }
53
+ const moduleOptions = Object.assign({ webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath, regexName }, this._pluginOptions);
54
+ return (0, codeGenerator_1.getSetPublicPathCode)(moduleOptions);
84
55
  }
85
- compilation.hooks.runtimeRequirementInTree
86
- .for(thisWebpack.RuntimeGlobals.publicPath)
87
- .tap(PLUGIN_NAME, (chunk, set) => {
88
- compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));
89
- });
90
- compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
91
- for (const chunkGroup of compilation.chunkGroups) {
92
- for (const chunk of chunkGroup.chunks) {
93
- if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
94
- for (const assetFilename of chunk.files) {
95
- let escapedAssetFilename;
96
- if (assetFilename.match(/\.map$/)) {
97
- // Trim the ".map" extension
98
- escapedAssetFilename = assetFilename.substr(0, assetFilename.length - 4 /* '.map'.length */);
99
- escapedAssetFilename = node_core_library_1.Text.escapeRegExp(escapedAssetFilename);
100
- // source in sourcemaps is JSON-encoded
101
- escapedAssetFilename = JSON.stringify(escapedAssetFilename);
102
- // Trim the quotes from the JSON encoding
103
- escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1);
104
- }
105
- else {
106
- escapedAssetFilename = node_core_library_1.Text.escapeRegExp(assetFilename);
107
- }
108
- const asset = assets[assetFilename];
109
- const newAsset = new thisWebpack.sources.ReplaceSource(asset);
110
- const sourceString = asset.source().toString();
111
- for (let index = sourceString.indexOf(ASSET_NAME_TOKEN); index >= 0; index = sourceString.indexOf(ASSET_NAME_TOKEN, index + 1)) {
112
- newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);
113
- }
114
- assets[assetFilename] = newAsset;
56
+ }
57
+ compilation.hooks.runtimeRequirementInTree
58
+ .for(thisWebpack.RuntimeGlobals.publicPath)
59
+ .tap(PLUGIN_NAME, (chunk, set) => {
60
+ compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));
61
+ });
62
+ compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
63
+ for (const chunkGroup of compilation.chunkGroups) {
64
+ for (const chunk of chunkGroup.chunks) {
65
+ if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
66
+ for (const assetFilename of chunk.files) {
67
+ let escapedAssetFilename;
68
+ if (assetFilename.match(/\.map$/)) {
69
+ // Trim the ".map" extension
70
+ escapedAssetFilename = assetFilename.slice(0, -4 /* '.map'.length */);
71
+ escapedAssetFilename = node_core_library_1.Text.escapeRegExp(escapedAssetFilename);
72
+ // source in sourcemaps is JSON-encoded
73
+ escapedAssetFilename = JSON.stringify(escapedAssetFilename);
74
+ // Trim the quotes from the JSON encoding
75
+ escapedAssetFilename = escapedAssetFilename.slice(1, -1);
115
76
  }
77
+ else {
78
+ escapedAssetFilename = node_core_library_1.Text.escapeRegExp(assetFilename);
79
+ }
80
+ const asset = assets[assetFilename];
81
+ const newAsset = new thisWebpack.sources.ReplaceSource(asset);
82
+ const sourceString = asset.source().toString();
83
+ for (let index = sourceString.lastIndexOf(ASSET_NAME_TOKEN); index >= 0; index = sourceString.lastIndexOf(ASSET_NAME_TOKEN, index - 1)) {
84
+ newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);
85
+ }
86
+ assets[assetFilename] = newAsset;
116
87
  }
117
88
  }
118
89
  }
119
- });
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;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhJD,kDAgJC","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 { VersionDetection } from '@rushstack/webpack-plugin-utilities';\nimport { Text, PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\n\nimport type webpack from 'webpack';\n\nimport { type IInternalOptions, getSetPublicPathCode } from './codeGenerator';\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 implements webpack.WebpackPluginInstance {\n public readonly options: ISetWebpackPublicPathPluginOptions;\n\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\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 public apply(compiler: webpack.Compiler): void {\n if (!VersionDetection.isWebpack5(compiler)) {\n const thisPackageJson: IPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);\n throw new Error(\n `The ${SetPublicPathPlugin.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(PLUGIN_NAME, (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 ${SetPublicPathPlugin.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: PLUGIN_NAME,\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 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 let regexName: string;\n if (regexpName) {\n regexName = regexpName;\n if (regexpIsTokenized) {\n regexName = regexName\n .replace(/\\[name\\]/g, Text.escapeRegExp(this.chunk.name))\n .replace(/\\[hash\\]/g, this.chunk.renderedHash || '');\n }\n } else if (useAssetName) {\n (this.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.substr(\n 0,\n assetFilename.length - 4 /* '.map'.length */\n );\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.substring(1, escapedAssetFilename.length - 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.indexOf(ASSET_NAME_TOKEN);\n index >= 0;\n index = sourceString.indexOf(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}\n"]}
1
+ {"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,oEAAoD;AAIpD,mDAA8E;AAC9E,uEAAoE;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,MAAa,mBAAoB,SAAQ,iDAAuB;IAG9D,YAAm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,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,wBAAI,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;AApGD,kDAoGC","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 { Text } from '@rushstack/node-core-library';\n\nimport type webpack from 'webpack';\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 let regexName: string;\n if (regexpName) {\n regexName = regexpName;\n if (regexpIsTokenized) {\n regexName = regexName\n .replace(/\\[name\\]/g, Text.escapeRegExp(this.chunk.name))\n .replace(/\\[hash\\]/g, this.chunk.renderedHash || '');\n }\n } else if (useAssetName) {\n (this.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,11 @@
1
+ import type webpack from 'webpack';
2
+ /**
3
+ * @public
4
+ */
5
+ export declare abstract class SetPublicPathPluginBase implements webpack.WebpackPluginInstance {
6
+ private readonly _pluginName;
7
+ constructor(pluginName: string);
8
+ apply(compiler: webpack.Compiler): void;
9
+ protected abstract _applyCompilation(thisWebpack: typeof webpack, compilation: webpack.Compilation): void;
10
+ }
11
+ //# sourceMappingURL=SetPublicPathPluginBase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetPublicPathPluginBase.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPluginBase.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC;;GAEG;AACH,8BAAsB,uBAAwB,YAAW,OAAO,CAAC,qBAAqB;IACpF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAElB,UAAU,EAAE,MAAM;IAI9B,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;IA8C9C,SAAS,CAAC,QAAQ,CAAC,iBAAiB,CAAC,WAAW,EAAE,OAAO,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,GAAG,IAAI;CAC1G"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
+ // See LICENSE in the project root for license information.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.SetPublicPathPluginBase = void 0;
6
+ const webpack_plugin_utilities_1 = require("@rushstack/webpack-plugin-utilities");
7
+ const node_core_library_1 = require("@rushstack/node-core-library");
8
+ /**
9
+ * @public
10
+ */
11
+ class SetPublicPathPluginBase {
12
+ constructor(pluginName) {
13
+ this._pluginName = pluginName;
14
+ }
15
+ apply(compiler) {
16
+ if (!webpack_plugin_utilities_1.VersionDetection.isWebpack5(compiler)) {
17
+ const thisPackageJson = node_core_library_1.PackageJsonLookup.loadOwnPackageJson(__dirname);
18
+ throw new Error(`The ${this.constructor.name} plugin requires Webpack 5. Use major version 4 of ` +
19
+ `${thisPackageJson.name} for Webpack 4 support.`);
20
+ }
21
+ const thisWebpack = compiler.webpack;
22
+ const initialOutputPublicPathSetting = compiler.options.output.publicPath;
23
+ compiler.hooks.thisCompilation.tap(this._pluginName, (compilation) => {
24
+ if (initialOutputPublicPathSetting) {
25
+ compilation.warnings.push(new compiler.webpack.WebpackError(`The "output.publicPath" option is set in the Webpack configuration. The ${this.constructor.name} ` +
26
+ 'plugin may produce unexpected results. It is recommended that the "output.publicPath" configuration option ' +
27
+ 'be unset when using this plugin.'));
28
+ }
29
+ else {
30
+ compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({
31
+ name: this._pluginName,
32
+ register: (tap) => {
33
+ if (tap.name === 'RuntimePlugin') {
34
+ // Disable the default public path runtime plugin
35
+ return Object.assign(Object.assign({}, tap), { fn: () => {
36
+ /* noop */
37
+ } });
38
+ }
39
+ else {
40
+ return tap;
41
+ }
42
+ }
43
+ });
44
+ }
45
+ this._applyCompilation(thisWebpack, compilation);
46
+ });
47
+ }
48
+ }
49
+ exports.SetPublicPathPluginBase = SetPublicPathPluginBase;
50
+ //# sourceMappingURL=SetPublicPathPluginBase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SetPublicPathPluginBase.js","sourceRoot":"","sources":["../src/SetPublicPathPluginBase.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,kFAAuE;AACvE,oEAAoF;AAIpF;;GAEG;AACH,MAAsB,uBAAuB;IAG3C,YAAmB,UAAkB;QACnC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;IAChC,CAAC;IAEM,KAAK,CAAC,QAA0B;QACrC,IAAI,CAAC,2CAAgB,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3C,MAAM,eAAe,GAAiB,qCAAiB,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,uCACK,GAAG,KACN,EAAE,EAAE,GAAG,EAAE;oCACP,UAAU;gCACZ,CAAC,IACD;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;AAtDD,0DAsDC","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 { VersionDetection } from '@rushstack/webpack-plugin-utilities';\nimport { PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\n\nimport type webpack from 'webpack';\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"]}
package/lib/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
- /**
2
- * This simple plugin sets the `__webpack_public_path__` variable to
3
- * a value specified in the arguments.
4
- * @packageDocumentation
5
- */
1
+ export { SetPublicPathPluginBase } from './SetPublicPathPluginBase';
6
2
  export { SetPublicPathPlugin, ISetWebpackPublicPathOptions, ISetWebpackPublicPathPluginOptions, IScriptNameAssetNameOptions, IScriptNameOptions, IScriptNameRegexOptions } from './SetPublicPathPlugin';
3
+ export { SetPublicPathCurrentScriptPlugin } from './SetPublicPathCurrentScriptPlugin';
7
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AAEH,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,kCAAkC,EAClC,2BAA2B,EAC3B,kBAAkB,EAClB,uBAAuB,EACxB,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,kCAAkC,EAClC,2BAA2B,EAC3B,kBAAkB,EAClB,uBAAuB,EACxB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gCAAgC,EAAE,MAAM,oCAAoC,CAAC"}
package/lib/index.js CHANGED
@@ -2,12 +2,11 @@
2
2
  // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3
3
  // See LICENSE in the project root for license information.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.SetPublicPathPlugin = void 0;
6
- /**
7
- * This simple plugin sets the `__webpack_public_path__` variable to
8
- * a value specified in the arguments.
9
- * @packageDocumentation
10
- */
5
+ exports.SetPublicPathCurrentScriptPlugin = exports.SetPublicPathPlugin = exports.SetPublicPathPluginBase = void 0;
6
+ var SetPublicPathPluginBase_1 = require("./SetPublicPathPluginBase");
7
+ Object.defineProperty(exports, "SetPublicPathPluginBase", { enumerable: true, get: function () { return SetPublicPathPluginBase_1.SetPublicPathPluginBase; } });
11
8
  var SetPublicPathPlugin_1 = require("./SetPublicPathPlugin");
12
9
  Object.defineProperty(exports, "SetPublicPathPlugin", { enumerable: true, get: function () { return SetPublicPathPlugin_1.SetPublicPathPlugin; } });
10
+ var SetPublicPathCurrentScriptPlugin_1 = require("./SetPublicPathCurrentScriptPlugin");
11
+ Object.defineProperty(exports, "SetPublicPathCurrentScriptPlugin", { enumerable: true, get: function () { return SetPublicPathCurrentScriptPlugin_1.SetPublicPathCurrentScriptPlugin; } });
13
12
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AAEH,6DAO+B;AAN7B,0HAAA,mBAAmB,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * This simple plugin sets the `__webpack_public_path__` variable to\n * a value specified in the arguments.\n * @packageDocumentation\n */\n\nexport {\n SetPublicPathPlugin,\n ISetWebpackPublicPathOptions,\n ISetWebpackPublicPathPluginOptions,\n IScriptNameAssetNameOptions,\n IScriptNameOptions,\n IScriptNameRegexOptions\n} from './SetPublicPathPlugin';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,qEAAoE;AAA3D,kIAAA,uBAAuB,OAAA;AAChC,6DAO+B;AAN7B,0HAAA,mBAAmB,OAAA;AAOrB,uFAAsF;AAA7E,oJAAA,gCAAgC,OAAA","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 ISetWebpackPublicPathOptions,\n ISetWebpackPublicPathPluginOptions,\n IScriptNameAssetNameOptions,\n IScriptNameOptions,\n IScriptNameRegexOptions\n} from './SetPublicPathPlugin';\nexport { SetPublicPathCurrentScriptPlugin } from './SetPublicPathCurrentScriptPlugin';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/set-webpack-public-path-plugin",
3
- "version": "5.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "This plugin sets the webpack public path at runtime.",
5
5
  "main": "lib/index.js",
6
6
  "typings": "dist/set-webpack-public-path-plugin.d.ts",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "devDependencies": {
27
27
  "webpack": "~5.82.1",
28
+ "memfs": "3.4.3",
28
29
  "@rushstack/heft": "0.64.0",
29
30
  "local-node-rig": "1.0.0"
30
31
  },