@esportsplus/typescript 0.22.0 → 0.23.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/build/cli/tsc.js +15 -5
- package/build/compiler/imports.d.ts +1 -0
- package/build/compiler/imports.js +21 -8
- package/build/compiler/index.d.ts +0 -1
- package/build/compiler/index.js +0 -1
- package/build/compiler/plugins/index.d.ts +5 -2
- package/build/compiler/plugins/tsc.d.ts +6 -2
- package/build/compiler/plugins/tsc.js +12 -7
- package/build/compiler/plugins/vite.d.ts +1 -1
- package/build/compiler/plugins/vite.js +6 -2
- package/build/compiler/types.d.ts +9 -2
- package/build/compiler/uid.d.ts +1 -1
- package/build/compiler/uid.js +12 -5
- package/package.json +1 -1
- package/src/cli/tsc.ts +38 -12
- package/src/compiler/imports.ts +32 -9
- package/src/compiler/index.ts +0 -1
- package/src/compiler/plugins/tsc.ts +19 -8
- package/src/compiler/plugins/vite.ts +14 -6
- package/src/compiler/types.ts +12 -2
- package/src/compiler/uid.ts +16 -6
- package/build/compiler/constants.d.ts +0 -2
- package/build/compiler/constants.js +0 -2
- package/src/compiler/constants.ts +0 -4
package/build/cli/tsc.js
CHANGED
|
@@ -13,15 +13,25 @@ async function build(config, tsconfig, plugins) {
|
|
|
13
13
|
}
|
|
14
14
|
process.exit(1);
|
|
15
15
|
}
|
|
16
|
-
await
|
|
17
|
-
let printer = ts.createPrinter(), program = ts.createProgram(parsed.fileNames, parsed.options);
|
|
18
|
-
let
|
|
16
|
+
await loadPlugins(plugins, root).then((factories) => {
|
|
17
|
+
let context = new Map(), printer = ts.createPrinter(), program = ts.createProgram(parsed.fileNames, parsed.options), transformedFiles = new Map();
|
|
18
|
+
let instances = factories.before.map(f => f(program, context));
|
|
19
19
|
for (let i = 0, n = parsed.fileNames.length; i < n; i++) {
|
|
20
20
|
let fileName = parsed.fileNames[i], sourceFile = program.getSourceFile(fileName);
|
|
21
21
|
if (!sourceFile) {
|
|
22
22
|
continue;
|
|
23
23
|
}
|
|
24
|
-
let
|
|
24
|
+
for (let j = 0, m = instances.length; j < m; j++) {
|
|
25
|
+
instances[j].analyze?.(sourceFile);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let transformers = instances.map(i => i.transform);
|
|
29
|
+
for (let i = 0, n = parsed.fileNames.length; i < n; i++) {
|
|
30
|
+
let fileName = parsed.fileNames[i], sourceFile = program.getSourceFile(fileName);
|
|
31
|
+
if (!sourceFile) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let result = ts.transform(sourceFile, transformers), transformed = result.transformed[0];
|
|
25
35
|
if (transformed !== sourceFile) {
|
|
26
36
|
transformedFiles.set(normalizePath(fileName), printer.printFile(transformed));
|
|
27
37
|
}
|
|
@@ -56,7 +66,7 @@ async function build(config, tsconfig, plugins) {
|
|
|
56
66
|
return runTscAlias(process.argv.slice(2)).then((code) => process.exit(code));
|
|
57
67
|
});
|
|
58
68
|
}
|
|
59
|
-
async function
|
|
69
|
+
async function loadPlugins(plugins, root) {
|
|
60
70
|
let after = [], before = [], promises = [];
|
|
61
71
|
for (let i = 0, n = plugins.length; i < n; i++) {
|
|
62
72
|
let plugin = plugins[i], pluginPath = plugin.transform;
|
|
@@ -27,18 +27,26 @@ const isFromPackage = (node, packageName, checker) => {
|
|
|
27
27
|
return true;
|
|
28
28
|
}
|
|
29
29
|
let origin = trace(node, checker);
|
|
30
|
-
return origin
|
|
30
|
+
return origin === null || origin.includes(packageName);
|
|
31
31
|
};
|
|
32
32
|
const modify = (sourceCode, sourceFile, packageName, options) => {
|
|
33
|
-
|
|
33
|
+
let { namespace } = options;
|
|
34
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
34
35
|
return sourceCode;
|
|
35
36
|
}
|
|
36
37
|
let add = options.add ? new Set(options.add) : null, imports = find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
|
|
37
38
|
if (imports.length === 0) {
|
|
38
|
-
|
|
39
|
+
let statements = [];
|
|
40
|
+
if (namespace) {
|
|
41
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
42
|
+
}
|
|
43
|
+
if (add && add.size > 0) {
|
|
44
|
+
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
45
|
+
}
|
|
46
|
+
if (statements.length === 0) {
|
|
39
47
|
return sourceCode;
|
|
40
48
|
}
|
|
41
|
-
return
|
|
49
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
42
50
|
}
|
|
43
51
|
let specifiers = new Set();
|
|
44
52
|
for (let i = 0, n = imports.length; i < n; i++) {
|
|
@@ -53,13 +61,18 @@ const modify = (sourceCode, sourceFile, packageName, options) => {
|
|
|
53
61
|
specifiers.add(name);
|
|
54
62
|
}
|
|
55
63
|
}
|
|
56
|
-
let
|
|
64
|
+
let statements = [];
|
|
65
|
+
if (namespace) {
|
|
66
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
67
|
+
}
|
|
68
|
+
if (specifiers.size > 0) {
|
|
69
|
+
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
70
|
+
}
|
|
71
|
+
let replacements = [];
|
|
57
72
|
for (let i = 0, n = imports.length; i < n; i++) {
|
|
58
73
|
replacements.push({
|
|
59
74
|
end: imports[i].end,
|
|
60
|
-
newText: i === 0
|
|
61
|
-
? `import { ${sorted} } from '${packageName}';`
|
|
62
|
-
: '',
|
|
75
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
63
76
|
start: imports[i].start
|
|
64
77
|
});
|
|
65
78
|
}
|
package/build/compiler/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
|
-
tsc: (transform: import("../index.js").
|
|
3
|
-
|
|
2
|
+
tsc: ({ analyze, transform }: import("../index.js").PluginDefinition) => (program: import("typescript").Program, context: import("../index.js").PluginContext) => {
|
|
3
|
+
analyze?: (sourceFile: import("typescript").SourceFile) => void;
|
|
4
|
+
transform: import("typescript").TransformerFactory<import("typescript").SourceFile>;
|
|
5
|
+
};
|
|
6
|
+
vite: ({ analyze, name, onWatchChange, transform }: import("../index.js").VitePluginOptions) => ({ root }?: {
|
|
4
7
|
root?: string;
|
|
5
8
|
}) => {
|
|
6
9
|
configResolved(config: import("vite").ResolvedConfig): void;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PluginContext, PluginDefinition } from '../types.js';
|
|
2
2
|
import { ts } from '../../index.js';
|
|
3
|
-
|
|
3
|
+
type TscPluginFactory = (program: ts.Program, context: PluginContext) => {
|
|
4
|
+
analyze?: (sourceFile: ts.SourceFile) => void;
|
|
5
|
+
transform: ts.TransformerFactory<ts.SourceFile>;
|
|
6
|
+
};
|
|
7
|
+
declare const _default: ({ analyze, transform }: PluginDefinition) => TscPluginFactory;
|
|
4
8
|
export default _default;
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
export default (transform) => {
|
|
2
|
-
return (program) => {
|
|
3
|
-
return
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export default ({ analyze, transform }) => {
|
|
2
|
+
return (program, context) => {
|
|
3
|
+
return {
|
|
4
|
+
analyze: analyze
|
|
5
|
+
? (sourceFile) => analyze(sourceFile, program, context)
|
|
6
|
+
: undefined,
|
|
7
|
+
transform: () => {
|
|
8
|
+
return (sourceFile) => {
|
|
9
|
+
let result = transform(sourceFile, program, context);
|
|
10
|
+
return result.changed ? result.sourceFile : sourceFile;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
8
13
|
};
|
|
9
14
|
};
|
|
10
15
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ResolvedConfig } from 'vite';
|
|
2
2
|
import type { VitePluginOptions } from '../types.js';
|
|
3
|
-
declare const _default: ({ name, onWatchChange, transform }: VitePluginOptions) => ({ root }?: {
|
|
3
|
+
declare const _default: ({ analyze, name, onWatchChange, transform }: VitePluginOptions) => ({ root }?: {
|
|
4
4
|
root?: string;
|
|
5
5
|
}) => {
|
|
6
6
|
configResolved(config: ResolvedConfig): void;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ts } from '../../index.js';
|
|
2
2
|
import program from '../program.js';
|
|
3
3
|
const FILE_REGEX = /\.[tj]sx?$/;
|
|
4
|
-
|
|
4
|
+
let contexts = new Map();
|
|
5
|
+
export default ({ analyze, name, onWatchChange, transform }) => {
|
|
5
6
|
return ({ root } = {}) => {
|
|
6
7
|
return {
|
|
7
8
|
configResolved(config) {
|
|
@@ -14,7 +15,9 @@ export default ({ name, onWatchChange, transform }) => {
|
|
|
14
15
|
return null;
|
|
15
16
|
}
|
|
16
17
|
try {
|
|
17
|
-
let
|
|
18
|
+
let context = contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || ''), prog = program.get(root || ''), sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
|
|
19
|
+
analyze?.(sourceFile, prog, context);
|
|
20
|
+
let result = transform(sourceFile, prog, context);
|
|
18
21
|
if (!result.changed) {
|
|
19
22
|
return null;
|
|
20
23
|
}
|
|
@@ -28,6 +31,7 @@ export default ({ name, onWatchChange, transform }) => {
|
|
|
28
31
|
watchChange(id) {
|
|
29
32
|
if (FILE_REGEX.test(id)) {
|
|
30
33
|
onWatchChange?.();
|
|
34
|
+
contexts.delete(root || '');
|
|
31
35
|
program.delete(root || '');
|
|
32
36
|
}
|
|
33
37
|
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type ts from 'typescript';
|
|
2
|
+
type AnalyzeFn = (sourceFile: ts.SourceFile, program: ts.Program, context: PluginContext) => void;
|
|
3
|
+
type PluginContext = Map<string, unknown>;
|
|
4
|
+
type PluginDefinition = {
|
|
5
|
+
analyze?: AnalyzeFn;
|
|
6
|
+
transform: TransformFn;
|
|
7
|
+
};
|
|
2
8
|
type QuickCheckPattern = {
|
|
3
9
|
patterns?: string[];
|
|
4
10
|
regex?: RegExp;
|
|
@@ -10,15 +16,16 @@ type Range = {
|
|
|
10
16
|
type Replacement = Range & {
|
|
11
17
|
newText: string;
|
|
12
18
|
};
|
|
13
|
-
type TransformFn = (sourceFile: ts.SourceFile, program: ts.Program) => TransformResult;
|
|
19
|
+
type TransformFn = (sourceFile: ts.SourceFile, program: ts.Program, context?: PluginContext) => TransformResult;
|
|
14
20
|
type TransformResult = {
|
|
15
21
|
changed: boolean;
|
|
16
22
|
code: string;
|
|
17
23
|
sourceFile: ts.SourceFile;
|
|
18
24
|
};
|
|
19
25
|
type VitePluginOptions = {
|
|
26
|
+
analyze?: AnalyzeFn;
|
|
20
27
|
name: string;
|
|
21
28
|
onWatchChange?: () => void;
|
|
22
29
|
transform: TransformFn;
|
|
23
30
|
};
|
|
24
|
-
export type { QuickCheckPattern, Range, Replacement, TransformFn, TransformResult, VitePluginOptions };
|
|
31
|
+
export type { AnalyzeFn, PluginContext, PluginDefinition, QuickCheckPattern, Range, Replacement, TransformFn, TransformResult, VitePluginOptions };
|
package/build/compiler/uid.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: (prefix: string
|
|
1
|
+
declare const _default: (prefix: string) => string;
|
|
2
2
|
export default _default;
|
package/build/compiler/uid.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const INVALID_CHARS = /[^A-Za-z0-9]/g;
|
|
2
|
+
let counter = 0;
|
|
3
|
+
function hash(str) {
|
|
4
|
+
let h = 0x811c9dc5;
|
|
5
|
+
for (let i = 0, n = str.length; i < n; i++) {
|
|
6
|
+
h ^= str.charCodeAt(i);
|
|
7
|
+
h = Math.imul(h, 0x01000193);
|
|
8
|
+
}
|
|
9
|
+
return ((h >>> 0).toString(36) + Math.abs(h).toString(36)).replace(INVALID_CHARS, '');
|
|
10
|
+
}
|
|
11
|
+
export default (prefix) => {
|
|
12
|
+
return prefix + '_' + hash(prefix) + (counter++).toString(36);
|
|
6
13
|
};
|
package/package.json
CHANGED
package/src/cli/tsc.ts
CHANGED
|
@@ -11,7 +11,14 @@ type PluginConfig = {
|
|
|
11
11
|
transform: string;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
type
|
|
14
|
+
type PluginContext = Map<string, unknown>;
|
|
15
|
+
|
|
16
|
+
type PluginInstance = {
|
|
17
|
+
analyze?: (sourceFile: ts.SourceFile) => void;
|
|
18
|
+
transform: ts.TransformerFactory<ts.SourceFile>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type PluginFactory = (program: ts.Program, context: PluginContext) => PluginInstance;
|
|
15
22
|
|
|
16
23
|
|
|
17
24
|
const BACKSLASH_REGEX = /\\/g;
|
|
@@ -35,13 +42,32 @@ async function build(config: object, tsconfig: string, plugins: PluginConfig[]):
|
|
|
35
42
|
process.exit(1);
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
await
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
let beforeTransformers = transformers.before.map(f => f(program)),
|
|
45
|
+
await loadPlugins(plugins, root).then((factories) => {
|
|
46
|
+
let context: PluginContext = new Map(),
|
|
47
|
+
printer = ts.createPrinter(),
|
|
48
|
+
program = ts.createProgram(parsed.fileNames, parsed.options),
|
|
43
49
|
transformedFiles = new Map<string, string>();
|
|
44
50
|
|
|
51
|
+
// Create plugin instances with shared context
|
|
52
|
+
let instances = factories.before.map(f => f(program, context));
|
|
53
|
+
|
|
54
|
+
// Phase 1: Analyze - all plugins analyze all files first
|
|
55
|
+
for (let i = 0, n = parsed.fileNames.length; i < n; i++) {
|
|
56
|
+
let fileName = parsed.fileNames[i],
|
|
57
|
+
sourceFile = program.getSourceFile(fileName);
|
|
58
|
+
|
|
59
|
+
if (!sourceFile) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (let j = 0, m = instances.length; j < m; j++) {
|
|
64
|
+
instances[j].analyze?.(sourceFile);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Phase 2: Transform - all plugins transform all files
|
|
69
|
+
let transformers = instances.map(i => i.transform);
|
|
70
|
+
|
|
45
71
|
for (let i = 0, n = parsed.fileNames.length; i < n; i++) {
|
|
46
72
|
let fileName = parsed.fileNames[i],
|
|
47
73
|
sourceFile = program.getSourceFile(fileName);
|
|
@@ -50,7 +76,7 @@ async function build(config: object, tsconfig: string, plugins: PluginConfig[]):
|
|
|
50
76
|
continue;
|
|
51
77
|
}
|
|
52
78
|
|
|
53
|
-
let result = ts.transform(sourceFile,
|
|
79
|
+
let result = ts.transform(sourceFile, transformers),
|
|
54
80
|
transformed = result.transformed[0];
|
|
55
81
|
|
|
56
82
|
if (transformed !== sourceFile) {
|
|
@@ -109,12 +135,12 @@ async function build(config: object, tsconfig: string, plugins: PluginConfig[]):
|
|
|
109
135
|
});
|
|
110
136
|
}
|
|
111
137
|
|
|
112
|
-
async function
|
|
113
|
-
after:
|
|
114
|
-
before:
|
|
138
|
+
async function loadPlugins(plugins: PluginConfig[], root: string): Promise<{
|
|
139
|
+
after: PluginFactory[];
|
|
140
|
+
before: PluginFactory[];
|
|
115
141
|
}> {
|
|
116
|
-
let after:
|
|
117
|
-
before:
|
|
142
|
+
let after: PluginFactory[] = [],
|
|
143
|
+
before: PluginFactory[] = [],
|
|
118
144
|
promises: Promise<void>[] = [];
|
|
119
145
|
|
|
120
146
|
for (let i = 0, n = plugins.length; i < n; i++) {
|
package/src/compiler/imports.ts
CHANGED
|
@@ -11,6 +11,7 @@ type ImportInfo = {
|
|
|
11
11
|
|
|
12
12
|
type ModifyOptions = {
|
|
13
13
|
add?: Iterable<string>;
|
|
14
|
+
namespace?: string;
|
|
14
15
|
remove?: Iterable<string>;
|
|
15
16
|
};
|
|
16
17
|
|
|
@@ -59,7 +60,9 @@ const isFromPackage = (node: ts.Identifier, packageName: string, checker?: ts.Ty
|
|
|
59
60
|
|
|
60
61
|
let origin = trace(node, checker);
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
// If can't resolve symbol (e.g., sourceFile not in program), assume valid
|
|
64
|
+
// False positives cause compile errors; false negatives silently skip transforms
|
|
65
|
+
return origin === null || origin.includes(packageName);
|
|
63
66
|
};
|
|
64
67
|
|
|
65
68
|
|
|
@@ -70,8 +73,10 @@ const modify = (
|
|
|
70
73
|
packageName: string,
|
|
71
74
|
options: ModifyOptions
|
|
72
75
|
): string => {
|
|
76
|
+
let { namespace } = options;
|
|
77
|
+
|
|
73
78
|
// Fast path: nothing to change
|
|
74
|
-
if (!options.add && !options.remove) {
|
|
79
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
75
80
|
return sourceCode;
|
|
76
81
|
}
|
|
77
82
|
|
|
@@ -80,11 +85,21 @@ const modify = (
|
|
|
80
85
|
remove = options.remove ? new Set(options.remove) : null;
|
|
81
86
|
|
|
82
87
|
if (imports.length === 0) {
|
|
83
|
-
|
|
88
|
+
let statements: string[] = [];
|
|
89
|
+
|
|
90
|
+
if (namespace) {
|
|
91
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (add && add.size > 0) {
|
|
95
|
+
statements.push(`import { ${[...add].sort().join(', ')} } from '${packageName}';`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (statements.length === 0) {
|
|
84
99
|
return sourceCode;
|
|
85
100
|
}
|
|
86
101
|
|
|
87
|
-
return
|
|
102
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
88
103
|
}
|
|
89
104
|
|
|
90
105
|
// Collect all non-removed specifiers from existing imports
|
|
@@ -104,16 +119,24 @@ const modify = (
|
|
|
104
119
|
}
|
|
105
120
|
}
|
|
106
121
|
|
|
122
|
+
// Build replacement text - namespace import first, then named imports
|
|
123
|
+
let statements: string[] = [];
|
|
124
|
+
|
|
125
|
+
if (namespace) {
|
|
126
|
+
statements.push(`import * as ${namespace} from '${packageName}';`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (specifiers.size > 0) {
|
|
130
|
+
statements.push(`import { ${[...specifiers].sort().join(', ')} } from '${packageName}';`);
|
|
131
|
+
}
|
|
132
|
+
|
|
107
133
|
// Build replacements - replace first import, remove others
|
|
108
|
-
let replacements: Replacement[] = []
|
|
109
|
-
sorted = [...specifiers].sort().join(', ');
|
|
134
|
+
let replacements: Replacement[] = [];
|
|
110
135
|
|
|
111
136
|
for (let i = 0, n = imports.length; i < n; i++) {
|
|
112
137
|
replacements.push({
|
|
113
138
|
end: imports[i].end,
|
|
114
|
-
newText: i === 0
|
|
115
|
-
? `import { ${sorted} } from '${packageName}';`
|
|
116
|
-
: '',
|
|
139
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
117
140
|
start: imports[i].start
|
|
118
141
|
});
|
|
119
142
|
}
|
package/src/compiler/index.ts
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { PluginContext, PluginDefinition } from '../types';
|
|
2
2
|
import { ts } from '../..';
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let result = transform(sourceFile, program);
|
|
5
|
+
type TscPluginFactory = (program: ts.Program, context: PluginContext) => {
|
|
6
|
+
analyze?: (sourceFile: ts.SourceFile) => void;
|
|
7
|
+
transform: ts.TransformerFactory<ts.SourceFile>;
|
|
8
|
+
};
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
|
|
11
|
+
export default ({ analyze, transform }: PluginDefinition): TscPluginFactory => {
|
|
12
|
+
return (program: ts.Program, context: PluginContext) => {
|
|
13
|
+
return {
|
|
14
|
+
analyze: analyze
|
|
15
|
+
? (sourceFile: ts.SourceFile) => analyze(sourceFile, program, context)
|
|
16
|
+
: undefined,
|
|
17
|
+
transform: () => {
|
|
18
|
+
return (sourceFile) => {
|
|
19
|
+
let result = transform(sourceFile, program, context);
|
|
20
|
+
|
|
21
|
+
return result.changed ? result.sourceFile : sourceFile;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
13
24
|
};
|
|
14
25
|
};
|
|
15
26
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ResolvedConfig } from 'vite';
|
|
2
|
-
import type { VitePluginOptions } from '../types';
|
|
2
|
+
import type { PluginContext, VitePluginOptions } from '../types';
|
|
3
3
|
import { ts } from '../..';
|
|
4
4
|
import program from '../program';
|
|
5
5
|
|
|
@@ -7,7 +7,10 @@ import program from '../program';
|
|
|
7
7
|
const FILE_REGEX = /\.[tj]sx?$/;
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
let contexts = new Map<string, PluginContext>();
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export default ({ analyze, name, onWatchChange, transform }: VitePluginOptions) => {
|
|
11
14
|
return ({ root }: { root?: string } = {}) => {
|
|
12
15
|
return {
|
|
13
16
|
configResolved(config: ResolvedConfig) {
|
|
@@ -21,10 +24,13 @@ export default ({ name, onWatchChange, transform }: VitePluginOptions) => {
|
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
try {
|
|
24
|
-
let
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
let context = contexts.get(root || '') ?? contexts.set(root || '', new Map()).get(root || '')!,
|
|
28
|
+
prog = program.get(root || ''),
|
|
29
|
+
sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
|
|
30
|
+
|
|
31
|
+
analyze?.(sourceFile, prog, context);
|
|
32
|
+
|
|
33
|
+
let result = transform(sourceFile, prog, context);
|
|
28
34
|
|
|
29
35
|
if (!result.changed) {
|
|
30
36
|
return null;
|
|
@@ -40,6 +46,8 @@ export default ({ name, onWatchChange, transform }: VitePluginOptions) => {
|
|
|
40
46
|
watchChange(id: string) {
|
|
41
47
|
if (FILE_REGEX.test(id)) {
|
|
42
48
|
onWatchChange?.();
|
|
49
|
+
|
|
50
|
+
contexts.delete(root || '');
|
|
43
51
|
program.delete(root || '');
|
|
44
52
|
}
|
|
45
53
|
}
|
package/src/compiler/types.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import type ts from 'typescript';
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
type AnalyzeFn = (sourceFile: ts.SourceFile, program: ts.Program, context: PluginContext) => void;
|
|
5
|
+
|
|
6
|
+
type PluginContext = Map<string, unknown>;
|
|
7
|
+
|
|
8
|
+
type PluginDefinition = {
|
|
9
|
+
analyze?: AnalyzeFn;
|
|
10
|
+
transform: TransformFn;
|
|
11
|
+
};
|
|
12
|
+
|
|
4
13
|
type QuickCheckPattern = {
|
|
5
14
|
patterns?: string[];
|
|
6
15
|
regex?: RegExp;
|
|
@@ -15,7 +24,7 @@ type Replacement = Range & {
|
|
|
15
24
|
newText: string;
|
|
16
25
|
};
|
|
17
26
|
|
|
18
|
-
type TransformFn = (sourceFile: ts.SourceFile, program: ts.Program) => TransformResult;
|
|
27
|
+
type TransformFn = (sourceFile: ts.SourceFile, program: ts.Program, context?: PluginContext) => TransformResult;
|
|
19
28
|
|
|
20
29
|
type TransformResult = {
|
|
21
30
|
changed: boolean;
|
|
@@ -24,10 +33,11 @@ type TransformResult = {
|
|
|
24
33
|
};
|
|
25
34
|
|
|
26
35
|
type VitePluginOptions = {
|
|
36
|
+
analyze?: AnalyzeFn;
|
|
27
37
|
name: string;
|
|
28
38
|
onWatchChange?: () => void;
|
|
29
39
|
transform: TransformFn;
|
|
30
40
|
};
|
|
31
41
|
|
|
32
42
|
|
|
33
|
-
export type { QuickCheckPattern, Range, Replacement, TransformFn, TransformResult, VitePluginOptions };
|
|
43
|
+
export type { AnalyzeFn, PluginContext, PluginDefinition, QuickCheckPattern, Range, Replacement, TransformFn, TransformResult, VitePluginOptions };
|
package/src/compiler/uid.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
import { UUID_REGEX } from './constants';
|
|
1
|
+
const INVALID_CHARS = /[^A-Za-z0-9]/g;
|
|
3
2
|
|
|
4
3
|
|
|
5
|
-
let
|
|
6
|
-
i = 0;
|
|
4
|
+
let counter = 0;
|
|
7
5
|
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
function hash(str: string): string {
|
|
8
|
+
let h = 0x811c9dc5;
|
|
9
|
+
|
|
10
|
+
for (let i = 0, n = str.length; i < n; i++) {
|
|
11
|
+
h ^= str.charCodeAt(i);
|
|
12
|
+
h = Math.imul(h, 0x01000193);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return ((h >>> 0).toString(36) + Math.abs(h).toString(36)).replace(INVALID_CHARS, '');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export default (prefix: string): string => {
|
|
20
|
+
return prefix + '_' + hash(prefix) + (counter++).toString(36);
|
|
11
21
|
};
|