@nx/rspack 20.1.0-beta.1 → 20.1.0-beta.3
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/package.json +17 -10
- package/src/executors/rspack/schema.d.ts +17 -0
- package/src/executors/rspack/schema.json +4 -6
- package/src/plugins/utils/apply-web-config.d.ts +5 -0
- package/src/plugins/utils/apply-web-config.js +383 -0
- package/src/plugins/utils/get-css-module-local-ident.d.ts +1 -0
- package/src/plugins/utils/get-css-module-local-ident.js +17 -0
- package/src/plugins/utils/hash-format.d.ts +7 -0
- package/src/plugins/utils/hash-format.js +22 -0
- package/src/plugins/utils/instantiate-script-plugins.d.ts +2 -0
- package/src/plugins/utils/instantiate-script-plugins.js +42 -0
- package/src/plugins/utils/loaders/stylesheet-loaders.d.ts +72 -0
- package/src/plugins/utils/loaders/stylesheet-loaders.js +119 -0
- package/src/plugins/utils/models.d.ts +229 -0
- package/src/plugins/utils/models.js +2 -0
- package/src/plugins/utils/normalize-entry.d.ts +2 -0
- package/src/plugins/utils/normalize-entry.js +27 -0
- package/src/plugins/utils/plugins/postcss-cli-resources.d.ts +14 -0
- package/src/plugins/utils/plugins/postcss-cli-resources.js +165 -0
- package/src/plugins/utils/plugins/scripts-rspack-plugin.d.ts +16 -0
- package/src/plugins/utils/plugins/scripts-rspack-plugin.js +77 -0
- package/src/utils/config.d.ts +9 -4
- package/src/utils/config.js +52 -5
- package/src/utils/generator-utils.js +1 -0
- package/src/utils/model.d.ts +15 -5
- package/src/utils/module-federation/with-module-federation/with-module-federation-ssr.d.ts +2 -2
- package/src/utils/module-federation/with-module-federation/with-module-federation.d.ts +2 -2
- package/src/utils/read-rspack-options.js +6 -0
- package/src/utils/with-nx.d.ts +2 -2
- package/src/utils/with-react.d.ts +2 -2
- package/src/utils/with-web.d.ts +13 -2
- package/src/utils/with-web.js +16 -102
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCommonLoadersForCssModules = getCommonLoadersForCssModules;
|
|
4
|
+
exports.getCommonLoadersForGlobalCss = getCommonLoadersForGlobalCss;
|
|
5
|
+
exports.getCommonLoadersForGlobalStyle = getCommonLoadersForGlobalStyle;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const path = tslib_1.__importStar(require("path"));
|
|
8
|
+
const autoprefixer = require("autoprefixer");
|
|
9
|
+
const postcssImports = require("postcss-import");
|
|
10
|
+
const core_1 = require("@rspack/core");
|
|
11
|
+
const get_css_module_local_ident_1 = require("../get-css-module-local-ident");
|
|
12
|
+
const hash_format_1 = require("../hash-format");
|
|
13
|
+
const postcss_cli_resources_1 = require("../plugins/postcss-cli-resources");
|
|
14
|
+
function getCommonLoadersForCssModules(options, includePaths) {
|
|
15
|
+
// load component css as raw strings
|
|
16
|
+
return [
|
|
17
|
+
{
|
|
18
|
+
loader: options.extractCss
|
|
19
|
+
? core_1.CssExtractRspackPlugin.loader
|
|
20
|
+
: require.resolve('style-loader'),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
loader: require.resolve('css-loader'),
|
|
24
|
+
options: {
|
|
25
|
+
modules: {
|
|
26
|
+
mode: 'local',
|
|
27
|
+
getLocalIdent: get_css_module_local_ident_1.getCSSModuleLocalIdent,
|
|
28
|
+
},
|
|
29
|
+
importLoaders: 1,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
loader: require.resolve('postcss-loader'),
|
|
34
|
+
options: {
|
|
35
|
+
implementation: require('postcss'),
|
|
36
|
+
postcssOptions: postcssOptionsCreator(options, {
|
|
37
|
+
includePaths,
|
|
38
|
+
forCssModules: true,
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
function getCommonLoadersForGlobalCss(options, includePaths) {
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
loader: options.extractCss
|
|
48
|
+
? core_1.CssExtractRspackPlugin.loader
|
|
49
|
+
: require.resolve('style-loader'),
|
|
50
|
+
},
|
|
51
|
+
{ loader: require.resolve('css-loader'), options: { url: false } },
|
|
52
|
+
{
|
|
53
|
+
loader: require.resolve('postcss-loader'),
|
|
54
|
+
options: {
|
|
55
|
+
implementation: require('postcss'),
|
|
56
|
+
postcssOptions: postcssOptionsCreator(options, {
|
|
57
|
+
includePaths,
|
|
58
|
+
}),
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
function getCommonLoadersForGlobalStyle(options, includePaths) {
|
|
64
|
+
return [
|
|
65
|
+
{
|
|
66
|
+
loader: options.extractCss
|
|
67
|
+
? core_1.CssExtractRspackPlugin.loader
|
|
68
|
+
: require.resolve('style-loader'),
|
|
69
|
+
options: { esModule: true },
|
|
70
|
+
},
|
|
71
|
+
{ loader: require.resolve('css-loader'), options: { url: false } },
|
|
72
|
+
{
|
|
73
|
+
loader: require.resolve('postcss-loader'),
|
|
74
|
+
options: {
|
|
75
|
+
implementation: require('postcss'),
|
|
76
|
+
postcssOptions: postcssOptionsCreator(options, {
|
|
77
|
+
includePaths,
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
function postcssOptionsCreator(options, { includePaths, forCssModules = false, }) {
|
|
84
|
+
const hashFormat = (0, hash_format_1.getOutputHashFormat)(options.outputHashing);
|
|
85
|
+
// PostCSS options depend on the webpack loader, but we need to set the `config` path as a string due to this check:
|
|
86
|
+
// https://github.com/webpack-contrib/postcss-loader/blob/0d342b1/src/utils.js#L36
|
|
87
|
+
const postcssOptions = (loader) => ({
|
|
88
|
+
map: options.sourceMap &&
|
|
89
|
+
options.sourceMap !== 'hidden' && {
|
|
90
|
+
inline: true,
|
|
91
|
+
annotation: false,
|
|
92
|
+
},
|
|
93
|
+
plugins: [
|
|
94
|
+
postcssImports({
|
|
95
|
+
addModulesDirectories: includePaths,
|
|
96
|
+
resolve: (url) => (url.startsWith('~') ? url.slice(1) : url),
|
|
97
|
+
}),
|
|
98
|
+
...(forCssModules
|
|
99
|
+
? []
|
|
100
|
+
: [
|
|
101
|
+
(0, postcss_cli_resources_1.PostcssCliResources)({
|
|
102
|
+
baseHref: options.baseHref,
|
|
103
|
+
deployUrl: options.deployUrl,
|
|
104
|
+
loader,
|
|
105
|
+
filename: `[name]${hashFormat.file}.[ext]`,
|
|
106
|
+
publicPath: options.publicPath,
|
|
107
|
+
rebaseRootRelative: options.rebaseRootRelative,
|
|
108
|
+
}),
|
|
109
|
+
autoprefixer(),
|
|
110
|
+
]),
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
// If a path to postcssConfig is passed in, set it for app and all libs, otherwise
|
|
114
|
+
// use automatic detection.
|
|
115
|
+
if (typeof options.postcssConfig === 'string') {
|
|
116
|
+
postcssOptions.config = path.join(options.root, options.postcssConfig);
|
|
117
|
+
}
|
|
118
|
+
return postcssOptions;
|
|
119
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type { Mode } from '@rspack/core';
|
|
2
|
+
import type { ProjectGraph } from '@nx/devkit';
|
|
3
|
+
import type { AssetGlob } from '@nx/js/src/utils/assets/assets';
|
|
4
|
+
export interface AssetGlobPattern {
|
|
5
|
+
glob: string;
|
|
6
|
+
input: string;
|
|
7
|
+
output: string;
|
|
8
|
+
ignore?: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface ExtraEntryPointClass {
|
|
11
|
+
bundleName?: string;
|
|
12
|
+
inject?: boolean;
|
|
13
|
+
input: string;
|
|
14
|
+
lazy?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface FileReplacement {
|
|
17
|
+
replace: string;
|
|
18
|
+
with: string;
|
|
19
|
+
}
|
|
20
|
+
export interface AdditionalEntryPoint {
|
|
21
|
+
entryName: string;
|
|
22
|
+
entryPath: string;
|
|
23
|
+
}
|
|
24
|
+
export interface TransformerPlugin {
|
|
25
|
+
name: string;
|
|
26
|
+
options: Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
export type TransformerEntry = string | TransformerPlugin;
|
|
29
|
+
export interface OptimizationOptions {
|
|
30
|
+
scripts: boolean;
|
|
31
|
+
styles: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface NxAppRspackPluginOptions {
|
|
34
|
+
/**
|
|
35
|
+
* The tsconfig file for the project. e.g. `tsconfig.json`
|
|
36
|
+
*/
|
|
37
|
+
tsConfig?: string;
|
|
38
|
+
/**
|
|
39
|
+
* The entry point for the bundle. e.g. `src/main.ts`
|
|
40
|
+
*/
|
|
41
|
+
main?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Secondary entry points for the bundle.
|
|
44
|
+
*/
|
|
45
|
+
additionalEntryPoints?: AdditionalEntryPoint[];
|
|
46
|
+
/**
|
|
47
|
+
* Assets to be copied over to the output path.
|
|
48
|
+
*/
|
|
49
|
+
assets?: Array<AssetGlob | string>;
|
|
50
|
+
/**
|
|
51
|
+
* Set <base href> for the resulting index.html.
|
|
52
|
+
*/
|
|
53
|
+
baseHref?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Build the libraries from source. Default is `true`.
|
|
56
|
+
*/
|
|
57
|
+
buildLibsFromSource?: boolean;
|
|
58
|
+
commonChunk?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Delete the output path before building.
|
|
61
|
+
*/
|
|
62
|
+
deleteOutputPath?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* The deploy path for the application. e.g. `/my-app/`
|
|
65
|
+
*/
|
|
66
|
+
deployUrl?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Define external packages that will not be bundled.
|
|
69
|
+
* Use `all` to exclude all 3rd party packages, and `none` to bundle all packages.
|
|
70
|
+
* Use an array to exclude specific packages from the bundle.
|
|
71
|
+
* Default is `none`.
|
|
72
|
+
*/
|
|
73
|
+
externalDependencies?: 'all' | 'none' | string[];
|
|
74
|
+
/**
|
|
75
|
+
* Extract CSS as an external file. Default is `true`.
|
|
76
|
+
*/
|
|
77
|
+
extractCss?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Extract licenses from 3rd party modules and add them to the output.
|
|
80
|
+
*/
|
|
81
|
+
extractLicenses?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Replace files at build time. e.g. `[{ "replace": "src/a.dev.ts", "with": "src/a.prod.ts" }]`
|
|
84
|
+
*/
|
|
85
|
+
fileReplacements?: FileReplacement[];
|
|
86
|
+
/**
|
|
87
|
+
* Generate an `index.html` file if `index.html` is passed. Default is `true`
|
|
88
|
+
*/
|
|
89
|
+
generateIndexHtml?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Generate a `package.json` file for the bundle. Useful for Node applications.
|
|
92
|
+
*/
|
|
93
|
+
generatePackageJson?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Path to the `index.html`.
|
|
96
|
+
*/
|
|
97
|
+
index?: string;
|
|
98
|
+
/**
|
|
99
|
+
* Mode to run the build in.
|
|
100
|
+
*/
|
|
101
|
+
mode?: Mode;
|
|
102
|
+
/**
|
|
103
|
+
* Set the memory limit for the type-checking process. Default is `2048`.
|
|
104
|
+
*/
|
|
105
|
+
memoryLimit?: number;
|
|
106
|
+
/**
|
|
107
|
+
* Use the source file name in output chunks. Useful for development or for Node.
|
|
108
|
+
*/
|
|
109
|
+
namedChunks?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Optimize the bundle using Terser.
|
|
112
|
+
*/
|
|
113
|
+
optimization?: boolean | OptimizationOptions;
|
|
114
|
+
/**
|
|
115
|
+
* Specify the output filename for the bundle. Useful for Node applications that use `@nx/js:node` to serve.
|
|
116
|
+
*/
|
|
117
|
+
outputFileName?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Use file hashes in the output filenames. Recommended for production web applications.
|
|
120
|
+
*/
|
|
121
|
+
outputHashing?: any;
|
|
122
|
+
/**
|
|
123
|
+
* Override `output.path` in webpack configuration. This setting is not recommended and exists for backwards compatibility.
|
|
124
|
+
*/
|
|
125
|
+
outputPath?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Override `watchOptions.poll` in webpack configuration. This setting is not recommended and exists for backwards compatibility.
|
|
128
|
+
*/
|
|
129
|
+
poll?: number;
|
|
130
|
+
/**
|
|
131
|
+
* The polyfill file to use. Useful for supporting legacy browsers. e.g. `src/polyfills.ts`
|
|
132
|
+
*/
|
|
133
|
+
polyfills?: string;
|
|
134
|
+
/**
|
|
135
|
+
* Manually set the PostCSS configuration file. By default, PostCSS will look for `postcss.config.js` in the directory.
|
|
136
|
+
*/
|
|
137
|
+
postcssConfig?: string;
|
|
138
|
+
/**
|
|
139
|
+
* Display build progress in the terminal.
|
|
140
|
+
*/
|
|
141
|
+
progress?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Add an additional chunk for the Webpack runtime. Defaults to `true` when `target === 'web'`.
|
|
144
|
+
*/
|
|
145
|
+
runtimeChunk?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* External scripts that will be included before the main application entry.
|
|
148
|
+
*/
|
|
149
|
+
scripts?: Array<ExtraEntryPointClass | string>;
|
|
150
|
+
/**
|
|
151
|
+
* Do not add a `overrides` and `resolutions` entries to the generated package.json file. Only works in conjunction with `generatePackageJson` option.
|
|
152
|
+
*/
|
|
153
|
+
skipOverrides?: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Do not add a `packageManager` entry to the generated package.json file. Only works in conjunction with `generatePackageJson` option.
|
|
156
|
+
*/
|
|
157
|
+
skipPackageManager?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Skip type checking. Default is `false`.
|
|
160
|
+
*/
|
|
161
|
+
skipTypeChecking?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Skip type checking. Default is `false`.
|
|
164
|
+
*/
|
|
165
|
+
typeCheck?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Generate source maps.
|
|
168
|
+
*/
|
|
169
|
+
sourceMap?: boolean | string;
|
|
170
|
+
/**
|
|
171
|
+
* When `true`, `process.env.NODE_ENV` will be excluded from the bundle. Useful for building a web application to run in a Node environment.
|
|
172
|
+
*/
|
|
173
|
+
ssr?: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Generate a `stats.json` file which can be analyzed using tools such as `webpack-bundle-analyzer`.
|
|
176
|
+
*/
|
|
177
|
+
statsJson?: boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Options for the style preprocessor. e.g. `{ "includePaths": [] }` for SASS.
|
|
180
|
+
*/
|
|
181
|
+
stylePreprocessorOptions?: any;
|
|
182
|
+
/**
|
|
183
|
+
* External stylesheets that will be included with the application.
|
|
184
|
+
*/
|
|
185
|
+
styles?: Array<ExtraEntryPointClass | string>;
|
|
186
|
+
/**
|
|
187
|
+
* Enables the use of subresource integrity validation.
|
|
188
|
+
*/
|
|
189
|
+
subresourceIntegrity?: boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Override the `target` option in webpack configuration. This setting is not recommended and exists for backwards compatibility.
|
|
192
|
+
*/
|
|
193
|
+
target?: string | string[];
|
|
194
|
+
/**
|
|
195
|
+
* List of TypeScript Compiler Transformers Plugins.
|
|
196
|
+
*/
|
|
197
|
+
transformers?: TransformerEntry[];
|
|
198
|
+
/**
|
|
199
|
+
* Generate a separate vendor chunk for 3rd party packages.
|
|
200
|
+
*/
|
|
201
|
+
vendorChunk?: boolean;
|
|
202
|
+
/**
|
|
203
|
+
* Log additional information for debugging purposes.
|
|
204
|
+
*/
|
|
205
|
+
verbose?: boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Watch for file changes.
|
|
208
|
+
*/
|
|
209
|
+
watch?: boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Set a public path for assets resources with absolute paths.
|
|
212
|
+
*/
|
|
213
|
+
publicPath?: string;
|
|
214
|
+
/**
|
|
215
|
+
* Whether to rebase absolute path for assets in postcss cli resources.
|
|
216
|
+
*/
|
|
217
|
+
rebaseRootRelative?: boolean;
|
|
218
|
+
}
|
|
219
|
+
export interface NormalizedNxAppRspackPluginOptions extends NxAppRspackPluginOptions {
|
|
220
|
+
projectName: string;
|
|
221
|
+
root: string;
|
|
222
|
+
projectRoot: string;
|
|
223
|
+
sourceRoot: string;
|
|
224
|
+
configurationName: string;
|
|
225
|
+
targetName: string;
|
|
226
|
+
projectGraph: ProjectGraph;
|
|
227
|
+
outputFileName: string;
|
|
228
|
+
assets: AssetGlobPattern[];
|
|
229
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeExtraEntryPoints = normalizeExtraEntryPoints;
|
|
4
|
+
function normalizeExtraEntryPoints(extraEntryPoints, defaultBundleName) {
|
|
5
|
+
return extraEntryPoints.map((entry) => {
|
|
6
|
+
let normalizedEntry;
|
|
7
|
+
if (typeof entry === 'string') {
|
|
8
|
+
normalizedEntry = {
|
|
9
|
+
input: entry,
|
|
10
|
+
inject: true,
|
|
11
|
+
bundleName: defaultBundleName,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
const { inject = true, ...newEntry } = entry;
|
|
16
|
+
let bundleName;
|
|
17
|
+
if (entry.bundleName) {
|
|
18
|
+
bundleName = entry.bundleName;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
bundleName = defaultBundleName;
|
|
22
|
+
}
|
|
23
|
+
normalizedEntry = { ...newEntry, bundleName };
|
|
24
|
+
}
|
|
25
|
+
return normalizedEntry;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LoaderContext } from '@rspack/core';
|
|
2
|
+
export interface PostcssCliResourcesOptions {
|
|
3
|
+
baseHref?: string;
|
|
4
|
+
deployUrl?: string;
|
|
5
|
+
resourcesOutputPath?: string;
|
|
6
|
+
rebaseRootRelative?: boolean;
|
|
7
|
+
filename: string;
|
|
8
|
+
loader: LoaderContext<unknown>;
|
|
9
|
+
publicPath: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function PostcssCliResources(options: PostcssCliResourcesOptions): {
|
|
12
|
+
postcssPlugin: string;
|
|
13
|
+
Once(root: any): Promise<void[]>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PostcssCliResources = PostcssCliResources;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const loader_utils_1 = require("loader-utils");
|
|
6
|
+
const path = tslib_1.__importStar(require("path"));
|
|
7
|
+
const url = tslib_1.__importStar(require("node:url"));
|
|
8
|
+
function wrapUrl(url) {
|
|
9
|
+
let wrappedUrl;
|
|
10
|
+
const hasSingleQuotes = url.indexOf("'") >= 0;
|
|
11
|
+
if (hasSingleQuotes) {
|
|
12
|
+
wrappedUrl = `"${url}"`;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
wrappedUrl = `'${url}'`;
|
|
16
|
+
}
|
|
17
|
+
return `url(${wrappedUrl})`;
|
|
18
|
+
}
|
|
19
|
+
async function resolve(file, base, resolver) {
|
|
20
|
+
try {
|
|
21
|
+
return await resolver(`./${file}`, base);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return resolver(file, base);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
module.exports.postcss = true;
|
|
28
|
+
function PostcssCliResources(options) {
|
|
29
|
+
const { deployUrl = '', baseHref = '', resourcesOutputPath = '', rebaseRootRelative = false, filename, loader, publicPath = '', } = options;
|
|
30
|
+
const dedupeSlashes = (url) => url.replace(/\/\/+/g, '/');
|
|
31
|
+
const process = async (inputUrl, context, resourceCache) => {
|
|
32
|
+
// If root-relative, absolute or protocol relative url, leave as is
|
|
33
|
+
if (/^((?:\w+:)?\/\/|data:|chrome:|#)/.test(inputUrl)) {
|
|
34
|
+
return inputUrl;
|
|
35
|
+
}
|
|
36
|
+
if (!rebaseRootRelative && /^\//.test(inputUrl)) {
|
|
37
|
+
return inputUrl;
|
|
38
|
+
}
|
|
39
|
+
// If starts with a caret, remove and return remainder
|
|
40
|
+
// this supports bypassing asset processing
|
|
41
|
+
if (inputUrl.startsWith('^')) {
|
|
42
|
+
return inputUrl.slice(1);
|
|
43
|
+
}
|
|
44
|
+
const cacheKey = path.resolve(context, inputUrl);
|
|
45
|
+
const cachedUrl = resourceCache.get(cacheKey);
|
|
46
|
+
if (cachedUrl) {
|
|
47
|
+
return cachedUrl;
|
|
48
|
+
}
|
|
49
|
+
if (inputUrl.startsWith('~')) {
|
|
50
|
+
inputUrl = inputUrl.slice(1);
|
|
51
|
+
}
|
|
52
|
+
if (inputUrl.startsWith('/')) {
|
|
53
|
+
let outputUrl = '';
|
|
54
|
+
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
|
|
55
|
+
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
|
|
56
|
+
outputUrl = `${deployUrl.replace(/\/$/, '')}${inputUrl}`;
|
|
57
|
+
}
|
|
58
|
+
else if (baseHref.match(/:\/\//)) {
|
|
59
|
+
// If baseHref contains a scheme, include it as is.
|
|
60
|
+
outputUrl =
|
|
61
|
+
baseHref.replace(/\/$/, '') +
|
|
62
|
+
dedupeSlashes(`/${deployUrl}/${inputUrl}`);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
// Join together base-href, deploy-url and the original URL.
|
|
66
|
+
outputUrl = dedupeSlashes(`/${baseHref}/${deployUrl}/${publicPath}/${inputUrl}`);
|
|
67
|
+
}
|
|
68
|
+
resourceCache.set(cacheKey, outputUrl);
|
|
69
|
+
return outputUrl;
|
|
70
|
+
}
|
|
71
|
+
const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
|
|
72
|
+
const resolver = (file, base) => new Promise((resolve, reject) => {
|
|
73
|
+
loader.resolve(base, decodeURI(file), (err, result) => {
|
|
74
|
+
if (err) {
|
|
75
|
+
reject(err);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
resolve(result);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
const result = await resolve(pathname, context, resolver);
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
loader.fs.readFile(result, (err, content) => {
|
|
84
|
+
if (err) {
|
|
85
|
+
reject(err);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let outputPath = (0, loader_utils_1.interpolateName)({ resourcePath: result }, filename, { content });
|
|
89
|
+
if (resourcesOutputPath) {
|
|
90
|
+
outputPath = path.posix.join(resourcesOutputPath, outputPath);
|
|
91
|
+
}
|
|
92
|
+
loader.addDependency(result);
|
|
93
|
+
loader.emitFile(outputPath, content, undefined);
|
|
94
|
+
let outputUrl = outputPath.replace(/\\/g, '/');
|
|
95
|
+
if (hash || search) {
|
|
96
|
+
outputUrl = url.format({ pathname: outputUrl, hash, search });
|
|
97
|
+
}
|
|
98
|
+
const loaderOptions = loader.loaders[loader.loaderIndex].options;
|
|
99
|
+
if (deployUrl && loaderOptions.ident !== 'extracted') {
|
|
100
|
+
outputUrl = url.resolve(deployUrl, outputUrl);
|
|
101
|
+
}
|
|
102
|
+
resourceCache.set(cacheKey, outputUrl);
|
|
103
|
+
resolve(outputUrl);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
postcssPlugin: 'postcss-cli-resources',
|
|
109
|
+
Once(root) {
|
|
110
|
+
const urlDeclarations = [];
|
|
111
|
+
/**
|
|
112
|
+
* TODO: Explore if this can be rewritten using the new `Declaration()`
|
|
113
|
+
* listener added in postcss v8
|
|
114
|
+
*/
|
|
115
|
+
root.walkDecls((decl) => {
|
|
116
|
+
if (decl.value && decl.value.includes('url')) {
|
|
117
|
+
urlDeclarations.push(decl);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
if (urlDeclarations.length === 0) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const resourceCache = new Map();
|
|
124
|
+
return Promise.all(urlDeclarations.map(async (decl) => {
|
|
125
|
+
const value = decl.value;
|
|
126
|
+
const urlRegex = /url(?:\(\s*(['"]?))(.*?)(?:\1\s*\))/g;
|
|
127
|
+
const segments = [];
|
|
128
|
+
let match;
|
|
129
|
+
let lastIndex = 0;
|
|
130
|
+
let modified = false;
|
|
131
|
+
// We want to load it relative to the file that imports
|
|
132
|
+
const inputFile = decl.source && decl.source.input.file;
|
|
133
|
+
const context = (inputFile && path.dirname(inputFile)) || loader.context;
|
|
134
|
+
while ((match = urlRegex.exec(value))) {
|
|
135
|
+
const originalUrl = match[2];
|
|
136
|
+
let processedUrl;
|
|
137
|
+
try {
|
|
138
|
+
processedUrl = await process(originalUrl, context, resourceCache);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
loader.emitError(decl.error(err.message, { word: originalUrl }));
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (lastIndex < match.index) {
|
|
145
|
+
segments.push(value.slice(lastIndex, match.index));
|
|
146
|
+
}
|
|
147
|
+
if (!processedUrl || originalUrl === processedUrl) {
|
|
148
|
+
segments.push(match[0]);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
segments.push(wrapUrl(processedUrl));
|
|
152
|
+
modified = true;
|
|
153
|
+
}
|
|
154
|
+
lastIndex = match.index + match[0].length;
|
|
155
|
+
}
|
|
156
|
+
if (lastIndex < value.length) {
|
|
157
|
+
segments.push(value.slice(lastIndex));
|
|
158
|
+
}
|
|
159
|
+
if (modified) {
|
|
160
|
+
decl.value = segments.join('');
|
|
161
|
+
}
|
|
162
|
+
}));
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Compiler } from '@rspack/core';
|
|
2
|
+
export interface ScriptsRspackPluginOptions {
|
|
3
|
+
name: string;
|
|
4
|
+
sourceMap: boolean;
|
|
5
|
+
scripts: string[];
|
|
6
|
+
filename: string;
|
|
7
|
+
basePath: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class ScriptsRspackPlugin {
|
|
10
|
+
private options;
|
|
11
|
+
private _lastBuildTime?;
|
|
12
|
+
private _cachedOutput?;
|
|
13
|
+
constructor(options?: Partial<ScriptsRspackPluginOptions>);
|
|
14
|
+
private _insertOutput;
|
|
15
|
+
apply(compiler: Compiler): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScriptsRspackPlugin = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const loader_utils_1 = require("loader-utils");
|
|
6
|
+
const path = tslib_1.__importStar(require("path"));
|
|
7
|
+
const core_1 = require("@rspack/core");
|
|
8
|
+
function addDependencies(compilation, scripts) {
|
|
9
|
+
for (const script of scripts) {
|
|
10
|
+
compilation.fileDependencies.add(script);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function hook(compiler, action) {
|
|
14
|
+
compiler.hooks.thisCompilation.tap('scripts-rspack-plugin', (compilation) => {
|
|
15
|
+
compilation.hooks.additionalAssets.tapAsync('scripts-rspack-plugin', (callback) => action(compilation, callback));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
class ScriptsRspackPlugin {
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.options = options;
|
|
21
|
+
}
|
|
22
|
+
_insertOutput(compiler, compilation, { filename, source }, cached = false) {
|
|
23
|
+
new core_1.EntryPlugin(compiler.context, this.options.name).apply(compiler);
|
|
24
|
+
compilation.assets[filename] = source;
|
|
25
|
+
}
|
|
26
|
+
apply(compiler) {
|
|
27
|
+
if (!this.options.scripts || this.options.scripts.length === 0) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const scripts = this.options.scripts
|
|
31
|
+
.filter((script) => !!script)
|
|
32
|
+
.map((script) => path.resolve(this.options.basePath || '', script));
|
|
33
|
+
hook(compiler, (compilation, callback) => {
|
|
34
|
+
const sourceGetters = scripts.map((fullPath) => {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
compilation.inputFileSystem.readFile(fullPath, (err, data) => {
|
|
37
|
+
if (err) {
|
|
38
|
+
reject(err);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const content = data.toString();
|
|
42
|
+
let source;
|
|
43
|
+
if (this.options.sourceMap) {
|
|
44
|
+
// TODO: Look for source map file (for '.min' scripts, etc.)
|
|
45
|
+
let adjustedPath = fullPath;
|
|
46
|
+
if (this.options.basePath) {
|
|
47
|
+
adjustedPath = path.relative(this.options.basePath, fullPath);
|
|
48
|
+
}
|
|
49
|
+
source = new core_1.sources.OriginalSource(content, adjustedPath);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
source = new core_1.sources.RawSource(content);
|
|
53
|
+
}
|
|
54
|
+
resolve(source);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
Promise.all(sourceGetters)
|
|
59
|
+
.then((_sources) => {
|
|
60
|
+
const concatSource = new core_1.sources.ConcatSource();
|
|
61
|
+
_sources.forEach((source) => {
|
|
62
|
+
concatSource.add(source);
|
|
63
|
+
concatSource.add('\n;');
|
|
64
|
+
});
|
|
65
|
+
const combinedSource = new core_1.sources.CachedSource(concatSource);
|
|
66
|
+
const filename = (0, loader_utils_1.interpolateName)({ resourcePath: 'scripts.js' }, this.options.filename, { content: combinedSource.source() });
|
|
67
|
+
const output = { filename, source: combinedSource };
|
|
68
|
+
this._insertOutput(compiler, compilation, output);
|
|
69
|
+
this._cachedOutput = output;
|
|
70
|
+
addDependencies(compilation, scripts);
|
|
71
|
+
callback();
|
|
72
|
+
})
|
|
73
|
+
.catch((err) => callback(err));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.ScriptsRspackPlugin = ScriptsRspackPlugin;
|
package/src/utils/config.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ExecutorContext } from '@nx/devkit';
|
|
2
2
|
import type { Configuration } from '@rspack/core';
|
|
3
|
-
import {
|
|
3
|
+
import { NormalizedRspackExecutorSchema } from '../executors/rspack/schema';
|
|
4
4
|
export declare const nxRspackComposablePlugin = "nxRspackComposablePlugin";
|
|
5
5
|
export declare function isNxRspackComposablePlugin(a: unknown): a is AsyncNxComposableRspackPlugin;
|
|
6
6
|
export interface NxRspackExecutionContext {
|
|
7
|
-
options:
|
|
7
|
+
options: NormalizedRspackExecutorSchema;
|
|
8
8
|
context: ExecutorContext;
|
|
9
9
|
configuration?: string;
|
|
10
10
|
}
|
|
@@ -14,4 +14,9 @@ export interface NxComposableRspackPlugin {
|
|
|
14
14
|
export interface AsyncNxComposableRspackPlugin {
|
|
15
15
|
(config: Configuration, ctx: NxRspackExecutionContext): Configuration | Promise<Configuration>;
|
|
16
16
|
}
|
|
17
|
-
export declare function composePlugins(...plugins:
|
|
17
|
+
export declare function composePlugins(...plugins: (NxComposableRspackPlugin | AsyncNxComposableRspackPlugin | Promise<NxComposableRspackPlugin | AsyncNxComposableRspackPlugin>)[]): ((config: Configuration, ctx: NxRspackExecutionContext) => Promise<Configuration>) & {
|
|
18
|
+
nxRspackComposablePlugin: boolean;
|
|
19
|
+
};
|
|
20
|
+
export declare function composePluginsSync(...plugins: NxComposableRspackPlugin[]): ((config: Configuration, ctx: NxRspackExecutionContext) => Configuration) & {
|
|
21
|
+
nxRspackComposablePlugin: boolean;
|
|
22
|
+
};
|