@rsbuild/core 0.1.4 → 0.1.6

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.
Files changed (52) hide show
  1. package/dist/cli/commands.d.ts +1 -1
  2. package/dist/cli/commands.js +10 -3
  3. package/dist/cli/config.d.ts +1 -1
  4. package/dist/cli/config.js +13 -9
  5. package/dist/cli/prepare.js +1 -1
  6. package/dist/createRsbuild.d.ts +8 -8
  7. package/dist/loadEnv.d.ts +8 -2
  8. package/dist/loadEnv.js +22 -10
  9. package/dist/plugins/html.d.ts +2 -2
  10. package/dist/plugins/html.js +6 -6
  11. package/dist/plugins/index.js +2 -1
  12. package/dist/plugins/inlineChunk.js +1 -1
  13. package/dist/plugins/networkPerformance.js +13 -3
  14. package/dist/plugins/preloadOrPrefetch.js +13 -3
  15. package/dist/plugins/server.d.ts +2 -0
  16. package/dist/plugins/server.js +56 -0
  17. package/dist/rspack-plugins/HtmlAppIconPlugin.d.ts +16 -0
  18. package/dist/rspack-plugins/HtmlAppIconPlugin.js +97 -0
  19. package/dist/rspack-plugins/HtmlCrossOriginPlugin.d.ts +15 -0
  20. package/dist/rspack-plugins/HtmlCrossOriginPlugin.js +59 -0
  21. package/dist/rspack-plugins/HtmlNetworkPerformancePlugin.d.ts +12 -0
  22. package/dist/rspack-plugins/HtmlNetworkPerformancePlugin.js +72 -0
  23. package/dist/rspack-plugins/HtmlNoncePlugin.d.ts +14 -0
  24. package/dist/rspack-plugins/HtmlNoncePlugin.js +52 -0
  25. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/determineAsValue.d.ts +25 -0
  26. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/determineAsValue.js +99 -0
  27. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/doesChunkBelongToHtml.d.ts +31 -0
  28. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/doesChunkBelongToHtml.js +75 -0
  29. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/extractChunks.d.ts +28 -0
  30. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/extractChunks.js +83 -0
  31. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/index.d.ts +4 -0
  32. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/index.js +28 -0
  33. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/type.d.ts +13 -0
  34. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/helpers/type.js +16 -0
  35. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/index.d.ts +30 -0
  36. package/dist/rspack-plugins/HtmlPreloadOrPrefetchPlugin/index.js +167 -0
  37. package/dist/rspack-plugins/HtmlTagsPlugin.d.ts +30 -0
  38. package/dist/rspack-plugins/HtmlTagsPlugin.js +173 -0
  39. package/dist/rspack-plugins/InlineChunkHtmlPlugin.d.ts +57 -0
  40. package/dist/rspack-plugins/InlineChunkHtmlPlugin.js +182 -0
  41. package/dist/rspack-provider/core/createContext.js +7 -3
  42. package/dist/rspack-provider/core/initHooks.d.ts +7 -6
  43. package/dist/rspack-provider/core/initHooks.js +4 -2
  44. package/dist/rspack-provider/core/initPlugins.d.ts +2 -2
  45. package/dist/rspack-provider/core/initPlugins.js +5 -7
  46. package/dist/rspack-provider/index.d.ts +2 -1
  47. package/dist/rspack-provider/index.js +8 -2
  48. package/dist/rspack-provider/provider.d.ts +3 -4
  49. package/dist/rspack-provider/shared.js +21 -2
  50. package/dist/server/devServer.js +10 -0
  51. package/dist/types.d.ts +2 -4
  52. package/package.json +5 -6
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var HtmlPreloadOrPrefetchPlugin_exports = {};
20
+ __export(HtmlPreloadOrPrefetchPlugin_exports, {
21
+ HTMLPreloadOrPrefetchPlugin: () => HTMLPreloadOrPrefetchPlugin
22
+ });
23
+ module.exports = __toCommonJS(HtmlPreloadOrPrefetchPlugin_exports);
24
+ var import_shared = require("@rsbuild/shared");
25
+ var import_helpers = require("./helpers");
26
+ /**
27
+ * @license
28
+ * Copyright 2018 Google Inc.
29
+ * https://github.com/vuejs/preload-webpack-plugin/blob/master/src/index.js
30
+ * Licensed under the Apache License, Version 2.0 (the "License");
31
+ * you may not use this file except in compliance with the License.
32
+ * You may obtain a copy of the License at
33
+ *
34
+ * http://www.apache.org/licenses/LICENSE-2.0
35
+ *
36
+ * Unless required by applicable law or agreed to in writing, software
37
+ * distributed under the License is distributed on an "AS IS" BASIS,
38
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39
+ * See the License for the specific language governing permissions and
40
+ * limitations under the License.
41
+ */
42
+ const defaultOptions = {
43
+ type: "async-chunks"
44
+ };
45
+ function filterResourceHints(resourceHints, scripts) {
46
+ return resourceHints.filter(
47
+ (resourceHint) => !scripts.find(
48
+ (script) => script.attributes.src === resourceHint.attributes.href
49
+ )
50
+ );
51
+ }
52
+ function generateLinks(options, type, compilation, htmlPluginData, HTMLCount) {
53
+ const extractedChunks = (0, import_helpers.extractChunks)({
54
+ // @ts-expect-error compilation type mismatch
55
+ compilation,
56
+ includeType: options.type
57
+ });
58
+ const htmlChunks = (
59
+ // Handle all chunks.
60
+ options.type === "all-assets" || HTMLCount === 1 ? extractedChunks : extractedChunks.filter(
61
+ (chunk) => (
62
+ // TODO: Not yet supported in rspack, maybe we should implement it in another way
63
+ // https://github.com/web-infra-dev/rspack/issues/3896
64
+ (0, import_helpers.doesChunkBelongToHtml)({
65
+ chunk,
66
+ compilation,
67
+ htmlPluginData,
68
+ pluginOptions: options
69
+ })
70
+ )
71
+ )
72
+ );
73
+ const allFiles = htmlChunks.reduce(
74
+ (accumulated, chunk) => accumulated.concat([
75
+ ...chunk.files,
76
+ // sourcemap files are inside auxiliaryFiles in webpack5
77
+ ...chunk.auxiliaryFiles || []
78
+ ]),
79
+ []
80
+ );
81
+ const uniqueFiles = new Set(allFiles);
82
+ const filteredFiles = [...uniqueFiles].filter((file) => [/.map$/].every((regex) => !regex.test(file))).filter(
83
+ (file) => !options.include || (typeof options.include === "function" ? options.include(file) : options.include.some((regex) => new RegExp(regex).test(file)))
84
+ ).filter(
85
+ (file) => !options.exclude || (typeof options.exclude === "function" ? !options.exclude(file) : options.exclude.every((regex) => !new RegExp(regex).test(file)))
86
+ );
87
+ const sortedFilteredFiles = filteredFiles.sort();
88
+ const links = [];
89
+ const publicPath = (0, import_shared.getPublicPathFromCompiler)(compilation.compiler);
90
+ const { crossOriginLoading } = compilation.compiler.options.output;
91
+ for (const file of sortedFilteredFiles) {
92
+ const href = (0, import_shared.withPublicPath)(file, publicPath);
93
+ const attributes = {
94
+ href,
95
+ rel: type
96
+ };
97
+ if (type === "preload") {
98
+ attributes.as = (0, import_helpers.determineAsValue)({
99
+ href,
100
+ file
101
+ });
102
+ if (attributes.as === "font") {
103
+ attributes.crossorigin = "";
104
+ }
105
+ if (attributes.as === "script" || attributes.as === "style") {
106
+ if (crossOriginLoading && !(crossOriginLoading !== "use-credentials" && publicPath === "/")) {
107
+ attributes.crossorigin = crossOriginLoading === "anonymous" ? "" : crossOriginLoading;
108
+ }
109
+ }
110
+ }
111
+ links.push({
112
+ tagName: "link",
113
+ attributes,
114
+ voidTag: true,
115
+ meta: {}
116
+ });
117
+ }
118
+ return links;
119
+ }
120
+ class HTMLPreloadOrPrefetchPlugin {
121
+ constructor(options, type, HtmlPlugin, HTMLCount) {
122
+ this.resourceHints = [];
123
+ this.options = {
124
+ ...defaultOptions,
125
+ ...typeof options === "boolean" ? {} : options
126
+ };
127
+ this.type = type;
128
+ this.HtmlPlugin = HtmlPlugin;
129
+ this.HTMLCount = HTMLCount;
130
+ }
131
+ apply(compiler) {
132
+ compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
133
+ this.HtmlPlugin.getHooks(compilation).beforeAssetTagGeneration.tap(
134
+ `HTML${(0, import_shared.upperFirst)(this.type)}Plugin`,
135
+ (htmlPluginData) => {
136
+ this.resourceHints = generateLinks(
137
+ this.options,
138
+ this.type,
139
+ compilation,
140
+ htmlPluginData,
141
+ this.HTMLCount
142
+ );
143
+ return htmlPluginData;
144
+ }
145
+ );
146
+ this.HtmlPlugin.getHooks(compilation).alterAssetTags.tap(
147
+ `HTML${(0, import_shared.upperFirst)(this.type)}Plugin`,
148
+ (htmlPluginData) => {
149
+ if (this.resourceHints) {
150
+ htmlPluginData.assetTags.styles = [
151
+ ...filterResourceHints(
152
+ this.resourceHints,
153
+ htmlPluginData.assetTags.scripts
154
+ ),
155
+ ...htmlPluginData.assetTags.styles
156
+ ];
157
+ }
158
+ return htmlPluginData;
159
+ }
160
+ );
161
+ });
162
+ }
163
+ }
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ HTMLPreloadOrPrefetchPlugin
167
+ });
@@ -0,0 +1,30 @@
1
+ import type HtmlWebpackPlugin from 'html-webpack-plugin';
2
+ import type { Compiler } from '@rspack/core';
3
+ import { type HtmlInjectTag, type HtmlInjectTagDescriptor } from '@rsbuild/shared';
4
+ export interface HtmlTagsPluginOptions {
5
+ hash?: HtmlInjectTag['hash'];
6
+ publicPath?: HtmlInjectTag['publicPath'];
7
+ append?: HtmlInjectTag['append'];
8
+ includes?: string[];
9
+ tags?: HtmlInjectTagDescriptor[];
10
+ HtmlPlugin: typeof HtmlWebpackPlugin;
11
+ }
12
+ export interface AdditionalContext {
13
+ HtmlPlugin: Extract<HtmlTagsPluginOptions['HtmlPlugin'], Function>;
14
+ }
15
+ export type Context = Omit<HtmlTagsPluginOptions, keyof AdditionalContext> & AdditionalContext;
16
+ /** @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Void_element} */
17
+ export declare const VOID_TAGS: string[];
18
+ /** @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also} */
19
+ export declare const HEAD_TAGS: string[];
20
+ export declare const FILE_ATTRS: {
21
+ link: string;
22
+ script: string;
23
+ };
24
+ export declare class HtmlTagsPlugin {
25
+ readonly name: string;
26
+ meta: Record<string, string>;
27
+ ctx: Context;
28
+ constructor(opts: HtmlTagsPluginOptions);
29
+ apply(compiler: Compiler): void;
30
+ }
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var HtmlTagsPlugin_exports = {};
20
+ __export(HtmlTagsPlugin_exports, {
21
+ FILE_ATTRS: () => FILE_ATTRS,
22
+ HEAD_TAGS: () => HEAD_TAGS,
23
+ HtmlTagsPlugin: () => HtmlTagsPlugin,
24
+ VOID_TAGS: () => VOID_TAGS
25
+ });
26
+ module.exports = __toCommonJS(HtmlTagsPlugin_exports);
27
+ var import_shared = require("@rsbuild/shared");
28
+ const VOID_TAGS = [
29
+ "area",
30
+ "base",
31
+ "br",
32
+ "col",
33
+ "embed",
34
+ "hr",
35
+ "img",
36
+ "input",
37
+ "keygen",
38
+ "link",
39
+ "meta",
40
+ "param",
41
+ "source",
42
+ "track",
43
+ "wbr"
44
+ ];
45
+ const HEAD_TAGS = [
46
+ "title",
47
+ "base",
48
+ "link",
49
+ "style",
50
+ "meta",
51
+ "script",
52
+ "noscript",
53
+ "template"
54
+ ];
55
+ const FILE_ATTRS = {
56
+ link: "href",
57
+ script: "src"
58
+ };
59
+ const withHash = (url, hash) => `${url}?${hash}`;
60
+ class HtmlTagsPlugin {
61
+ constructor(opts) {
62
+ this.name = "HtmlTagsPlugin";
63
+ this.meta = { plugin: this.name };
64
+ this.ctx = {
65
+ append: true,
66
+ ...opts
67
+ };
68
+ }
69
+ apply(compiler) {
70
+ compiler.hooks.compilation.tap(this.name, (compilation) => {
71
+ const compilationHash = compilation.hash || "";
72
+ const hooks = this.ctx.HtmlPlugin.getHooks(compilation);
73
+ hooks.alterAssetTagGroups.tap(this.name, (params) => {
74
+ const includesCurrentFile = !this.ctx.includes || this.ctx.includes.includes(params.outputName);
75
+ if (!includesCurrentFile || !this.ctx.tags?.length) {
76
+ return params;
77
+ }
78
+ const fromWebpackTags = (tags2, override) => {
79
+ const ret = [];
80
+ for (const tag of tags2) {
81
+ ret.push({
82
+ tag: tag.tagName,
83
+ attrs: tag.attributes,
84
+ children: tag.innerHTML,
85
+ publicPath: false,
86
+ ...override
87
+ });
88
+ }
89
+ return ret;
90
+ };
91
+ const fromInjectTags = (tags2) => {
92
+ const ret = [];
93
+ for (const tag of tags2) {
94
+ const attrs = { ...tag.attrs };
95
+ const filenameTag = FILE_ATTRS[tag.tag];
96
+ let filename = attrs[filenameTag];
97
+ if (typeof filename === "string") {
98
+ const optPublicPath = tag.publicPath ?? this.ctx.publicPath;
99
+ if (typeof optPublicPath === "function") {
100
+ filename = optPublicPath(filename, params.publicPath);
101
+ } else if (typeof optPublicPath === "string") {
102
+ filename = (0, import_shared.withPublicPath)(filename, optPublicPath);
103
+ } else if (optPublicPath !== false) {
104
+ filename = (0, import_shared.withPublicPath)(filename, params.publicPath);
105
+ }
106
+ const optHash = tag.hash ?? this.ctx.hash;
107
+ if (typeof optHash === "function") {
108
+ compilationHash.length && (filename = optHash(filename, compilationHash));
109
+ } else if (typeof optHash === "string") {
110
+ optHash.length && (filename = withHash(filename, optHash));
111
+ } else if (optHash === true) {
112
+ compilationHash.length && (filename = withHash(filename, compilationHash));
113
+ }
114
+ attrs[filenameTag] = filename;
115
+ }
116
+ ret.push({
117
+ tagName: tag.tag,
118
+ attributes: attrs,
119
+ meta: this.meta,
120
+ voidTag: VOID_TAGS.includes(tag.tag),
121
+ innerHTML: tag.children
122
+ });
123
+ }
124
+ return ret;
125
+ };
126
+ const handlers = [];
127
+ let tags = [
128
+ ...fromWebpackTags(params.headTags, { head: true }),
129
+ ...fromWebpackTags(params.bodyTags, { head: false })
130
+ ];
131
+ this.ctx.tags.forEach((tag) => {
132
+ if ((0, import_shared.isFunction)(tag)) {
133
+ handlers.push(tag);
134
+ } else {
135
+ tags.push(tag);
136
+ }
137
+ });
138
+ const getPriority = (tag) => {
139
+ const head = tag.head ?? HEAD_TAGS.includes(tag.tag);
140
+ let priority = head ? -2 : 2;
141
+ const append = tag.append ?? this.ctx.append;
142
+ if (typeof append === "boolean") {
143
+ priority += append ? 1 : -1;
144
+ }
145
+ return priority;
146
+ };
147
+ tags = tags.sort((tag1, tag2) => getPriority(tag1) - getPriority(tag2));
148
+ const utils = {
149
+ outputName: params.outputName,
150
+ publicPath: params.publicPath,
151
+ hash: compilationHash
152
+ };
153
+ for (const handler of handlers) {
154
+ tags = handler(tags, utils) || tags;
155
+ }
156
+ const [headTags, bodyTags] = (0, import_shared.partition)(
157
+ tags,
158
+ (tag) => tag.head ?? HEAD_TAGS.includes(tag.tag)
159
+ );
160
+ params.headTags = fromInjectTags(headTags);
161
+ params.bodyTags = fromInjectTags(bodyTags);
162
+ return params;
163
+ });
164
+ });
165
+ }
166
+ }
167
+ // Annotate the CommonJS export names for ESM import in node:
168
+ 0 && (module.exports = {
169
+ FILE_ATTRS,
170
+ HEAD_TAGS,
171
+ HtmlTagsPlugin,
172
+ VOID_TAGS
173
+ });
@@ -0,0 +1,57 @@
1
+ import { type InlineChunkTest } from '@rsbuild/shared';
2
+ import type { Compiler, Compilation } from '@rspack/core';
3
+ import type HtmlWebpackPlugin from 'html-webpack-plugin';
4
+ import type { HtmlTagObject } from 'html-webpack-plugin';
5
+ export type InlineChunkHtmlPluginOptions = {
6
+ styleTests: InlineChunkTest[];
7
+ scriptTests: InlineChunkTest[];
8
+ distPath: {
9
+ js?: string;
10
+ css?: string;
11
+ };
12
+ };
13
+ export declare class InlineChunkHtmlPlugin {
14
+ name: string;
15
+ styleTests: InlineChunkTest[];
16
+ scriptTests: InlineChunkTest[];
17
+ distPath: InlineChunkHtmlPluginOptions['distPath'];
18
+ inlinedAssets: Set<string>;
19
+ htmlPlugin: typeof HtmlWebpackPlugin;
20
+ constructor(htmlPlugin: typeof HtmlWebpackPlugin, {
21
+ styleTests,
22
+ scriptTests,
23
+ distPath
24
+ }: InlineChunkHtmlPluginOptions);
25
+ /**
26
+ * If we inlined the chunk to HTML,we should update the value of sourceMappingURL,
27
+ * because the relative path of source code has been changed.
28
+ * @param source
29
+ */
30
+ updateSourceMappingURL({
31
+ source,
32
+ compilation,
33
+ publicPath,
34
+ type
35
+ }: {
36
+ source: string;
37
+ compilation: Compilation;
38
+ publicPath: string;
39
+ type: 'js' | 'css';
40
+ }): string;
41
+ matchTests(name: string, source: string, tests: InlineChunkTest[]): boolean;
42
+ getInlinedScriptTag(publicPath: string, tag: HtmlTagObject, compilation: Compilation): HtmlWebpackPlugin.HtmlTagObject | {
43
+ tagName: string;
44
+ innerHTML: string;
45
+ attributes: {
46
+ [x: string]: string | boolean | null | undefined;
47
+ };
48
+ closeTag: boolean;
49
+ };
50
+ getInlinedCSSTag(publicPath: string, tag: HtmlTagObject, compilation: Compilation): HtmlWebpackPlugin.HtmlTagObject | {
51
+ tagName: string;
52
+ innerHTML: string;
53
+ closeTag: boolean;
54
+ };
55
+ getInlinedTag(publicPath: string, tag: HtmlTagObject, compilation: Compilation): HtmlWebpackPlugin.HtmlTagObject;
56
+ apply(compiler: Compiler): void;
57
+ }
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var InlineChunkHtmlPlugin_exports = {};
20
+ __export(InlineChunkHtmlPlugin_exports, {
21
+ InlineChunkHtmlPlugin: () => InlineChunkHtmlPlugin
22
+ });
23
+ module.exports = __toCommonJS(InlineChunkHtmlPlugin_exports);
24
+ var import_path = require("path");
25
+ var import_shared = require("@rsbuild/shared");
26
+ class InlineChunkHtmlPlugin {
27
+ constructor(htmlPlugin, { styleTests, scriptTests, distPath }) {
28
+ this.name = "InlineChunkHtmlPlugin";
29
+ this.styleTests = styleTests;
30
+ this.scriptTests = scriptTests;
31
+ this.distPath = distPath;
32
+ this.inlinedAssets = /* @__PURE__ */ new Set();
33
+ this.htmlPlugin = htmlPlugin;
34
+ }
35
+ /**
36
+ * If we inlined the chunk to HTML,we should update the value of sourceMappingURL,
37
+ * because the relative path of source code has been changed.
38
+ * @param source
39
+ */
40
+ updateSourceMappingURL({
41
+ source,
42
+ compilation,
43
+ publicPath,
44
+ type
45
+ }) {
46
+ const { devtool } = compilation.options;
47
+ if (devtool && // If the source map is inlined, we do not need to update the sourceMappingURL
48
+ !devtool.includes("inline") && source.includes("# sourceMappingURL")) {
49
+ const prefix = (0, import_shared.addTrailingSlash)(
50
+ (0, import_path.join)(publicPath, this.distPath[type] || "")
51
+ );
52
+ return source.replace(
53
+ /# sourceMappingURL=/,
54
+ `# sourceMappingURL=${prefix}`
55
+ );
56
+ }
57
+ return source;
58
+ }
59
+ matchTests(name, source, tests) {
60
+ return tests.some((test) => {
61
+ if ((0, import_shared.isFunction)(test)) {
62
+ const size = source.length;
63
+ return test({ name, size });
64
+ }
65
+ return test.exec(name);
66
+ });
67
+ }
68
+ getInlinedScriptTag(publicPath, tag, compilation) {
69
+ const { assets } = compilation;
70
+ if (!(tag?.attributes.src && typeof tag.attributes.src === "string")) {
71
+ return tag;
72
+ }
73
+ const { src, ...otherAttrs } = tag.attributes;
74
+ const scriptName = publicPath ? src.replace(publicPath, "") : src;
75
+ const asset = assets[scriptName];
76
+ if (asset == null) {
77
+ return tag;
78
+ }
79
+ const source = asset.source().toString();
80
+ const shouldInline = this.matchTests(scriptName, source, this.scriptTests);
81
+ if (!shouldInline) {
82
+ return tag;
83
+ }
84
+ const ret = {
85
+ tagName: "script",
86
+ innerHTML: this.updateSourceMappingURL({
87
+ source,
88
+ compilation,
89
+ publicPath,
90
+ type: "js"
91
+ }),
92
+ attributes: {
93
+ ...otherAttrs
94
+ },
95
+ closeTag: true
96
+ };
97
+ this.inlinedAssets.add(scriptName);
98
+ return ret;
99
+ }
100
+ getInlinedCSSTag(publicPath, tag, compilation) {
101
+ const { assets } = compilation;
102
+ if (!(tag.attributes.href && typeof tag.attributes.href === "string")) {
103
+ return tag;
104
+ }
105
+ const linkName = publicPath ? tag.attributes.href.replace(publicPath, "") : tag.attributes.href;
106
+ const asset = assets[linkName];
107
+ if (asset == null) {
108
+ return tag;
109
+ }
110
+ const source = asset.source().toString();
111
+ const shouldInline = this.matchTests(linkName, source, this.styleTests);
112
+ if (!shouldInline) {
113
+ return tag;
114
+ }
115
+ const ret = {
116
+ tagName: "style",
117
+ innerHTML: this.updateSourceMappingURL({
118
+ source,
119
+ compilation,
120
+ publicPath,
121
+ type: "css"
122
+ }),
123
+ closeTag: true
124
+ };
125
+ this.inlinedAssets.add(linkName);
126
+ return ret;
127
+ }
128
+ getInlinedTag(publicPath, tag, compilation) {
129
+ if (tag.tagName === "script") {
130
+ return this.getInlinedScriptTag(
131
+ publicPath,
132
+ tag,
133
+ compilation
134
+ );
135
+ }
136
+ if (tag.tagName === "link" && tag.attributes && tag.attributes.rel === "stylesheet") {
137
+ return this.getInlinedCSSTag(
138
+ publicPath,
139
+ tag,
140
+ compilation
141
+ );
142
+ }
143
+ return tag;
144
+ }
145
+ apply(compiler) {
146
+ compiler.hooks.compilation.tap(this.name, (compilation) => {
147
+ const publicPath = (0, import_shared.getPublicPathFromCompiler)(compiler);
148
+ const tagFunction = (tag) => this.getInlinedTag(publicPath, tag, compilation);
149
+ const hooks = this.htmlPlugin.getHooks(compilation);
150
+ hooks.alterAssetTagGroups.tap(this.name, (assets) => {
151
+ assets.headTags = assets.headTags.map(tagFunction);
152
+ assets.bodyTags = assets.bodyTags.map(tagFunction);
153
+ return assets;
154
+ });
155
+ compilation.hooks.processAssets.tap(
156
+ {
157
+ name: "InlineChunkHtmlPlugin",
158
+ /**
159
+ * Remove marked inline assets in summarize stage,
160
+ * which should be later than the emitting of html-webpack-plugin
161
+ */
162
+ stage: import_shared.COMPILATION_PROCESS_STAGE.PROCESS_ASSETS_STAGE_SUMMARIZE
163
+ },
164
+ () => {
165
+ const { devtool } = compiler.options;
166
+ this.inlinedAssets.forEach((name) => {
167
+ if (devtool === "hidden-source-map") {
168
+ compilation.deleteAsset(name);
169
+ } else {
170
+ delete compilation.assets[name];
171
+ }
172
+ });
173
+ this.inlinedAssets.clear();
174
+ }
175
+ );
176
+ });
177
+ }
178
+ }
179
+ // Annotate the CommonJS export names for ESM import in node:
180
+ 0 && (module.exports = {
181
+ InlineChunkHtmlPlugin
182
+ });
@@ -47,10 +47,14 @@ function getDefaultEntry(root) {
47
47
  }
