solid-optimizer 1.1.0 → 1.1.1
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/dist/{compile-D2f8eXFk.mjs → compile-DAmk1J8f.mjs} +20 -5
- package/dist/compile-DAmk1J8f.mjs.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +50 -6
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/inline.ts +38 -4
- package/src/vite/index.ts +65 -5
- package/dist/compile-D2f8eXFk.mjs.map +0 -1
package/package.json
CHANGED
package/src/inline.ts
CHANGED
|
@@ -210,6 +210,8 @@ interface PreparedSite {
|
|
|
210
210
|
readonly host: { fn: Node; host: Host } | undefined;
|
|
211
211
|
readonly statements: Content[];
|
|
212
212
|
readonly root: Text;
|
|
213
|
+
/** What replaces the call, when it is not `root`: a fragment root's children. */
|
|
214
|
+
readonly spliced?: Text;
|
|
213
215
|
}
|
|
214
216
|
|
|
215
217
|
function isWithin(node: Node, container: Node): boolean {
|
|
@@ -1641,11 +1643,33 @@ class Inliner {
|
|
|
1641
1643
|
const text = copy.slice(first.start, last.end);
|
|
1642
1644
|
statements.push(text.code.endsWith(';') ? text : joinText`${text};`);
|
|
1643
1645
|
}
|
|
1646
|
+
// Solid's JSX transform only takes a fragment at the top of an expression, so a fragment
|
|
1647
|
+
// root that lands among an element's children gives them its own children instead.
|
|
1648
|
+
const { root } = component;
|
|
1649
|
+
const parent = this.context.parents.get(element);
|
|
1650
|
+
const spliceChildren =
|
|
1651
|
+
root.type === 'JSXFragment' &&
|
|
1652
|
+
(parent?.type === 'JSXElement' || parent?.type === 'JSXFragment');
|
|
1653
|
+
// Edge text that JSX keeps would join the text around the call, and JSX collapses the
|
|
1654
|
+
// joined run differently: `a<Label />b` with `<>\n hello\n</>` would render `a hello b`.
|
|
1655
|
+
// JSX keeps text with a non-space character, and space-only text with no line break.
|
|
1656
|
+
if (spliceChildren) {
|
|
1657
|
+
const edges = [root.children.at(0), root.children.at(-1)];
|
|
1658
|
+
const kept = (value: string): boolean =>
|
|
1659
|
+
/\S/.test(value) || (value !== '' && !value.includes('\n'));
|
|
1660
|
+
if (edges.some((child) => child?.type === 'JSXText' && kept(child.value))) {
|
|
1661
|
+
this.reason = 'fragment-text';
|
|
1662
|
+
return undefined;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1644
1665
|
return {
|
|
1645
1666
|
element,
|
|
1646
1667
|
host,
|
|
1647
1668
|
statements,
|
|
1648
|
-
root: copy.slice(
|
|
1669
|
+
root: copy.slice(root.start, root.end),
|
|
1670
|
+
spliced: spliceChildren
|
|
1671
|
+
? copy.slice(root.openingFragment.end, root.closingFragment.start)
|
|
1672
|
+
: undefined,
|
|
1649
1673
|
};
|
|
1650
1674
|
}
|
|
1651
1675
|
|
|
@@ -1661,7 +1685,7 @@ class Inliner {
|
|
|
1661
1685
|
* Writes a prepared copy over its call site.
|
|
1662
1686
|
*/
|
|
1663
1687
|
private commit(site: PreparedSite): void {
|
|
1664
|
-
this.context.s.overwrite(site.element.start, site.element.end, site.root);
|
|
1688
|
+
this.context.s.overwrite(site.element.start, site.element.end, site.spliced ?? site.root);
|
|
1665
1689
|
if (site.host) {
|
|
1666
1690
|
this.addToHost(site.host, site.statements);
|
|
1667
1691
|
}
|
|
@@ -2638,8 +2662,14 @@ class Inliner {
|
|
|
2638
2662
|
// from its new body. A copy made now would miss what was inlined into it.
|
|
2639
2663
|
const changedBodies = new Set<Component>(this.findTransparentProviders(components, sites));
|
|
2640
2664
|
const worthwhile = this.worthwhileComponents(sites, changedBodies, otherUses);
|
|
2665
|
+
// The hosts that copies made in this pass moved statements into. They are written at the
|
|
2666
|
+
// end of the pass, so an outer call holding one copies it in the next pass instead: a
|
|
2667
|
+
// copy made now would miss the statements, and writing them later would edit text the
|
|
2668
|
+
// outer copy has already replaced.
|
|
2669
|
+
const pendingHosts: Node[] = [];
|
|
2641
2670
|
for (const site of inBodyOrder(sites)) {
|
|
2642
|
-
const
|
|
2671
|
+
const holdsPendingHost = pendingHosts.some((host) => isWithin(host, site.element));
|
|
2672
|
+
const postponed = changedBodies.has(site.component) || holdsPendingHost;
|
|
2643
2673
|
// Each copy is made now, so it takes in the call sites inside it that
|
|
2644
2674
|
// were inlined before it.
|
|
2645
2675
|
const copy =
|
|
@@ -2650,6 +2680,9 @@ class Inliner {
|
|
|
2650
2680
|
if (copy) {
|
|
2651
2681
|
this.commit(copy);
|
|
2652
2682
|
this.changed = true;
|
|
2683
|
+
if (copy.host) {
|
|
2684
|
+
pendingHosts.push(copy.host.fn);
|
|
2685
|
+
}
|
|
2653
2686
|
const elements = inlined.get(site.component) ?? [];
|
|
2654
2687
|
elements.push(site.element);
|
|
2655
2688
|
inlined.set(site.component, elements);
|
|
@@ -2736,7 +2769,8 @@ class Inliner {
|
|
|
2736
2769
|
): { reason: string; name: string } | undefined {
|
|
2737
2770
|
const tag = element.openingElement.name;
|
|
2738
2771
|
if (tag.type === 'JSXMemberExpression') {
|
|
2739
|
-
|
|
2772
|
+
// The pass has edited the code by now, so the name comes from the original.
|
|
2773
|
+
return { reason: 'member-tag', name: this.context.code.slice(tag.start, tag.end) };
|
|
2740
2774
|
}
|
|
2741
2775
|
if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
|
|
2742
2776
|
return undefined;
|
package/src/vite/index.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* differently, and hydration needs both to render the same tree, so
|
|
15
15
|
* hydrating builds only optimize within a module.
|
|
16
16
|
*/
|
|
17
|
-
import { readFile } from 'node:fs/promises';
|
|
17
|
+
import { access, readFile } from 'node:fs/promises';
|
|
18
18
|
import { createRequire } from 'node:module';
|
|
19
19
|
import path from 'node:path';
|
|
20
20
|
import { gzipSync } from 'node:zlib';
|
|
@@ -25,7 +25,7 @@ import type { Options as SolidPluginOptions } from '@solidjs/vite-plugin';
|
|
|
25
25
|
import solidPlugin from '@solidjs/vite-plugin';
|
|
26
26
|
import type { Node, Program } from 'oxc-parser';
|
|
27
27
|
import type { Plugin, ResolvedConfig, Rollup } from 'vite';
|
|
28
|
-
import { createFilter, minifySync, transformWithOxc } from 'vite';
|
|
28
|
+
import { createFilter, createIdResolver, minifySync, transformWithOxc } from 'vite';
|
|
29
29
|
import type { CompileOptions, CompileResult, ModuleConstants } from '../compile';
|
|
30
30
|
import { compile, readModuleConstants } from '../compile';
|
|
31
31
|
import { collectParents, forEachChild, isIntrinsicTag, parse } from '../ast';
|
|
@@ -53,6 +53,8 @@ import {
|
|
|
53
53
|
repairJSXSequences,
|
|
54
54
|
} from './runtime';
|
|
55
55
|
|
|
56
|
+
type IdResolver = ReturnType<typeof createIdResolver>;
|
|
57
|
+
|
|
56
58
|
export interface OptimizerOptions extends Pick<
|
|
57
59
|
CompileOptions,
|
|
58
60
|
'fold' | 'inline' | 'alwaysInline' | 'contexts' | 'memos' | 'conditionals' | 'maxPasses'
|
|
@@ -397,6 +399,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
397
399
|
};
|
|
398
400
|
|
|
399
401
|
let config: ResolvedConfig | undefined;
|
|
402
|
+
let idResolver: IdResolver | undefined;
|
|
400
403
|
/** The `define` entries and `import.meta.env` values every environment shares. */
|
|
401
404
|
let globals: Record<string, Primitive> = {};
|
|
402
405
|
let compiler: Compiler | undefined;
|
|
@@ -511,6 +514,21 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
511
514
|
readonly partial: boolean;
|
|
512
515
|
}
|
|
513
516
|
|
|
517
|
+
/**
|
|
518
|
+
* Whether `id` names a file in the project that does not exist. An alias can resolve a
|
|
519
|
+
* Babel macro module, which a later plugin compiles away, to such a path. Loading it fails
|
|
520
|
+
* the build even when the error is caught, and it has nothing to read, so it is left to the
|
|
521
|
+
* bundler. A virtual module has an id that is not a project path, and is still loaded.
|
|
522
|
+
*/
|
|
523
|
+
async function isMissingProjectFile(id: string): Promise<boolean> {
|
|
524
|
+
const file = stripQuery(id);
|
|
525
|
+
const root = config?.root ?? process.cwd();
|
|
526
|
+
const relative = path.relative(root, file);
|
|
527
|
+
const inProject =
|
|
528
|
+
path.isAbsolute(file) && !relative.startsWith('..') && !path.isAbsolute(relative);
|
|
529
|
+
return inProject && !(await fileExists(file));
|
|
530
|
+
}
|
|
531
|
+
|
|
514
532
|
/**
|
|
515
533
|
* The module an import resolves to, when its constants can be read, and
|
|
516
534
|
* whether it has JSX. Dependencies are skipped to keep the build fast.
|
|
@@ -527,6 +545,9 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
527
545
|
if (!resolved || resolved.external || resolved.id.includes('/node_modules/')) {
|
|
528
546
|
return undefined;
|
|
529
547
|
}
|
|
548
|
+
if (await isMissingProjectFile(resolved.id)) {
|
|
549
|
+
return undefined;
|
|
550
|
+
}
|
|
530
551
|
return { id: resolved.id, jsx: isJSXModule(resolved.id) };
|
|
531
552
|
}
|
|
532
553
|
|
|
@@ -782,7 +803,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
782
803
|
try {
|
|
783
804
|
return await readFile(stripQuery(id), 'utf8');
|
|
784
805
|
} catch {
|
|
785
|
-
if (isJSXModule(id)) {
|
|
806
|
+
if (isJSXModule(id) || (await isMissingProjectFile(id))) {
|
|
786
807
|
return undefined;
|
|
787
808
|
}
|
|
788
809
|
}
|
|
@@ -829,7 +850,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
829
850
|
const resolving = [];
|
|
830
851
|
for (const source of runtimeImportSources(program)) {
|
|
831
852
|
if (!moduleSources.includes(source) && !source.startsWith('node:')) {
|
|
832
|
-
resolving.push(context
|
|
853
|
+
resolving.push(resolveForWalk(context, source, id));
|
|
833
854
|
}
|
|
834
855
|
}
|
|
835
856
|
const resolved = await Promise.all(resolving);
|
|
@@ -846,6 +867,34 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
846
867
|
return pending;
|
|
847
868
|
}
|
|
848
869
|
|
|
870
|
+
/**
|
|
871
|
+
* Resolves an import of a file on disk, for the walks that read imports from files. The
|
|
872
|
+
* import may never reach the bundler: a framework plugin can compile it away first, like
|
|
873
|
+
* the server-only imports of a module whose server functions become client stubs. Other
|
|
874
|
+
* plugins' `resolveId` hooks can act on every resolution they see, and TanStack Start's
|
|
875
|
+
* import protection reports one of those imports as a violation. So aliases and files are
|
|
876
|
+
* resolved without plugin hooks. Anything that is not a file path, like a virtual module or
|
|
877
|
+
* a runtime built-in, goes through the plugins.
|
|
878
|
+
*/
|
|
879
|
+
async function resolveForWalk(
|
|
880
|
+
context: Rollup.PluginContext,
|
|
881
|
+
source: string,
|
|
882
|
+
importer: string,
|
|
883
|
+
): Promise<{ id: string; external: boolean | 'absolute' | 'relative' } | null> {
|
|
884
|
+
const environment = (context as { environment?: Parameters<IdResolver>[0] }).environment;
|
|
885
|
+
if (config && environment) {
|
|
886
|
+
idResolver ??= createIdResolver(config);
|
|
887
|
+
const id = await idResolver(environment, source, importer);
|
|
888
|
+
// It returns a runtime built-in, like `cloudflare:workers`, as its own id. Only a file
|
|
889
|
+
// path is a module to read. Anything else gets the plugins' answer, which marks a
|
|
890
|
+
// built-in external.
|
|
891
|
+
if (id !== undefined && path.isAbsolute(stripQuery(id))) {
|
|
892
|
+
return { id, external: false };
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return context.resolve(source, importer);
|
|
896
|
+
}
|
|
897
|
+
|
|
849
898
|
/** Whether `from` imports `to`, directly or through other modules. */
|
|
850
899
|
async function reaches(
|
|
851
900
|
context: Rollup.PluginContext,
|
|
@@ -991,7 +1040,7 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
991
1040
|
}
|
|
992
1041
|
return buildUsageIndex(known ? scriptIds : [], {
|
|
993
1042
|
resolve: async (source, importer) => {
|
|
994
|
-
const resolved = await context
|
|
1043
|
+
const resolved = await resolveForWalk(context, source, importer);
|
|
995
1044
|
return !resolved || resolved.external || resolved.id.includes('/node_modules/')
|
|
996
1045
|
? undefined
|
|
997
1046
|
: resolved.id;
|
|
@@ -1542,6 +1591,8 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1542
1591
|
configResolved(resolved) {
|
|
1543
1592
|
config = resolved;
|
|
1544
1593
|
globals = sharedGlobals(resolved);
|
|
1594
|
+
// The resolver captures the config, so a new config needs a new one.
|
|
1595
|
+
idResolver = undefined;
|
|
1545
1596
|
},
|
|
1546
1597
|
buildStart() {
|
|
1547
1598
|
moduleConstants.clear();
|
|
@@ -1747,3 +1798,12 @@ export default function solidOptimizer(options: Options = {}): Plugin[] {
|
|
|
1747
1798
|
|
|
1748
1799
|
return [modulePlugin, ...plugins, moduleTypePlugin, chunkPlugin];
|
|
1749
1800
|
}
|
|
1801
|
+
|
|
1802
|
+
async function fileExists(file: string): Promise<boolean> {
|
|
1803
|
+
try {
|
|
1804
|
+
await access(file);
|
|
1805
|
+
return true;
|
|
1806
|
+
} catch {
|
|
1807
|
+
return false;
|
|
1808
|
+
}
|
|
1809
|
+
}
|