@rushstack/set-webpack-public-path-plugin 4.1.16 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -73
- package/dist/set-webpack-public-path-plugin.d.ts +34 -54
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/SetPublicPathPlugin.d.ts +33 -40
- package/lib/SetPublicPathPlugin.d.ts.map +1 -1
- package/lib/SetPublicPathPlugin.js +72 -95
- package/lib/SetPublicPathPlugin.js.map +1 -1
- package/lib/codeGenerator.d.ts +3 -16
- package/lib/codeGenerator.d.ts.map +1 -1
- package/lib/codeGenerator.js +29 -106
- package/lib/codeGenerator.js.map +1 -1
- package/lib/index.d.ts +2 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -21
- package/lib/index.js.map +1 -1
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
## Overview
|
|
8
8
|
|
|
9
|
-
This simple plugin
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
This simple plugin uses a specified regular expression or the emitted asset name to set the `__webpack_public_path__`
|
|
10
|
+
variable. This is useful for scenarios where the Webpack automatic public path detection does not work. For example,
|
|
11
|
+
when emitting AMD-style assets that are initialized by a callback.
|
|
12
12
|
|
|
13
13
|
# Plugin
|
|
14
14
|
|
|
@@ -49,21 +49,6 @@ property. `useAssetName` is exclusive to `name` and `isTokenized`.
|
|
|
49
49
|
|
|
50
50
|
This option is exclusive to other options. If it is set, `systemJs`, `publicPath`, and `urlPrefix` will be ignored.
|
|
51
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
52
|
#### `regexVariable = '...'`
|
|
68
53
|
|
|
69
54
|
Check for a variable with name `...` on the page and use its value as a regular expression against script paths to
|
|
@@ -130,58 +115,3 @@ Note that the existing value of the variable already ends in a slash (`/`).
|
|
|
130
115
|
|
|
131
116
|
If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.
|
|
132
117
|
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,41 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This simple plugin sets the `__webpack_public_path__` variable to
|
|
3
|
-
*
|
|
4
|
-
* property.
|
|
3
|
+
* a value specified in the arguments.
|
|
5
4
|
* @packageDocumentation
|
|
6
5
|
*/
|
|
7
6
|
|
|
8
|
-
import type
|
|
7
|
+
import type webpack from 'webpack';
|
|
9
8
|
|
|
10
9
|
/**
|
|
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
10
|
* @public
|
|
17
11
|
*/
|
|
18
|
-
export declare
|
|
12
|
+
export declare interface IScriptNameAssetNameOptions {
|
|
13
|
+
/**
|
|
14
|
+
* If set to true, use the webpack generated asset's name. This option is not compatible with
|
|
15
|
+
* andy other scriptName options.
|
|
16
|
+
*/
|
|
17
|
+
useAssetName: true;
|
|
18
|
+
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* The base options for setting the webpack public path at runtime.
|
|
22
|
-
*
|
|
23
21
|
* @public
|
|
24
22
|
*/
|
|
25
|
-
export declare
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
export declare type IScriptNameOptions = IScriptNameAssetNameOptions | IScriptNameRegexOptions;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare interface IScriptNameRegexOptions {
|
|
30
29
|
/**
|
|
31
|
-
*
|
|
32
|
-
* If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.
|
|
30
|
+
* A regular expression expressed as a string to be applied to all script paths on the page.
|
|
33
31
|
*/
|
|
34
|
-
|
|
32
|
+
name: string;
|
|
35
33
|
/**
|
|
36
|
-
*
|
|
34
|
+
* If true, the name property is tokenized.
|
|
35
|
+
*
|
|
36
|
+
* See the README for more information.
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
isTokenized?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The base options for setting the webpack public path at runtime.
|
|
43
|
+
*
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export declare interface ISetWebpackPublicPathOptions {
|
|
39
47
|
/**
|
|
40
48
|
* Check for a variable with this name on the page and use its value as a regular expression against script paths to
|
|
41
49
|
* the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable
|
|
@@ -58,10 +66,6 @@ export declare interface ISetWebpackPublicPathOptions {
|
|
|
58
66
|
* This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
|
|
59
67
|
*/
|
|
60
68
|
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
69
|
}
|
|
66
70
|
|
|
67
71
|
/**
|
|
@@ -73,42 +77,18 @@ export declare interface ISetWebpackPublicPathPluginOptions extends ISetWebpackP
|
|
|
73
77
|
/**
|
|
74
78
|
* An object that describes how the public path should be discovered.
|
|
75
79
|
*/
|
|
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
|
-
};
|
|
80
|
+
scriptName: IScriptNameOptions;
|
|
93
81
|
}
|
|
94
82
|
|
|
95
83
|
/**
|
|
96
|
-
*
|
|
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.
|
|
84
|
+
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.
|
|
103
85
|
*
|
|
104
86
|
* @public
|
|
105
87
|
*/
|
|
106
|
-
export declare class SetPublicPathPlugin implements
|
|
107
|
-
options: ISetWebpackPublicPathPluginOptions;
|
|
88
|
+
export declare class SetPublicPathPlugin implements webpack.WebpackPluginInstance {
|
|
89
|
+
readonly options: ISetWebpackPublicPathPluginOptions;
|
|
108
90
|
constructor(options: ISetWebpackPublicPathPluginOptions);
|
|
109
|
-
apply(compiler:
|
|
110
|
-
private _detectAssetsOrChunks;
|
|
111
|
-
private _getStartupCode;
|
|
91
|
+
apply(compiler: webpack.Compiler): void;
|
|
112
92
|
}
|
|
113
93
|
|
|
114
94
|
export { }
|
package/dist/tsdoc-metadata.json
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type webpack from 'webpack';
|
|
2
2
|
/**
|
|
3
3
|
* The base options for setting the webpack public path at runtime.
|
|
4
4
|
*
|
|
5
5
|
* @public
|
|
6
6
|
*/
|
|
7
7
|
export interface ISetWebpackPublicPathOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Use the System.baseURL property if it is defined.
|
|
10
|
-
*/
|
|
11
|
-
systemJs?: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* Use the specified string as a URL prefix after the SystemJS path or the publicPath option.
|
|
14
|
-
* If neither systemJs nor publicPath is defined, this option will not apply and an exception will be thrown.
|
|
15
|
-
*/
|
|
16
|
-
urlPrefix?: string;
|
|
17
|
-
/**
|
|
18
|
-
* Use the specified path as the base public path.
|
|
19
|
-
*/
|
|
20
|
-
publicPath?: string;
|
|
21
8
|
/**
|
|
22
9
|
* Check for a variable with this name on the page and use its value as a regular expression against script paths to
|
|
23
10
|
* the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable
|
|
@@ -40,11 +27,36 @@ export interface ISetWebpackPublicPathOptions {
|
|
|
40
27
|
* This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.
|
|
41
28
|
*/
|
|
42
29
|
preferLastFoundScript?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export interface IScriptNameAssetNameOptions {
|
|
43
35
|
/**
|
|
44
|
-
* If true,
|
|
36
|
+
* If set to true, use the webpack generated asset's name. This option is not compatible with
|
|
37
|
+
* andy other scriptName options.
|
|
45
38
|
*/
|
|
46
|
-
|
|
39
|
+
useAssetName: true;
|
|
47
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export interface IScriptNameRegexOptions {
|
|
45
|
+
/**
|
|
46
|
+
* A regular expression expressed as a string to be applied to all script paths on the page.
|
|
47
|
+
*/
|
|
48
|
+
name: string;
|
|
49
|
+
/**
|
|
50
|
+
* If true, the name property is tokenized.
|
|
51
|
+
*
|
|
52
|
+
* See the README for more information.
|
|
53
|
+
*/
|
|
54
|
+
isTokenized?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
export type IScriptNameOptions = IScriptNameAssetNameOptions | IScriptNameRegexOptions;
|
|
48
60
|
/**
|
|
49
61
|
* Options for the set-webpack-public-path plugin.
|
|
50
62
|
*
|
|
@@ -54,35 +66,16 @@ export interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPat
|
|
|
54
66
|
/**
|
|
55
67
|
* An object that describes how the public path should be discovered.
|
|
56
68
|
*/
|
|
57
|
-
scriptName
|
|
58
|
-
/**
|
|
59
|
-
* If set to true, use the webpack generated asset's name. This option is not compatible with
|
|
60
|
-
* andy other scriptName options.
|
|
61
|
-
*/
|
|
62
|
-
useAssetName?: boolean;
|
|
63
|
-
/**
|
|
64
|
-
* A regular expression expressed as a string to be applied to all script paths on the page.
|
|
65
|
-
*/
|
|
66
|
-
name?: string;
|
|
67
|
-
/**
|
|
68
|
-
* If true, the name property is tokenized.
|
|
69
|
-
*
|
|
70
|
-
* See the README for more information.
|
|
71
|
-
*/
|
|
72
|
-
isTokenized?: boolean;
|
|
73
|
-
};
|
|
69
|
+
scriptName: IScriptNameOptions;
|
|
74
70
|
}
|
|
75
71
|
/**
|
|
76
|
-
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments
|
|
77
|
-
* optionally appended to the SystemJs baseURL property.
|
|
72
|
+
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.
|
|
78
73
|
*
|
|
79
74
|
* @public
|
|
80
75
|
*/
|
|
81
|
-
export declare class SetPublicPathPlugin implements
|
|
82
|
-
options: ISetWebpackPublicPathPluginOptions;
|
|
76
|
+
export declare class SetPublicPathPlugin implements webpack.WebpackPluginInstance {
|
|
77
|
+
readonly options: ISetWebpackPublicPathPluginOptions;
|
|
83
78
|
constructor(options: ISetWebpackPublicPathPluginOptions);
|
|
84
|
-
apply(compiler:
|
|
85
|
-
private _detectAssetsOrChunks;
|
|
86
|
-
private _getStartupCode;
|
|
79
|
+
apply(compiler: webpack.Compiler): void;
|
|
87
80
|
}
|
|
88
81
|
//# sourceMappingURL=SetPublicPathPlugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SetPublicPathPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SetPublicPathPlugin.d.ts","sourceRoot":"","sources":["../src/SetPublicPathPlugin.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAInC;;;;GAIG;AACH,MAAM,WAAW,4BAA4B;IAC3C;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAEnD;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,YAAY,EAAE,IAAI,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,2BAA2B,GAAG,uBAAuB,CAAC;AAMvF;;;;GAIG;AACH,MAAM,WAAW,kCAAmC,SAAQ,4BAA4B;IACtF;;OAEG;IACH,UAAU,EAAE,kBAAkB,CAAC;CAChC;AAcD;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,OAAO,CAAC,qBAAqB;IACvE,SAAgB,OAAO,EAAE,kCAAkC,CAAC;gBAEzC,OAAO,EAAE,kCAAkC;IAWvD,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,GAAG,IAAI;CAkI/C"}
|
|
@@ -3,72 +3,91 @@
|
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.SetPublicPathPlugin = void 0;
|
|
6
|
-
const os_1 = require("os");
|
|
7
6
|
const webpack_plugin_utilities_1 = require("@rushstack/webpack-plugin-utilities");
|
|
8
7
|
const node_core_library_1 = require("@rushstack/node-core-library");
|
|
9
8
|
const codeGenerator_1 = require("./codeGenerator");
|
|
10
9
|
const SHOULD_REPLACE_ASSET_NAME_TOKEN = Symbol('set-public-path-plugin-should-replace-asset-name');
|
|
11
10
|
const PLUGIN_NAME = 'set-webpack-public-path';
|
|
12
11
|
const ASSET_NAME_TOKEN = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';
|
|
13
|
-
const ASSET_NAME_TOKEN_REGEX = new RegExp(ASSET_NAME_TOKEN);
|
|
14
12
|
/**
|
|
15
|
-
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments
|
|
16
|
-
* optionally appended to the SystemJs baseURL property.
|
|
13
|
+
* This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.
|
|
17
14
|
*
|
|
18
15
|
* @public
|
|
19
16
|
*/
|
|
20
17
|
class SetPublicPathPlugin {
|
|
21
18
|
constructor(options) {
|
|
22
19
|
this.options = options;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
20
|
+
const scriptNameOptions = options.scriptName;
|
|
21
|
+
if (scriptNameOptions.useAssetName && scriptNameOptions.name) {
|
|
22
|
+
throw new Error('scriptName.userAssetName and scriptName.name must not be used together');
|
|
23
|
+
}
|
|
24
|
+
else if (scriptNameOptions.isTokenized && !scriptNameOptions.name) {
|
|
25
|
+
throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');
|
|
30
26
|
}
|
|
31
27
|
}
|
|
32
28
|
apply(compiler) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 4`
|
|
29
|
+
if (!webpack_plugin_utilities_1.VersionDetection.isWebpack5(compiler)) {
|
|
30
|
+
const thisPackageJson = node_core_library_1.PackageJsonLookup.loadOwnPackageJson(__dirname);
|
|
31
|
+
throw new Error(`The ${SetPublicPathPlugin.name} plugin requires Webpack 5. Use major version 4 of ` +
|
|
32
|
+
`${thisPackageJson.name} for Webpack 4 support.`);
|
|
36
33
|
}
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
compiler.hooks.
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
const thisWebpack = compiler.webpack;
|
|
35
|
+
const initialOutputPublicPathSetting = compiler.options.output.publicPath;
|
|
36
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
37
|
+
if (initialOutputPublicPathSetting) {
|
|
38
|
+
compilation.warnings.push(new compiler.webpack.WebpackError(`The "output.publicPath" option is set in the Webpack configuration. The ${SetPublicPathPlugin.name} ` +
|
|
39
|
+
'plugin may produce unexpected results. It is recommended that the "output.publicPath" configuration option ' +
|
|
40
|
+
'be unset when using this plugin.'));
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({
|
|
44
|
+
name: PLUGIN_NAME,
|
|
45
|
+
register: (tap) => {
|
|
46
|
+
if (tap.name === 'RuntimePlugin') {
|
|
47
|
+
// Disable the default public path runtime plugin
|
|
48
|
+
return Object.assign(Object.assign({}, tap), { fn: () => {
|
|
49
|
+
/* noop */
|
|
50
|
+
} });
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return tap;
|
|
54
|
+
}
|
|
56
55
|
}
|
|
57
56
|
});
|
|
58
57
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {
|
|
59
|
+
constructor(pluginOptions) {
|
|
60
|
+
super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);
|
|
61
|
+
this._pluginOptions = pluginOptions;
|
|
62
|
+
}
|
|
63
|
+
generate() {
|
|
64
|
+
const { name: regexpName, isTokenized: regexpIsTokenized, useAssetName } = this._pluginOptions.scriptName;
|
|
65
|
+
let regexName;
|
|
66
|
+
if (regexpName) {
|
|
67
|
+
regexName = regexpName;
|
|
68
|
+
if (regexpIsTokenized) {
|
|
69
|
+
regexName = regexName
|
|
70
|
+
.replace(/\[name\]/g, node_core_library_1.Text.escapeRegExp(this.chunk.name))
|
|
71
|
+
.replace(/\[hash\]/g, this.chunk.renderedHash || '');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (useAssetName) {
|
|
75
|
+
this.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;
|
|
76
|
+
regexName = ASSET_NAME_TOKEN;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
throw new Error('scriptName.name or scriptName.useAssetName must be set');
|
|
80
|
+
}
|
|
81
|
+
const moduleOptions = Object.assign({ webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath, regexName }, this._pluginOptions);
|
|
82
|
+
return (0, codeGenerator_1.getSetPublicPathCode)(moduleOptions);
|
|
83
|
+
}
|
|
67
84
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
85
|
+
compilation.hooks.runtimeRequirementInTree
|
|
86
|
+
.for(thisWebpack.RuntimeGlobals.publicPath)
|
|
87
|
+
.tap(PLUGIN_NAME, (chunk, set) => {
|
|
88
|
+
compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));
|
|
89
|
+
});
|
|
90
|
+
compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {
|
|
72
91
|
for (const chunkGroup of compilation.chunkGroups) {
|
|
73
92
|
for (const chunk of chunkGroup.chunks) {
|
|
74
93
|
if (chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {
|
|
@@ -86,61 +105,19 @@ class SetPublicPathPlugin {
|
|
|
86
105
|
else {
|
|
87
106
|
escapedAssetFilename = node_core_library_1.Text.escapeRegExp(assetFilename);
|
|
88
107
|
}
|
|
89
|
-
const asset =
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
const asset = assets[assetFilename];
|
|
109
|
+
const newAsset = new thisWebpack.sources.ReplaceSource(asset);
|
|
110
|
+
const sourceString = asset.source().toString();
|
|
111
|
+
for (let index = sourceString.indexOf(ASSET_NAME_TOKEN); index >= 0; index = sourceString.indexOf(ASSET_NAME_TOKEN, index + 1)) {
|
|
112
|
+
newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);
|
|
113
|
+
}
|
|
114
|
+
assets[assetFilename] = newAsset;
|
|
96
115
|
}
|
|
97
116
|
}
|
|
98
117
|
}
|
|
99
118
|
}
|
|
100
119
|
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
_detectAssetsOrChunks(chunk) {
|
|
104
|
-
for (const chunkGroup of chunk.groupsIterable) {
|
|
105
|
-
if (chunkGroup.childrenIterable.size > 0) {
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
for (const innerModule of chunk.modulesIterable) {
|
|
110
|
-
if (innerModule.buildInfo.assets && Object.keys(innerModule.buildInfo.assets).length > 0) {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
_getStartupCode(options) {
|
|
117
|
-
const moduleOptions = Object.assign({}, this.options);
|
|
118
|
-
// If this module has ownership over any chunks or assets, inject the public path code
|
|
119
|
-
moduleOptions.webpackPublicPathVariable = `${options.requireFn}.p`;
|
|
120
|
-
moduleOptions.linePrefix = ' ';
|
|
121
|
-
if (this.options.scriptName) {
|
|
122
|
-
if (this.options.scriptName.name) {
|
|
123
|
-
moduleOptions.regexName = this.options.scriptName.name;
|
|
124
|
-
if (this.options.scriptName.isTokenized) {
|
|
125
|
-
moduleOptions.regexName = moduleOptions.regexName
|
|
126
|
-
.replace(/\[name\]/g, node_core_library_1.Text.escapeRegExp(options.chunk.name))
|
|
127
|
-
.replace(/\[hash\]/g, options.chunk.renderedHash || '');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
else if (this.options.scriptName.useAssetName) {
|
|
131
|
-
options.chunk[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;
|
|
132
|
-
moduleOptions.regexName = ASSET_NAME_TOKEN;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return [
|
|
136
|
-
'// Set the webpack public path',
|
|
137
|
-
'(function () {',
|
|
138
|
-
// eslint-disable-next-line no-console
|
|
139
|
-
(0, codeGenerator_1.getSetPublicPathCode)(moduleOptions, console.error),
|
|
140
|
-
'})();',
|
|
141
|
-
'',
|
|
142
|
-
options.source
|
|
143
|
-
].join(os_1.EOL);
|
|
120
|
+
});
|
|
144
121
|
}
|
|
145
122
|
}
|
|
146
123
|
exports.SetPublicPathPlugin = SetPublicPathPlugin;
|
|
@@ -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,QAA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O,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.\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,kFAAuE;AACvE,oEAA0F;AAI1F,mDAA8E;AAmF9E,MAAM,+BAA+B,GAAkB,MAAM,CAC3D,kDAAkD,CACnD,CAAC;AAMF,MAAM,WAAW,GAAW,yBAAyB,CAAC;AAEtD,MAAM,gBAAgB,GAAW,k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qCAAM;oCACL,oBAAoB,GAAG,wBAAI,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;iCACzD;gCAED,MAAM,KAAK,GAA2B,MAAM,CAAC,aAAa,CAAC,CAAC;gCAE5D,MAAM,QAAQ,GAAkC,IAAI,WAAW,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;gCAC7F,MAAM,YAAY,GAAW,KAAK,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC;gCACvD,KACE,IAAI,KAAK,GAAW,YAAY,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAC1D,KAAK,IAAI,CAAC,EACV,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,gBAAgB,EAAE,KAAK,GAAG,CAAC,CAAC,EACzD;oCACA,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,oBAAoB,CAAC,CAAC;iCACpF;gCAED,MAAM,CAAC,aAAa,CAAC,GAAG,QAAQ,CAAC;6BAClC;yBACF;qBACF;iBACF;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAhJD,kDAgJC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { VersionDetection } from '@rushstack/webpack-plugin-utilities';\nimport { Text, PackageJsonLookup, type IPackageJson } from '@rushstack/node-core-library';\n\nimport type webpack from 'webpack';\n\nimport { type IInternalOptions, getSetPublicPathCode } from './codeGenerator';\n\n/**\n * The base options for setting the webpack public path at runtime.\n *\n * @public\n */\nexport interface ISetWebpackPublicPathOptions {\n /**\n * Check for a variable with this name on the page and use its value as a regular expression against script paths to\n * the bundle's script. If a value foo is passed into regexVariable, the produced bundle will look for a variable\n * called foo during initialization, and if a foo variable is found, use its value as a regular expression to detect\n * the bundle's script.\n *\n * See the README for more information.\n */\n regexVariable?: string;\n\n /**\n * A function that returns a snippet of code that manipulates the variable with the name that's specified in the\n * parameter. If this parameter isn't provided, no post-processing code is included. The variable must be modified\n * in-place - the processed value should not be returned.\n *\n * See the README for more information.\n */\n getPostProcessScript?: (varName: string) => string;\n\n /**\n * If true, find the last script matching the regexVariable (if it is set). If false, find the first matching script.\n * This can be useful if there are multiple scripts loaded in the DOM that match the regexVariable.\n */\n preferLastFoundScript?: boolean;\n}\n\n/**\n * @public\n */\nexport interface IScriptNameAssetNameOptions {\n /**\n * If set to true, use the webpack generated asset's name. This option is not compatible with\n * andy other scriptName options.\n */\n useAssetName: true;\n}\n\n/**\n * @public\n */\nexport interface IScriptNameRegexOptions {\n /**\n * A regular expression expressed as a string to be applied to all script paths on the page.\n */\n name: string;\n\n /**\n * If true, the name property is tokenized.\n *\n * See the README for more information.\n */\n isTokenized?: boolean;\n}\n\n/**\n * @public\n */\nexport type IScriptNameOptions = IScriptNameAssetNameOptions | IScriptNameRegexOptions;\n\ntype IScriptNameInternalOptions =\n | (IScriptNameAssetNameOptions & { [key in keyof IScriptNameRegexOptions]?: never })\n | (IScriptNameRegexOptions & { [key in keyof IScriptNameAssetNameOptions]?: never });\n\n/**\n * Options for the set-webpack-public-path plugin.\n *\n * @public\n */\nexport interface ISetWebpackPublicPathPluginOptions extends ISetWebpackPublicPathOptions {\n /**\n * An object that describes how the public path should be discovered.\n */\n scriptName: IScriptNameOptions;\n}\n\nconst SHOULD_REPLACE_ASSET_NAME_TOKEN: unique symbol = Symbol(\n 'set-public-path-plugin-should-replace-asset-name'\n);\n\ninterface IExtendedChunk extends webpack.Chunk {\n [SHOULD_REPLACE_ASSET_NAME_TOKEN]?: boolean;\n}\n\nconst PLUGIN_NAME: string = 'set-webpack-public-path';\n\nconst ASSET_NAME_TOKEN: string = '-ASSET-NAME-c0ef4f86-b570-44d3-b210-4428c5b7825c';\n\n/**\n * This simple plugin sets the __webpack_public_path__ variable to a value specified in the arguments.\n *\n * @public\n */\nexport class SetPublicPathPlugin implements webpack.WebpackPluginInstance {\n public readonly options: ISetWebpackPublicPathPluginOptions;\n\n public constructor(options: ISetWebpackPublicPathPluginOptions) {\n this.options = options;\n\n const scriptNameOptions: IScriptNameInternalOptions = options.scriptName;\n if (scriptNameOptions.useAssetName && scriptNameOptions.name) {\n throw new Error('scriptName.userAssetName and scriptName.name must not be used together');\n } else if (scriptNameOptions.isTokenized && !scriptNameOptions.name) {\n throw new Error('scriptName.isTokenized is only valid if scriptName.name is set');\n }\n }\n\n public apply(compiler: webpack.Compiler): void {\n if (!VersionDetection.isWebpack5(compiler)) {\n const thisPackageJson: IPackageJson = PackageJsonLookup.loadOwnPackageJson(__dirname);\n throw new Error(\n `The ${SetPublicPathPlugin.name} plugin requires Webpack 5. Use major version 4 of ` +\n `${thisPackageJson.name} for Webpack 4 support.`\n );\n }\n\n const thisWebpack: typeof webpack = compiler.webpack;\n\n const initialOutputPublicPathSetting: typeof compiler.options.output.publicPath =\n compiler.options.output.publicPath;\n\n compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: webpack.Compilation) => {\n if (initialOutputPublicPathSetting) {\n compilation.warnings.push(\n new compiler.webpack.WebpackError(\n `The \"output.publicPath\" option is set in the Webpack configuration. The ${SetPublicPathPlugin.name} ` +\n 'plugin may produce unexpected results. It is recommended that the \"output.publicPath\" configuration option ' +\n 'be unset when using this plugin.'\n )\n );\n } else {\n compilation.hooks.runtimeRequirementInTree.for(thisWebpack.RuntimeGlobals.publicPath).intercept({\n name: PLUGIN_NAME,\n register: (tap) => {\n if (tap.name === 'RuntimePlugin') {\n // Disable the default public path runtime plugin\n return {\n ...tap,\n fn: () => {\n /* noop */\n }\n };\n } else {\n return tap;\n }\n }\n });\n }\n\n class SetPublicPathRuntimeModule extends thisWebpack.RuntimeModule {\n private readonly _pluginOptions: ISetWebpackPublicPathPluginOptions;\n\n public constructor(pluginOptions: ISetWebpackPublicPathPluginOptions) {\n super('publicPath', thisWebpack.RuntimeModule.STAGE_BASIC);\n this._pluginOptions = pluginOptions;\n }\n\n public generate(): string {\n const {\n name: regexpName,\n isTokenized: regexpIsTokenized,\n useAssetName\n } = this._pluginOptions.scriptName as IScriptNameInternalOptions;\n\n let regexName: string;\n if (regexpName) {\n regexName = regexpName;\n if (regexpIsTokenized) {\n regexName = regexName\n .replace(/\\[name\\]/g, Text.escapeRegExp(this.chunk.name))\n .replace(/\\[hash\\]/g, this.chunk.renderedHash || '');\n }\n } else if (useAssetName) {\n (this.chunk as IExtendedChunk)[SHOULD_REPLACE_ASSET_NAME_TOKEN] = true;\n\n regexName = ASSET_NAME_TOKEN;\n } else {\n throw new Error('scriptName.name or scriptName.useAssetName must be set');\n }\n\n const moduleOptions: IInternalOptions = {\n webpackPublicPathVariable: thisWebpack.RuntimeGlobals.publicPath,\n regexName,\n ...this._pluginOptions\n };\n\n return getSetPublicPathCode(moduleOptions);\n }\n }\n\n compilation.hooks.runtimeRequirementInTree\n .for(thisWebpack.RuntimeGlobals.publicPath)\n .tap(PLUGIN_NAME, (chunk: webpack.Chunk, set: Set<string>) => {\n compilation.addRuntimeModule(chunk, new SetPublicPathRuntimeModule(this.options));\n });\n\n compilation.hooks.processAssets.tap(PLUGIN_NAME, (assets) => {\n for (const chunkGroup of compilation.chunkGroups) {\n for (const chunk of chunkGroup.chunks) {\n if ((chunk as IExtendedChunk)[SHOULD_REPLACE_ASSET_NAME_TOKEN]) {\n for (const assetFilename of chunk.files) {\n let escapedAssetFilename: string;\n if (assetFilename.match(/\\.map$/)) {\n // Trim the \".map\" extension\n escapedAssetFilename = assetFilename.substr(\n 0,\n assetFilename.length - 4 /* '.map'.length */\n );\n escapedAssetFilename = Text.escapeRegExp(escapedAssetFilename);\n // source in sourcemaps is JSON-encoded\n escapedAssetFilename = JSON.stringify(escapedAssetFilename);\n // Trim the quotes from the JSON encoding\n escapedAssetFilename = escapedAssetFilename.substring(1, escapedAssetFilename.length - 1);\n } else {\n escapedAssetFilename = Text.escapeRegExp(assetFilename);\n }\n\n const asset: webpack.sources.Source = assets[assetFilename];\n\n const newAsset: webpack.sources.ReplaceSource = new thisWebpack.sources.ReplaceSource(asset);\n const sourceString: string = asset.source().toString();\n for (\n let index: number = sourceString.indexOf(ASSET_NAME_TOKEN);\n index >= 0;\n index = sourceString.indexOf(ASSET_NAME_TOKEN, index + 1)\n ) {\n newAsset.replace(index, index + ASSET_NAME_TOKEN.length - 1, escapedAssetFilename);\n }\n\n assets[assetFilename] = newAsset;\n }\n }\n }\n }\n });\n });\n }\n}\n"]}
|
package/lib/codeGenerator.d.ts
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
import type { ISetWebpackPublicPathOptions } from './SetPublicPathPlugin';
|
|
2
|
-
/**
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
export declare const registryVariableName: string;
|
|
6
2
|
export interface IInternalOptions extends ISetWebpackPublicPathOptions {
|
|
7
|
-
webpackPublicPathVariable
|
|
8
|
-
regexName
|
|
3
|
+
webpackPublicPathVariable: string;
|
|
4
|
+
regexName: string;
|
|
9
5
|
linePrefix?: string;
|
|
10
6
|
}
|
|
11
|
-
export declare function getSetPublicPathCode(
|
|
12
|
-
/**
|
|
13
|
-
* /**
|
|
14
|
-
* This function returns a block of JavaScript that maintains a global register of script tags.
|
|
15
|
-
*
|
|
16
|
-
* @param debug - If true, the code returned code is not minified. Defaults to false.
|
|
17
|
-
*
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
export declare function getGlobalRegisterCode(debug?: boolean): string;
|
|
7
|
+
export declare function getSetPublicPathCode({ regexName, regexVariable, preferLastFoundScript, webpackPublicPathVariable, getPostProcessScript, linePrefix }: IInternalOptions): string;
|
|
21
8
|
//# sourceMappingURL=codeGenerator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codeGenerator.d.ts","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,uBAAuB,CAAC;AAE1E
|
|
1
|
+
{"version":3,"file":"codeGenerator.d.ts","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,uBAAuB,CAAC;AAE1E,MAAM,WAAW,gBAAiB,SAAQ,4BAA4B;IACpE,yBAAyB,EAAE,MAAM,CAAC;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAiBD,wBAAgB,oBAAoB,CAAC,EACnC,SAAS,EACT,aAAa,EACb,qBAAqB,EACrB,yBAAyB,EACzB,oBAAoB,EACpB,UAAU,EACX,EAAE,gBAAgB,GAAG,MAAM,CAyC3B"}
|
package/lib/codeGenerator.js
CHANGED
|
@@ -2,12 +2,8 @@
|
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.
|
|
6
|
-
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
exports.registryVariableName = 'window.__setWebpackPublicPathLoaderSrcRegistry__';
|
|
10
|
-
const varName = 'publicPath';
|
|
5
|
+
exports.getSetPublicPathCode = void 0;
|
|
6
|
+
const VAR_NAME = 'publicPath';
|
|
11
7
|
function joinLines(lines, linePrefix) {
|
|
12
8
|
return lines
|
|
13
9
|
.map((line) => {
|
|
@@ -21,112 +17,39 @@ function joinLines(lines, linePrefix) {
|
|
|
21
17
|
.join('\n')
|
|
22
18
|
.replace(/\n\n+/g, '\n\n');
|
|
23
19
|
}
|
|
24
|
-
function
|
|
25
|
-
if (str) {
|
|
26
|
-
return str.replace("'", "\\'");
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function appendSlashAndEscapeSingleQuotes(str) {
|
|
33
|
-
if (str && str.substr(-1) !== '/') {
|
|
34
|
-
str = str + '/';
|
|
35
|
-
}
|
|
36
|
-
return escapeSingleQuotes(str);
|
|
37
|
-
}
|
|
38
|
-
function getSetPublicPathCode(options, emitWarning) {
|
|
39
|
-
if (!options.webpackPublicPathVariable) {
|
|
40
|
-
throw new Error('"webpackPublicPathVariable" option must be defined.');
|
|
41
|
-
}
|
|
20
|
+
function getSetPublicPathCode({ regexName, regexVariable, preferLastFoundScript, webpackPublicPathVariable, getPostProcessScript, linePrefix }) {
|
|
42
21
|
let lines = [];
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (options.regexVariable) {
|
|
48
|
-
lines.push(...[
|
|
49
|
-
`var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`
|
|
50
|
-
]);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
lines.push(...[`var regex = ${regexInitializationSnippet};`]);
|
|
54
|
-
}
|
|
22
|
+
lines = [`var scripts = document.getElementsByTagName('script');`];
|
|
23
|
+
const regexInitializationSnippet = `/${regexName}/i`;
|
|
24
|
+
const regexVarName = regexVariable;
|
|
25
|
+
if (regexVariable) {
|
|
55
26
|
lines.push(...[
|
|
56
|
-
`var ${
|
|
57
|
-
'',
|
|
58
|
-
'if (scripts && scripts.length) {',
|
|
59
|
-
' for (var i = 0; i < scripts.length; i++) {',
|
|
60
|
-
' if (!scripts[i]) continue;',
|
|
61
|
-
` var path = scripts[i].getAttribute('src');`,
|
|
62
|
-
' if (path && path.match(regex)) {',
|
|
63
|
-
` ${varName} = path.substring(0, path.lastIndexOf('/') + 1);`,
|
|
64
|
-
...(options.preferLastFoundScript ? [] : [' break;']),
|
|
65
|
-
' }',
|
|
66
|
-
' }',
|
|
67
|
-
'}',
|
|
68
|
-
'',
|
|
69
|
-
`if (!${varName}) {`,
|
|
70
|
-
` for (var global in ${exports.registryVariableName}) {`,
|
|
71
|
-
' if (global && global.match(regex)) {',
|
|
72
|
-
` ${varName} = global.substring(0, global.lastIndexOf('/') + 1);`,
|
|
73
|
-
...(options.preferLastFoundScript ? [] : [' break;']),
|
|
74
|
-
' }',
|
|
75
|
-
' }',
|
|
76
|
-
'}'
|
|
27
|
+
`var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`
|
|
77
28
|
]);
|
|
78
|
-
if (options.getPostProcessScript) {
|
|
79
|
-
lines.push(...['', `if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);
|
|
80
|
-
}
|
|
81
29
|
}
|
|
82
30
|
else {
|
|
83
|
-
|
|
84
|
-
lines.push(...[`var ${varName} = '${appendSlashAndEscapeSingleQuotes(options.publicPath)}';`, '']);
|
|
85
|
-
}
|
|
86
|
-
else if (options.systemJs) {
|
|
87
|
-
lines.push(...[
|
|
88
|
-
`var ${varName} = window.System ? window.System.baseURL || '' : '';`,
|
|
89
|
-
`if (${varName} !== '' && ${varName}.substr(-1) !== '/') ${varName} += '/';`,
|
|
90
|
-
''
|
|
91
|
-
]);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
emitWarning(`Neither 'publicPath' nor 'systemJs' is defined, so the public path will not be modified`);
|
|
95
|
-
return '';
|
|
96
|
-
}
|
|
97
|
-
if (options.urlPrefix && options.urlPrefix !== '') {
|
|
98
|
-
lines.push(...[`${varName} += '${appendSlashAndEscapeSingleQuotes(options.urlPrefix)}';`, '']);
|
|
99
|
-
}
|
|
100
|
-
if (options.getPostProcessScript) {
|
|
101
|
-
lines.push(...[`if (${varName}) {`, ` ${options.getPostProcessScript(varName)};`, '}', '']);
|
|
102
|
-
}
|
|
31
|
+
lines.push(...[`var regex = ${regexInitializationSnippet};`]);
|
|
103
32
|
}
|
|
104
|
-
lines.push(
|
|
105
|
-
|
|
33
|
+
lines.push(...[
|
|
34
|
+
`var ${VAR_NAME};`,
|
|
35
|
+
'',
|
|
36
|
+
'if (scripts && scripts.length) {',
|
|
37
|
+
' for (var i = 0; i < scripts.length; i++) {',
|
|
38
|
+
' if (!scripts[i]) continue;',
|
|
39
|
+
` var path = scripts[i].getAttribute('src');`,
|
|
40
|
+
' if (path && path.match(regex)) {',
|
|
41
|
+
` ${VAR_NAME} = path.substring(0, path.lastIndexOf('/') + 1);`,
|
|
42
|
+
...(preferLastFoundScript ? [] : [' break;']),
|
|
43
|
+
' }',
|
|
44
|
+
' }',
|
|
45
|
+
'}',
|
|
46
|
+
''
|
|
47
|
+
]);
|
|
48
|
+
if (getPostProcessScript) {
|
|
49
|
+
lines.push(...['', `if (${VAR_NAME}) {`, ` ${getPostProcessScript(VAR_NAME)};`, '}', '']);
|
|
50
|
+
}
|
|
51
|
+
lines.push(`${webpackPublicPathVariable} = ${VAR_NAME};`);
|
|
52
|
+
return joinLines(lines, linePrefix);
|
|
106
53
|
}
|
|
107
54
|
exports.getSetPublicPathCode = getSetPublicPathCode;
|
|
108
|
-
/**
|
|
109
|
-
* /**
|
|
110
|
-
* This function returns a block of JavaScript that maintains a global register of script tags.
|
|
111
|
-
*
|
|
112
|
-
* @param debug - If true, the code returned code is not minified. Defaults to false.
|
|
113
|
-
*
|
|
114
|
-
* @public
|
|
115
|
-
*/
|
|
116
|
-
function getGlobalRegisterCode(debug = false) {
|
|
117
|
-
// Minified version of this code:
|
|
118
|
-
// (function(){
|
|
119
|
-
// if (!window.__setWebpackPublicPathLoaderSrcRegistry__) window.__setWebpackPublicPathLoaderSrcRegistry__={};
|
|
120
|
-
// var scripts = document.getElementsByTagName('script');
|
|
121
|
-
// if (scripts && scripts.length) {
|
|
122
|
-
// for (var i = 0; i < scripts.length; i++) {
|
|
123
|
-
// if (!scripts[i]) continue;
|
|
124
|
-
// var path = scripts[i].getAttribute('src');
|
|
125
|
-
// if (path) window.__setWebpackPublicPathLoaderSrcRegistry__[path]=true;
|
|
126
|
-
// }
|
|
127
|
-
// }
|
|
128
|
-
// })()
|
|
129
|
-
return `\n!function(){${exports.registryVariableName}||(${exports.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&&(${exports.registryVariableName}[r]=!0)}}();`;
|
|
130
|
-
}
|
|
131
|
-
exports.getGlobalRegisterCode = getGlobalRegisterCode;
|
|
132
55
|
//# sourceMappingURL=codeGenerator.js.map
|
package/lib/codeGenerator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codeGenerator.js","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;
|
|
1
|
+
{"version":3,"file":"codeGenerator.js","sourceRoot":"","sources":["../src/codeGenerator.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAU3D,MAAM,QAAQ,GAAW,YAAY,CAAC;AAEtC,SAAS,SAAS,CAAC,KAAe,EAAE,UAAmB;IACrD,OAAO,KAAK;SACT,GAAG,CAAC,CAAC,IAAY,EAAE,EAAE;QACpB,IAAI,IAAI,EAAE;YACR,OAAO,GAAG,UAAU,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;SACrC;aAAM;YACL,OAAO,IAAI,CAAC;SACb;IACH,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC;SACV,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC/B,CAAC;AAED,SAAgB,oBAAoB,CAAC,EACnC,SAAS,EACT,aAAa,EACb,qBAAqB,EACrB,yBAAyB,EACzB,oBAAoB,EACpB,UAAU,EACO;IACjB,IAAI,KAAK,GAAa,EAAE,CAAC;IACzB,KAAK,GAAG,CAAC,wDAAwD,CAAC,CAAC;IAEnE,MAAM,0BAA0B,GAAW,IAAI,SAAS,IAAI,CAAC;IAC7D,MAAM,YAAY,GAAuB,aAAa,CAAC;IACvD,IAAI,aAAa,EAAE;QACjB,KAAK,CAAC,IAAI,CACR,GAAG;YACD,uBAAuB,YAAY,uBAAuB,YAAY,MAAM,0BAA0B,GAAG;SAC1G,CACF,CAAC;KACH;SAAM;QACL,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,0BAA0B,GAAG,CAAC,CAAC,CAAC;KAC/D;IAED,KAAK,CAAC,IAAI,CACR,GAAG;QACD,OAAO,QAAQ,GAAG;QAClB,EAAE;QACF,kCAAkC;QAClC,8CAA8C;QAC9C,gCAAgC;QAChC,gDAAgD;QAChD,sCAAsC;QACtC,SAAS,QAAQ,kDAAkD;QACnE,GAAG,CAAC,qBAAqB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;QAClD,OAAO;QACP,KAAK;QACL,GAAG;QACH,EAAE;KACH,CACF,CAAC;IAEF,IAAI,oBAAoB,EAAE;QACxB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,QAAQ,KAAK,EAAE,KAAK,oBAAoB,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;KAC5F;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,yBAAyB,MAAM,QAAQ,GAAG,CAAC,CAAC;IAE1D,OAAO,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACtC,CAAC;AAhDD,oDAgDC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { ISetWebpackPublicPathOptions } from './SetPublicPathPlugin';\n\nexport interface IInternalOptions extends ISetWebpackPublicPathOptions {\n webpackPublicPathVariable: string;\n regexName: string;\n linePrefix?: string;\n}\n\nconst VAR_NAME: string = 'publicPath';\n\nfunction joinLines(lines: string[], linePrefix?: string): string {\n return lines\n .map((line: string) => {\n if (line) {\n return `${linePrefix || ''}${line}`;\n } else {\n return line;\n }\n })\n .join('\\n')\n .replace(/\\n\\n+/g, '\\n\\n');\n}\n\nexport function getSetPublicPathCode({\n regexName,\n regexVariable,\n preferLastFoundScript,\n webpackPublicPathVariable,\n getPostProcessScript,\n linePrefix\n}: IInternalOptions): string {\n let lines: string[] = [];\n lines = [`var scripts = document.getElementsByTagName('script');`];\n\n const regexInitializationSnippet: string = `/${regexName}/i`;\n const regexVarName: string | undefined = regexVariable;\n if (regexVariable) {\n lines.push(\n ...[\n `var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`\n ]\n );\n } else {\n lines.push(...[`var regex = ${regexInitializationSnippet};`]);\n }\n\n lines.push(\n ...[\n `var ${VAR_NAME};`,\n '',\n 'if (scripts && scripts.length) {',\n ' for (var i = 0; i < scripts.length; i++) {',\n ' if (!scripts[i]) continue;',\n ` var path = scripts[i].getAttribute('src');`,\n ' if (path && path.match(regex)) {',\n ` ${VAR_NAME} = path.substring(0, path.lastIndexOf('/') + 1);`,\n ...(preferLastFoundScript ? [] : [' break;']),\n ' }',\n ' }',\n '}',\n ''\n ]\n );\n\n if (getPostProcessScript) {\n lines.push(...['', `if (${VAR_NAME}) {`, ` ${getPostProcessScript(VAR_NAME)};`, '}', '']);\n }\n\n lines.push(`${webpackPublicPathVariable} = ${VAR_NAME};`);\n\n return joinLines(lines, linePrefix);\n}\n"]}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This simple plugin sets the `__webpack_public_path__` variable to
|
|
3
|
-
*
|
|
4
|
-
* property.
|
|
3
|
+
* a value specified in the arguments.
|
|
5
4
|
* @packageDocumentation
|
|
6
5
|
*/
|
|
7
|
-
export
|
|
8
|
-
export { getGlobalRegisterCode, registryVariableName } from './codeGenerator';
|
|
6
|
+
export { SetPublicPathPlugin, ISetWebpackPublicPathOptions, ISetWebpackPublicPathPluginOptions, IScriptNameAssetNameOptions, IScriptNameOptions, IScriptNameRegexOptions } from './SetPublicPathPlugin';
|
|
9
7
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AAEH,OAAO,EACL,mBAAmB,EACnB,4BAA4B,EAC5B,kCAAkC,EAClC,2BAA2B,EAC3B,kBAAkB,EAClB,uBAAuB,EACxB,MAAM,uBAAuB,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -1,30 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
3
|
// See LICENSE in the project root for license information.
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
16
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
17
|
-
};
|
|
18
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.
|
|
5
|
+
exports.SetPublicPathPlugin = void 0;
|
|
20
6
|
/**
|
|
21
7
|
* This simple plugin sets the `__webpack_public_path__` variable to
|
|
22
|
-
*
|
|
23
|
-
* property.
|
|
8
|
+
* a value specified in the arguments.
|
|
24
9
|
* @packageDocumentation
|
|
25
10
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Object.defineProperty(exports, "getGlobalRegisterCode", { enumerable: true, get: function () { return codeGenerator_1.getGlobalRegisterCode; } });
|
|
29
|
-
Object.defineProperty(exports, "registryVariableName", { enumerable: true, get: function () { return codeGenerator_1.registryVariableName; } });
|
|
11
|
+
var SetPublicPathPlugin_1 = require("./SetPublicPathPlugin");
|
|
12
|
+
Object.defineProperty(exports, "SetPublicPathPlugin", { enumerable: true, get: function () { return SetPublicPathPlugin_1.SetPublicPathPlugin; } });
|
|
30
13
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AAEH,6DAO+B;AAN7B,0HAAA,mBAAmB,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * This simple plugin sets the `__webpack_public_path__` variable to\n * a value specified in the arguments.\n * @packageDocumentation\n */\n\nexport {\n SetPublicPathPlugin,\n ISetWebpackPublicPathOptions,\n ISetWebpackPublicPathPluginOptions,\n IScriptNameAssetNameOptions,\n IScriptNameOptions,\n IScriptNameRegexOptions\n} from './SetPublicPathPlugin';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/set-webpack-public-path-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "This plugin sets the webpack public path at runtime.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "dist/set-webpack-public-path-plugin.d.ts",
|
|
@@ -11,23 +11,22 @@
|
|
|
11
11
|
"directory": "webpack/set-webpack-public-path-plugin"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"
|
|
14
|
+
"webpack": "^5.68.0",
|
|
15
|
+
"@types/node": "*"
|
|
15
16
|
},
|
|
16
17
|
"peerDependenciesMeta": {
|
|
17
|
-
"@types/
|
|
18
|
+
"@types/node": {
|
|
18
19
|
"optional": true
|
|
19
20
|
}
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
23
|
"@rushstack/node-core-library": "3.63.0",
|
|
23
|
-
"@rushstack/webpack-plugin-utilities": "0.
|
|
24
|
+
"@rushstack/webpack-plugin-utilities": "0.4.0"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"
|
|
27
|
-
"@types/webpack": "4.41.32",
|
|
27
|
+
"webpack": "~5.82.1",
|
|
28
28
|
"@rushstack/heft": "0.63.6",
|
|
29
|
-
"local-node-rig": "1.0.0"
|
|
30
|
-
"@rushstack/heft-webpack5-plugin": "0.9.16"
|
|
29
|
+
"local-node-rig": "1.0.0"
|
|
31
30
|
},
|
|
32
31
|
"scripts": {
|
|
33
32
|
"build": "heft build --clean",
|