@rushstack/set-webpack-public-path-plugin 4.1.9 → 4.1.10

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/LICENSE CHANGED
@@ -1,24 +1,24 @@
1
- @rushstack/set-webpack-public-path-plugin
2
-
3
- Copyright (c) Microsoft Corporation. All rights reserved.
4
-
5
- MIT License
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining
8
- a copy of this software and associated documentation files (the
9
- "Software"), to deal in the Software without restriction, including
10
- without limitation the rights to use, copy, modify, merge, publish,
11
- distribute, sublicense, and/or sell copies of the Software, and to
12
- permit persons to whom the Software is furnished to do so, subject to
13
- the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be
16
- included in all copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ @rushstack/set-webpack-public-path-plugin
2
+
3
+ Copyright (c) Microsoft Corporation. All rights reserved.
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,187 +1,187 @@
1
- # set-webpack-public-path plugin for webpack
2
-
3
- ## Installation
4
-
5
- `npm install @rushstack/set-webpack-public-path-plugin --save-dev`
6
-
7
- ## Overview
8
-
9
- This simple plugin sets the `__webpack_public_path__` variable to
10
- a value specified in the arguments, optionally appended to the SystemJs baseURL
11
- property.
12
-
13
- # Plugin
14
-
15
- To use the plugin, add it to the `plugins` array of your Webpack config. For example:
16
-
17
- ```JavaScript
18
- import { SetPublicPathPlugin } from '@rushstack/set-webpack-public-path-plugin';
19
-
20
- {
21
- plugins: [
22
- new SetPublicPathPlugin( /* webpackPublicPathOptions */ )
23
- ]
24
- }
25
- ```
26
-
27
- ## Options
28
-
29
- #### `scriptName = { }`
30
-
31
- This parameter is an object that takes three properties: a string property `name`, a boolean property `isTokenized`,
32
- and a boolean property `useAssetName`
33
-
34
- The `name` property is a regular expression string that is applied to all script URLs on the page. The last directory
35
- of the URL that matches the regular expression is used as the public path. For example, if the `name` property
36
- is set to `my\-bundle_?[a-zA-Z0-9-_]*\.js` and a script's URL is `https://mycdn.net/files/build_id/assets/my-bundle_10fae182eb.js`,
37
- the public path will be set to `https://mycdn.net/files/build_id/assets/`.
38
-
39
- If the `isTokenized` parameter is set to `true`, the regular expression string in `name` is treated as a tokenized
40
- string. The supported tokens are `[name]` and `[hash]`. Instances of the `[name]` substring are replaced with the
41
- chunk's name, and instances of the `[hash]` substring are replaced with the chunk's rendered hash. The name
42
- is regular expression-escaped. For example, if the `name` property is set to `[name]_?[a-zA-Z0-9-_]*\.js`,
43
- `isTokenized` is set to `true`, and the chunk's name is `my-bundle`, and a script's URL is
44
- `https://mycdn.net/files/build_id/assets/my-bundle_10fae182eb.js`, the public path will be set to
45
- `https://mycdn.net/files/build_id/assets/`.
46
-
47
- If the `useAssetName` property is set, the plugin will use the Webpack-produced asset name as it would the `name`
48
- property. `useAssetName` is exclusive to `name` and `isTokenized`.
49
-
50
- This option is exclusive to other options. If it is set, `systemJs`, `publicPath`, and `urlPrefix` will be ignored.
51
-
52
- #### `systemJs = true`
53
-
54
- Use `System.baseURL` if it is defined.
55
-
56
- #### `publicPath = '...'`
57
-
58
- Use the specified path as the base public path. If `urlPrefix` is also defined, the public path will
59
- be the concatenation of the two (i.e. - `__webpack_public_path__ = URL.concat({publicPath} + {urlPrefix}`).
60
- This option takes precedence over the `systemJs` option.
61
-
62
- #### `urlPrefix = '...'`
63
-
64
- Use the specified string as a URL prefix after the SystemJS path or the `publicPath` option. If neither
65
- `systemJs` nor `publicPath` is defined, this option will not apply and an exception will be thrown.
66
-
67
- #### `regexVariable = '...'`
68
-
69
- Check for a variable with name `...` on the page and use its value as a regular expression against script paths to
70
- the bundle's script. If a value `foo` is passed into `regexVariable`, the produced bundle will look for a variable
71
- called `foo` during initialization, and if a `foo` variable is found, use its value as a regular expression to
72
- detect the bundle's script.
73
-
74
- For example, if the `regexVariable` option is set to `scriptRegex` and `scriptName` is set to `{ name: 'myscript' }`,
75
- consider two cases:
76
-
77
- ##### Case 1
78
-
79
- ```HTML
80
- <html>
81
- <head>
82
- <script>
83
- var scriptRegex = /thescript/i;
84
- </script>
85
- <script src="theScript.js"></script>
86
- </head>
87
-
88
- ...
89
- </html>
90
- ```
91
-
92
- In this case, because there is a `scriptRegex` variable defined on the page, the bundle will use its value
93
- (`/thescript/i`) to find the script.
94
-
95
- ##### Case 2
96
-
97
- ```HTML
98
- <html>
99
- <head>
100
- <script src="myScript.js"></script>
101
- </head>
102
-
103
- ...
104
- </html>
105
- ```
106
-
107
- In this case, because there is not a `scriptRegex` variable defined on the page, the bundle will use the value
108
- passed into the `scriptName` option to find the script.
109
-
110
- #### `getPostProcessScript = (variableName) => { ... }`
111
-
112
- A function that returns a snippet of code that manipulates the variable with the name that's specified in the
113
- parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified
114
- in-place - the processed value should not be returned. This is useful when non-entry assets are deployed to
115
- a parent directory or subdirectory of the directory to which the entry assets are deployed.
116
-
117
- For example, if this parameter is set to this function
118
-
119
- ```JavaScript
120
- getPostProcessScript = (variableName) => {
121
- return `${variableName} = ${variableName} + 'assets/';`;
122
- };
123
- ```
124
-
125
- the public path variable will have `/assets/` appended to the found path.
126
-
127
- Note that the existing value of the variable already ends in a slash (`/`).
128
-
129
- #### `preferLastFoundScript = false`
130
-
131
- If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.
132
- This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
133
-
134
- #### `skipDetection = false`
135
-
136
- If true, always include the code snippet to detect the public path regardless of whether chunks or assets are present.
137
-
138
- # SystemJS Caveat
139
-
140
- When modules are loaded with SystemJS (and with the , `scriptLoad: true` meta option) `<script src="..."></script>`
141
- tags are injected onto the page, evaludated and then immediately removed. This causes an issue because they are removed
142
- before webpack module code begins to execute, so the `publicPath=...` option won't work for modules loaded with SystemJS.
143
-
144
- To circumvent this issue, a small bit of code is availble to that will maintain a global register of script paths
145
- that have been inserted onto the page. This code block should be appended to bundles that are expected to be loaded
146
- with SystemJS and use the `publicPath=...` option.
147
-
148
- ## `getGlobalRegisterCode(bool)`
149
-
150
- This function returns a block of JavaScript that maintains a global register of script tags. If the optional boolean parameter
151
- is set to `true`, the code is not minified. By default, it is minified. You can detect if the plugin may require
152
- the global register code by searching for the value of the `registryVariableName` field.
153
-
154
- ## Usage without registryVariableName
155
-
156
- ``` javascript
157
- var setWebpackPublicPath = require('@rushstack/set-webpack-public-path-plugin');
158
- var gulpInsert = require('gulp-insert');
159
-
160
- gulp.src('finizlied/webpack/bundle/path')
161
- .pipe(gulpInsert.append(setWebpackPublicPath.getGlobalRegisterCode(true)))
162
- .pipe(gulp.dest('dest/path'));
163
- ```
164
-
165
- ## Usage with registryVariableName
166
-
167
- ``` javascript
168
- var setWebpackPublicPath = require('@rushstack/set-webpack-public-path-plugin');
169
- var gulpInsert = require('gulp-insert');
170
- var gulpIf = require('gulp-if');
171
-
172
- var detectRegistryVariableName = function (file) {
173
- return file.contents.toString().indexOf(setWebpackPublicPath.registryVariableName) !== -1;
174
- };
175
-
176
- gulp.src('finizlied/webpack/bundle/path')
177
- .pipe(gulpIf(detectRegistryVariableName, gulpInsert.append(setWebpackPublicPath.getGlobalRegisterCode(true))))
178
- .pipe(gulp.dest('dest/path'));
179
- ```
180
-
181
- ## Links
182
-
183
- - [CHANGELOG.md](
184
- https://github.com/microsoft/rushstack/blob/main/webpack/set-webpack-public-path-plugin/CHANGELOG.md) - Find
185
- out what's new in the latest version
186
-
187
- `@rushstack/set-webpack-public-path-plugin` is part of the [Rush Stack](https://rushstack.io/) family of projects.
1
+ # set-webpack-public-path plugin for webpack
2
+
3
+ ## Installation
4
+
5
+ `npm install @rushstack/set-webpack-public-path-plugin --save-dev`
6
+
7
+ ## Overview
8
+
9
+ This simple plugin sets the `__webpack_public_path__` variable to
10
+ a value specified in the arguments, optionally appended to the SystemJs baseURL
11
+ property.
12
+
13
+ # Plugin
14
+
15
+ To use the plugin, add it to the `plugins` array of your Webpack config. For example:
16
+
17
+ ```JavaScript
18
+ import { SetPublicPathPlugin } from '@rushstack/set-webpack-public-path-plugin';
19
+
20
+ {
21
+ plugins: [
22
+ new SetPublicPathPlugin( /* webpackPublicPathOptions */ )
23
+ ]
24
+ }
25
+ ```
26
+
27
+ ## Options
28
+
29
+ #### `scriptName = { }`
30
+
31
+ This parameter is an object that takes three properties: a string property `name`, a boolean property `isTokenized`,
32
+ and a boolean property `useAssetName`
33
+
34
+ The `name` property is a regular expression string that is applied to all script URLs on the page. The last directory
35
+ of the URL that matches the regular expression is used as the public path. For example, if the `name` property
36
+ is set to `my\-bundle_?[a-zA-Z0-9-_]*\.js` and a script's URL is `https://mycdn.net/files/build_id/assets/my-bundle_10fae182eb.js`,
37
+ the public path will be set to `https://mycdn.net/files/build_id/assets/`.
38
+
39
+ If the `isTokenized` parameter is set to `true`, the regular expression string in `name` is treated as a tokenized
40
+ string. The supported tokens are `[name]` and `[hash]`. Instances of the `[name]` substring are replaced with the
41
+ chunk's name, and instances of the `[hash]` substring are replaced with the chunk's rendered hash. The name
42
+ is regular expression-escaped. For example, if the `name` property is set to `[name]_?[a-zA-Z0-9-_]*\.js`,
43
+ `isTokenized` is set to `true`, and the chunk's name is `my-bundle`, and a script's URL is
44
+ `https://mycdn.net/files/build_id/assets/my-bundle_10fae182eb.js`, the public path will be set to
45
+ `https://mycdn.net/files/build_id/assets/`.
46
+
47
+ If the `useAssetName` property is set, the plugin will use the Webpack-produced asset name as it would the `name`
48
+ property. `useAssetName` is exclusive to `name` and `isTokenized`.
49
+
50
+ This option is exclusive to other options. If it is set, `systemJs`, `publicPath`, and `urlPrefix` will be ignored.
51
+
52
+ #### `systemJs = true`
53
+
54
+ Use `System.baseURL` if it is defined.
55
+
56
+ #### `publicPath = '...'`
57
+
58
+ Use the specified path as the base public path. If `urlPrefix` is also defined, the public path will
59
+ be the concatenation of the two (i.e. - `__webpack_public_path__ = URL.concat({publicPath} + {urlPrefix}`).
60
+ This option takes precedence over the `systemJs` option.
61
+
62
+ #### `urlPrefix = '...'`
63
+
64
+ Use the specified string as a URL prefix after the SystemJS path or the `publicPath` option. If neither
65
+ `systemJs` nor `publicPath` is defined, this option will not apply and an exception will be thrown.
66
+
67
+ #### `regexVariable = '...'`
68
+
69
+ Check for a variable with name `...` on the page and use its value as a regular expression against script paths to
70
+ the bundle's script. If a value `foo` is passed into `regexVariable`, the produced bundle will look for a variable
71
+ called `foo` during initialization, and if a `foo` variable is found, use its value as a regular expression to
72
+ detect the bundle's script.
73
+
74
+ For example, if the `regexVariable` option is set to `scriptRegex` and `scriptName` is set to `{ name: 'myscript' }`,
75
+ consider two cases:
76
+
77
+ ##### Case 1
78
+
79
+ ```HTML
80
+ <html>
81
+ <head>
82
+ <script>
83
+ var scriptRegex = /thescript/i;
84
+ </script>
85
+ <script src="theScript.js"></script>
86
+ </head>
87
+
88
+ ...
89
+ </html>
90
+ ```
91
+
92
+ In this case, because there is a `scriptRegex` variable defined on the page, the bundle will use its value
93
+ (`/thescript/i`) to find the script.
94
+
95
+ ##### Case 2
96
+
97
+ ```HTML
98
+ <html>
99
+ <head>
100
+ <script src="myScript.js"></script>
101
+ </head>
102
+
103
+ ...
104
+ </html>
105
+ ```
106
+
107
+ In this case, because there is not a `scriptRegex` variable defined on the page, the bundle will use the value
108
+ passed into the `scriptName` option to find the script.
109
+
110
+ #### `getPostProcessScript = (variableName) => { ... }`
111
+
112
+ A function that returns a snippet of code that manipulates the variable with the name that's specified in the
113
+ parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified
114
+ in-place - the processed value should not be returned. This is useful when non-entry assets are deployed to
115
+ a parent directory or subdirectory of the directory to which the entry assets are deployed.
116
+
117
+ For example, if this parameter is set to this function
118
+
119
+ ```JavaScript
120
+ getPostProcessScript = (variableName) => {
121
+ return `${variableName} = ${variableName} + 'assets/';`;
122
+ };
123
+ ```
124
+
125
+ the public path variable will have `/assets/` appended to the found path.
126
+
127
+ Note that the existing value of the variable already ends in a slash (`/`).
128
+
129
+ #### `preferLastFoundScript = false`
130
+
131
+ If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.
132
+ This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
133
+
134
+ #### `skipDetection = false`
135
+
136
+ If true, always include the code snippet to detect the public path regardless of whether chunks or assets are present.
137
+
138
+ # SystemJS Caveat
139
+
140
+ When modules are loaded with SystemJS (and with the , `scriptLoad: true` meta option) `<script src="..."></script>`
141
+ tags are injected onto the page, evaludated and then immediately removed. This causes an issue because they are removed
142
+ before webpack module code begins to execute, so the `publicPath=...` option won't work for modules loaded with SystemJS.
143
+
144
+ To circumvent this issue, a small bit of code is availble to that will maintain a global register of script paths
145
+ that have been inserted onto the page. This code block should be appended to bundles that are expected to be loaded
146
+ with SystemJS and use the `publicPath=...` option.
147
+
148
+ ## `getGlobalRegisterCode(bool)`
149
+
150
+ This function returns a block of JavaScript that maintains a global register of script tags. If the optional boolean parameter
151
+ is set to `true`, the code is not minified. By default, it is minified. You can detect if the plugin may require
152
+ the global register code by searching for the value of the `registryVariableName` field.
153
+
154
+ ## Usage without registryVariableName
155
+
156
+ ``` javascript
157
+ var setWebpackPublicPath = require('@rushstack/set-webpack-public-path-plugin');
158
+ var gulpInsert = require('gulp-insert');
159
+
160
+ gulp.src('finizlied/webpack/bundle/path')
161
+ .pipe(gulpInsert.append(setWebpackPublicPath.getGlobalRegisterCode(true)))
162
+ .pipe(gulp.dest('dest/path'));
163
+ ```
164
+
165
+ ## Usage with registryVariableName
166
+
167
+ ``` javascript
168
+ var setWebpackPublicPath = require('@rushstack/set-webpack-public-path-plugin');
169
+ var gulpInsert = require('gulp-insert');
170
+ var gulpIf = require('gulp-if');
171
+
172
+ var detectRegistryVariableName = function (file) {
173
+ return file.contents.toString().indexOf(setWebpackPublicPath.registryVariableName) !== -1;
174
+ };
175
+
176
+ gulp.src('finizlied/webpack/bundle/path')
177
+ .pipe(gulpIf(detectRegistryVariableName, gulpInsert.append(setWebpackPublicPath.getGlobalRegisterCode(true))))
178
+ .pipe(gulp.dest('dest/path'));
179
+ ```
180
+
181
+ ## Links
182
+
183
+ - [CHANGELOG.md](
184
+ https://github.com/microsoft/rushstack/blob/main/webpack/set-webpack-public-path-plugin/CHANGELOG.md) - Find
185
+ out what's new in the latest version
186
+
187
+ `@rushstack/set-webpack-public-path-plugin` is part of the [Rush Stack](https://rushstack.io/) family of projects.
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.37.3"
8
+ "packageVersion": "7.38.0"
9
9
  }
10
10
  ]
11
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AACzB,kFAAuE;AACvE,oEAA0F;AAO1F,mDAA8E;AAkG9E,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;;;;;GAKG;AACH,MAAa,mBAAmB;IAG9B,YAAmB,OAA2C;QAC5D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,OAAO,CAAC,UAAU,EAAE;YACtB,IAAI,OAAO,CAAC,UAAU,CAAC,YAAY,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE;gBAC9D,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;aAC3F;iBAAM,IAAI,OAAO,CAAC,UAAU,CAAC,WAAW,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE;gBACrE,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;aACnF;SACF;IACH,CAAC;IAEM,KAAK,CAAC,QAA0B;QACrC,oEAAoE;QACpE,IAAI,2CAAgB,CAAC,mBAAmB,CAAC,QAAwC,CAAC,EAAE;YAClF,MAAM,IAAI,KAAK,CAAC,OAAO,mBAAmB,CAAC,IAAI,4BAA4B,CAAC,CAAC;SAC9E;QAED,oEAAoE;QACpE,MA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AAO,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,sCAAsC;YACtC,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;AA/JD,kDA+JC","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 { EOL } from 'os';\nimport { VersionDetection } from '@rushstack/webpack-plugin-utilities';\nimport { Text, PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\n\nimport type * as Webpack from 'webpack';\nimport type * as Tapable from 'tapable';\n// Workaround for https://github.com/pnpm/pnpm/issues/4301\nimport type * as Webpack5 from '@rushstack/heft-webpack5-plugin/node_modules/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 * Use the System.baseURL property if it is defined.\n */\n systemJs?: boolean;\n\n /**\n * Use the specified string as a URL prefix after the SystemJS path or the publicPath option.\n * If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.\n */\n urlPrefix?: string;\n\n /**\n * Use the specified path as the base public path.\n */\n publicPath?: string;\n\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 * If true, always include the public path-setting code. Don't try to detect if any chunks or assets are present.\n */\n skipDetection?: boolean;\n}\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?: {\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?: boolean;\n\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\ninterface IAsset {\n size(): number;\n source(): string;\n}\n\n// eslint-disable-next-line @typescript-eslint/naming-convention\ndeclare const __dummyWebpack4MainTemplate: Webpack.compilation.MainTemplate;\ninterface IWebpack4ExtendedMainTemplate extends Webpack.compilation.MainTemplate {\n hooks: {\n startup: Tapable.SyncHook<string, Webpack.compilation.Chunk, string>;\n } & typeof __dummyWebpack4MainTemplate.hooks;\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.compilation.Chunk {\n [SHOULD_REPLACE_ASSET_NAME_TOKEN]: boolean;\n}\n\ninterface IStartupCodeOptions {\n source: string;\n chunk: IExtendedChunk;\n hash: string;\n requireFn: string;\n}\n\nconst PLUGIN_NAME: string = 'set-webpack-public-path';\n\nconst ASSET_NAME_TOKEN: string = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';\n\nconst ASSET_NAME_TOKEN_REGEX: RegExp = new RegExp(ASSET_NAME_TOKEN);\n\n/**\n * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments,\n * optionally appended to the SystemJs baseURL property.\n *\n * @public\n */\nexport class SetPublicPathPlugin implements Webpack.Plugin {\n public options: ISetWebpackPublicPathPluginOptions;\n\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\n this.options = options;\n\n if (options.scriptName) {\n if (options.scriptName.useAssetName && options.scriptName.name) {\n throw new Error('scriptName.userAssetName and scriptName.name must not be used together');\n } else if (options.scriptName.isTokenized && !options.scriptName.name) {\n throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');\n }\n }\n }\n\n public apply(compiler: Webpack.Compiler): void {\n // Casting here because VersionDetection refers to webpack 5 typings\n if (VersionDetection.isWebpack3OrEarlier(compiler as unknown as Webpack5.Compiler)) {\n throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4`);\n }\n\n // Casting here because VersionDetection refers to webpack 5 typings\n const isWebpack4: boolean = VersionDetection.isWebpack4(compiler as unknown as Webpack5.Compiler);\n\n compiler.hooks.compilation.tap(\n PLUGIN_NAME,\n (compilation: Webpack.compilation.Compilation | Webpack5.Compilation) => {\n if (isWebpack4) {\n const webpack4Compilation: Webpack.compilation.Compilation =\n compilation as Webpack.compilation.Compilation;\n const mainTemplate: IWebpack4ExtendedMainTemplate =\n webpack4Compilation.mainTemplate as IWebpack4ExtendedMainTemplate;\n mainTemplate.hooks.startup.tap(\n PLUGIN_NAME,\n (source: string, chunk: Webpack.compilation.Chunk, hash: string) => {\n const extendedChunk: IExtendedChunk = chunk as IExtendedChunk;\n const assetOrChunkFound: boolean =\n !!this.options.skipDetection || this._detectAssetsOrChunks(extendedChunk);\n if (assetOrChunkFound) {\n return this._getStartupCode({\n source,\n chunk: extendedChunk,\n hash,\n requireFn: mainTemplate.requireFn\n });\n } else {\n return source;\n }\n }\n );\n } else {\n // Webpack 5 has its own automatic public path code, so only apply for Webpack 4\n const Webpack5Error: typeof Webpack5.WebpackError = (compiler as unknown as Webpack5.Compiler)\n .webpack.WebpackError;\n const thisPackageJson: IPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);\n compilation.errors.push(\n new Webpack5Error(\n 'Webpack 5 supports its own automatic public path detection, ' +\n `so ${thisPackageJson.name} is unnecessary. Remove the ${SetPublicPathPlugin.name} plugin ` +\n 'from the Webpack configuration.'\n )\n );\n }\n }\n );\n\n // Webpack 5 has its own automatic public path code, so only apply for Webpack 4\n if (isWebpack4) {\n compiler.hooks.emit.tap(\n PLUGIN_NAME,\n (compilation: Webpack.compilation.Compilation | Webpack5.Compilation) => {\n for (const chunkGroup of compilation.chunkGroups) {\n for (const chunk of chunkGroup.chunks) {\n if (chunk[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: IAsset = compilation.assets[assetFilename];\n const originalAssetSource: string = asset.source();\n const originalAssetSize: number = asset.size();\n\n const newAssetSource: string = originalAssetSource.replace(\n ASSET_NAME_TOKEN_REGEX,\n escapedAssetFilename\n );\n const sizeDifference: number = assetFilename.length - ASSET_NAME_TOKEN.length;\n asset.source = () => newAssetSource;\n asset.size = () => originalAssetSize + sizeDifference;\n }\n }\n }\n }\n }\n );\n }\n }\n\n private _detectAssetsOrChunks(chunk: IExtendedChunk): boolean {\n for (const chunkGroup of chunk.groupsIterable) {\n if (chunkGroup.childrenIterable.size > 0) {\n return true;\n }\n }\n\n for (const innerModule of chunk.modulesIterable) {\n if (innerModule.buildInfo.assets && Object.keys(innerModule.buildInfo.assets).length > 0) {\n return true;\n }\n }\n\n return false;\n }\n\n private _getStartupCode(options: IStartupCodeOptions): string {\n const moduleOptions: IInternalOptions = { ...this.options };\n\n // If this module has ownership over any chunks or assets, inject the public path code\n moduleOptions.webpackPublicPathVariable = `${options.requireFn}.p`;\n moduleOptions.linePrefix = ' ';\n\n if (this.options.scriptName) {\n if (this.options.scriptName.name) {\n moduleOptions.regexName = this.options.scriptName.name;\n if (this.options.scriptName.isTokenized) {\n moduleOptions.regexName = moduleOptions.regexName\n .replace(/\\[name\\]/g, Text.escapeRegExp(options.chunk.name))\n .replace(/\\[hash\\]/g, options.chunk.renderedHash || '');\n }\n } else if (this.options.scriptName.useAssetName) {\n options.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;\n\n moduleOptions.regexName = ASSET_NAME_TOKEN;\n }\n }\n\n return [\n '// Set the webpack public path',\n '(function () {',\n // eslint-disable-next-line no-console\n getSetPublicPathCode(moduleOptions, console.error),\n '})();',\n '',\n options.source\n ].join(EOL);\n }\n}\n"]}
1
+ {"version":3,"file":"SetPublicPathPlugin.js","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,2BAAyB;AACzB,kFAAuE;AACvE,oEAA0F;AAO1F,mDAA8E;AAkG9E,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;;;;;GAKG;AACH,MAAa,mBAAm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B;QAClD,MAAM,aAAa,qBAA0B,IAAI,CAAC,OAAO,CAAE,CAAC;QAE5D,sFAAsF;QACtF,aAAa,CAAC,yBAAyB,GAAG,GAAG,OAAO,CAAC,SAAS,IAAI,CAAC;QACnE,aAAa,CAAC,UAAU,GAAG,IAAI,CAAC;QAEhC,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE;YAC3B,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE;gBAChC,aAAa,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC;gBACvD,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,WAAW,EAAE;oBACvC,aAAa,CAAC,SAAS,GAAG,aAAa,CAAC,SAAS;yBAC9C,OAAO,CAAC,WAAW,EAAE,wBAAI,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;yBAC3D,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,sCAAsC;YACtC,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;AA/JD,kDA+JC","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 { VersionDetection } from '@rushstack/webpack-plugin-utilities';\r\nimport { Text, PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\r\n\r\nimport type * as Webpack from 'webpack';\r\nimport type * as Tapable from 'tapable';\r\n// Workaround for https://github.com/pnpm/pnpm/issues/4301\r\nimport type * as Webpack5 from '@rushstack/heft-webpack5-plugin/node_modules/webpack';\r\n\r\nimport { type 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\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 // Casting here because VersionDetection refers to webpack 5 typings\r\n if (VersionDetection.isWebpack3OrEarlier(compiler as unknown as Webpack5.Compiler)) {\r\n throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4`);\r\n }\r\n\r\n // Casting here because VersionDetection refers to webpack 5 typings\r\n const isWebpack4: boolean = VersionDetection.isWebpack4(compiler as unknown as Webpack5.Compiler);\r\n\r\n compiler.hooks.compilation.tap(\r\n PLUGIN_NAME,\r\n (compilation: Webpack.compilation.Compilation | Webpack5.Compilation) => {\r\n if (isWebpack4) {\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 const thisPackageJson: IPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);\r\n compilation.errors.push(\r\n new Webpack5Error(\r\n 'Webpack 5 supports its own automatic public path detection, ' +\r\n `so ${thisPackageJson.name} is unnecessary. Remove the ${SetPublicPathPlugin.name} plugin ` +\r\n 'from the Webpack configuration.'\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 (isWebpack4) {\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 = Text.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 = Text.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, Text.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 // eslint-disable-next-line no-console\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 +1 @@
1
- {"version":3,"file":"codeGenerator.js","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAI3D;;GAEG;AACU,QAAA,oBAAoB,GAAW,kDAAkD,CAAC;AAQ/F,MAAM,OAAO,GAAW,YAAY,CAAC;AAErC,SAAS,SAAS,CAAC,KAAe,EAAE,UAAmB;IACrD,OAAO,KAAK;SACT,GAAG,CAAC,CAAC,IAAY,EAAE,EAAE;QACpB,I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;QAED,IAAI,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,EAAE,EAAE;YACjD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,QAAQ,gCAAgC,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;SAChG;QAED,IAAI,OAAO,CAAC,oBAAoB,EAAE;YAChC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,OAAO,KAAK,EAAE,KAAK,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;SAC9F;KACF;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,yBAAyB,MAAM,OAAO,GAAG,CAAC,CAAC;IAEjE,OAAO,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;AAC9C,CAAC;AAlFD,oDAkFC;AAED;;;;;;;GAOG;AACH,SAAgB,qBAAqB,CAAC,QAAiB,KAAK;IAC1D,iCAAiC;IACjC,eAAe;IACf,8GAA8G;IAC9G,yDAAyD;IACzD,mCAAmC;IACnC,+CAA+C;IAC/C,iCAAiC;IACjC,iDAAiD;IACjD,6EAA6E;IAC7E,MAAM;IACN,IAAI;IACJ,OAAO;IAEP,OAAO,iBAAiB,4BAAoB,MAAM,4BAAoB,4IAA4I,4BAAoB,cAAc,CAAC;AACvP,CAAC;AAfD,sDAeC","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\n/**\n * @public\n */\nexport const registryVariableName: string = 'window.__setWebpackPublicPathLoaderSrcRegistry__';\n\nexport interface IInternalOptions extends ISetWebpackPublicPathOptions {\n webpackPublicPathVariable?: string;\n regexName?: string;\n linePrefix?: string;\n}\n\nconst varName: 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\nfunction escapeSingleQuotes(str: string): string | undefined {\n if (str) {\n return str.replace(\"'\", \"\\\\'\");\n } else {\n return undefined;\n }\n}\n\nfunction appendSlashAndEscapeSingleQuotes(str: string): string | undefined {\n if (str && str.substr(-1) !== '/') {\n str = str + '/';\n }\n\n return escapeSingleQuotes(str);\n}\n\nexport function getSetPublicPathCode(\n options: IInternalOptions,\n emitWarning: (warning: string) => void\n): string {\n if (!options.webpackPublicPathVariable) {\n throw new Error('\"webpackPublicPathVariable\" option must be defined.');\n }\n\n let lines: string[] = [];\n if (options.regexName) {\n lines = [`var scripts = document.getElementsByTagName('script');`];\n\n const regexInitializationSnippet: string = `/${options.regexName}/i`;\n const regexVarName: string | undefined = options.regexVariable;\n if (options.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 ${varName};`,\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 ` ${varName} = path.substring(0, path.lastIndexOf('/') + 1);`,\n ...(options.preferLastFoundScript ? [] : [' break;']),\n ' }',\n ' }',\n '}',\n '',\n `if (!${varName}) {`,\n ` for (var global in ${registryVariableName}) {`,\n ' if (global && global.match(regex)) {',\n ` ${varName} = global.substring(0, global.lastIndexOf('/') + 1);`,\n ...(options.preferLastFoundScript ? [] : [' break;']),\n ' }',\n ' }',\n '}'\n ]\n );\n\n if (options.getPostProcessScript) {\n lines.push(...['', `if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);\n }\n } else {\n if (options.publicPath) {\n lines.push(...[`var ${varName} = '${appendSlashAndEscapeSingleQuotes(options.publicPath)}';`, '']);\n } else if (options.systemJs) {\n lines.push(\n ...[\n `var ${varName} = window.System ? window.System.baseURL || '' : '';`,\n `if (${varName} !== '' && ${varName}.substr(-1) !== '/') ${varName} += '/';`,\n ''\n ]\n );\n } else {\n emitWarning(`Neither 'publicPath' nor 'systemJs' is defined, so the public path will not be modified`);\n\n return '';\n }\n\n if (options.urlPrefix && options.urlPrefix !== '') {\n lines.push(...[`${varName} += '${appendSlashAndEscapeSingleQuotes(options.urlPrefix)}';`, '']);\n }\n\n if (options.getPostProcessScript) {\n lines.push(...[`if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);\n }\n }\n\n lines.push(`${options.webpackPublicPathVariable} = ${varName};`);\n\n return joinLines(lines, options.linePrefix);\n}\n\n/**\n * /**\n * This function returns a block of JavaScript that maintains a global register of script tags.\n *\n * @param debug - If true, the code returned code is not minified. Defaults to false.\n *\n * @public\n */\nexport function getGlobalRegisterCode(debug: boolean = false): string {\n // Minified version of this code:\n // (function(){\n // if (!window.__setWebpackPublicPathLoaderSrcRegistry__) window.__setWebpackPublicPathLoaderSrcRegistry__={};\n // var scripts = document.getElementsByTagName('script');\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) window.__setWebpackPublicPathLoaderSrcRegistry__[path]=true;\n // }\n // }\n // })()\n\n return `\\n!function(){${registryVariableName}||(${registryVariableName}={});var e=document.getElementsByTagName(\"script\");if(e&&e.length)for(var t=0;t<e.length;t++)if(e[t]){var r=e[t].getAttribute(\"src\");r&&(${registryVariableName}[r]=!0)}}();`;\n}\n"]}
1
+ {"version":3,"file":"codeGenerator.js","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAI3D;;GAEG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AG;gBACD,OAAO,OAAO,sDAAsD;gBACpE,OAAO,OAAO,cAAc,OAAO,wBAAwB,OAAO,UAAU;gBAC5E,EAAE;aACH,CACF,CAAC;SACH;aAAM;YACL,WAAW,CAAC,yFAAyF,CAAC,CAAC;YAEvG,OAAO,EAAE,CAAC;SACX;QAED,IAAI,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,EAAE,EAAE;YACjD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,QAAQ,gCAAgC,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;SAChG;QAED,IAAI,OAAO,CAAC,oBAAoB,EAAE;YAChC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,OAAO,KAAK,EAAE,KAAK,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;SAC9F;KACF;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,yBAAyB,MAAM,OAAO,GAAG,CAAC,CAAC;IAEjE,OAAO,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;AAC9C,CAAC;AAlFD,oDAkFC;AAED;;;;;;;GAOG;AACH,SAAgB,qBAAqB,CAAC,QAAiB,KAAK;IAC1D,iCAAiC;IACjC,eAAe;IACf,8GAA8G;IAC9G,yDAAyD;IACzD,mCAAmC;IACnC,+CAA+C;IAC/C,iCAAiC;IACjC,iDAAiD;IACjD,6EAA6E;IAC7E,MAAM;IACN,IAAI;IACJ,OAAO;IAEP,OAAO,iBAAiB,4BAAoB,MAAM,4BAAoB,4IAA4I,4BAAoB,cAAc,CAAC;AACvP,CAAC;AAfD,sDAeC","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 type { ISetWebpackPublicPathOptions } from './SetPublicPathPlugin';\r\n\r\n/**\r\n * @public\r\n */\r\nexport const registryVariableName: string = 'window.__setWebpackPublicPathLoaderSrcRegistry__';\r\n\r\nexport interface IInternalOptions extends ISetWebpackPublicPathOptions {\r\n webpackPublicPathVariable?: string;\r\n regexName?: string;\r\n linePrefix?: string;\r\n}\r\n\r\nconst varName: string = 'publicPath';\r\n\r\nfunction joinLines(lines: string[], linePrefix?: string): string {\r\n return lines\r\n .map((line: string) => {\r\n if (line) {\r\n return `${linePrefix || ''}${line}`;\r\n } else {\r\n return line;\r\n }\r\n })\r\n .join('\\n')\r\n .replace(/\\n\\n+/g, '\\n\\n');\r\n}\r\n\r\nfunction escapeSingleQuotes(str: string): string | undefined {\r\n if (str) {\r\n return str.replace(\"'\", \"\\\\'\");\r\n } else {\r\n return undefined;\r\n }\r\n}\r\n\r\nfunction appendSlashAndEscapeSingleQuotes(str: string): string | undefined {\r\n if (str && str.substr(-1) !== '/') {\r\n str = str + '/';\r\n }\r\n\r\n return escapeSingleQuotes(str);\r\n}\r\n\r\nexport function getSetPublicPathCode(\r\n options: IInternalOptions,\r\n emitWarning: (warning: string) => void\r\n): string {\r\n if (!options.webpackPublicPathVariable) {\r\n throw new Error('\"webpackPublicPathVariable\" option must be defined.');\r\n }\r\n\r\n let lines: string[] = [];\r\n if (options.regexName) {\r\n lines = [`var scripts = document.getElementsByTagName('script');`];\r\n\r\n const regexInitializationSnippet: string = `/${options.regexName}/i`;\r\n const regexVarName: string | undefined = options.regexVariable;\r\n if (options.regexVariable) {\r\n lines.push(\r\n ...[\r\n `var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`\r\n ]\r\n );\r\n } else {\r\n lines.push(...[`var regex = ${regexInitializationSnippet};`]);\r\n }\r\n\r\n lines.push(\r\n ...[\r\n `var ${varName};`,\r\n '',\r\n 'if (scripts && scripts.length) {',\r\n ' for (var i = 0; i < scripts.length; i++) {',\r\n ' if (!scripts[i]) continue;',\r\n ` var path = scripts[i].getAttribute('src');`,\r\n ' if (path && path.match(regex)) {',\r\n ` ${varName} = path.substring(0, path.lastIndexOf('/') + 1);`,\r\n ...(options.preferLastFoundScript ? [] : [' break;']),\r\n ' }',\r\n ' }',\r\n '}',\r\n '',\r\n `if (!${varName}) {`,\r\n ` for (var global in ${registryVariableName}) {`,\r\n ' if (global && global.match(regex)) {',\r\n ` ${varName} = global.substring(0, global.lastIndexOf('/') + 1);`,\r\n ...(options.preferLastFoundScript ? [] : [' break;']),\r\n ' }',\r\n ' }',\r\n '}'\r\n ]\r\n );\r\n\r\n if (options.getPostProcessScript) {\r\n lines.push(...['', `if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);\r\n }\r\n } else {\r\n if (options.publicPath) {\r\n lines.push(...[`var ${varName} = '${appendSlashAndEscapeSingleQuotes(options.publicPath)}';`, '']);\r\n } else if (options.systemJs) {\r\n lines.push(\r\n ...[\r\n `var ${varName} = window.System ? window.System.baseURL || '' : '';`,\r\n `if (${varName} !== '' && ${varName}.substr(-1) !== '/') ${varName} += '/';`,\r\n ''\r\n ]\r\n );\r\n } else {\r\n emitWarning(`Neither 'publicPath' nor 'systemJs' is defined, so the public path will not be modified`);\r\n\r\n return '';\r\n }\r\n\r\n if (options.urlPrefix && options.urlPrefix !== '') {\r\n lines.push(...[`${varName} += '${appendSlashAndEscapeSingleQuotes(options.urlPrefix)}';`, '']);\r\n }\r\n\r\n if (options.getPostProcessScript) {\r\n lines.push(...[`if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);\r\n }\r\n }\r\n\r\n lines.push(`${options.webpackPublicPathVariable} = ${varName};`);\r\n\r\n return joinLines(lines, options.linePrefix);\r\n}\r\n\r\n/**\r\n * /**\r\n * This function returns a block of JavaScript that maintains a global register of script tags.\r\n *\r\n * @param debug - If true, the code returned code is not minified. Defaults to false.\r\n *\r\n * @public\r\n */\r\nexport function getGlobalRegisterCode(debug: boolean = false): string {\r\n // Minified version of this code:\r\n // (function(){\r\n // if (!window.__setWebpackPublicPathLoaderSrcRegistry__) window.__setWebpackPublicPathLoaderSrcRegistry__={};\r\n // var scripts = document.getElementsByTagName('script');\r\n // if (scripts && scripts.length) {\r\n // for (var i = 0; i < scripts.length; i++) {\r\n // if (!scripts[i]) continue;\r\n // var path = scripts[i].getAttribute('src');\r\n // if (path) window.__setWebpackPublicPathLoaderSrcRegistry__[path]=true;\r\n // }\r\n // }\r\n // })()\r\n\r\n return `\\n!function(){${registryVariableName}||(${registryVariableName}={});var e=document.getElementsByTagName(\"script\");if(e&&e.length)for(var t=0;t<e.length;t++)if(e[t]){var r=e[t].getAttribute(\"src\");r&&(${registryVariableName}[r]=!0)}}();`;\r\n}\r\n"]}
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;;;;;GAKG;AAEH,wDAAsC;AACtC,iDAA8E;AAArE,sHAAA,qBAAqB,OAAA;AAAE,qHAAA,oBAAoB,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, optionally appended to the SystemJs baseURL\n * property.\n * @packageDocumentation\n */\n\nexport * from './SetPublicPathPlugin';\nexport { getGlobalRegisterCode, registryVariableName } from './codeGenerator';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;;;;;;;;;;;;;;;AAE3D;;;;;GAKG;AAEH,wDAAsC;AACtC,iDAA8E;AAArE,sHAAA,qBAAqB,OAAA;AAAE,qHAAA,oBAAoB,OAAA","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\n/**\r\n * This simple plugin sets the `__webpack_public_path__` variable to\r\n * a value specified in the arguments, optionally appended to the SystemJs baseURL\r\n * property.\r\n * @packageDocumentation\r\n */\r\n\r\nexport * from './SetPublicPathPlugin';\r\nexport { getGlobalRegisterCode, registryVariableName } from './codeGenerator';\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rushstack/set-webpack-public-path-plugin",
3
- "version": "4.1.9",
3
+ "version": "4.1.10",
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",
@@ -19,15 +19,15 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@rushstack/node-core-library": "3.61.0",
23
- "@rushstack/webpack-plugin-utilities": "0.3.9"
22
+ "@rushstack/webpack-plugin-utilities": "0.3.10",
23
+ "@rushstack/node-core-library": "3.61.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/tapable": "1.0.6",
27
27
  "@types/webpack": "4.41.32",
28
- "@rushstack/heft": "0.62.3",
28
+ "@rushstack/heft": "0.63.0",
29
29
  "local-node-rig": "1.0.0",
30
- "@rushstack/heft-webpack5-plugin": "0.9.9"
30
+ "@rushstack/heft-webpack5-plugin": "0.9.10"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "heft build --clean",