@rushstack/set-webpack-public-path-plugin 3.2.88 → 3.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 CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@rushstack/set-webpack-public-path-plugin",
3
3
  "entries": [
4
+ {
5
+ "version": "3.3.1",
6
+ "tag": "@rushstack/set-webpack-public-path-plugin_v3.3.1",
7
+ "date": "Thu, 11 Nov 2021 01:17:02 GMT",
8
+ "comments": {
9
+ "patch": [
10
+ {
11
+ "comment": "Update typings to only import from \"webpack.\""
12
+ }
13
+ ]
14
+ }
15
+ },
16
+ {
17
+ "version": "3.3.0",
18
+ "tag": "@rushstack/set-webpack-public-path-plugin_v3.3.0",
19
+ "date": "Wed, 10 Nov 2021 16:09:47 GMT",
20
+ "comments": {
21
+ "minor": [
22
+ {
23
+ "comment": "Introduce a warning when running in Webpack 5."
24
+ }
25
+ ]
26
+ }
27
+ },
28
+ {
29
+ "version": "3.2.90",
30
+ "tag": "@rushstack/set-webpack-public-path-plugin_v3.2.90",
31
+ "date": "Sat, 06 Nov 2021 00:09:13 GMT",
32
+ "comments": {
33
+ "dependency": [
34
+ {
35
+ "comment": "Updating dependency \"@rushstack/heft\" to `0.42.2`"
36
+ },
37
+ {
38
+ "comment": "Updating dependency \"@rushstack/heft-node-rig\" to `1.2.30`"
39
+ }
40
+ ]
41
+ }
42
+ },
43
+ {
44
+ "version": "3.2.89",
45
+ "tag": "@rushstack/set-webpack-public-path-plugin_v3.2.89",
46
+ "date": "Fri, 05 Nov 2021 15:09:18 GMT",
47
+ "comments": {
48
+ "dependency": [
49
+ {
50
+ "comment": "Updating dependency \"@rushstack/eslint-config\" to `2.4.5`"
51
+ },
52
+ {
53
+ "comment": "Updating dependency \"@rushstack/heft\" to `0.42.1`"
54
+ },
55
+ {
56
+ "comment": "Updating dependency \"@rushstack/heft-node-rig\" to `1.2.29`"
57
+ }
58
+ ]
59
+ }
60
+ },
4
61
  {
5
62
  "version": "3.2.88",
6
63
  "tag": "@rushstack/set-webpack-public-path-plugin_v3.2.88",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,30 @@
1
1
  # Change Log - @rushstack/set-webpack-public-path-plugin
2
2
 
3
- This log was last generated on Thu, 28 Oct 2021 00:08:22 GMT and should not be manually modified.
3
+ This log was last generated on Thu, 11 Nov 2021 01:17:02 GMT and should not be manually modified.
4
+
5
+ ## 3.3.1
6
+ Thu, 11 Nov 2021 01:17:02 GMT
7
+
8
+ ### Patches
9
+
10
+ - Update typings to only import from "webpack."
11
+
12
+ ## 3.3.0
13
+ Wed, 10 Nov 2021 16:09:47 GMT
14
+
15
+ ### Minor changes
16
+
17
+ - Introduce a warning when running in Webpack 5.
18
+
19
+ ## 3.2.90
20
+ Sat, 06 Nov 2021 00:09:13 GMT
21
+
22
+ _Version update only_
23
+
24
+ ## 3.2.89
25
+ Fri, 05 Nov 2021 15:09:18 GMT
26
+
27
+ _Version update only_
4
28
 
5
29
  ## 3.2.88
6
30
  Thu, 28 Oct 2021 00:08:22 GMT
@@ -0,0 +1,114 @@
1
+ /**
2
+ * This simple plugin sets the `__webpack_public_path__` variable to
3
+ * a value specified in the arguments, optionally appended to the SystemJs baseURL
4
+ * property.
5
+ * @packageDocumentation
6
+ */
7
+
8
+ import type * as Webpack from 'webpack';
9
+
10
+ /**
11
+ * /**
12
+ * This function returns a block of JavaScript that maintains a global register of script tags.
13
+ *
14
+ * @param debug - If true, the code returned code is not minified. Defaults to false.
15
+ *
16
+ * @public
17
+ */
18
+ export declare function getGlobalRegisterCode(debug?: boolean): string;
19
+
20
+ /**
21
+ * The base options for setting the webpack public path at runtime.
22
+ *
23
+ * @public
24
+ */
25
+ export declare interface ISetWebpackPublicPathOptions {
26
+ /**
27
+ * Use the System.baseURL property if it is defined.
28
+ */
29
+ systemJs?: boolean;
30
+ /**
31
+ * Use the specified string as a URL prefix after the SystemJS path or the publicPath option.
32
+ * If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.
33
+ */
34
+ urlPrefix?: string;
35
+ /**
36
+ * Use the specified path as the base public path.
37
+ */
38
+ publicPath?: string;
39
+ /**
40
+ * Check for a variable with this name on the page and use its value as a regular expression against script paths to
41
+ * the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable
42
+ * called foo during initialization, and if a foo variable is found, use its value as a regular expression to detect
43
+ * the bundle's script.
44
+ *
45
+ * See the README for more information.
46
+ */
47
+ regexVariable?: string;
48
+ /**
49
+ * A function that returns a snippet of code that manipulates the variable with the name that's specified in the
50
+ * parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified
51
+ * in-place - the processed value should not be returned.
52
+ *
53
+ * See the README for more information.
54
+ */
55
+ getPostProcessScript?: (varName: string) => string;
56
+ /**
57
+ * If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.
58
+ * This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
59
+ */
60
+ preferLastFoundScript?: boolean;
61
+ /**
62
+ * If true, always include the public path-setting code. Don't try to detect if any chunks or assets are present.
63
+ */
64
+ skipDetection?: boolean;
65
+ }
66
+
67
+ /**
68
+ * Options for the set-webpack-public-path plugin.
69
+ *
70
+ * @public
71
+ */
72
+ export declare interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPathOptions {
73
+ /**
74
+ * An object that describes how the public path should be discovered.
75
+ */
76
+ scriptName?: {
77
+ /**
78
+ * If set to true, use the webpack generated asset's name. This option is not compatible with
79
+ * andy other scriptName options.
80
+ */
81
+ useAssetName?: boolean;
82
+ /**
83
+ * A regular expression expressed as a string to be applied to all script paths on the page.
84
+ */
85
+ name?: string;
86
+ /**
87
+ * If true, the name property is tokenized.
88
+ *
89
+ * See the README for more information.
90
+ */
91
+ isTokenized?: boolean;
92
+ };
93
+ }
94
+
95
+ /**
96
+ * @public
97
+ */
98
+ export declare const registryVariableName: string;
99
+
100
+ /**
101
+ * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments,
102
+ * optionally appended to the SystemJs baseURL property.
103
+ *
104
+ * @public
105
+ */
106
+ export declare class SetPublicPathPlugin implements Webpack.Plugin {
107
+ options: ISetWebpackPublicPathPluginOptions;
108
+ constructor(options: ISetWebpackPublicPathPluginOptions);
109
+ apply(compiler: Webpack.Compiler): void;
110
+ private _detectAssetsOrChunks;
111
+ private _getStartupCode;
112
+ }
113
+
114
+ export { }
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.18.17"
8
+ "packageVersion": "7.18.19"
9
9
  }
10
10
  ]