48
48
  return {};
49
49
  }
50
+ function getAbsoluteDistPath(cwd, outputConfig) {
51
+ const root = (0, import_shared.getDistPath)(outputConfig, "root");
52
+ return (0, import_path.isAbsolute)(root) ? root : (0, import_path.join)(cwd, root);
53
+ }
50
54
  function createContextByConfig(options, bundlerType, sourceConfig = {}, outputConfig = {}) {
51
55
  const { cwd, target } = options;
52
56
  const rootPath = cwd;
53
- const distPath = (0, import_shared.getAbsoluteDistPath)(cwd, outputConfig);
57
+ const distPath = getAbsoluteDistPath(cwd, outputConfig);
54
58
  const cachePath = (0, import_path.join)(rootPath, "node_modules", ".cache");
55
59
  if (sourceConfig.entries) {
56
60
  import_shared.logger.warn(
@@ -64,7 +68,7 @@ function createContextByConfig(options, bundlerType, sourceConfig = {}, outputCo
64
68
  entry: sourceConfig.entry || // TODO: remove sourceConfig.entries in v0.2.0
65
69
  // compat with previous config
66
70
  sourceConfig.entries || getDefaultEntry(rootPath),
67
- version: "0.1.4",
71
+ version: "0.1.6",
68
72
  target,
69
73
  rootPath,
70
74
  distPath,
@@ -74,7 +78,7 @@ function createContextByConfig(options, bundlerType, sourceConfig = {}, outputCo
74
78
  return context;
75
79
  }
76
80
  function updateContextByNormalizedConfig(context, config) {
77
- context.distPath = (0, import_shared.getAbsoluteDistPath)(context.rootPath, config.output);
81
+ context.distPath = getAbsoluteDistPath(context.rootPath, config.output);
78
82
  if (config.source.entry) {
79
83
  context.entry = config.source.entry;
80
84
  }
@@ -1,5 +1,4 @@
1
- import { type OnExitFn, type OnAfterBuildFn, type OnBeforeBuildFn, type OnDevCompileDoneFn, type ModifyRsbuildConfigFn, type OnAfterStartDevServerFn, type OnBeforeStartDevServerFn, type OnAfterStartProdServerFn, type OnBeforeStartProdServerFn, type OnAfterCreateCompilerFn, type OnBeforeCreateCompilerFn, type ModifyBundlerChainFn, type RspackConfig, type ModifyRspackConfigFn } from '@rsbuild/shared';
2
- import type { RsbuildConfig } from '../../types';
1
+ import type { OnExitFn, OnAfterBuildFn, OnBeforeBuildFn, OnDevCompileDoneFn, ModifyBundlerChainFn, ModifyRspackConfigFn, ModifyWebpackChainFn, ModifyWebpackConfigFn, ModifyRsbuildConfigFn, OnAfterStartDevServerFn, OnBeforeStartDevServerFn, OnAfterStartProdServerFn, OnBeforeStartProdServerFn, OnAfterCreateCompilerFn, OnBeforeCreateCompilerFn } from '@rsbuild/shared';
3
2
  export declare function initHooks(): {
4
3
  /** parameters are not bundler-related */
5
4
  onExitHook: import("@rsbuild/shared").AsyncHook<OnExitFn>;
@@ -10,11 +9,13 @@ export declare function initHooks(): {
10
9
  onBeforeStartProdServerHook: import("@rsbuild/shared").AsyncHook<OnBeforeStartProdServerFn>;
11
10
  /** parameters are bundler-related */
12
11
  onAfterBuildHook: import("@rsbuild/shared").AsyncHook<OnAfterBuildFn>;
13
- onBeforeBuildHook: import("@rsbuild/shared").AsyncHook<OnBeforeBuildFn<RspackConfig>>;
12
+ onBeforeBuildHook: import("@rsbuild/shared").AsyncHook<OnBeforeBuildFn>;
14
13
  modifyRspackConfigHook: import("@rsbuild/shared").AsyncHook<ModifyRspackConfigFn>;
15
- modifyRsbuildConfigHook: import("@rsbuild/shared").AsyncHook<ModifyRsbuildConfigFn<RsbuildConfig>>;
16
- onAfterCreateCompilerHook: import("@rsbuild/shared").AsyncHook<OnAfterCreateCompilerFn>;
17
- onBeforeCreateCompilerHook: import("@rsbuild/shared").AsyncHook<OnBeforeCreateCompilerFn<RspackConfig>>;
18
14
  modifyBundlerChainHook: import("@rsbuild/shared").AsyncHook<ModifyBundlerChainFn>;
15
+ modifyWebpackChainHook: import("@rsbuild/shared").AsyncHook<ModifyWebpackChainFn>;
16
+ modifyWebpackConfigHook: import("@rsbuild/shared").AsyncHook<ModifyWebpackConfigFn>;
17
+ modifyRsbuildConfigHook: import("@rsbuild/shared").AsyncHook<ModifyRsbuildConfigFn>;
18
+ onAfterCreateCompilerHook: import("@rsbuild/shared").AsyncHook<OnAfterCreateCompilerFn>;
19
+ onBeforeCreateCompilerHook: import("@rsbuild/shared").AsyncHook<OnBeforeCreateCompilerFn>;
19
20
  };
20
21
  export type Hooks = ReturnType<typeof initHooks>;
@@ -35,10 +35,12 @@ function initHooks() {
35
35
  onAfterBuildHook: (0, import_shared.createAsyncHook)(),
36
36
  onBeforeBuildHook: (0, import_shared.createAsyncHook)(),
37
37
  modifyRspackConfigHook: (0, import_shared.createAsyncHook)(),
38
+ modifyBundlerChainHook: (0, import_shared.createAsyncHook)(),
39
+ modifyWebpackChainHook: (0, import_shared.createAsyncHook)(),
40
+ modifyWebpackConfigHook: (0, import_shared.createAsyncHook)(),
38
41
  modifyRsbuildConfigHook: (0, import_shared.createAsyncHook)(),
39
42
  onAfterCreateCompilerHook: (0, import_shared.createAsyncHook)(),
40
- onBeforeCreateCompilerHook: (0, import_shared.createAsyncHook)(),
41
- modifyBundlerChainHook: (0, import_shared.createAsyncHook)()
43
+ onBeforeCreateCompilerHook: (0, import_shared.createAsyncHook)()
42
44
  };
43
45
  }
44
46
  // Annotate the CommonJS export names for ESM import in node: