@lynx-js/template-webpack-plugin-canary 0.8.1-canary-20250626-0bbb23af → 0.8.1-canary-20250627-a7e8b5bb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @lynx-js/template-webpack-plugin
2
2
 
3
- ## 0.8.1-canary-20250626120135-0bbb23af76b1852328c8a4f38b0309b6ed1da9b4
3
+ ## 0.8.1-canary-20250627143427-a7e8b5bbbab0490e7cf6f47581130e7b32739abb
4
4
 
5
5
  ### Patch Changes
6
6
 
@@ -20,6 +20,34 @@
20
20
  }
21
21
  ```
22
22
 
23
+ - Enable fine-grained control for `output.inlineScripts` ([#883](https://github.com/lynx-family/lynx-stack/pull/883))
24
+
25
+ ```ts
26
+ type InlineChunkTestFunction = (params: {
27
+ size: number;
28
+ name: string;
29
+ }) => boolean;
30
+
31
+ type InlineChunkTest = RegExp | InlineChunkTestFunction;
32
+
33
+ type InlineChunkConfig =
34
+ | boolean
35
+ | InlineChunkTest
36
+ | { enable?: boolean | "auto"; test: InlineChunkTest };
37
+ ```
38
+
39
+ ```ts
40
+ import { defineConfig } from "@lynx-js/rspeedy";
41
+
42
+ export default defineConfig({
43
+ output: {
44
+ inlineScripts: ({ name, size }) => {
45
+ return name.includes("foo") && size < 1000;
46
+ },
47
+ },
48
+ });
49
+ ```
50
+
23
51
  ## 0.8.0
24
52
 
25
53
  ### Minor Changes
@@ -1,11 +1,20 @@
1
1
  import type { Compiler } from 'webpack';
2
+ type InlineChunkTestFunction = (params: {
3
+ size: number;
4
+ name: string;
5
+ }) => boolean;
6
+ type InlineChunkTest = RegExp | InlineChunkTestFunction;
7
+ type InlineChunkConfig = boolean | InlineChunkTest | {
8
+ enable?: boolean | 'auto';
9
+ test: InlineChunkTest;
10
+ };
2
11
  /**
3
12
  * The options for LynxEncodePluginOptions
4
13
  *
5
14
  * @public
6
15
  */
7
16
  export interface LynxEncodePluginOptions {
8
- inlineScripts?: boolean | undefined;
17
+ inlineScripts?: InlineChunkConfig | undefined;
9
18
  }
10
19
  /**
11
20
  * LynxEncodePlugin
@@ -63,3 +72,4 @@ export declare class LynxEncodePluginImpl {
63
72
  }
64
73
  export declare function isDebug(): boolean;
65
74
  export declare function isRsdoctor(): boolean;
75
+ export {};
@@ -85,20 +85,30 @@ export class LynxEncodePluginImpl {
85
85
  }, async (args) => {
86
86
  const { encodeData } = args;
87
87
  const { manifest } = encodeData;
88
- let publicPath = '/';
89
- if (!this.options.inlineScripts) {
90
- if (typeof compilation?.outputOptions.publicPath === 'function') {
91
- compilation.errors.push(new compiler.webpack.WebpackError('`publicPath` as a function is not supported yet.'));
88
+ const [inlinedManifest, externalManifest] = Object.entries(manifest)
89
+ .reduce(([inlined, external], [name, content]) => {
90
+ const assert = compilation.getAsset(name);
91
+ const shouldInline = this.#shouldInlineScript(name, assert.source.size());
92
+ if (shouldInline) {
93
+ inlined[name] = content;
92
94
  }
93
95
  else {
94
- publicPath = compilation?.outputOptions.publicPath ?? '/';
96
+ external[name] = content;
95
97
  }
98
+ return [inlined, external];
99
+ }, [{}, {}]);
100
+ let publicPath = '/';
101
+ if (typeof compilation?.outputOptions.publicPath === 'function') {
102
+ compilation.errors.push(new compiler.webpack.WebpackError('`publicPath` as a function is not supported yet.'));
103
+ }
104
+ else {
105
+ publicPath = compilation?.outputOptions.publicPath ?? '/';
96
106
  }
97
107
  if (!isDebug() && !isDev && !isRsdoctor()) {
98
108
  [
99
109
  encodeData.lepusCode.root,
100
110
  ...encodeData.lepusCode.chunks,
101
- ...Object.keys(manifest).map(name => ({ name })),
111
+ ...Object.keys(inlinedManifest).map(name => ({ name })),
102
112
  ...encodeData.css.chunks,
103
113
  ]
104
114
  .filter(asset => asset !== undefined)
@@ -113,26 +123,31 @@ export class LynxEncodePluginImpl {
113
123
  // '/app-service.js': `