11
11
  }
@@ -1,4 +1,4 @@
1
- import * as Webpack from 'webpack';
1
+ import type * as Webpack from 'webpack';
2
2
  /**
3
3
  * The base options for setting the webpack public path at runtime.
4
4
  *
@@ -1 +1 @@
1
- {"version":3,"file":"SetPublicPathPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAKnC;;;;GAIG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;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;IAEhC;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;GAIG;AACH,MAAM,WAAW,kCAAmC,SAAQ,4BAA4B;IACtF;;OAEG;IACH,UAAU,CAAC,EAAE;QACX;;;WAGG;QACH,YAAY,CAAC,EA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;;GAKG;AACH,qBAAa,mBAAoB,YAAW,OAAO,CAAC,MAAM;IACjD,OAAO,EAAE,kCAAkC,CAAC;gBAEhC,OAAO,EAAE,kCAAkC;IAYvD,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;IAqG9C,OAAO,CAAC,qBAAqB;IAgB7B,OAAO,CAAC,eAAe;CA+BxB"}
@@ -4,12 +4,14 @@
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.SetPublicPathPlugin = void 0;
6
6
  const os_1 = require("os");
7
- const lodash_1 = require("lodash");
8
7
  const codeGenerator_1 = require("./codeGenerator");
9
8
  const SHOULD_REPLACE_ASSET_NAME_TOKEN = Symbol('set-public-path-plugin-should-replace-asset-name');
10
9
  const PLUGIN_NAME = 'set-webpack-public-path';
11
10
  const ASSET_NAME_TOKEN = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';
12
11
  const ASSET_NAME_TOKEN_REGEX = new RegExp(ASSET_NAME_TOKEN);
12
+ function escapeRegExp(str) {
13
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
14
+ }
13
15
  /**
14
16
  * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments,
15
17
  * optionally appended to the SystemJs baseURL property.
@@ -29,55 +31,78 @@ class SetPublicPathPlugin {
29
31
  }
30
32
  }
31
33
  apply(compiler) {
32
- const isWebpack4 = !!compiler.hooks;
33
- if (!isWebpack4) {
34
- throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4`);
34
+ var _a;
35
+ const isWebpack3OrEarlier = !compiler.hooks;
36
+ if (isWebpack3OrEarlier) {
37
+ throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4 or Webpack 5`);
35
38
  }
39
+ const webpackVersion = (_a = compiler.webpack) === null || _a === void 0 ? void 0 : _a.version;
40
+ const webpackMajorVersion = webpackVersion
41
+ ? Number(webpackVersion.substr(0, webpackVersion.indexOf('.')))
42
+ : 4;
36
43
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
37
- const mainTemplate = compilation.mainTemplate;
38
- mainTemplate.hooks.startup.tap(PLUGIN_NAME, (source, chunk, hash) => {
39
- const extendedChunk = chunk;
40
- const assetOrChunkFound = !!this.options.skipDetection || this._detectAssetsOrChunks(extendedChunk);
41
- if (assetOrChunkFound) {
42
- return this._getStartupCode({
43
- source,
44
- chunk: extendedChunk,
45
- hash,
46
- requireFn: mainTemplate.requireFn
47
- });
48
- }
49
- else {
50
- return source;
51
- }
52
- });
44
+ if (webpackMajorVersion === 4) {
45
+ const webpack4Compilation = compilation;
46
+ const mainTemplate = webpack4Compilation.mainTemplate;
47
+ mainTemplate.hooks.startup.tap(PLUGIN_NAME, (source, chunk, hash) => {
48
+ const extendedChunk = chunk;
49
+ const assetOrChunkFound = !!this.options.skipDetection || this._detectAssetsOrChunks(extendedChunk);
50
+ if (assetOrChunkFound) {
51
+ return this._getStartupCode({
52
+ source,
53
+ chunk: extendedChunk,
54
+ hash,
55
+ requireFn: mainTemplate.requireFn
56
+ });
57
+ }
58
+ else {
59
+ return source;
60
+ }
61
+ });
62
+ }
63
+ else {
64
+ // Webpack 5 has its own automatic public path code, so only apply for Webpack 4
65
+ const Webpack5Error = compiler
66
+ .webpack.WebpackError;
67
+ // Don't bother importing node-core-library for this
68
+ const thisPackageJson = require('../package.json');
69
+ compilation.warnings.push(new Webpack5Error(`Webpack 5 supports its own automatic public path detection, ` +
70
+ `so ${thisPackageJson.name} won't do anything in this compilation.`));
71
+ }
53
72
  });
54
- compiler.hooks.emit.tap(PLUGIN_NAME, (compilation) => {
55
- for (const chunkGroup of compilation.chunkGroups) {
56
- for (const chunk of chunkGroup.chunks) {
57
- if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
58
- for (const assetFilename of chunk.files) {
59
- let escapedAssetFilename;
60
- if (assetFilename.match(/\.map$/)) {
61
- escapedAssetFilename = assetFilename.substr(0, assetFilename.length - 4 /* '.map'.length */); // Trim the ".map" extension
62
- escapedAssetFilename = (0, lodash_1.escapeRegExp)(escapedAssetFilename);
63
- escapedAssetFilename = JSON.stringify(escapedAssetFilename); // source in sourcemaps is JSON-encoded
64
- escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1); // Trim the quotes from the JSON encoding
65
- }
66
- else {
67
- escapedAssetFilename = (0, lodash_1.escapeRegExp)(assetFilename);
73
+ // Webpack 5 has its own automatic public path code, so only apply for Webpack 4
74
+ if (webpackMajorVersion === 4) {
75
+ compiler.hooks.emit.tap(PLUGIN_NAME, (compilation) => {
76
+ for (const chunkGroup of compilation.chunkGroups) {
77
+ for (const chunk of chunkGroup.chunks) {
78
+ if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
79
+ for (const assetFilename of chunk.files) {
80
+ let escapedAssetFilename;
81
+ if (assetFilename.match(/\.map$/)) {
82
+ // Trim the ".map" extension
83
+ escapedAssetFilename = assetFilename.substr(0, assetFilename.length - 4 /* '.map'.length */);
84
+ escapedAssetFilename = escapeRegExp(escapedAssetFilename);
85
+ // source in sourcemaps is JSON-encoded
86
+ escapedAssetFilename = JSON.stringify(escapedAssetFilename);
87
+ // Trim the quotes from the JSON encoding
88
+ escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1);
89
+ }
90
+ else {
91
+ escapedAssetFilename = escapeRegExp(assetFilename);
92
+ }
93
+ const asset = compilation.assets[assetFilename];
94
+ const originalAssetSource = asset.source();
95
+ const originalAssetSize = asset.size();
96
+ const newAssetSource = originalAssetSource.replace(ASSET_NAME_TOKEN_REGEX, escapedAssetFilename);
97
+ const sizeDifference = assetFilename.length - ASSET_NAME_TOKEN.length;
98
+ asset.source = () => newAssetSource;
99
+ asset.size = () => originalAssetSize + sizeDifference;
68
100
  }
69
- const asset = compilation.assets[assetFilename];
70
- const originalAssetSource = asset.source();
71
- const originalAssetSize = asset.size();
72
- const newAssetSource = originalAssetSource.replace(ASSET_NAME_TOKEN_REGEX, escapedAssetFilename);
73
- const sizeDifference = assetFilename.length - ASSET_NAME_TOKEN.length;
74
- asset.source = () => newAssetSource;
75
- asset.size = () => originalAssetSize + sizeDifference;
76
101
  }
77
102
  }
78
103
  }
79
- }
80
- });
104
+ });
105
+ }
81
106
  }
82
107
  _detectAssetsOrChunks(chunk) {
83
108
  for (const chunkGroup of chunk.groupsIterable) {
@@ -93,7 +118,7 @@ class SetPublicPathPlugin {
93
118
  return false;
94
119
  }
95
120
  _getStartupCode(options) {
96
- const moduleOptions = (0, lodash_1.cloneDeep)(this.options);
121
+ const moduleOptions = Object.assign({}, this.options);
97
122
  // If this module has ownership over any chunks or assets, inject the public path code
98
123
  moduleOptions.webpackPublicPathVariable = `${options.requireFn}.p`;
99
124
  moduleOptions.linePrefix = ' ';
@@ -102,7 +127,7 @@ class SetPublicPathPlugin {
102
127
  moduleOptions.regexName = this.options.scriptName.name;
103
128
  if (this.options.scriptName.isTokenized) {
104
129
  moduleOptions.regexName = moduleOptions.regexName
105
- .replace(/\[name\]/g, (0, lodash_1.escapeRegExp)(options.chunk.name))
130
+ .replace(/\[name\]/g, escapeRegExp(options.chunk.name))
106
131
  .replace(/\[hash\]/g, options.chunk.renderedHash || '');
107
132
  }
108
133
  }
@@ -1 +1 @@
1
- {"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AACzB,mCAAiD;AAIjD,mDAAyE;AAmGzE,MAAM,+BAA+B,GAAkB,MAAM,CAC3D,kDAAkD,CACnD,CAAC;AAcF,MAAM,WAAW,GAAW,yBAAyB,CAAC;AAEtD,MAAM,gBAAgB,GAAW,kDAAkD,CAAC;AAEpF,MAAM,sBAAsB,GAAW,IAAI,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAEpE;;;;;GAKG;AACH,MAAa,mBAAmB;IAG9B,YAAmB,OAA2C;QAC5D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CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;yBACtD,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,KAAK,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;iBAC3D;aACF;iBAAM,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,YAAY,EAAE;gBAC/C,OAAO,CAAC,KAAK,CAAC,+BAA+B,CAAC,GAAG,IAAI,CAAC;gBAEtD,aAAa,CAAC,SAAS,GAAG,gBAAgB,CAAC;aAC5C;SACF;QAED,OAAO;YACL,gCAAgC;YAChC,gBAAgB;YAChB,IAAA,oCAAoB,EAAC,aAAa,EAAE,OAAO,CAAC,KAAK,CAAC;YAClD,OAAO;YACP,EAAE;YACF,OAAO,CAAC,MAAM;SACf,CAAC,IAAI,CAAC,QAAG,CAAC,CAAC;IACd,CAAC;CACF;AA7HD,kDA6HC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { EOL } from 'os';\r\nimport { cloneDeep, escapeRegExp } from 'lodash';\r\nimport * as Webpack from 'webpack';\r\nimport * as Tapable from 'tapable';\r\n\r\nimport { IInternalOptions, getSetPublicPathCode } from './codeGenerator';\r\n\r\n/**\r\n * The base options for setting the webpack public path at runtime.\r\n *\r\n * @public\r\n */\r\nexport interface ISetWebpackPublicPathOptions {\r\n /**\r\n * Use the System.baseURL property if it is defined.\r\n */\r\n systemJs?: boolean;\r\n\r\n /**\r\n * Use the specified string as a URL prefix after the SystemJS path or the publicPath option.\r\n * If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.\r\n */\r\n urlPrefix?: string;\r\n\r\n /**\r\n * Use the specified path as the base public path.\r\n */\r\n publicPath?: string;\r\n\r\n /**\r\n * Check for a variable with this name on the page and use its value as a regular expression against script paths to\r\n * the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable\r\n * called foo during initialization, and if a foo variable is found, use its value as a regular expression to detect\r\n * the bundle's script.\r\n *\r\n * See the README for more information.\r\n */\r\n regexVariable?: string;\r\n\r\n /**\r\n * A function that returns a snippet of code that manipulates the variable with the name that's specified in the\r\n * parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified\r\n * in-place - the processed value should not be returned.\r\n *\r\n * See the README for more information.\r\n */\r\n getPostProcessScript?: (varName: string) => string;\r\n\r\n /**\r\n * If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.\r\n * This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.\r\n */\r\n preferLastFoundScript?: boolean;\r\n\r\n /**\r\n * If true, always include the public path-setting code. Don't try to detect if any chunks or assets are present.\r\n */\r\n skipDetection?: boolean;\r\n}\r\n\r\n/**\r\n * Options for the set-webpack-public-path plugin.\r\n *\r\n * @public\r\n */\r\nexport interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPathOptions {\r\n /**\r\n * An object that describes how the public path should be discovered.\r\n */\r\n scriptName?: {\r\n /**\r\n * If set to true, use the webpack generated asset's name. This option is not compatible with\r\n * andy other scriptName options.\r\n */\r\n useAssetName?: boolean;\r\n\r\n /**\r\n * A regular expression expressed as a string to be applied to all script paths on the page.\r\n */\r\n name?: string;\r\n\r\n /**\r\n * If true, the name property is tokenized.\r\n *\r\n * See the README for more information.\r\n */\r\n isTokenized?: boolean;\r\n };\r\n}\r\n\r\ninterface IAsset {\r\n size(): number;\r\n source(): string;\r\n}\r\n\r\ninterface IExtendedMainTemplate {\r\n hooks: {\r\n jsonpScript?: Tapable.SyncWaterfallHook<string, Webpack.compilation.Chunk, string>;\r\n requireExtensions: Tapable.SyncWaterfallHook<string, Webpack.compilation.Chunk, string>;\r\n startup: Tapable.SyncHook<string, Webpack.compilation.Chunk, string>;\r\n };\r\n requireFn: string;\r\n}\r\n\r\nconst SHOULD_REPLACE_ASSET_NAME_TOKEN: unique symbol = Symbol(\r\n 'set-public-path-plugin-should-replace-asset-name'\r\n);\r\n\r\ninterface IExtendedChunk extends Webpack.compilation.Chunk {\r\n [SHOULD_REPLACE_ASSET_NAME_TOKEN]: boolean;\r\n forEachModule(callback: (module: Webpack.compilation.Module) => void): void;\r\n}\r\n\r\ninterface IStartupCodeOptions {\r\n source: string;\r\n chunk: IExtendedChunk;\r\n hash: string;\r\n requireFn: string;\r\n}\r\n\r\nconst PLUGIN_NAME: string = 'set-webpack-public-path';\r\n\r\nconst ASSET_NAME_TOKEN: string = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';\r\n\r\nconst ASSET_NAME_TOKEN_REGEX: RegExp = new RegExp(ASSET_NAME_TOKEN);\r\n\r\n/**\r\n * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments,\r\n * optionally appended to the SystemJs baseURL property.\r\n *\r\n * @public\r\n */\r\nexport class SetPublicPathPlugin implements Webpack.Plugin {\r\n public options: ISetWebpackPublicPathPluginOptions;\r\n\r\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\r\n this.options = options;\r\n\r\n if (options.scriptName) {\r\n if (options.scriptName.useAssetName && options.scriptName.name) {\r\n throw new Error('scriptName.userAssetName and scriptName.name must not be used together');\r\n } else if (options.scriptName.isTokenized && !options.scriptName.name) {\r\n throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');\r\n }\r\n }\r\n }\r\n\r\n public apply(compiler: Webpack.Compiler): void {\r\n const isWebpack4: boolean = !!compiler.hooks;\r\n\r\n if (!isWebpack4) {\r\n throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4`);\r\n }\r\n\r\n compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation: Webpack.compilation.Compilation) => {\r\n const mainTemplate: IExtendedMainTemplate =\r\n compilation.mainTemplate as unknown as IExtendedMainTemplate;\r\n mainTemplate.hooks.startup.tap(\r\n PLUGIN_NAME,\r\n (source: string, chunk: Webpack.compilation.Chunk, hash: string) => {\r\n const extendedChunk: IExtendedChunk = chunk as IExtendedChunk;\r\n const assetOrChunkFound: boolean =\r\n !!this.options.skipDetection || this._detectAssetsOrChunks(extendedChunk);\r\n if (assetOrChunkFound) {\r\n return this._getStartupCode({\r\n source,\r\n chunk: extendedChunk,\r\n hash,\r\n requireFn: mainTemplate.requireFn\r\n });\r\n } else {\r\n return source;\r\n }\r\n }\r\n );\r\n });\r\n\r\n compiler.hooks.emit.tap(PLUGIN_NAME, (compilation: Webpack.compilation.Compilation) => {\r\n for (const chunkGroup of compilation.chunkGroups) {\r\n for (const chunk of chunkGroup.chunks) {\r\n if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {\r\n for (const assetFilename of chunk.files) {\r\n let escapedAssetFilename: string;\r\n if (assetFilename.match(/\\.map$/)) {\r\n escapedAssetFilename = assetFilename.substr(0, assetFilename.length - 4 /* '.map'.length */); // Trim the \".map\" extension\r\n escapedAssetFilename = escapeRegExp(escapedAssetFilename);\r\n escapedAssetFilename = JSON.stringify(escapedAssetFilename); // source in sourcemaps is JSON-encoded\r\n escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1); // Trim the quotes from the JSON encoding\r\n } else {\r\n escapedAssetFilename = escapeRegExp(assetFilename);\r\n }\r\n\r\n const asset: IAsset = compilation.assets[assetFilename];\r\n const originalAssetSource: string = asset.source();\r\n const originalAssetSize: number = asset.size();\r\n\r\n const newAssetSource: string = originalAssetSource.replace(\r\n ASSET_NAME_TOKEN_REGEX,\r\n escapedAssetFilename\r\n );\r\n const sizeDifference: number = assetFilename.length - ASSET_NAME_TOKEN.length;\r\n asset.source = () => newAssetSource;\r\n asset.size = () => originalAssetSize + sizeDifference;\r\n }\r\n }\r\n }\r\n }\r\n });\r\n }\r\n\r\n private _detectAssetsOrChunks(chunk: IExtendedChunk): boolean {\r\n for (const chunkGroup of chunk.groupsIterable) {\r\n if (chunkGroup.childrenIterable.size > 0) {\r\n return true;\r\n }\r\n }\r\n\r\n for (const innerModule of chunk.modulesIterable) {\r\n if (innerModule.buildInfo.assets && Object.keys(innerModule.buildInfo.assets).length > 0) {\r\n return true;\r\n }\r\n }\r\n\r\n return false;\r\n }\r\n\r\n private _getStartupCode(options: IStartupCodeOptions): string {\r\n const moduleOptions: IInternalOptions = cloneDeep(this.options);\r\n\r\n // If this module has ownership over any chunks or assets, inject the public path code\r\n moduleOptions.webpackPublicPathVariable = `${options.requireFn}.p`;\r\n moduleOptions.linePrefix = ' ';\r\n\r\n if (this.options.scriptName) {\r\n if (this.options.scriptName.name) {\r\n moduleOptions.regexName = this.options.scriptName.name;\r\n if (this.options.scriptName.isTokenized) {\r\n moduleOptions.regexName = moduleOptions.regexName\r\n .replace(/\\[name\\]/g, escapeRegExp(options.chunk.name))\r\n .replace(/\\[hash\\]/g, options.chunk.renderedHash || '');\r\n }\r\n } else if (this.options.scriptName.useAssetName) {\r\n options.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;\r\n\r\n moduleOptions.regexName = ASSET_NAME_TOKEN;\r\n }\r\n }\r\n\r\n return [\r\n '// Set the webpack public path',\r\n '(function () {',\r\n getSetPublicPathCode(moduleOptions, console.error),\r\n '})();',\r\n '',\r\n options.source\r\n ].join(EOL);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AAKzB,mDAAyE;AAkGzE,MAAM,+BAA+B,GAAkB,MAAM,CAC3D,kDAAkD,CACnD,CAAC;AAaF,MAAM,WAAW,GAAW,yBAAyB,CAAC;AAEtD,MAAM,gBAAgB,GAAW,kDAAkD,CAAC;AAEpF,MAAM,sBAAsB,GAAW,IAAI,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAEpE,SAAS,YAAY,CAAC,GAAW;IAC/B,OAAO,GAAG,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC;AAED;;;;;GAKG;AACH,MAAa,mBAAmB;IAG9B,YAAmB,OAA2C;QA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under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { EOL } from 'os';\r\nimport type * as Webpack from 'webpack';\r\nimport type * as Webpack5 from 'webpack5';\r\nimport type * as Tapable from 'tapable';\r\n\r\nimport { IInternalOptions, getSetPublicPathCode } from './codeGenerator';\r\n\r\n/**\r\n * The base options for setting the webpack public path at runtime.\r\n *\r\n * @public\r\n */\r\nexport interface ISetWebpackPublicPathOptions {\r\n /**\r\n * Use the System.baseURL property if it is defined.\r\n */\r\n systemJs?: boolean;\r\n\r\n /**\r\n * Use the specified string as a URL prefix after the SystemJS path or the publicPath option.\r\n * If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.\r\n */\r\n urlPrefix?: string;\r\n\r\n /**\r\n * Use the specified path as the base public path.\r\n */\r\n publicPath?: string;\r\n\r\n /**\r\n * Check for a variable with this name on the page and use its value as a regular expression against script paths to\r\n * the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable\r\n * called foo during initialization, and if a foo variable is found, use its value as a regular expression to detect\r\n * the bundle's script.\r\n *\r\n * See the README for more information.\r\n */\r\n regexVariable?: string;\r\n\r\n /**\r\n * A function that returns a snippet of code that manipulates the variable with the name that's specified in the\r\n * parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified\r\n * in-place - the processed value should not be returned.\r\n *\r\n * See the README for more information.\r\n */\r\n getPostProcessScript?: (varName: string) => string;\r\n\r\n /**\r\n * If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.\r\n * This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.\r\n */\r\n preferLastFoundScript?: boolean;\r\n\r\n /**\r\n * If true, always include the public path-setting code. Don't try to detect if any chunks or assets are present.\r\n */\r\n skipDetection?: boolean;\r\n}\r\n\r\n/**\r\n * Options for the set-webpack-public-path plugin.\r\n *\r\n * @public\r\n */\r\nexport interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPathOptions {\r\n /**\r\n * An object that describes how the public path should be discovered.\r\n */\r\n scriptName?: {\r\n /**\r\n * If set to true, use the webpack generated asset's name. This option is not compatible with\r\n * andy other scriptName options.\r\n */\r\n useAssetName?: boolean;\r\n\r\n /**\r\n * A regular expression expressed as a string to be applied to all script paths on the page.\r\n */\r\n name?: string;\r\n\r\n /**\r\n * If true, the name property is tokenized.\r\n *\r\n * See the README for more information.\r\n */\r\n isTokenized?: boolean;\r\n };\r\n}\r\n\r\ninterface IAsset {\r\n size(): number;\r\n source(): string;\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/naming-convention\r\ndeclare const __dummyWebpack4MainTemplate: Webpack.compilation.MainTemplate;\r\ninterface IWebpack4ExtendedMainTemplate extends Webpack.compilation.MainTemplate {\r\n hooks: {\r\n startup: Tapable.SyncHook<string, Webpack.compilation.Chunk, string>;\r\n } & typeof __dummyWebpack4MainTemplate.hooks;\r\n}\r\n\r\nconst SHOULD_REPLACE_ASSET_NAME_TOKEN: unique symbol = Symbol(\r\n 'set-public-path-plugin-should-replace-asset-name'\r\n);\r\n\r\ninterface IExtendedChunk extends Webpack.compilation.Chunk {\r\n [SHOULD_REPLACE_ASSET_NAME_TOKEN]: boolean;\r\n}\r\n\r\ninterface IStartupCodeOptions {\r\n source: string;\r\n chunk: IExtendedChunk;\r\n hash: string;\r\n requireFn: string;\r\n}\r\n\r\nconst PLUGIN_NAME: string = 'set-webpack-public-path';\r\n\r\nconst ASSET_NAME_TOKEN: string = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';\r\n\r\nconst ASSET_NAME_TOKEN_REGEX: RegExp = new RegExp(ASSET_NAME_TOKEN);\r\n\r\nfunction escapeRegExp(str: string): string {\r\n return str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\r\n}\r\n\r\n/**\r\n * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments,\r\n * optionally appended to the SystemJs baseURL property.\r\n *\r\n * @public\r\n */\r\nexport class SetPublicPathPlugin implements Webpack.Plugin {\r\n public options: ISetWebpackPublicPathPluginOptions;\r\n\r\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\r\n this.options = options;\r\n\r\n if (options.scriptName) {\r\n if (options.scriptName.useAssetName && options.scriptName.name) {\r\n throw new Error('scriptName.userAssetName and scriptName.name must not be used together');\r\n } else if (options.scriptName.isTokenized && !options.scriptName.name) {\r\n throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');\r\n }\r\n }\r\n }\r\n\r\n public apply(compiler: Webpack.Compiler): void {\r\n const isWebpack3OrEarlier: boolean = !compiler.hooks;\r\n\r\n if (isWebpack3OrEarlier) {\r\n throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4 or Webpack 5`);\r\n }\r\n\r\n const webpackVersion: string | undefined = (\r\n compiler as unknown as Webpack5.Compiler | { webpack: undefined }\r\n ).webpack?.version;\r\n const webpackMajorVersion: number = webpackVersion\r\n ? Number(webpackVersion.substr(0, webpackVersion.indexOf('.')))\r\n : 4;\r\n\r\n compiler.hooks.compilation.tap(\r\n PLUGIN_NAME,\r\n (compilation: Webpack.compilation.Compilation | Webpack5.Compilation) => {\r\n if (webpackMajorVersion === 4) {\r\n const webpack4Compilation: Webpack.compilation.Compilation =\r\n compilation as Webpack.compilation.Compilation;\r\n const mainTemplate: IWebpack4ExtendedMainTemplate =\r\n webpack4Compilation.mainTemplate as IWebpack4ExtendedMainTemplate;\r\n mainTemplate.hooks.startup.tap(\r\n PLUGIN_NAME,\r\n (source: string, chunk: Webpack.compilation.Chunk, hash: string) => {\r\n const extendedChunk: IExtendedChunk = chunk as IExtendedChunk;\r\n const assetOrChunkFound: boolean =\r\n !!this.options.skipDetection || this._detectAssetsOrChunks(extendedChunk);\r\n if (assetOrChunkFound) {\r\n return this._getStartupCode({\r\n source,\r\n chunk: extendedChunk,\r\n hash,\r\n requireFn: mainTemplate.requireFn\r\n });\r\n } else {\r\n return source;\r\n }\r\n }\r\n );\r\n } else {\r\n // Webpack 5 has its own automatic public path code, so only apply for Webpack 4\r\n const Webpack5Error: typeof Webpack5.WebpackError = (compiler as unknown as Webpack5.Compiler)\r\n .webpack.WebpackError;\r\n // Don't bother importing node-core-library for this\r\n const thisPackageJson: { name: string } = require('../package.json');\r\n compilation.warnings.push(\r\n new Webpack5Error(\r\n `Webpack 5 supports its own automatic public path detection, ` +\r\n `so ${thisPackageJson.name} won't do anything in this compilation.`\r\n )\r\n );\r\n }\r\n }\r\n );\r\n\r\n // Webpack 5 has its own automatic public path code, so only apply for Webpack 4\r\n if (webpackMajorVersion === 4) {\r\n compiler.hooks.emit.tap(\r\n PLUGIN_NAME,\r\n (compilation: Webpack.compilation.Compilation | Webpack5.Compilation) => {\r\n for (const chunkGroup of compilation.chunkGroups) {\r\n for (const chunk of chunkGroup.chunks) {\r\n if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {\r\n for (const assetFilename of chunk.files) {\r\n let escapedAssetFilename: string;\r\n if (assetFilename.match(/\\.map$/)) {\r\n // Trim the \".map\" extension\r\n escapedAssetFilename = assetFilename.substr(\r\n 0,\r\n assetFilename.length - 4 /* '.map'.length */\r\n );\r\n escapedAssetFilename = escapeRegExp(escapedAssetFilename);\r\n // source in sourcemaps is JSON-encoded\r\n escapedAssetFilename = JSON.stringify(escapedAssetFilename);\r\n // Trim the quotes from the JSON encoding\r\n escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1);\r\n } else {\r\n escapedAssetFilename = escapeRegExp(assetFilename);\r\n }\r\n\r\n const asset: IAsset = compilation.assets[assetFilename];\r\n const originalAssetSource: string = asset.source();\r\n const originalAssetSize: number = asset.size();\r\n\r\n const newAssetSource: string = originalAssetSource.replace(\r\n ASSET_NAME_TOKEN_REGEX,\r\n escapedAssetFilename\r\n );\r\n const sizeDifference: number = assetFilename.length - ASSET_NAME_TOKEN.length;\r\n asset.source = () => newAssetSource;\r\n asset.size = () => originalAssetSize + sizeDifference;\r\n }\r\n }\r\n }\r\n }\r\n }\r\n );\r\n }\r\n }\r\n\r\n private _detectAssetsOrChunks(chunk: IExtendedChunk): boolean {\r\n for (const chunkGroup of chunk.groupsIterable) {\r\n if (chunkGroup.childrenIterable.size > 0) {\r\n return true;\r\n }\r\n }\r\n\r\n for (const innerModule of chunk.modulesIterable) {\r\n if (innerModule.buildInfo.assets && Object.keys(innerModule.buildInfo.assets).length > 0) {\r\n return true;\r\n }\r\n }\r\n\r\n return false;\r\n }\r\n\r\n private _getStartupCode(options: IStartupCodeOptions): string {\r\n const moduleOptions: IInternalOptions = { ...this.options };\r\n\r\n // If this module has ownership over any chunks or assets, inject the public path code\r\n moduleOptions.webpackPublicPathVariable = `${options.requireFn}.p`;\r\n moduleOptions.linePrefix = ' ';\r\n\r\n if (this.options.scriptName) {\r\n if (this.options.scriptName.name) {\r\n moduleOptions.regexName = this.options.scriptName.name;\r\n if (this.options.scriptName.isTokenized) {\r\n moduleOptions.regexName = moduleOptions.regexName\r\n .replace(/\\[name\\]/g, escapeRegExp(options.chunk.name))\r\n .replace(/\\[hash\\]/g, options.chunk.renderedHash || '');\r\n }\r\n } else if (this.options.scriptName.useAssetName) {\r\n options.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;\r\n\r\n moduleOptions.regexName = ASSET_NAME_TOKEN;\r\n }\r\n }\r\n\r\n return [\r\n '// Set the webpack public path',\r\n '(function () {',\r\n getSetPublicPathCode(moduleOptions, console.error),\r\n '})();',\r\n '',\r\n options.source\r\n ].join(EOL);\r\n }\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
1
  {
2
2
  "name": "@rushstack/set-webpack-public-path-plugin",
3
- "version": "3.2.88",
3
+ "version": "3.3.1",
4
4
  "description": "This plugin sets the webpack public path at runtime.",
5
5
  "main": "lib/index.js",
6
- "typings": "lib/index.d.ts",
6
+ "typings": "dist/set-webpack-public-path-plugin.d.ts",
7
7
  "license": "MIT",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/microsoft/rushstack.git",
11
11
  "directory": "webpack/set-webpack-public-path-plugin"
12
12
  },
13
- "dependencies": {
14
- "lodash": "~4.17.15"
15
- },
16
13
  "peerDependencies": {
17
- "@types/webpack": "^4.39.8"
14
+ "@types/webpack": "^4.39.8",
15
+ "webpack": "^5.35.1"
18
16
  },
19
17
  "peerDependenciesMeta": {
20
18
  "@types/webpack": {
21
19
  "optional": true
20
+ },
21
+ "webpack": {
22
+ "optional": true
22
23
  }
23
24
  },
24
25
  "devDependencies": {
25
- "@rushstack/eslint-config": "2.4.4",
26
- "@rushstack/heft": "0.42.0",
27
- "@rushstack/heft-node-rig": "1.2.28",
26
+ "@rushstack/eslint-config": "2.4.5",
27
+ "@rushstack/heft": "0.42.2",
28
+ "@rushstack/heft-node-rig": "1.2.30",
28
29
  "@types/heft-jest": "1.0.1",
29
- "@types/lodash": "4.14.116",
30
30
  "@types/node": "12.20.24",
31
31
  "@types/tapable": "1.0.6",
32
- "@types/uglify-js": "2.6.29",
33
- "@types/webpack": "4.41.24"
32
+ "@types/webpack": "4.41.24",
33
+ "webpack5": "npm:webpack@~5.35.1"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "heft build --clean"