@esportsplus/typescript 0.21.0 → 0.23.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/build/cli/tsc.js +15 -5
- package/build/compiler/imports.d.ts +19 -0
- package/build/compiler/imports.js +95 -0
- package/build/compiler/index.d.ts +1 -1
- package/build/compiler/index.js +1 -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 +170 -0
- package/src/compiler/index.ts +1 -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;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ts } from '../index.js';
|
|
2
|
+
type ImportInfo = {
|
|
3
|
+
end: number;
|
|
4
|
+
specifiers: Map<string, string>;
|
|
5
|
+
start: number;
|
|
6
|
+
};
|
|
7
|
+
type ModifyOptions = {
|
|
8
|
+
add?: Iterable<string>;
|
|
9
|
+
namespace?: string;
|
|
10
|
+
remove?: Iterable<string>;
|
|
11
|
+
};
|
|
12
|
+
declare const _default: {
|
|
13
|
+
find: (sourceFile: ts.SourceFile, packageName: string) => ImportInfo[];
|
|
14
|
+
isFromPackage: (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker) => boolean;
|
|
15
|
+
modify: (sourceCode: string, sourceFile: ts.SourceFile, packageName: string, options: ModifyOptions) => string;
|
|
16
|
+
trace: (node: ts.Identifier, checker: ts.TypeChecker) => string | null;
|
|
17
|
+
};
|
|
18
|
+
export default _default;
|
|
19
|
+
export type { ImportInfo, ModifyOptions };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ts } from '../index.js';
|
|
2
|
+
import code from './code.js';
|
|
3
|
+
const find = (sourceFile, packageName) => {
|
|
4
|
+
let imports = [];
|
|
5
|
+
for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
|
|
6
|
+
let stmt = sourceFile.statements[i];
|
|
7
|
+
if (!ts.isImportDeclaration(stmt)) {
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
let moduleSpecifier = stmt.moduleSpecifier;
|
|
11
|
+
if (!ts.isStringLiteral(moduleSpecifier) || moduleSpecifier.text !== packageName) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
let bindings = stmt.importClause?.namedBindings, specifiers = new Map();
|
|
15
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
16
|
+
for (let j = 0, m = bindings.elements.length; j < m; j++) {
|
|
17
|
+
let element = bindings.elements[j], name = element.name.text, propertyName = element.propertyName?.text || name;
|
|
18
|
+
specifiers.set(propertyName, name);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
imports.push({ end: stmt.end, specifiers, start: stmt.getStart() });
|
|
22
|
+
}
|
|
23
|
+
return imports;
|
|
24
|
+
};
|
|
25
|
+
const isFromPackage = (node, packageName, checker) => {
|
|
26
|
+
if (!checker) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
let origin = trace(node, checker);
|
|
30
|
+
return origin === null || origin.includes(packageName);
|
|
31
|
+
};
|
|
32
|
+
const modify = (sourceCode, sourceFile, packageName, options) => {
|
|
33
|
+
let { namespace } = options;
|
|
34
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
35
|
+
return sourceCode;
|
|
36
|
+
}
|
|
37
|
+
let add = options.add ? new Set(options.add) : null, imports = find(sourceFile, packageName), remove = options.remove ? new Set(options.remove) : null;
|
|
38
|
+
if (imports.length === 0) {
|
|
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) {
|
|
47
|
+
return sourceCode;
|
|
48
|
+
}
|
|
49
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
50
|
+
}
|
|
51
|
+
let specifiers = new Set();
|
|
52
|
+
for (let i = 0, n = imports.length; i < n; i++) {
|
|
53
|
+
for (let [name, alias] of imports[i].specifiers) {
|
|
54
|
+
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
55
|
+
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (add) {
|
|
60
|
+
for (let name of add) {
|
|
61
|
+
specifiers.add(name);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
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 = [];
|
|
72
|
+
for (let i = 0, n = imports.length; i < n; i++) {
|
|
73
|
+
replacements.push({
|
|
74
|
+
end: imports[i].end,
|
|
75
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
76
|
+
start: imports[i].start
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return code.replaceReverse(sourceCode, replacements);
|
|
80
|
+
};
|
|
81
|
+
const trace = (node, checker) => {
|
|
82
|
+
let symbol = checker.getSymbolAtLocation(node);
|
|
83
|
+
if (!symbol) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
87
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
88
|
+
}
|
|
89
|
+
let declarations = symbol.getDeclarations();
|
|
90
|
+
if (!declarations || declarations.length === 0) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return declarations[0].getSourceFile().fileName;
|
|
94
|
+
};
|
|
95
|
+
export default { find, isFromPackage, modify, trace };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * as ast from './ast/index.js';
|
|
2
2
|
export { default as code } from './code.js';
|
|
3
|
+
export { default as imports } from './imports.js';
|
|
3
4
|
export { default as plugin } from './plugins/index.js';
|
|
4
5
|
export { default as program } from './program.js';
|
|
5
6
|
export { default as uid } from './uid.js';
|
|
6
7
|
export type * from './types.js';
|
|
7
|
-
export * from './constants.js';
|
package/build/compiler/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * as ast from './ast/index.js';
|
|
2
2
|
export { default as code } from './code.js';
|
|
3
|
+
export { default as imports } from './imports.js';
|
|
3
4
|
export { default as plugin } from './plugins/index.js';
|
|
4
5
|
export { default as program } from './program.js';
|
|
5
6
|
export { default as uid } from './uid.js';
|
|
6
|
-
export * from './constants.js';
|
|
@@ -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++) {
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { Replacement } from './types.js';
|
|
2
|
+
import { ts } from '~/index.js';
|
|
3
|
+
import code from './code.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
type ImportInfo = {
|
|
7
|
+
end: number;
|
|
8
|
+
specifiers: Map<string, string>;
|
|
9
|
+
start: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type ModifyOptions = {
|
|
13
|
+
add?: Iterable<string>;
|
|
14
|
+
namespace?: string;
|
|
15
|
+
remove?: Iterable<string>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// Find all named imports from a specific package
|
|
20
|
+
const find = (sourceFile: ts.SourceFile, packageName: string): ImportInfo[] => {
|
|
21
|
+
let imports: ImportInfo[] = [];
|
|
22
|
+
|
|
23
|
+
for (let i = 0, n = sourceFile.statements.length; i < n; i++) {
|
|
24
|
+
let stmt = sourceFile.statements[i];
|
|
25
|
+
|
|
26
|
+
if (!ts.isImportDeclaration(stmt)) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let moduleSpecifier = stmt.moduleSpecifier;
|
|
31
|
+
|
|
32
|
+
if (!ts.isStringLiteral(moduleSpecifier) || moduleSpecifier.text !== packageName) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let bindings = stmt.importClause?.namedBindings,
|
|
37
|
+
specifiers = new Map<string, string>();
|
|
38
|
+
|
|
39
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
40
|
+
for (let j = 0, m = bindings.elements.length; j < m; j++) {
|
|
41
|
+
let element = bindings.elements[j],
|
|
42
|
+
name = element.name.text,
|
|
43
|
+
propertyName = element.propertyName?.text || name;
|
|
44
|
+
|
|
45
|
+
specifiers.set(propertyName, name);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
imports.push({ end: stmt.end, specifiers, start: stmt.getStart() });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return imports;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Check if identifier traces back to a specific package
|
|
56
|
+
const isFromPackage = (node: ts.Identifier, packageName: string, checker?: ts.TypeChecker): boolean => {
|
|
57
|
+
if (!checker) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let origin = trace(node, checker);
|
|
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);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// Modify imports: remove specified, add needed, delete if empty
|
|
70
|
+
const modify = (
|
|
71
|
+
sourceCode: string,
|
|
72
|
+
sourceFile: ts.SourceFile,
|
|
73
|
+
packageName: string,
|
|
74
|
+
options: ModifyOptions
|
|
75
|
+
): string => {
|
|
76
|
+
let { namespace } = options;
|
|
77
|
+
|
|
78
|
+
// Fast path: nothing to change
|
|
79
|
+
if (!options.add && !options.namespace && !options.remove) {
|
|
80
|
+
return sourceCode;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let add = options.add ? new Set(options.add) : null,
|
|
84
|
+
imports = find(sourceFile, packageName),
|
|
85
|
+
remove = options.remove ? new Set(options.remove) : null;
|
|
86
|
+
|
|
87
|
+
if (imports.length === 0) {
|
|
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) {
|
|
99
|
+
return sourceCode;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return statements.join('\n') + '\n' + sourceCode;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Collect all non-removed specifiers from existing imports
|
|
106
|
+
let specifiers = new Set<string>();
|
|
107
|
+
|
|
108
|
+
for (let i = 0, n = imports.length; i < n; i++) {
|
|
109
|
+
for (let [name, alias] of imports[i].specifiers) {
|
|
110
|
+
if (!remove || (!remove.has(name) && !remove.has(alias))) {
|
|
111
|
+
specifiers.add(name === alias ? name : `${name} as ${alias}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (add) {
|
|
117
|
+
for (let name of add) {
|
|
118
|
+
specifiers.add(name);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
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
|
+
|
|
133
|
+
// Build replacements - replace first import, remove others
|
|
134
|
+
let replacements: Replacement[] = [];
|
|
135
|
+
|
|
136
|
+
for (let i = 0, n = imports.length; i < n; i++) {
|
|
137
|
+
replacements.push({
|
|
138
|
+
end: imports[i].end,
|
|
139
|
+
newText: i === 0 ? statements.join('\n') : '',
|
|
140
|
+
start: imports[i].start
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return code.replaceReverse(sourceCode, replacements);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// Trace symbol through re-exports to find original declaration source file
|
|
148
|
+
const trace = (node: ts.Identifier, checker: ts.TypeChecker): string | null => {
|
|
149
|
+
let symbol = checker.getSymbolAtLocation(node);
|
|
150
|
+
|
|
151
|
+
if (!symbol) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (symbol.flags & ts.SymbolFlags.Alias) {
|
|
156
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let declarations = symbol.getDeclarations();
|
|
160
|
+
|
|
161
|
+
if (!declarations || declarations.length === 0) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return declarations[0].getSourceFile().fileName;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
export default { find, isFromPackage, modify, trace };
|
|
170
|
+
export type { ImportInfo, ModifyOptions };
|
package/src/compiler/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * as ast from './ast';
|
|
2
2
|
export { default as code } from './code';
|
|
3
|
+
export { default as imports } from './imports';
|
|
3
4
|
export { default as plugin } from './plugins';
|
|
4
5
|
export { default as program } from './program';
|
|
5
6
|
export { default as uid } from './uid';
|
|
6
7
|
export type * from './types';
|
|
7
|
-
export * from './constants';
|
|
@@ -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
|
};
|