114
124
  // lynx.requireModule('async-chunk1')
115
125
  // lynx.requireModule('async-chunk2')
116
- // lynx.requireModule('initial-chunk1')
117
- // lynx.requireModule('initial-chunk2')
126
+ // lynx.requireModule('inlined-initial-chunk1')
127
+ // lynx.requireModule('inlined-initial-chunk2')
128
+ // lynx.requireModuleAsync('external-initial-chunk1')
129
+ // lynx.requireModuleAsync('external-initial-chunk2')
118
130
  // `,
119
- // 'initial-chunk1': `<content-of-initial-chunk>`,
120
- // 'initial-chunk2': `<content-of-initial-chunk>`,
131
+ // 'inlined-initial-chunk1': `<content>`,
132
+ // 'inlined-initial-chunk2': `<content>`,
121
133
  // },
122
134
  // ```
123
135
  '/app-service.js': [
124
136
  this.#appServiceBanner(),
125
- Object.keys(manifest)
126
- .map((name) => `module.exports=lynx.requireModule('${this.#formatJSName(name, publicPath)}',globDynamicComponentEntry?globDynamicComponentEntry:'__Card__')`)
127
- .join(','),
137
+ ...[externalManifest, inlinedManifest].flatMap(manifest => Object.keys(manifest).map(name => {
138
+ if (manifest === externalManifest) {
139
+ return `lynx.requireModuleAsync('${this.#formatJSName(name, publicPath)}')`;
140
+ }
141
+ else {
142
+ return `module.exports=lynx.requireModule('${this.#formatJSName(name, '/')}',globDynamicComponentEntry?globDynamicComponentEntry:'__Card__')`;
143
+ }
144
+ }).join(',')),
128
145
  this.#appServiceFooter(),
129
146
  ].join(''),
130
- ...(this.options.inlineScripts
131
- ? Object.fromEntries(Object.entries(manifest).map(([name, source]) => [
132
- this.#formatJSName(name, publicPath),
133
- source,
134
- ]))
135
- : {}),
147
+ ...Object.fromEntries(Object.entries(inlinedManifest).map(([name, content]) => [
148
+ this.#formatJSName(name, '/'),
149
+ content,
150
+ ])),
136
151
  };
137
152
  return args;
138
153
  });
@@ -162,6 +177,24 @@ export class LynxEncodePluginImpl {
162
177
  #formatJSName(name, publicPath) {
163
178
  return publicPath + name;
164
179
  }
180
+ #shouldInlineScript(name, size) {
181
+ const inlineConfig = this.options.inlineScripts;
182
+ if (inlineConfig instanceof RegExp) {
183
+ return inlineConfig.test(name);
184
+ }
185
+ if (typeof inlineConfig === 'function') {
186
+ return inlineConfig({ size, name });
187
+ }
188
+ if (typeof inlineConfig === 'object') {
189
+ if (inlineConfig.enable === false)
190
+ return false;
191
+ if (inlineConfig.test instanceof RegExp) {
192
+ return inlineConfig.test.test(name);
193
+ }
194
+ return inlineConfig.test({ size, name });
195
+ }
196
+ return inlineConfig !== false;
197
+ }
165
198
  options;
166
199
  }
167
200
  export function isDebug() {
@@ -2,6 +2,10 @@ import { AsyncSeriesBailHook, AsyncSeriesWaterfallHook, SyncWaterfallHook } from
2
2
  import type { Asset, Compilation, Compiler } from 'webpack';
3
3
  import type * as CSS from '@lynx-js/css-serializer';
4
4
  import { cssChunksToMap } from './css/cssChunksToMap.js';
5
+ export type OriginManifest = Record<string, {
6
+ content: string;
7
+ size: number;
8
+ }>;
5
9
  /**
6
10
  * The options for encoding a Lynx bundle.
7
11
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/template-webpack-plugin-canary",
3
- "version": "0.8.1-canary-20250626-0bbb23af",
3
+ "version": "0.8.1-canary-20250627-a7e8b5bb",
4
4
  "description": "Simplifies creation of Lynx template files to serve your webpack bundles",
5
5
  "keywords": [
6
6
  "webpack",