solid-optimizer 0.3.0 → 1.0.0-next.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 +94 -42
- package/dist/{compile-BroGlagZ.d.mts → compile-BRCj4UN7.d.mts} +39 -19
- package/dist/compile-BRCj4UN7.d.mts.map +1 -0
- package/dist/{compile-ocDYvJUW.mjs → compile-Cz6hbiQv.mjs} +1312 -705
- package/dist/compile-Cz6hbiQv.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +21 -10
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +648 -269
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -11
- package/src/ast.ts +12 -4
- package/src/compile.ts +69 -42
- package/src/constants.ts +42 -2
- package/src/context.ts +32 -0
- package/src/copy-size.ts +73 -0
- package/src/fold.ts +619 -17
- package/src/index.ts +1 -0
- package/src/inline.ts +568 -299
- package/src/memo.ts +197 -33
- package/src/provider.ts +104 -82
- package/src/scope.ts +14 -1
- package/src/size.ts +35 -73
- package/src/vite/globals.ts +67 -0
- package/src/vite/index.ts +475 -217
- package/src/vite/inject.ts +51 -2
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +3 -3
- package/dist/compile-BroGlagZ.d.mts.map +0 -1
- package/dist/compile-ocDYvJUW.mjs.map +0 -1
- package/src/server.ts +0 -275
package/src/vite/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `solid-optimizer/vite`:
|
|
2
|
+
* `solid-optimizer/vite`: `@solidjs/vite-plugin` with the optimizer built in.
|
|
3
3
|
*
|
|
4
|
-
* It takes the same options as
|
|
5
|
-
* returns the official
|
|
4
|
+
* It takes the same options as `@solidjs/vite-plugin`, plus `optimizer`, and
|
|
5
|
+
* returns the official plugins with the optimizer wired in. It runs in one of
|
|
6
6
|
* two modes.
|
|
7
7
|
*
|
|
8
8
|
* - Chunk mode keeps JSX through bundling. Each chunk is optimized as a whole
|
|
@@ -16,23 +16,29 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { readFile } from 'node:fs/promises';
|
|
18
18
|
import { createRequire } from 'node:module';
|
|
19
|
+
import path from 'node:path';
|
|
19
20
|
import remapping from '@jridgewell/remapping';
|
|
20
|
-
import type * as Babel from '@babel/core';
|
|
21
21
|
import MagicString from 'magic-string';
|
|
22
|
+
import type * as SolidCompiler from '@solidjs/compiler';
|
|
23
|
+
import type { Options as SolidPluginOptions } from '@solidjs/vite-plugin';
|
|
24
|
+
import solidPlugin from '@solidjs/vite-plugin';
|
|
22
25
|
import type { Node, Program } from 'oxc-parser';
|
|
23
26
|
import type { Plugin, ResolvedConfig, Rollup } from 'vite';
|
|
24
27
|
import { createFilter, transformWithOxc } from 'vite';
|
|
25
|
-
import type { Options as SolidPluginOptions } from 'vite-plugin-solid';
|
|
26
|
-
import solidPlugin from 'vite-plugin-solid';
|
|
27
28
|
import type { CompileOptions, CompileResult, ModuleConstants } from '../compile';
|
|
28
29
|
import { compile, readModuleConstants } from '../compile';
|
|
29
|
-
import { collectParents, parse } from '../ast';
|
|
30
|
+
import { collectParents, forEachChild, isIntrinsicTag, parse } from '../ast';
|
|
30
31
|
import { analyzeScopes } from '../scope';
|
|
31
32
|
import { CONTEXT_SAFE_PRIMITIVES } from '../provider';
|
|
32
33
|
import type { ImportedConstants } from '../constants';
|
|
33
34
|
import type { Primitive } from '../value';
|
|
35
|
+
import { sharedGlobals } from './globals';
|
|
36
|
+
import type { Skip } from '../context';
|
|
37
|
+
import type { OptimizerReport } from './report';
|
|
38
|
+
import { formatReport } from './report';
|
|
34
39
|
import { exposeLocals, readExportedComponent } from './inject';
|
|
35
40
|
import { nameNamespaceTags } from './namespace';
|
|
41
|
+
import { protectRefs, restoreRefs } from './refs';
|
|
36
42
|
import type { UsageIndex } from './usage';
|
|
37
43
|
import { buildUsageIndex } from './usage';
|
|
38
44
|
import {
|
|
@@ -49,13 +55,6 @@ export interface OptimizerOptions extends Pick<
|
|
|
49
55
|
CompileOptions,
|
|
50
56
|
'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'maxPasses'
|
|
51
57
|
> {
|
|
52
|
-
/**
|
|
53
|
-
* Reduce Solid's reactive primitives to what they do on the server, in
|
|
54
|
-
* server builds. See the `server` option of `compile`.
|
|
55
|
-
*
|
|
56
|
-
* @default true
|
|
57
|
-
*/
|
|
58
|
-
server?: boolean;
|
|
59
58
|
/**
|
|
60
59
|
* Where the optimizer runs.
|
|
61
60
|
*
|
|
@@ -71,38 +70,28 @@ export interface OptimizerOptions extends Pick<
|
|
|
71
70
|
* @default false
|
|
72
71
|
*/
|
|
73
72
|
dev?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Report what the optimizer leaves alone in each build, and why: each
|
|
75
|
+
* component call that stays a call, each provider that stays, and each
|
|
76
|
+
* memo that stays. `true` prints the report. A function receives it
|
|
77
|
+
* instead, once per build environment.
|
|
78
|
+
*
|
|
79
|
+
* @default false
|
|
80
|
+
*/
|
|
81
|
+
report?: boolean | ((report: OptimizerReport) => void);
|
|
74
82
|
}
|
|
75
83
|
|
|
84
|
+
export type { OptimizerReport } from './report';
|
|
85
|
+
|
|
76
86
|
export interface Options extends Partial<SolidPluginOptions> {
|
|
77
|
-
/** Set to `false` to use
|
|
87
|
+
/** Set to `false` to use `@solidjs/vite-plugin` as it is. */
|
|
78
88
|
optimizer?: OptimizerOptions | false;
|
|
79
89
|
}
|
|
80
90
|
|
|
81
|
-
|
|
82
|
-
type JSXOptions =
|
|
83
|
-
generate?: 'dom' | 'ssr' | 'universal';
|
|
84
|
-
hydratable?: boolean;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
interface Compiler {
|
|
88
|
-
readonly babel: typeof Babel;
|
|
89
|
-
readonly preset: Babel.PluginTarget;
|
|
90
|
-
}
|
|
91
|
+
type Compiler = typeof SolidCompiler;
|
|
92
|
+
type JSXOptions = SolidCompiler.TransformOptions;
|
|
91
93
|
|
|
92
|
-
const DEFAULT_MODULE_NAME = '
|
|
93
|
-
|
|
94
|
-
const DEFAULT_BUILT_INS = [
|
|
95
|
-
'For',
|
|
96
|
-
'Show',
|
|
97
|
-
'Switch',
|
|
98
|
-
'Match',
|
|
99
|
-
'Suspense',
|
|
100
|
-
'SuspenseList',
|
|
101
|
-
'Portal',
|
|
102
|
-
'Index',
|
|
103
|
-
'Dynamic',
|
|
104
|
-
'ErrorBoundary',
|
|
105
|
-
];
|
|
94
|
+
const DEFAULT_MODULE_NAME = '@solidjs/web';
|
|
106
95
|
|
|
107
96
|
const JSX_MODULE = /\.[mc]?[jt]sx$/i;
|
|
108
97
|
|
|
@@ -127,55 +116,64 @@ function withRelated(helpers: readonly string[]): Set<string> {
|
|
|
127
116
|
return result;
|
|
128
117
|
}
|
|
129
118
|
|
|
130
|
-
|
|
131
|
-
// Use the Babel and preset `vite-plugin-solid` uses, so both lower JSX the same way.
|
|
132
|
-
const require = createRequire(import.meta.url);
|
|
133
|
-
const pluginRequire = createRequire(require.resolve('vite-plugin-solid'));
|
|
134
|
-
const presetRequire = createRequire(pluginRequire.resolve('babel-preset-solid'));
|
|
135
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
136
|
-
const babel = presetRequire('@babel/core') as typeof Babel;
|
|
137
|
-
const preset: unknown = pluginRequire('babel-preset-solid');
|
|
138
|
-
if (typeof preset !== 'function') {
|
|
139
|
-
throw new TypeError('[solid-optimizer] babel-preset-solid did not load');
|
|
140
|
-
}
|
|
141
|
-
return { babel, preset };
|
|
142
|
-
}
|
|
119
|
+
const LAZY_PLACEHOLDER = /"__SOLID_LAZY_MODULE__:([^"]+)"/g;
|
|
143
120
|
|
|
144
121
|
/**
|
|
145
|
-
*
|
|
122
|
+
* The common kinds of attribute: a class, any other attribute, events, and
|
|
123
|
+
* children. Inlining a component turns a spread onto an element into
|
|
124
|
+
* attributes like these, in a module that may not use their helpers itself.
|
|
125
|
+
* A rarer kind, like `style`, can still need a helper no module imports.
|
|
126
|
+
* The chunk is then lowered without optimizing it.
|
|
146
127
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
presets: [[compiler.preset, options]],
|
|
157
|
-
ast: false,
|
|
158
|
-
sourceMaps: true,
|
|
159
|
-
configFile: false,
|
|
160
|
-
babelrc: false,
|
|
161
|
-
parserOpts: {
|
|
162
|
-
plugins: /\.[mc]?tsx$/i.test(filename) ? ['jsx', 'typescript'] : ['jsx'],
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
// An empty module, like a chunk a library build left empty, lowers to ''.
|
|
166
|
-
if (typeof result?.code !== 'string') {
|
|
167
|
-
throw new Error(`[solid-optimizer] babel-preset-solid produced no code for ${filename}`);
|
|
128
|
+
const ATTRIBUTE_SAMPLE = `const a = () => 0;
|
|
129
|
+
export const Sample = () => (
|
|
130
|
+
<div class={a()} id={a()} onClick={a()} onScroll={a()}>{a()}</div>
|
|
131
|
+
);`;
|
|
132
|
+
|
|
133
|
+
/** Whether the code spreads anything onto an intrinsic element. */
|
|
134
|
+
function spreadsOntoElement(code: string, filename: string): boolean {
|
|
135
|
+
if (!code.includes('...')) {
|
|
136
|
+
return false;
|
|
168
137
|
}
|
|
169
|
-
|
|
138
|
+
let found = false;
|
|
139
|
+
const visit = (node: Node): void => {
|
|
140
|
+
if (found) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (
|
|
144
|
+
node.type === 'JSXOpeningElement' &&
|
|
145
|
+
node.name.type === 'JSXIdentifier' &&
|
|
146
|
+
isIntrinsicTag(node.name.name) &&
|
|
147
|
+
node.attributes.some((attribute) => attribute.type === 'JSXSpreadAttribute')
|
|
148
|
+
) {
|
|
149
|
+
found = true;
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
forEachChild(node, visit);
|
|
153
|
+
};
|
|
154
|
+
visit(parse(filename, code));
|
|
155
|
+
return found;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function loadCompiler(): Compiler {
|
|
159
|
+
// Use the compiler `@solidjs/vite-plugin` uses, so both lower JSX the same way.
|
|
160
|
+
const require = createRequire(import.meta.url);
|
|
161
|
+
const pluginRequire = createRequire(require.resolve('@solidjs/vite-plugin'));
|
|
162
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
163
|
+
return pluginRequire('@solidjs/compiler') as Compiler;
|
|
170
164
|
}
|
|
171
165
|
|
|
172
166
|
function stripQuery(id: string): string {
|
|
173
167
|
return id.replace(/\?.*$/, '');
|
|
174
168
|
}
|
|
175
169
|
|
|
176
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Chains the maps of each step into one, or `null` when the build writes no
|
|
172
|
+
* source maps, since chaining the maps of a large chunk takes a while.
|
|
173
|
+
*/
|
|
174
|
+
function combineMaps(wanted: boolean, maps: (string | null | undefined)[]): string | null {
|
|
177
175
|
const present = maps.filter((map): map is string => typeof map === 'string');
|
|
178
|
-
if (present.length === 0) {
|
|
176
|
+
if (!wanted || present.length === 0) {
|
|
179
177
|
return null;
|
|
180
178
|
}
|
|
181
179
|
return remapping(present.reverse(), () => null).toString();
|
|
@@ -224,14 +222,27 @@ function isClient(context: { environment?: { config: { consumer: string } } }):
|
|
|
224
222
|
|
|
225
223
|
export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
226
224
|
const { optimizer: optimizerOption, ...solidOptions } = options;
|
|
227
|
-
const
|
|
225
|
+
const plugins = solidPlugin(solidOptions);
|
|
228
226
|
if (optimizerOption === false) {
|
|
229
|
-
return
|
|
227
|
+
return plugins;
|
|
230
228
|
}
|
|
231
229
|
const optimizer = optimizerOption ?? {};
|
|
232
230
|
const moduleName = solidOptions.solid?.moduleName ?? DEFAULT_MODULE_NAME;
|
|
233
231
|
const moduleSources = [...new Set(['solid-js', DEFAULT_MODULE_NAME, moduleName])];
|
|
234
|
-
const builtIns = new Set(
|
|
232
|
+
const builtIns = new Set(
|
|
233
|
+
solidOptions.solid?.builtIns ?? [
|
|
234
|
+
'For',
|
|
235
|
+
'Show',
|
|
236
|
+
'Switch',
|
|
237
|
+
'Match',
|
|
238
|
+
'Loading',
|
|
239
|
+
'Reveal',
|
|
240
|
+
'Portal',
|
|
241
|
+
'Repeat',
|
|
242
|
+
'Dynamic',
|
|
243
|
+
'Errored',
|
|
244
|
+
],
|
|
245
|
+
);
|
|
235
246
|
const compileOptions: CompileOptions = {
|
|
236
247
|
fold: optimizer.fold,
|
|
237
248
|
inline: optimizer.inline,
|
|
@@ -241,9 +252,24 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
241
252
|
maxPasses: optimizer.maxPasses,
|
|
242
253
|
builtIns: [...builtIns],
|
|
243
254
|
moduleSources,
|
|
255
|
+
report: optimizer.report !== undefined && optimizer.report !== false,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/** What the final compile of each chunk or module left alone, by environment. */
|
|
259
|
+
const skips = new Map<string, Skip[]>();
|
|
260
|
+
const addSkips = (context: { environment?: { name: string } }, list?: readonly Skip[]): void => {
|
|
261
|
+
if (!list) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const environment = context.environment?.name ?? 'client';
|
|
265
|
+
const entries = skips.get(environment) ?? [];
|
|
266
|
+
entries.push(...list);
|
|
267
|
+
skips.set(environment, entries);
|
|
244
268
|
};
|
|
245
269
|
|
|
246
270
|
let config: ResolvedConfig | undefined;
|
|
271
|
+
/** The `define` entries and `import.meta.env` values every environment shares. */
|
|
272
|
+
let globals: Record<string, Primitive> = {};
|
|
247
273
|
let compiler: Compiler | undefined;
|
|
248
274
|
let filter: ((id: string) => boolean) | undefined;
|
|
249
275
|
const getCompiler = (): Compiler => {
|
|
@@ -252,17 +278,46 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
252
278
|
};
|
|
253
279
|
|
|
254
280
|
const isBuild = (): boolean => config?.command === 'build';
|
|
281
|
+
/** Whether the output needs source maps: while serving, or when the build writes them. */
|
|
282
|
+
const wantsMaps = (): boolean => !isBuild() || Boolean(config?.build.sourcemap);
|
|
255
283
|
|
|
256
284
|
/**
|
|
257
285
|
* The JSX options the official plugin passes for a posture, so the
|
|
258
|
-
* lowering here matches it. Mirrors
|
|
286
|
+
* lowering here matches it. Mirrors `getSolidOptions` in `@solidjs/vite-plugin`.
|
|
259
287
|
*/
|
|
260
288
|
const jsxOptions = (isSsr: boolean): JSXOptions => {
|
|
261
|
-
let posture: JSXOptions
|
|
262
|
-
if (solidOptions.ssr) {
|
|
289
|
+
let posture: Pick<JSXOptions, 'generate' | 'hydratable'>;
|
|
290
|
+
if (solidOptions.start && !solidOptions.ssr) {
|
|
291
|
+
posture = { generate: isSsr ? 'ssr' : 'dom', hydratable: false };
|
|
292
|
+
} else if (solidOptions.ssr) {
|
|
263
293
|
posture = { generate: isSsr ? 'ssr' : 'dom', hydratable: true };
|
|
294
|
+
} else {
|
|
295
|
+
posture = { generate: 'dom', hydratable: false };
|
|
296
|
+
}
|
|
297
|
+
const dev = solidOptions.dev === true || (solidOptions.dev !== false && !isBuild());
|
|
298
|
+
const names = dev || solidOptions.observe === true;
|
|
299
|
+
const { sourceNames, ...rest } = solidOptions.solid ?? {};
|
|
300
|
+
let resolvedNames: JSXOptions['sourceNames'] = false;
|
|
301
|
+
if (typeof sourceNames === 'boolean') {
|
|
302
|
+
resolvedNames = sourceNames;
|
|
303
|
+
} else if (sourceNames !== undefined || names) {
|
|
304
|
+
const components = sourceNames?.components ?? names;
|
|
305
|
+
const bindings = sourceNames?.bindings ?? names;
|
|
306
|
+
resolvedNames = components || bindings ? { components, bindings } : false;
|
|
264
307
|
}
|
|
265
|
-
return { ...posture, ...
|
|
308
|
+
return { ...posture, dev, sourceNames: resolvedNames, ...rest };
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
let sampleHelpers: string[] | undefined;
|
|
312
|
+
/** The helpers the JSX transform uses for attributes in chunk mode. */
|
|
313
|
+
const attributeHelpers = (): string[] => {
|
|
314
|
+
sampleHelpers ??= generatedImports(
|
|
315
|
+
getCompiler().transform(ATTRIBUTE_SAMPLE, { ...jsxOptions(false), filename: 'sample.jsx' })
|
|
316
|
+
.code,
|
|
317
|
+
'sample.jsx',
|
|
318
|
+
moduleName,
|
|
319
|
+
);
|
|
320
|
+
return sampleHelpers;
|
|
266
321
|
};
|
|
267
322
|
|
|
268
323
|
/**
|
|
@@ -272,8 +327,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
272
327
|
if (!isBuild() || (optimizer.mode ?? 'auto') !== 'auto') {
|
|
273
328
|
return false;
|
|
274
329
|
}
|
|
275
|
-
//
|
|
276
|
-
if (solidOptions.babel) {
|
|
330
|
+
// A Babel pass or backend has to see the modules, which chunk mode skips.
|
|
331
|
+
if (solidOptions.babel || solidOptions.compiler === 'babel') {
|
|
277
332
|
return false;
|
|
278
333
|
}
|
|
279
334
|
const client = jsxOptions(false);
|
|
@@ -281,114 +336,162 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
281
336
|
};
|
|
282
337
|
|
|
283
338
|
const isJSXModule = (id: string): boolean => {
|
|
284
|
-
filter ??= createFilter(solidOptions.include, solidOptions.exclude
|
|
339
|
+
filter ??= createFilter(solidOptions.include, solidOptions.exclude, {
|
|
340
|
+
resolve: config?.root,
|
|
341
|
+
});
|
|
285
342
|
return filter(id) && JSX_MODULE.test(stripQuery(id));
|
|
286
343
|
};
|
|
287
344
|
|
|
288
345
|
/** Modules that keep their JSX, which the bundler must parse as JSX. */
|
|
289
346
|
const preserved = new Set<string>();
|
|
290
347
|
|
|
291
|
-
/** What each module imports and the constants it exports, by module id. */
|
|
292
|
-
const moduleConstants = new Map<string, Promise<ModuleConstants | undefined>>();
|
|
293
|
-
|
|
294
348
|
/**
|
|
295
|
-
*
|
|
349
|
+
* What each module imports and the constants it exports, by module id,
|
|
350
|
+
* once read in full. A module whose constants needed a module with JSX
|
|
351
|
+
* that could not be loaded, to avoid a cycle, is not kept, since another
|
|
352
|
+
* transform may load it.
|
|
296
353
|
*
|
|
297
|
-
* A
|
|
298
|
-
*
|
|
299
|
-
*
|
|
354
|
+
* A read in progress is never shared. It can wait for a module with JSX,
|
|
355
|
+
* and a read that waited for it would wait for that module too, without
|
|
356
|
+
* the cycle checks knowing. Two reads of modules that import each other
|
|
357
|
+
* would also wait for each other.
|
|
358
|
+
*/
|
|
359
|
+
const moduleConstants = new Map<string, ConstantsResult>();
|
|
360
|
+
|
|
361
|
+
interface ConstantsResult {
|
|
362
|
+
readonly constants: ModuleConstants | undefined;
|
|
363
|
+
/** Whether a module with JSX was left out to avoid a cycle. */
|
|
364
|
+
readonly partial: boolean;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* The module an import resolves to, when its constants can be read, and
|
|
369
|
+
* whether it has JSX. Dependencies are skipped to keep the build fast.
|
|
300
370
|
*/
|
|
301
371
|
async function resolveConstantModule(
|
|
302
372
|
context: Rollup.PluginContext,
|
|
303
373
|
specifier: string,
|
|
304
374
|
importer: string,
|
|
305
|
-
): Promise<string | undefined> {
|
|
375
|
+
): Promise<{ id: string; jsx: boolean } | undefined> {
|
|
306
376
|
if (moduleSources.includes(specifier)) {
|
|
307
377
|
return undefined;
|
|
308
378
|
}
|
|
309
379
|
const resolved = await context.resolve(specifier, importer);
|
|
310
|
-
if (
|
|
311
|
-
!resolved ||
|
|
312
|
-
resolved.external ||
|
|
313
|
-
resolved.id.includes('/node_modules/') ||
|
|
314
|
-
isJSXModule(resolved.id)
|
|
315
|
-
) {
|
|
380
|
+
if (!resolved || resolved.external || resolved.id.includes('/node_modules/')) {
|
|
316
381
|
return undefined;
|
|
317
382
|
}
|
|
318
|
-
return resolved.id;
|
|
383
|
+
return { id: resolved.id, jsx: isJSXModule(resolved.id) };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* The code of a module with JSX to read constants from, as this plugin's
|
|
388
|
+
* transform returns it. `transform` is the module whose transform waits for
|
|
389
|
+
* it, which the cycle checks are about. `undefined` when that could wait
|
|
390
|
+
* for itself.
|
|
391
|
+
*/
|
|
392
|
+
async function loadJSXModule(
|
|
393
|
+
context: Rollup.PluginContext,
|
|
394
|
+
transform: string,
|
|
395
|
+
target: string,
|
|
396
|
+
): Promise<string | undefined> {
|
|
397
|
+
return chunkMode() && isClient(context)
|
|
398
|
+
? loadModule(context, transform, target)
|
|
399
|
+
: loadRecorded(context, transform, target);
|
|
319
400
|
}
|
|
320
401
|
|
|
321
402
|
/**
|
|
322
403
|
* The constants of the modules `code` imports, keyed by import specifier.
|
|
323
404
|
*
|
|
324
405
|
* Each module is read as the bundler loads it, after every plugin has
|
|
325
|
-
* transformed it
|
|
326
|
-
*
|
|
327
|
-
* leaves the modules in it without them.
|
|
406
|
+
* transformed it. A module with JSX is read from this plugin's own output
|
|
407
|
+
* for it, unless its transform could be waiting for `transform`.
|
|
328
408
|
*/
|
|
329
409
|
async function importedConstants(
|
|
330
410
|
context: Rollup.PluginContext,
|
|
331
411
|
imports: readonly string[],
|
|
332
412
|
importer: string,
|
|
333
413
|
visiting: ReadonlySet<string>,
|
|
334
|
-
|
|
414
|
+
transform: string,
|
|
415
|
+
): Promise<{ constants: ImportedConstants; partial: boolean }> {
|
|
416
|
+
let partial = false;
|
|
335
417
|
const entries = await Promise.all(
|
|
336
418
|
imports.map(async (specifier) => {
|
|
337
|
-
const
|
|
338
|
-
if (
|
|
419
|
+
const target = await resolveConstantModule(context, specifier, importer);
|
|
420
|
+
if (target === undefined || visiting.has(target.id)) {
|
|
339
421
|
return undefined;
|
|
340
422
|
}
|
|
341
|
-
const
|
|
342
|
-
|
|
423
|
+
const result = await constantsOf(context, target, visiting, transform);
|
|
424
|
+
partial ||= result.partial;
|
|
425
|
+
if (!result.constants || Object.keys(result.constants.exports).length === 0) {
|
|
343
426
|
return undefined;
|
|
344
427
|
}
|
|
345
|
-
return [specifier, constants.exports] as const;
|
|
428
|
+
return [specifier, result.constants.exports] as const;
|
|
346
429
|
}),
|
|
347
430
|
);
|
|
348
|
-
return
|
|
349
|
-
entries.filter(
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
);
|
|
431
|
+
return {
|
|
432
|
+
constants: Object.fromEntries(entries.filter((entry) => entry !== undefined)),
|
|
433
|
+
partial,
|
|
434
|
+
};
|
|
353
435
|
}
|
|
354
436
|
|
|
355
437
|
async function constantsOf(
|
|
356
438
|
context: Rollup.PluginContext,
|
|
357
|
-
id: string,
|
|
439
|
+
target: { id: string; jsx: boolean },
|
|
358
440
|
visiting: ReadonlySet<string>,
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
441
|
+
transform: string,
|
|
442
|
+
): Promise<ConstantsResult> {
|
|
443
|
+
const { id } = target;
|
|
444
|
+
const known = moduleConstants.get(id);
|
|
445
|
+
if (known) {
|
|
446
|
+
return known;
|
|
447
|
+
}
|
|
448
|
+
const result = await (async (): Promise<ConstantsResult> => {
|
|
449
|
+
let code: string | null | undefined;
|
|
450
|
+
if (target.jsx) {
|
|
451
|
+
code = await loadJSXModule(context, transform, id);
|
|
452
|
+
if (code === undefined) {
|
|
453
|
+
return { constants: undefined, partial: true };
|
|
366
454
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
);
|
|
380
|
-
return readModuleConstants(code, { filename, importedConstants: imported });
|
|
381
|
-
} catch (error) {
|
|
382
|
-
// A module the parser rejects fails its own transform, with a better error.
|
|
383
|
-
if (error instanceof SyntaxError) {
|
|
384
|
-
return undefined;
|
|
385
|
-
}
|
|
386
|
-
throw error;
|
|
455
|
+
} else {
|
|
456
|
+
({ code } = await context.load({ id }));
|
|
457
|
+
}
|
|
458
|
+
if (code === null) {
|
|
459
|
+
return { constants: undefined, partial: false };
|
|
460
|
+
}
|
|
461
|
+
// Loaded code is JavaScript, whatever the module was written in, and keeps its JSX.
|
|
462
|
+
const filename = `${stripQuery(id)}.${target.jsx ? 'jsx' : 'js'}`;
|
|
463
|
+
try {
|
|
464
|
+
const own = readModuleConstants(code, { filename, globalConstants: globals });
|
|
465
|
+
if (own.imports.length === 0) {
|
|
466
|
+
return { constants: own, partial: false };
|
|
387
467
|
}
|
|
388
|
-
|
|
389
|
-
|
|
468
|
+
const imported = await importedConstants(
|
|
469
|
+
context,
|
|
470
|
+
own.imports,
|
|
471
|
+
id,
|
|
472
|
+
new Set([...visiting, id]),
|
|
473
|
+
transform,
|
|
474
|
+
);
|
|
475
|
+
return {
|
|
476
|
+
constants: readModuleConstants(code, {
|
|
477
|
+
filename,
|
|
478
|
+
importedConstants: imported.constants,
|
|
479
|
+
globalConstants: globals,
|
|
480
|
+
}),
|
|
481
|
+
partial: imported.partial,
|
|
482
|
+
};
|
|
483
|
+
} catch (error) {
|
|
484
|
+
// A module the parser rejects fails its own transform, with a better error.
|
|
485
|
+
if (error instanceof SyntaxError) {
|
|
486
|
+
return { constants: undefined, partial: false };
|
|
487
|
+
}
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
})();
|
|
491
|
+
if (!result.partial) {
|
|
492
|
+
moduleConstants.set(id, result);
|
|
390
493
|
}
|
|
391
|
-
return
|
|
494
|
+
return result;
|
|
392
495
|
}
|
|
393
496
|
|
|
394
497
|
/**
|
|
@@ -402,16 +505,44 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
402
505
|
const filename = stripQuery(id);
|
|
403
506
|
// The dev server cannot give back the code of a loaded module.
|
|
404
507
|
if (optimizer.fold === false || !isBuild()) {
|
|
405
|
-
return { ...compileOptions, filename };
|
|
508
|
+
return { ...compileOptions, filename, sourceMap: wantsMaps() };
|
|
406
509
|
}
|
|
407
510
|
const { imports } = readModuleConstants(code, { filename });
|
|
511
|
+
const imported = await importedConstants(context, imports, filename, new Set([filename]), id);
|
|
408
512
|
return {
|
|
409
513
|
...compileOptions,
|
|
410
514
|
filename,
|
|
411
|
-
|
|
515
|
+
sourceMap: wantsMaps(),
|
|
516
|
+
importedConstants: imported.constants,
|
|
517
|
+
globalConstants: globals,
|
|
412
518
|
};
|
|
413
519
|
}
|
|
414
520
|
|
|
521
|
+
async function resolveLazyModuleUrls(
|
|
522
|
+
context: Rollup.TransformPluginContext,
|
|
523
|
+
code: string,
|
|
524
|
+
importer: string,
|
|
525
|
+
): Promise<string> {
|
|
526
|
+
const root = config?.root ?? process.cwd();
|
|
527
|
+
let result = code;
|
|
528
|
+
for (const match of code.matchAll(LAZY_PLACEHOLDER)) {
|
|
529
|
+
const specifier = match.at(1);
|
|
530
|
+
if (specifier === undefined) {
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
534
|
+
const resolved = await context.resolve(specifier, importer);
|
|
535
|
+
if (resolved) {
|
|
536
|
+
const queryIndex = resolved.id.indexOf('?');
|
|
537
|
+
const file = queryIndex === -1 ? resolved.id : resolved.id.slice(0, queryIndex);
|
|
538
|
+
const query = queryIndex === -1 ? '' : resolved.id.slice(queryIndex);
|
|
539
|
+
const relative = path.relative(root, file).split(path.sep).join('/') + query;
|
|
540
|
+
result = result.replace(match[0], JSON.stringify(relative));
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
|
|
415
546
|
/** The modules each transform waits for, so two never wait for each other. */
|
|
416
547
|
const waiting = new Map<string, Set<string>>();
|
|
417
548
|
|
|
@@ -462,15 +593,17 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
462
593
|
*/
|
|
463
594
|
const recorded = new Map<string, string>();
|
|
464
595
|
|
|
465
|
-
/** The modules
|
|
466
|
-
const
|
|
596
|
+
/** The modules of the project each module imports, read from its file. */
|
|
597
|
+
const localImports = new Map<string, Promise<string[]>>();
|
|
467
598
|
|
|
468
599
|
/**
|
|
469
|
-
* The modules
|
|
470
|
-
* so they do not depend on the order
|
|
600
|
+
* The modules of the project a module imports, leaving out dependencies.
|
|
601
|
+
* They come from the file on disk, so they do not depend on the order
|
|
602
|
+
* modules are transformed in. Modules without JSX count too, since reading
|
|
603
|
+
* their constants can load a module with JSX.
|
|
471
604
|
*/
|
|
472
|
-
async function
|
|
473
|
-
let pending =
|
|
605
|
+
async function localImportsOf(context: Rollup.PluginContext, id: string): Promise<string[]> {
|
|
606
|
+
let pending = localImports.get(id);
|
|
474
607
|
if (!pending) {
|
|
475
608
|
pending = (async () => {
|
|
476
609
|
let code: string;
|
|
@@ -504,15 +637,15 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
504
637
|
[...sources].map(async (source) => context.resolve(source, id)),
|
|
505
638
|
);
|
|
506
639
|
return resolved
|
|
507
|
-
.filter((target) => target && !target.external &&
|
|
640
|
+
.filter((target) => target && !target.external && !target.id.includes('/node_modules/'))
|
|
508
641
|
.map((target) => target?.id ?? '');
|
|
509
642
|
})();
|
|
510
|
-
|
|
643
|
+
localImports.set(id, pending);
|
|
511
644
|
}
|
|
512
645
|
return pending;
|
|
513
646
|
}
|
|
514
647
|
|
|
515
|
-
/** Whether `from` imports `to`, directly or through other modules
|
|
648
|
+
/** Whether `from` imports `to`, directly or through other modules of the project. */
|
|
516
649
|
async function reaches(
|
|
517
650
|
context: Rollup.PluginContext,
|
|
518
651
|
from: string,
|
|
@@ -528,7 +661,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
528
661
|
seen.add(id);
|
|
529
662
|
}
|
|
530
663
|
// oxlint-disable-next-line no-await-in-loop
|
|
531
|
-
const next = await Promise.all(frontier.map(async (id) =>
|
|
664
|
+
const next = await Promise.all(frontier.map(async (id) => localImportsOf(context, id)));
|
|
532
665
|
frontier = [...new Set(next.flat())].filter((id) => !seen.has(id));
|
|
533
666
|
}
|
|
534
667
|
return false;
|
|
@@ -668,6 +801,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
668
801
|
readonly importAs: (base: string, imported: string, from: string) => string;
|
|
669
802
|
/** The code of a module with JSX to copy from, or `undefined` to skip it. */
|
|
670
803
|
readonly load: (target: string) => Promise<string | undefined>;
|
|
804
|
+
/** Where the copies that are not made are recorded, when the report is on. */
|
|
805
|
+
readonly skips: Skip[] | undefined;
|
|
671
806
|
}
|
|
672
807
|
|
|
673
808
|
/**
|
|
@@ -681,14 +816,23 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
681
816
|
imported: string,
|
|
682
817
|
state: CopyState,
|
|
683
818
|
): Promise<{ key: string; text: (name: string) => string } | undefined> {
|
|
819
|
+
const skip = (reason: string): void => {
|
|
820
|
+
state.skips?.push({ pass: 'inline', reason, name: `${imported} (${source})` });
|
|
821
|
+
};
|
|
684
822
|
const resolved = await context.resolve(source, id);
|
|
685
|
-
if (
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
823
|
+
if (!resolved || resolved.external) {
|
|
824
|
+
skip('copy-external');
|
|
825
|
+
return undefined;
|
|
826
|
+
}
|
|
827
|
+
if (resolved.id.includes('/node_modules/')) {
|
|
828
|
+
skip('copy-package');
|
|
829
|
+
return undefined;
|
|
830
|
+
}
|
|
831
|
+
if (resolved.id === id) {
|
|
832
|
+
return undefined;
|
|
833
|
+
}
|
|
834
|
+
if (!isJSXModule(resolved.id)) {
|
|
835
|
+
skip('copy-not-jsx');
|
|
692
836
|
return undefined;
|
|
693
837
|
}
|
|
694
838
|
const key = `${resolved.id}\0${imported}`;
|
|
@@ -696,11 +840,28 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
696
840
|
return undefined;
|
|
697
841
|
}
|
|
698
842
|
const loaded = await state.load(resolved.id);
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
843
|
+
if (loaded === undefined) {
|
|
844
|
+
state.excluded.add(key);
|
|
845
|
+
skip('copy-cycle');
|
|
846
|
+
return undefined;
|
|
847
|
+
}
|
|
848
|
+
const component = readExportedComponent(
|
|
849
|
+
loaded,
|
|
850
|
+
stripQuery(resolved.id),
|
|
851
|
+
imported,
|
|
852
|
+
moduleSources,
|
|
853
|
+
);
|
|
702
854
|
if (!component) {
|
|
703
855
|
state.excluded.add(key);
|
|
856
|
+
skip('copy-shape');
|
|
857
|
+
return undefined;
|
|
858
|
+
}
|
|
859
|
+
// Its copy would still call a component of that module, so the call
|
|
860
|
+
// site gains nothing. Moving its template out of its module makes both
|
|
861
|
+
// compress worse.
|
|
862
|
+
if (component.wrapsLocal) {
|
|
863
|
+
state.excluded.add(key);
|
|
864
|
+
skip('copy-wrapper');
|
|
704
865
|
return undefined;
|
|
705
866
|
}
|
|
706
867
|
// Each import of the module resolves from it, so the copy imports the same module.
|
|
@@ -711,6 +872,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
711
872
|
const target = await context.resolve(dependency.source, resolved.id);
|
|
712
873
|
if (!target) {
|
|
713
874
|
state.excluded.add(key);
|
|
875
|
+
skip('copy-external');
|
|
714
876
|
return undefined;
|
|
715
877
|
}
|
|
716
878
|
targets.set(dependency.source, target.external ? dependency.source : target.id);
|
|
@@ -745,9 +907,11 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
745
907
|
id: string,
|
|
746
908
|
compileOptionsOfModule: CompileOptions,
|
|
747
909
|
load: (target: string) => Promise<string | undefined>,
|
|
748
|
-
|
|
910
|
+
copySkips?: Skip[],
|
|
911
|
+
): Promise<{ code: string; maps: string[]; skipped?: Skip[] } | undefined> {
|
|
749
912
|
const filename = stripQuery(id);
|
|
750
913
|
const maps: string[] = [];
|
|
914
|
+
let skipped: Skip[] | undefined;
|
|
751
915
|
const excluded = new Set<string>();
|
|
752
916
|
const usage = await usageIndex(context);
|
|
753
917
|
// The components copied here that leave their module, by module.
|
|
@@ -760,6 +924,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
760
924
|
// The contexts the copies import, which the provider pass needs to know.
|
|
761
925
|
const contexts = new Map<string, Set<string>>();
|
|
762
926
|
let code = source;
|
|
927
|
+
const copyNames = new Map<string, string>();
|
|
763
928
|
for (let round = 0; round < 4; round += 1) {
|
|
764
929
|
const program = parse(filename, code);
|
|
765
930
|
const scopes = analyzeScopes(program);
|
|
@@ -825,7 +990,12 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
825
990
|
contexts,
|
|
826
991
|
importAs,
|
|
827
992
|
load,
|
|
993
|
+
skips: copySkips,
|
|
828
994
|
});
|
|
995
|
+
if (copy && copySkips) {
|
|
996
|
+
// A copy the inline pass leaves behind is recorded under the reason it gave.
|
|
997
|
+
copyNames.set(copy.key, `${binding.imported} (${statement.source.value})`);
|
|
998
|
+
}
|
|
829
999
|
if (copy) {
|
|
830
1000
|
const name = fresh(specifier.local.name);
|
|
831
1001
|
copies.push({ key: copy.key, name, tags, text: copy.text(name) });
|
|
@@ -851,7 +1021,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
851
1021
|
result = compile(edit.toString(), {
|
|
852
1022
|
...compileOptionsOfModule,
|
|
853
1023
|
filename,
|
|
854
|
-
sourceMap:
|
|
1024
|
+
sourceMap: wantsMaps(),
|
|
855
1025
|
importedContexts: Object.fromEntries(
|
|
856
1026
|
[...contexts].map(([module, exported]) => [module, [...exported]]),
|
|
857
1027
|
),
|
|
@@ -870,6 +1040,12 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
870
1040
|
for (const copy of stuck) {
|
|
871
1041
|
excluded.add(copy.key);
|
|
872
1042
|
copies.splice(copies.indexOf(copy), 1);
|
|
1043
|
+
const reason = result.skipped?.find((entry) => entry.name === copy.name)?.reason;
|
|
1044
|
+
copySkips?.push({
|
|
1045
|
+
pass: 'inline',
|
|
1046
|
+
reason: `copy-${reason ?? 'left'}`,
|
|
1047
|
+
name: copyNames.get(copy.key) ?? copy.name,
|
|
1048
|
+
});
|
|
873
1049
|
}
|
|
874
1050
|
result = undefined;
|
|
875
1051
|
}
|
|
@@ -881,35 +1057,46 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
881
1057
|
const [module = '', exported = ''] = copy.key.split('\0');
|
|
882
1058
|
absorbed.set(module, new Set([...(absorbed.get(module) ?? []), exported]));
|
|
883
1059
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
1060
|
+
if (wantsMaps()) {
|
|
1061
|
+
maps.push(
|
|
1062
|
+
edit.generateMap({ source: filename, hires: true, includeContent: true }).toString(),
|
|
1063
|
+
);
|
|
1064
|
+
}
|
|
887
1065
|
if (result.map) {
|
|
888
1066
|
maps.push(result.map.toString());
|
|
889
1067
|
}
|
|
890
1068
|
code = result.code;
|
|
1069
|
+
({ skipped } = result);
|
|
891
1070
|
}
|
|
892
|
-
return code === source ? undefined : { code, maps };
|
|
1071
|
+
return code === source ? undefined : { code, maps, skipped };
|
|
893
1072
|
}
|
|
894
1073
|
|
|
895
1074
|
/**
|
|
896
|
-
* Prepares a client module for chunk mode. It
|
|
897
|
-
*
|
|
1075
|
+
* Prepares a client module for chunk mode. It runs the passes the official
|
|
1076
|
+
* plugin runs before lowering JSX, and adds the helper marker instead of
|
|
1077
|
+
* lowering.
|
|
898
1078
|
*/
|
|
899
1079
|
async function keepJSX(
|
|
900
|
-
context: Rollup.
|
|
1080
|
+
context: Rollup.TransformPluginContext,
|
|
901
1081
|
source: string,
|
|
902
1082
|
id: string,
|
|
903
1083
|
): Promise<Rollup.TransformResult> {
|
|
904
1084
|
const filename = stripQuery(id);
|
|
1085
|
+
const solidCompiler = getCompiler();
|
|
1086
|
+
const maps: (string | null | undefined)[] = [];
|
|
1087
|
+
let code = source;
|
|
1088
|
+
|
|
905
1089
|
// Fold and inline memos before bundling. A branch that folds away takes
|
|
906
1090
|
// its imports and `lazy()` chunks out of the module graph, which the
|
|
907
1091
|
// chunk step can no longer do. Inlining waits for the chunk, where the
|
|
908
1092
|
// components it can reach are known.
|
|
909
|
-
const moduleCompileOptions = await moduleOptions(context,
|
|
910
|
-
const local = compile(
|
|
911
|
-
|
|
912
|
-
|
|
1093
|
+
const moduleCompileOptions = await moduleOptions(context, code, id);
|
|
1094
|
+
const local = compile(code, { ...moduleCompileOptions, inline: false });
|
|
1095
|
+
// `map` is `null` when nothing changed, and when the build writes no maps.
|
|
1096
|
+
code = local.code;
|
|
1097
|
+
if (local.map) {
|
|
1098
|
+
maps.push(local.map.toString());
|
|
1099
|
+
}
|
|
913
1100
|
// `<Ns.Member>` would keep a namespace object, which lowered code does not.
|
|
914
1101
|
const named = nameNamespaceTags(code, filename);
|
|
915
1102
|
if (named) {
|
|
@@ -920,13 +1107,17 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
920
1107
|
// Components imported from other modules inline here, so the bundler
|
|
921
1108
|
// drops them, and what only they used, before it splits the chunks.
|
|
922
1109
|
if (moduleCompileOptions.inline !== false) {
|
|
1110
|
+
const copySkips: Skip[] | undefined = compileOptions.report ? [] : undefined;
|
|
923
1111
|
const imported = await inlineImports(
|
|
924
1112
|
context,
|
|
925
1113
|
code,
|
|
926
1114
|
id,
|
|
927
1115
|
moduleCompileOptions,
|
|
928
1116
|
async (target) => loadModule(context, id, target),
|
|
1117
|
+
copySkips,
|
|
929
1118
|
);
|
|
1119
|
+
// The rest of the report comes from the chunk this module ends up in.
|
|
1120
|
+
addSkips(context, copySkips);
|
|
930
1121
|
if (imported) {
|
|
931
1122
|
code = imported.code;
|
|
932
1123
|
maps.push(...imported.maps);
|
|
@@ -938,44 +1129,74 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
938
1129
|
code = exposed.code;
|
|
939
1130
|
maps.push(exposed.map);
|
|
940
1131
|
}
|
|
1132
|
+
|
|
1133
|
+
const lazy = await solidCompiler.transformLazyAsync(code, { filename, sourceMap: wantsMaps() });
|
|
1134
|
+
code = lazy.code;
|
|
1135
|
+
maps.push(lazy.map);
|
|
1136
|
+
|
|
941
1137
|
// The helpers the module's JSX needs, as written and with its own
|
|
942
1138
|
// components inlined. Inlining can turn a spread into attributes, which
|
|
943
1139
|
// need helpers the spread did not.
|
|
1140
|
+
const lowering = jsxOptions(false);
|
|
1141
|
+
const needed = new Set<string>();
|
|
944
1142
|
const inlined =
|
|
945
1143
|
moduleCompileOptions.inline === false
|
|
946
1144
|
? undefined
|
|
947
1145
|
: compile(code, { ...moduleCompileOptions, sourceMap: false });
|
|
948
|
-
const needed = new Set<string>();
|
|
949
1146
|
for (const version of new Set([code, inlined?.code ?? code])) {
|
|
950
|
-
|
|
1147
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
1148
|
+
const dryRun = await solidCompiler.transformAsync(version, { ...lowering, filename });
|
|
951
1149
|
for (const helper of generatedImports(dryRun.code, filename, moduleName)) {
|
|
952
1150
|
needed.add(helper);
|
|
953
1151
|
}
|
|
954
1152
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
1153
|
+
// A component inlined into another module of the chunk can turn its
|
|
1154
|
+
// spread into any attribute.
|
|
1155
|
+
if (moduleCompileOptions.inline !== false && spreadsOntoElement(code, filename)) {
|
|
1156
|
+
for (const helper of attributeHelpers()) {
|
|
1157
|
+
needed.add(helper);
|
|
1158
|
+
}
|
|
958
1159
|
}
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1160
|
+
|
|
1161
|
+
if (needed.size > 0) {
|
|
1162
|
+
const marked = addMarker(code, filename, {
|
|
1163
|
+
moduleName,
|
|
1164
|
+
helpers: withRelated([...needed]),
|
|
1165
|
+
moduleSources,
|
|
1166
|
+
// The passes also recognize these primitives, which a chunk renames
|
|
1167
|
+
// too. `render` takes a callback that statements can move into, and
|
|
1168
|
+
// `children` can be inlined like a memo.
|
|
1169
|
+
builtIns: new Set([
|
|
1170
|
+
...builtIns,
|
|
1171
|
+
'createContext',
|
|
1172
|
+
'render',
|
|
1173
|
+
'children',
|
|
1174
|
+
...CONTEXT_SAFE_PRIMITIVES,
|
|
1175
|
+
]),
|
|
1176
|
+
});
|
|
1177
|
+
if (marked) {
|
|
1178
|
+
code = marked.code;
|
|
1179
|
+
maps.push(marked.map);
|
|
1180
|
+
}
|
|
968
1181
|
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
1182
|
+
|
|
1183
|
+
// The bundler has to see that a `ref` writes its variable. See `refs.ts`.
|
|
1184
|
+
const protectedRefs = protectRefs(code, filename);
|
|
1185
|
+
if (protectedRefs) {
|
|
1186
|
+
code = protectedRefs.code;
|
|
1187
|
+
maps.push(protectedRefs.map);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
code = await resolveLazyModuleUrls(context, code, filename);
|
|
1191
|
+
preserved.add(id);
|
|
1192
|
+
return { code, map: combineMaps(wantsMaps(), maps) };
|
|
973
1193
|
}
|
|
974
1194
|
|
|
975
|
-
const
|
|
1195
|
+
const main = plugins.find((plugin) => plugin.name === 'solid');
|
|
1196
|
+
const originalTransform = main?.transform;
|
|
976
1197
|
const originalHandler =
|
|
977
1198
|
typeof originalTransform === 'function' ? originalTransform : originalTransform?.handler;
|
|
978
|
-
if (originalHandler) {
|
|
1199
|
+
if (main && originalHandler) {
|
|
979
1200
|
main.transform = async function transform(code, id, transformOptions) {
|
|
980
1201
|
if (chunkMode() && isClient(this) && isJSXModule(id)) {
|
|
981
1202
|
return keepJSX(this, code, id);
|
|
@@ -989,15 +1210,17 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
989
1210
|
enforce: 'pre',
|
|
990
1211
|
configResolved(resolved) {
|
|
991
1212
|
config = resolved;
|
|
1213
|
+
globals = sharedGlobals(resolved);
|
|
992
1214
|
},
|
|
993
1215
|
buildStart() {
|
|
994
1216
|
moduleConstants.clear();
|
|
995
|
-
|
|
1217
|
+
localImports.clear();
|
|
996
1218
|
usageIndexes.clear();
|
|
1219
|
+
skips.delete(this.environment.name);
|
|
997
1220
|
},
|
|
998
1221
|
watchChange() {
|
|
999
1222
|
moduleConstants.clear();
|
|
1000
|
-
|
|
1223
|
+
localImports.clear();
|
|
1001
1224
|
usageIndexes.clear();
|
|
1002
1225
|
},
|
|
1003
1226
|
async transform(source, id) {
|
|
@@ -1008,23 +1231,28 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1008
1231
|
return null;
|
|
1009
1232
|
}
|
|
1010
1233
|
const filename = stripQuery(id);
|
|
1011
|
-
const sourceOptions
|
|
1012
|
-
...(await moduleOptions(this, source, id)),
|
|
1013
|
-
// The server renders once, so its reactive primitives reduce to plain calls.
|
|
1014
|
-
server: optimizer.server !== false && !isClient(this),
|
|
1015
|
-
};
|
|
1234
|
+
const sourceOptions = await moduleOptions(this, source, id);
|
|
1016
1235
|
const result = compile(source, sourceOptions);
|
|
1017
1236
|
let code = result.code;
|
|
1018
1237
|
const maps: (string | null | undefined)[] = [result.map?.toString()];
|
|
1019
1238
|
// Components imported from other modules inline here too. The server
|
|
1020
1239
|
// and client builds read the same modules, so they inline the same ones.
|
|
1240
|
+
let { skipped } = result;
|
|
1021
1241
|
if (isBuild() && sourceOptions.inline !== false) {
|
|
1022
|
-
const
|
|
1023
|
-
|
|
1242
|
+
const copySkips: Skip[] | undefined = compileOptions.report ? [] : undefined;
|
|
1243
|
+
const imported = await inlineImports(
|
|
1244
|
+
this,
|
|
1245
|
+
code,
|
|
1246
|
+
id,
|
|
1247
|
+
sourceOptions,
|
|
1248
|
+
async (target) => loadRecorded(this, id, target),
|
|
1249
|
+
copySkips,
|
|
1024
1250
|
);
|
|
1251
|
+
addSkips(this, copySkips);
|
|
1025
1252
|
if (imported) {
|
|
1026
1253
|
code = imported.code;
|
|
1027
1254
|
maps.push(...imported.maps);
|
|
1255
|
+
skipped = imported.skipped ?? skipped;
|
|
1028
1256
|
}
|
|
1029
1257
|
// Entries get the extra exports too. The server and client builds
|
|
1030
1258
|
// have different entries, and both have to copy the same components.
|
|
@@ -1034,11 +1262,12 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1034
1262
|
maps.push(exposed.map);
|
|
1035
1263
|
}
|
|
1036
1264
|
}
|
|
1265
|
+
addSkips(this, skipped);
|
|
1037
1266
|
recorded.set(environmentKey(this, id), code);
|
|
1038
1267
|
if (code === source) {
|
|
1039
1268
|
return null;
|
|
1040
1269
|
}
|
|
1041
|
-
return { code, map: combineMaps(maps) };
|
|
1270
|
+
return { code, map: combineMaps(wantsMaps(), maps) };
|
|
1042
1271
|
},
|
|
1043
1272
|
};
|
|
1044
1273
|
|
|
@@ -1077,16 +1306,28 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1077
1306
|
if (!runtime) {
|
|
1078
1307
|
return null;
|
|
1079
1308
|
}
|
|
1080
|
-
|
|
1309
|
+
const restored = restoreRefs(runtime.code, filename);
|
|
1310
|
+
const chunkCode = restored?.code ?? runtime.code;
|
|
1311
|
+
const lowerChunk = (source: string): SolidCompiler.TransformResult =>
|
|
1312
|
+
getCompiler().transform(source, {
|
|
1313
|
+
...jsxOptions(false),
|
|
1314
|
+
// The compiler picks its parser by extension, and a chunk is a `.js` file with JSX.
|
|
1315
|
+
filename: `${filename}.jsx`,
|
|
1316
|
+
sourceMap: wantsMaps(),
|
|
1317
|
+
});
|
|
1318
|
+
let optimized: CompileResult | undefined = compile(chunkCode, {
|
|
1081
1319
|
...compileOptions,
|
|
1082
1320
|
filename,
|
|
1321
|
+
sourceMap: wantsMaps(),
|
|
1083
1322
|
constantVars: true,
|
|
1084
1323
|
builtInAliases: runtime.builtInAliases,
|
|
1085
1324
|
});
|
|
1086
|
-
let lowered = lower(getCompiler(), optimized.code, filename, jsxOptions(false));
|
|
1087
1325
|
let linked: { code: string; map: string };
|
|
1326
|
+
let lowered: SolidCompiler.TransformResult;
|
|
1088
1327
|
try {
|
|
1328
|
+
lowered = lowerChunk(optimized.code);
|
|
1089
1329
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1330
|
+
addSkips(this, optimized.skipped);
|
|
1090
1331
|
} catch (error) {
|
|
1091
1332
|
if (!(error instanceof MissingHelperError)) {
|
|
1092
1333
|
throw error;
|
|
@@ -1097,14 +1338,16 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1097
1338
|
// so the chunks do not have to match anything else.
|
|
1098
1339
|
this.warn(`${error.message} ${filename} is not optimized.`);
|
|
1099
1340
|
optimized = undefined;
|
|
1100
|
-
|
|
1341
|
+
addSkips(this, [{ pass: 'inline', reason: 'missing-helper', name: filename }]);
|
|
1342
|
+
lowered = lowerChunk(chunkCode);
|
|
1101
1343
|
linked = linkHelpers(lowered.code, filename, moduleName, runtime.helpers);
|
|
1102
1344
|
}
|
|
1103
1345
|
return {
|
|
1104
1346
|
code: linked.code,
|
|
1105
|
-
map: combineMaps([
|
|
1347
|
+
map: combineMaps(wantsMaps(), [
|
|
1106
1348
|
repaired?.map,
|
|
1107
1349
|
runtime.map,
|
|
1350
|
+
restored?.map,
|
|
1108
1351
|
optimized?.map?.toString(),
|
|
1109
1352
|
lowered.map,
|
|
1110
1353
|
linked.map,
|
|
@@ -1114,6 +1357,21 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1114
1357
|
},
|
|
1115
1358
|
};
|
|
1116
1359
|
|
|
1360
|
+
// Prints the report once every chunk is rendered.
|
|
1361
|
+
chunkPlugin.generateBundle = function generateBundle() {
|
|
1362
|
+
const { report } = optimizer;
|
|
1363
|
+
if (report === undefined || report === false) {
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
const environment = this.environment.name;
|
|
1367
|
+
const result: OptimizerReport = { environment, skips: skips.get(environment) ?? [] };
|
|
1368
|
+
if (typeof report === 'function') {
|
|
1369
|
+
report(result);
|
|
1370
|
+
} else {
|
|
1371
|
+
this.environment.logger.info(formatReport(result));
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
|
|
1117
1375
|
// Runs after `vite:oxc` strips types, which reports the module as plain
|
|
1118
1376
|
// JavaScript, and before the built-in plugins that parse it again.
|
|
1119
1377
|
const moduleTypePlugin: Plugin = {
|
|
@@ -1131,5 +1389,5 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1131
1389
|
},
|
|
1132
1390
|
};
|
|
1133
1391
|
|
|
1134
|
-
return [modulePlugin,
|
|
1392
|
+
return [modulePlugin, ...plugins, moduleTypePlugin, chunkPlugin];
|
|
1135
1393
|
}
|