@fluentic/style 0.1.0-beta.16 → 0.1.0-beta.18
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 +18 -0
- package/compiler/compiler/cache_identity.ts +115 -0
- package/compiler/compiler/compiler.ts +28 -1
- package/compiler/compiler/persistent_cache.ts +310 -0
- package/compiler/compiler/types.ts +12 -0
- package/compiler/extract/collector.ts +2 -2
- package/compiler/transform/evaluator/lazy_bindings.ts +63 -0
- package/compiler/transform/evaluator/metadata_cache.ts +124 -0
- package/compiler/transform/evaluator/tracer.ts +101 -86
- package/compiler/transform/extract/plugin.ts +21 -11
- package/compiler/transform/extract/utils/state.ts +3 -2
- package/compiler/utils/check_diagnostics.ts +18 -0
- package/compiler/utils/file_resolver.ts +24 -0
- package/dist/combine-BpOQWCtM.js.map +1 -1
- package/dist/compiler/index.mjs +1 -1
- package/dist/{compiler-AOb_VaKQ.mjs → compiler-Dpd41bCS.mjs} +646 -69
- package/dist/compiler-Dpd41bCS.mjs.map +1 -0
- package/dist/entry/rsc-dev.js.map +1 -1
- package/dist/{loader-fAuIjIik.mjs → loader-CR_NH6ub.mjs} +3 -3
- package/dist/{loader-fAuIjIik.mjs.map → loader-CR_NH6ub.mjs.map} +1 -1
- package/dist/{misc-CyZgGtc9.mjs → misc-D5AjOWP-.mjs} +31 -5
- package/dist/misc-D5AjOWP-.mjs.map +1 -0
- package/dist/plugin/farm/index.mjs +4 -4
- package/dist/plugin/farm/index.mjs.map +1 -1
- package/dist/plugin/nextjs/index.mjs +6 -6
- package/dist/plugin/nextjs/index.mjs.map +1 -1
- package/dist/plugin/nextjs/loader.mjs +4 -4
- package/dist/plugin/parcel/index.cjs +1 -1
- package/dist/plugin/parcel/index.mjs +2 -2
- package/dist/plugin/parcel/optimizer.cjs +1 -1
- package/dist/plugin/parcel/optimizer.mjs +2 -2
- package/dist/plugin/parcel/resolver.cjs +1 -1
- package/dist/plugin/parcel/resolver.mjs +2 -2
- package/dist/plugin/parcel/runtime.cjs +1 -1
- package/dist/plugin/parcel/runtime.mjs +1 -1
- package/dist/plugin/parcel/transformer.cjs +1 -1
- package/dist/plugin/parcel/transformer.mjs +2 -2
- package/dist/plugin/rspack/index.mjs +2 -2
- package/dist/plugin/vite/index.mjs +23 -20
- package/dist/plugin/vite/index.mjs.map +1 -1
- package/dist/plugin/webpack/index.mjs +2 -2
- package/dist/plugin/webpack/loader.mjs +2 -2
- package/dist/{registry-BMijh1CM.mjs → registry-CdSHrX-o.mjs} +2 -2
- package/dist/{registry-BMijh1CM.mjs.map → registry-CdSHrX-o.mjs.map} +1 -1
- package/dist/{shared-DY0kuBcN.cjs → shared-CcB1KDFI.cjs} +666 -76
- package/dist/shared-CcB1KDFI.cjs.map +1 -0
- package/dist/{shared-BfASWZo1.mjs → shared-CobOEWzt.mjs} +5 -5
- package/dist/shared-CobOEWzt.mjs.map +1 -0
- package/dist/{sidecar-cCzTDkzg.mjs → sidecar-CHDVQqN_.mjs} +3 -3
- package/dist/{sidecar-cCzTDkzg.mjs.map → sidecar-CHDVQqN_.mjs.map} +1 -1
- package/dist/style-CRs-vfY1.js.map +1 -1
- package/dist/types/compiler/compiler/cache_identity.d.ts +4 -0
- package/dist/types/compiler/compiler/compiler.d.ts +12 -0
- package/dist/types/compiler/compiler/persistent_cache.d.ts +14 -0
- package/dist/types/compiler/compiler/types.d.ts +11 -0
- package/dist/types/compiler/extract/collector.d.ts +1 -1
- package/dist/types/compiler/transform/evaluator/lazy_bindings.d.ts +22 -0
- package/dist/types/compiler/transform/evaluator/metadata_cache.d.ts +13 -0
- package/dist/types/compiler/transform/evaluator/tracer.d.ts +7 -0
- package/dist/types/compiler/transform/extract/utils/state.d.ts +3 -2
- package/dist/types/compiler/utils/check_diagnostics.d.ts +7 -0
- package/dist/types/compiler/utils/file_resolver.d.ts +4 -0
- package/dist/types/plugin/bundler/vite/state.d.ts +24 -0
- package/dist/types/plugin/utils/compiler.d.ts +12 -1
- package/dist/types/plugin/utils/filter.d.ts +11 -0
- package/dist/types/style/tokens.d.ts +1 -1
- package/dist/{utils-BO6Ix8t5.mjs → utils-CICsXGqS.mjs} +5 -5
- package/dist/utils-CICsXGqS.mjs.map +1 -0
- package/dist/{utils-EG9gh2g3.mjs → utils-XfsnoteV.mjs} +3 -3
- package/dist/{utils-EG9gh2g3.mjs.map → utils-XfsnoteV.mjs.map} +1 -1
- package/dist/{webpack-WXAJ3IYn.mjs → webpack-BAflvDbq.mjs} +7 -7
- package/dist/webpack-BAflvDbq.mjs.map +1 -0
- package/package.json +1 -1
- package/plugin/bundler/farm/plugin.ts +1 -1
- package/plugin/bundler/parcel/shared.ts +1 -1
- package/plugin/bundler/vite/plugin.ts +20 -17
- package/plugin/bundler/vite/state.ts +5 -6
- package/plugin/bundler/webpack/plugin.ts +2 -2
- package/plugin/nextjs/plugin.ts +2 -2
- package/plugin/nextjs/utils.ts +1 -1
- package/plugin/utils/compiler.ts +1 -2
- package/plugin/utils/filter.ts +65 -1
- package/style/tokens.ts +2 -1
- package/dist/compiler-AOb_VaKQ.mjs.map +0 -1
- package/dist/misc-CyZgGtc9.mjs.map +0 -1
- package/dist/shared-BfASWZo1.mjs.map +0 -1
- package/dist/shared-DY0kuBcN.cjs.map +0 -1
- package/dist/utils-BO6Ix8t5.mjs.map +0 -1
- package/dist/webpack-WXAJ3IYn.mjs.map +0 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import type { CompilerInternal } from '../../compiler';
|
|
5
|
+
import { getCompilerCacheIdentity } from '../../compiler/cache_identity';
|
|
6
|
+
import { BabelCore, babelTransformOptions } from '../utils/babel';
|
|
7
|
+
import type { BabelTypes } from '../utils/babel';
|
|
8
|
+
|
|
9
|
+
const defaultParser = BabelCore.parseSync;
|
|
10
|
+
const MAX_MEMORY_BYTES = 32 * 1024 * 1024;
|
|
11
|
+
type MemoryEntry = { contentHash: string; json: string; };
|
|
12
|
+
type DiskEntry = { version: 1; filePath: string; contentHash: string; checksum: string; ast: BabelTypes.File; };
|
|
13
|
+
|
|
14
|
+
/** Immutable syntax only. Every consumer gets fresh nodes before annotations/evaluation. */
|
|
15
|
+
export function createTraceMetadataCache(internal: CompilerInternal) {
|
|
16
|
+
const memory = new Map<string, MemoryEntry>();
|
|
17
|
+
let memoryBytes = 0;
|
|
18
|
+
let memoryVersion = internal.options.cache?.version;
|
|
19
|
+
const stats = { parses: 0, memoryHits: 0, diskHits: 0, writes: 0 };
|
|
20
|
+
const remember = (filePath: string, contentHash: string, json: string) => {
|
|
21
|
+
const previous = memory.get(filePath);
|
|
22
|
+
if (previous) memoryBytes -= previous.json.length * 2;
|
|
23
|
+
memory.delete(filePath);
|
|
24
|
+
if (json.length * 2 > MAX_MEMORY_BYTES) return;
|
|
25
|
+
while (memoryBytes + json.length * 2 > MAX_MEMORY_BYTES) {
|
|
26
|
+
const oldest = memory.keys().next().value!;
|
|
27
|
+
memoryBytes -= memory.get(oldest)!.json.length * 2;
|
|
28
|
+
memory.delete(oldest);
|
|
29
|
+
}
|
|
30
|
+
memory.set(filePath, { contentHash, json });
|
|
31
|
+
memoryBytes += json.length * 2;
|
|
32
|
+
};
|
|
33
|
+
const parse = (babel: typeof BabelCore, filePath: string, content: string): BabelTypes.File | null => {
|
|
34
|
+
if (!Object.is(memoryVersion, internal.options.cache?.version)) {
|
|
35
|
+
memory.clear();
|
|
36
|
+
memoryBytes = 0;
|
|
37
|
+
memoryVersion = internal.options.cache?.version;
|
|
38
|
+
}
|
|
39
|
+
const supportedParser = babel.parseSync === defaultParser;
|
|
40
|
+
const contentHash = hash(content);
|
|
41
|
+
const cached = supportedParser ? memory.get(filePath) : undefined;
|
|
42
|
+
if (cached?.contentHash === contentHash) {
|
|
43
|
+
stats.memoryHits++;
|
|
44
|
+
memory.delete(filePath);
|
|
45
|
+
memory.set(filePath, cached);
|
|
46
|
+
return JSON.parse(cached.json) as BabelTypes.File;
|
|
47
|
+
}
|
|
48
|
+
const identity = supportedParser && internal.options.cache?.persistent !== false
|
|
49
|
+
? getCompilerCacheIdentity()
|
|
50
|
+
: null;
|
|
51
|
+
const filename = identity
|
|
52
|
+
? path.join(
|
|
53
|
+
internal.cacheDir,
|
|
54
|
+
'trace-metadata-v1',
|
|
55
|
+
identity,
|
|
56
|
+
hash(JSON.stringify(internal.options.cache?.version ?? null)),
|
|
57
|
+
hash(filePath) + '.json',
|
|
58
|
+
)
|
|
59
|
+
: null;
|
|
60
|
+
if (filename) {
|
|
61
|
+
try {
|
|
62
|
+
const record = JSON.parse(fs.readFileSync(filename, 'utf8')) as DiskEntry;
|
|
63
|
+
if (
|
|
64
|
+
record.version === 1 && record.filePath === filePath && record.contentHash === contentHash &&
|
|
65
|
+
record.ast?.type === 'File' && record.ast.program?.type === 'Program' &&
|
|
66
|
+
Array.isArray(record.ast.program.body)
|
|
67
|
+
) {
|
|
68
|
+
const json = JSON.stringify(record.ast);
|
|
69
|
+
if (record.checksum === hash(json)) {
|
|
70
|
+
remember(filePath, contentHash, json);
|
|
71
|
+
stats.diskHits++;
|
|
72
|
+
return record.ast;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
// Unreadable/corrupt metadata is a cache miss, never a compiler error.
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
let ast: BabelTypes.File | null;
|
|
80
|
+
try {
|
|
81
|
+
stats.parses++;
|
|
82
|
+
ast = babel.parseSync(content, { ...babelTransformOptions(), filename: filePath }) as BabelTypes.File | null;
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (!ast || !supportedParser) return ast;
|
|
87
|
+
let temporary: string | undefined;
|
|
88
|
+
try {
|
|
89
|
+
// JSON cannot preserve non-finite literal values or runtime objects.
|
|
90
|
+
const json = JSON.stringify(ast, (_key, value: unknown) => {
|
|
91
|
+
if (
|
|
92
|
+
typeof value === 'number' && (!Number.isFinite(value) || Object.is(value, -0)) ||
|
|
93
|
+
typeof value === 'bigint' || typeof value === 'function' || typeof value === 'symbol'
|
|
94
|
+
) {
|
|
95
|
+
throw new Error('Non-JSON syntax data');
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
});
|
|
99
|
+
remember(filePath, contentHash, json);
|
|
100
|
+
if (filename) {
|
|
101
|
+
const record: DiskEntry = { version: 1, filePath, contentHash, checksum: hash(json), ast };
|
|
102
|
+
fs.mkdirSync(path.dirname(filename), { recursive: true });
|
|
103
|
+
temporary = filename + '.' + randomUUID() + '.tmp';
|
|
104
|
+
fs.writeFileSync(temporary, JSON.stringify(record), 'utf8');
|
|
105
|
+
fs.renameSync(temporary, filename);
|
|
106
|
+
stats.writes++;
|
|
107
|
+
}
|
|
108
|
+
} catch {
|
|
109
|
+
// Metadata reuse is optional. Parsing/evaluation still proceeds normally.
|
|
110
|
+
} finally {
|
|
111
|
+
if (temporary) {
|
|
112
|
+
try {
|
|
113
|
+
fs.unlinkSync(temporary);
|
|
114
|
+
} catch { /* Already renamed or unavailable. */ }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return ast;
|
|
118
|
+
};
|
|
119
|
+
return { parse, getStats: () => ({ ...stats }) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function hash(content: string): string {
|
|
123
|
+
return createHash('sha256').update(content).digest('hex');
|
|
124
|
+
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import * as crypto from 'node:crypto';
|
|
2
|
-
import * as fs from 'node:fs';
|
|
3
2
|
import { BUILDER_TYPE_SCOPE } from '../../../builder/data';
|
|
4
3
|
import { TRACE_STYLE, TRACE_VALUE } from '../../../builder/data/debug';
|
|
5
4
|
import { createExtractedScope, createExtractedSheet, createExtractedStyle } from '../../../builder/extract';
|
|
6
5
|
import type { ExtractedSheetTuple, ExtractedStyleTuple } from '../../../builder/extract';
|
|
7
6
|
import type { CompilerInternal } from '../../compiler';
|
|
8
7
|
import type { CompilerOptions } from '../../compiler/types';
|
|
9
|
-
import { resolveFile } from '../../utils/file_resolver';
|
|
8
|
+
import { clearResolverCache, resolveFile } from '../../utils/file_resolver';
|
|
10
9
|
import { createImportSourceMatcher } from '../../utils/import_source';
|
|
11
10
|
import {
|
|
12
11
|
compileChain,
|
|
@@ -18,33 +17,40 @@ import {
|
|
|
18
17
|
} from '../extract/chain';
|
|
19
18
|
import { annotateAtRuleDeclaration, annotateTokenDeclaration } from '../syntax/static_ids';
|
|
20
19
|
import type { BabelCore, BabelTypes } from '../utils/babel';
|
|
21
|
-
import { babelTransformOptions } from '../utils/babel';
|
|
22
20
|
import { getProjectFileId } from '../utils/path';
|
|
23
21
|
import type { CompiledStyleObject, CompiledStyleObjectLocations, EvalScope } from './evaluator';
|
|
24
22
|
import { COMPILED_STYLE_OBJECT_LOCATIONS, evalFail, evalOk, evaluateEnumDeclaration, evaluateNode } from './evaluator';
|
|
23
|
+
import { LazyBindings } from './lazy_bindings';
|
|
24
|
+
import { createTraceMetadataCache } from './metadata_cache';
|
|
25
25
|
import type { EvalModuleBindings, EvalResult, EvalSlotRef, ImportMap, ResolveImportFn } from './types';
|
|
26
26
|
|
|
27
27
|
export type Tracer = ReturnType<typeof createTracer>;
|
|
28
28
|
|
|
29
29
|
type TraceCacheContent = {
|
|
30
|
-
bindings:
|
|
30
|
+
bindings: EvalModuleBindings;
|
|
31
31
|
dependencies?: TraceDependency[];
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
const CACHE_TYPE = 'transform-trace';
|
|
34
|
+
const CACHE_TYPE = 'transform-trace-links-v2';
|
|
35
35
|
|
|
36
36
|
type TraceDependency = {
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
source: string;
|
|
38
|
+
fromFile: string;
|
|
39
|
+
filePath: string | null;
|
|
40
|
+
contentHash: string | null;
|
|
39
41
|
};
|
|
40
42
|
|
|
41
43
|
type TraceResult = TraceDependency & {
|
|
44
|
+
filePath: string;
|
|
45
|
+
contentHash: string;
|
|
42
46
|
bindings: EvalModuleBindings;
|
|
43
47
|
dependencies: TraceDependency[];
|
|
44
48
|
};
|
|
45
49
|
|
|
46
50
|
export function createTracer(internal: CompilerInternal) {
|
|
47
51
|
const active = new Set<string>();
|
|
52
|
+
const cyclic = new Set<string>();
|
|
53
|
+
const metadata = createTraceMetadataCache(internal);
|
|
48
54
|
|
|
49
55
|
const resolveImport = (
|
|
50
56
|
babel: typeof BabelCore,
|
|
@@ -70,51 +76,75 @@ export function createTracer(internal: CompilerInternal) {
|
|
|
70
76
|
cacheType: CACHE_TYPE,
|
|
71
77
|
});
|
|
72
78
|
|
|
73
|
-
if (cached && areDependenciesFresh(cached.dependencies ?? [])) {
|
|
79
|
+
if (cached?.bindings instanceof Map && areDependenciesFresh(cached.dependencies ?? [])) {
|
|
74
80
|
return {
|
|
81
|
+
source,
|
|
82
|
+
fromFile,
|
|
75
83
|
filePath: resolved.filePath,
|
|
76
84
|
contentHash,
|
|
77
|
-
bindings:
|
|
85
|
+
bindings: cached.bindings,
|
|
78
86
|
dependencies: cached.dependencies ?? [],
|
|
79
87
|
};
|
|
80
88
|
}
|
|
81
89
|
|
|
82
|
-
if (active.has(resolved.filePath))
|
|
90
|
+
if (active.has(resolved.filePath)) {
|
|
91
|
+
// Partial export-star maps depend on which module entered the cycle first.
|
|
92
|
+
// Reconstruct them from syntax instead of retaining that partial evaluation.
|
|
93
|
+
for (const file of active) cyclic.add(file);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
83
96
|
active.add(resolved.filePath);
|
|
84
97
|
|
|
85
98
|
try {
|
|
86
99
|
const dependencies = new Map<string, TraceDependency>();
|
|
100
|
+
const dependencyList: TraceDependency[] = [];
|
|
101
|
+
const onTrack = () => {
|
|
102
|
+
dependencyList.splice(0, dependencyList.length, ...dependencies.values());
|
|
103
|
+
};
|
|
87
104
|
const resolveDependency: ResolveImportFn = (source, fromFile) => {
|
|
105
|
+
// Record lookup requests, including failures, so resolution changes are visible.
|
|
106
|
+
const dependency = resolveFile(source, fromFile);
|
|
107
|
+
addTraceDependency(dependencies, {
|
|
108
|
+
source,
|
|
109
|
+
fromFile,
|
|
110
|
+
filePath: dependency?.filePath ?? null,
|
|
111
|
+
contentHash: dependency ? hashContent(dependency.content) : null,
|
|
112
|
+
});
|
|
113
|
+
onTrack();
|
|
88
114
|
const traced = traceModuleResult(babel, source, fromFile);
|
|
89
115
|
if (!traced) return null;
|
|
90
116
|
|
|
91
|
-
return createTrackedBindings(traced, dependencies);
|
|
117
|
+
return createTrackedBindings(traced, dependencies, onTrack, () => traceModuleResult(babel, source, fromFile));
|
|
92
118
|
};
|
|
93
119
|
|
|
94
|
-
const
|
|
120
|
+
const ast = metadata.parse(babel, resolved.filePath, resolved.content);
|
|
121
|
+
if (!ast) return null;
|
|
122
|
+
const bindings = extractModuleBindings(
|
|
95
123
|
internal.projectDir,
|
|
96
124
|
resolved.filePath,
|
|
97
|
-
|
|
125
|
+
ast,
|
|
98
126
|
babel,
|
|
99
127
|
resolveDependency,
|
|
100
128
|
internal.options,
|
|
101
129
|
);
|
|
102
130
|
if (!bindings) return null;
|
|
103
131
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
}
|
|
132
|
+
if (!cyclic.has(resolved.filePath)) {
|
|
133
|
+
internal.cache.setItem<TraceCacheContent>({
|
|
134
|
+
filePath: resolved.filePath,
|
|
135
|
+
contentHash,
|
|
136
|
+
cacheType: CACHE_TYPE,
|
|
137
|
+
persistent: false,
|
|
138
|
+
cacheContent: {
|
|
139
|
+
bindings,
|
|
140
|
+
dependencies: dependencyList,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
116
144
|
|
|
117
145
|
return {
|
|
146
|
+
source,
|
|
147
|
+
fromFile,
|
|
118
148
|
filePath: resolved.filePath,
|
|
119
149
|
contentHash,
|
|
120
150
|
bindings,
|
|
@@ -136,34 +166,25 @@ export function createTracer(internal: CompilerInternal) {
|
|
|
136
166
|
return {
|
|
137
167
|
resolveImport,
|
|
138
168
|
traceModule,
|
|
169
|
+
getMetadataCacheStats: metadata.getStats,
|
|
170
|
+
beginCompilation() {
|
|
171
|
+
clearResolverCache();
|
|
172
|
+
cyclic.clear();
|
|
173
|
+
},
|
|
139
174
|
};
|
|
140
175
|
}
|
|
141
176
|
|
|
142
|
-
function
|
|
177
|
+
function extractModuleBindings(
|
|
143
178
|
projectDir: string,
|
|
144
179
|
filePath: string,
|
|
145
|
-
|
|
180
|
+
ast: BabelTypes.File,
|
|
146
181
|
babel: typeof BabelCore,
|
|
147
182
|
resolveImport: ResolveImportFn,
|
|
148
183
|
options: CompilerOptions,
|
|
149
184
|
): EvalModuleBindings | null {
|
|
150
|
-
let ast: BabelTypes.File | null = null;
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
const parsed = babel.parseSync(content, {
|
|
154
|
-
...babelTransformOptions(),
|
|
155
|
-
filename: filePath,
|
|
156
|
-
});
|
|
157
|
-
ast = parsed as BabelTypes.File;
|
|
158
|
-
} catch {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!ast) return null;
|
|
163
|
-
|
|
164
185
|
const imports: ImportMap = new Map();
|
|
165
186
|
const rawBindings: Map<string, BabelTypes.Node> = new Map();
|
|
166
|
-
const bindings
|
|
187
|
+
const bindings = new LazyBindings();
|
|
167
188
|
const styleNames = new Set<string>();
|
|
168
189
|
const styleMetas = new Map();
|
|
169
190
|
const fileId = getProjectFileId(projectDir, filePath);
|
|
@@ -261,9 +282,9 @@ function parseAndExtractModule(
|
|
|
261
282
|
if (stmt.type === 'ExportAllDeclaration') {
|
|
262
283
|
const exported = resolveImport(stmt.source.value, filePath);
|
|
263
284
|
if (exported) {
|
|
264
|
-
exported.
|
|
265
|
-
if (key !== 'default') bindings.
|
|
266
|
-
}
|
|
285
|
+
for (const key of exported.keys()) {
|
|
286
|
+
if (key !== 'default') bindings.setLazy(key, () => exported.get(key)!);
|
|
287
|
+
}
|
|
267
288
|
}
|
|
268
289
|
}
|
|
269
290
|
|
|
@@ -285,7 +306,14 @@ function parseAndExtractModule(
|
|
|
285
306
|
};
|
|
286
307
|
|
|
287
308
|
rawBindings.forEach((node, name) => {
|
|
288
|
-
bindings.
|
|
309
|
+
bindings.setLazy(name, () => {
|
|
310
|
+
const imported = imports.get(name);
|
|
311
|
+
if (imported && node.type === 'Identifier' && node.name === name) {
|
|
312
|
+
return resolveImport(imported.source, filePath)?.get(imported.name) ??
|
|
313
|
+
evalFail(`Cannot resolve export: ${name}`);
|
|
314
|
+
}
|
|
315
|
+
return evaluateResolvedNode(node, scope, styleNames, fileId, options);
|
|
316
|
+
});
|
|
289
317
|
});
|
|
290
318
|
|
|
291
319
|
return bindings;
|
|
@@ -582,67 +610,54 @@ function createSlotRef(
|
|
|
582
610
|
}
|
|
583
611
|
|
|
584
612
|
function hashContent(content: string): string {
|
|
585
|
-
return crypto.createHash('sha256').update(content).digest('hex')
|
|
613
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
586
614
|
}
|
|
587
615
|
|
|
588
616
|
function addTraceDependency(
|
|
589
617
|
dependencies: Map<string, TraceDependency>,
|
|
590
618
|
dependency: TraceDependency,
|
|
591
619
|
) {
|
|
592
|
-
dependencies.set(dependency.
|
|
620
|
+
dependencies.set(JSON.stringify([dependency.source, dependency.fromFile]), dependency);
|
|
593
621
|
}
|
|
594
622
|
|
|
595
623
|
function createTrackedBindings(
|
|
596
624
|
traced: TraceResult,
|
|
597
625
|
dependencies: Map<string, TraceDependency>,
|
|
626
|
+
onTrack: () => void,
|
|
627
|
+
refresh: () => TraceResult | null,
|
|
598
628
|
): EvalModuleBindings {
|
|
599
629
|
const track = () => {
|
|
600
630
|
addTraceDependency(dependencies, traced);
|
|
601
631
|
traced.dependencies.forEach((dependency) => addTraceDependency(dependencies, dependency));
|
|
632
|
+
onTrack();
|
|
602
633
|
};
|
|
603
634
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
override has(key: string): boolean {
|
|
616
|
-
const result = super.has(key);
|
|
617
|
-
if (result) track();
|
|
618
|
-
return result;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
override forEach(
|
|
622
|
-
callbackfn: (value: EvalResult, key: string, map: Map<string, EvalResult>) => void,
|
|
623
|
-
thisArg?: unknown,
|
|
624
|
-
): void {
|
|
635
|
+
const bindings = new LazyBindings();
|
|
636
|
+
for (const key of traced.bindings.keys()) {
|
|
637
|
+
bindings.setLazy(key, () => {
|
|
638
|
+
// A lazy export may first be requested in a later compilation. Refresh its
|
|
639
|
+
// module before using a previously captured binding map.
|
|
640
|
+
const current = refresh();
|
|
641
|
+
if (!current) return evalFail(`Cannot resolve export: ${key}`);
|
|
642
|
+
traced = current;
|
|
643
|
+
const value = traced.bindings.get(key) ?? evalFail(`Cannot resolve export: ${key}`);
|
|
625
644
|
track();
|
|
626
|
-
|
|
627
|
-
}
|
|
628
|
-
}
|
|
645
|
+
return value;
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
// Record the module itself even when a requested export is missing. Values
|
|
649
|
+
// add their transitive dependencies after lazy resolution has completed.
|
|
650
|
+
track();
|
|
651
|
+
return bindings;
|
|
629
652
|
}
|
|
630
653
|
|
|
631
|
-
function areDependenciesFresh(
|
|
632
|
-
dependencies: TraceDependency[],
|
|
633
|
-
): boolean {
|
|
654
|
+
function areDependenciesFresh(dependencies: TraceDependency[]): boolean {
|
|
634
655
|
for (const dependency of dependencies) {
|
|
635
|
-
const
|
|
636
|
-
if (
|
|
656
|
+
const current = resolveFile(dependency.source, dependency.fromFile);
|
|
657
|
+
if (
|
|
658
|
+
(current?.filePath ?? null) !== dependency.filePath ||
|
|
659
|
+
(current ? hashContent(current.content) : null) !== dependency.contentHash
|
|
660
|
+
) return false;
|
|
637
661
|
}
|
|
638
|
-
|
|
639
662
|
return true;
|
|
640
663
|
}
|
|
641
|
-
|
|
642
|
-
function hashFileContent(filePath: string): string | null {
|
|
643
|
-
try {
|
|
644
|
-
return hashContent(fs.readFileSync(filePath, 'utf8'));
|
|
645
|
-
} catch {
|
|
646
|
-
return null;
|
|
647
|
-
}
|
|
648
|
-
}
|
|
@@ -3,6 +3,7 @@ import { hashString } from '../../../utils/hash';
|
|
|
3
3
|
import type { CompilerRuntimeMode } from '../../compiler';
|
|
4
4
|
import type { CompilerOptions } from '../../compiler/types';
|
|
5
5
|
import type { CompilerCssCollector } from '../../extract/collector';
|
|
6
|
+
import { recordCheckDiagnostic } from '../../utils/check_diagnostics';
|
|
6
7
|
import {
|
|
7
8
|
FN_CREATE_EXTRACTED_THEME,
|
|
8
9
|
FN_CREATE_EXTRACTED_TOKEN,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
evaluateEnumDeclaration,
|
|
23
24
|
evaluateNode,
|
|
24
25
|
} from '../evaluator/evaluator';
|
|
26
|
+
import { LazyBindings } from '../evaluator/lazy_bindings';
|
|
25
27
|
import { getImportedName } from '../syntax';
|
|
26
28
|
import { annotateAtRuleDeclaration } from '../syntax/static_ids';
|
|
27
29
|
import { BabelCore } from '../utils/babel';
|
|
@@ -69,7 +71,7 @@ export function createExtractPlugin(args: PluginArgs) {
|
|
|
69
71
|
this.styleNames = new Set();
|
|
70
72
|
this.sheetNames = new Set();
|
|
71
73
|
this.styleMetas = new Map();
|
|
72
|
-
this.bindings = new
|
|
74
|
+
this.bindings = new LazyBindings();
|
|
73
75
|
this.bindingNodes = new Map();
|
|
74
76
|
this.imports = new Map();
|
|
75
77
|
this.needsExtractImport = false;
|
|
@@ -137,11 +139,7 @@ export function createExtractPlugin(args: PluginArgs) {
|
|
|
137
139
|
annotateAtRuleDeclaration(decl, state, t);
|
|
138
140
|
|
|
139
141
|
const name = decl.id.name;
|
|
140
|
-
|
|
141
|
-
const result = evaluateNode(decl.init, scope);
|
|
142
|
-
|
|
143
|
-
state.bindings.set(name, result);
|
|
144
|
-
state.bindingNodes.set(name, decl.init);
|
|
142
|
+
recordLazyBinding(state, name, t.cloneNode(decl.init, true));
|
|
145
143
|
});
|
|
146
144
|
},
|
|
147
145
|
|
|
@@ -156,11 +154,7 @@ export function createExtractPlugin(args: PluginArgs) {
|
|
|
156
154
|
annotateAtRuleDeclaration(decl, state, t);
|
|
157
155
|
|
|
158
156
|
const name = decl.id.name;
|
|
159
|
-
|
|
160
|
-
const result = evaluateNode(decl.init, scope);
|
|
161
|
-
|
|
162
|
-
state.bindings.set(name, result);
|
|
163
|
-
state.bindingNodes.set(name, decl.init);
|
|
157
|
+
recordLazyBinding(state, name, t.cloneNode(decl.init, true));
|
|
164
158
|
});
|
|
165
159
|
},
|
|
166
160
|
|
|
@@ -250,6 +244,7 @@ export function createExtractPlugin(args: PluginArgs) {
|
|
|
250
244
|
if (!result) return;
|
|
251
245
|
|
|
252
246
|
checkCompiledImportantUsage(result, options.check?.important, (level, message) => {
|
|
247
|
+
recordCheckDiagnostic({ level, message });
|
|
253
248
|
if (level === 'error') throw path.buildCodeFrameError(message);
|
|
254
249
|
console.warn('[fluentic-style] ' + message);
|
|
255
250
|
});
|
|
@@ -328,6 +323,21 @@ export function createExtractPlugin(args: PluginArgs) {
|
|
|
328
323
|
});
|
|
329
324
|
}
|
|
330
325
|
|
|
326
|
+
function recordLazyBinding(state: ExtractPluginState, name: string, initializer: BabelTypes.Node) {
|
|
327
|
+
// Capture the declaration environment without evaluating it. Later shadowed
|
|
328
|
+
// declarations must not change the meaning of a pending initializer.
|
|
329
|
+
const bindings = state.bindings.fork();
|
|
330
|
+
const scope = {
|
|
331
|
+
...getEvalScope(state),
|
|
332
|
+
bindings,
|
|
333
|
+
bindingNodes: new Map(state.bindingNodes),
|
|
334
|
+
imports: new Map(state.imports),
|
|
335
|
+
};
|
|
336
|
+
bindings.setLazy(name, () => evaluateNode(initializer, scope));
|
|
337
|
+
state.bindings.setLazy(name, () => bindings.get(name)!);
|
|
338
|
+
state.bindingNodes.set(name, initializer);
|
|
339
|
+
}
|
|
340
|
+
|
|
331
341
|
function checkCompiledImportantUsage(
|
|
332
342
|
result: NonNullable<ReturnType<typeof compileChain>>,
|
|
333
343
|
options: CheckImportantOptions | undefined,
|
|
@@ -4,7 +4,8 @@ import type { CompilerOptions } from '../../../compiler/types';
|
|
|
4
4
|
import type { CompilerCssCollector } from '../../../extract';
|
|
5
5
|
import type { ImportSourceMatcher } from '../../../utils/import_source';
|
|
6
6
|
import type { EvalScope } from '../../evaluator/evaluator';
|
|
7
|
-
import type {
|
|
7
|
+
import type { LazyBindings } from '../../evaluator/lazy_bindings';
|
|
8
|
+
import type { ImportMap, ResolveImportFn } from '../../evaluator/types';
|
|
8
9
|
import type { BabelTypes } from '../../utils/babel';
|
|
9
10
|
|
|
10
11
|
export type HoistedDeclaration = {
|
|
@@ -21,7 +22,7 @@ export type ExtractPluginState = {
|
|
|
21
22
|
styleNames: Set<string>;
|
|
22
23
|
sheetNames: Set<string>;
|
|
23
24
|
styleMetas: Map<string, StyleFnMeta>;
|
|
24
|
-
bindings:
|
|
25
|
+
bindings: LazyBindings;
|
|
25
26
|
bindingNodes: Map<string, BabelTypes.Node>;
|
|
26
27
|
imports: ImportMap;
|
|
27
28
|
needsExtractImport: boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type CheckDiagnostic = { level: 'warn' | 'error'; message: string; };
|
|
2
|
+
|
|
3
|
+
let observer: ((diagnostic: CheckDiagnostic) => void) | undefined;
|
|
4
|
+
|
|
5
|
+
/** Extraction is synchronous; restore the previous observer for nested compilations. */
|
|
6
|
+
export function observeCheckDiagnostics<T>(onDiagnostic: (diagnostic: CheckDiagnostic) => void, run: () => T): T {
|
|
7
|
+
const previous = observer;
|
|
8
|
+
observer = onDiagnostic;
|
|
9
|
+
try {
|
|
10
|
+
return run();
|
|
11
|
+
} finally {
|
|
12
|
+
observer = previous;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function recordCheckDiagnostic(diagnostic: CheckDiagnostic) {
|
|
17
|
+
observer?.(diagnostic);
|
|
18
|
+
}
|
|
@@ -15,6 +15,24 @@ export type ResolvedFile = {
|
|
|
15
15
|
export type ResolveFileFn = (info: ResolveFileInfo) => ResolvedFile | null;
|
|
16
16
|
|
|
17
17
|
const contentCache = new Map<string, ResolvedFile>();
|
|
18
|
+
let resolutionObserver: ((source: string, fromFile: string, result: ResolvedFile | null) => void) | null = null;
|
|
19
|
+
|
|
20
|
+
/** Synchronous extraction only; restore the previous observer even when extraction throws. */
|
|
21
|
+
export function observeFileResolutions<T>(
|
|
22
|
+
observer: NonNullable<typeof resolutionObserver>,
|
|
23
|
+
run: () => T,
|
|
24
|
+
): T {
|
|
25
|
+
const previous = resolutionObserver;
|
|
26
|
+
resolutionObserver = (source, fromFile, result) => {
|
|
27
|
+
previous?.(source, fromFile, result);
|
|
28
|
+
observer(source, fromFile, result);
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
return run();
|
|
32
|
+
} finally {
|
|
33
|
+
resolutionObserver = previous;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
18
36
|
|
|
19
37
|
const resolver = new ResolverFactory({
|
|
20
38
|
// Static evaluation needs executable values, never declaration-only exports.
|
|
@@ -33,6 +51,12 @@ export function resolveFile(
|
|
|
33
51
|
source: string,
|
|
34
52
|
fromFile: string,
|
|
35
53
|
): ResolvedFile | null {
|
|
54
|
+
const result = readResolvedFile(source, fromFile);
|
|
55
|
+
resolutionObserver?.(source, fromFile, result);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function readResolvedFile(source: string, fromFile: string): ResolvedFile | null {
|
|
36
60
|
const filePath = resolveFilePath(source, fromFile);
|
|
37
61
|
if (!filePath) return null;
|
|
38
62
|
|