-
Notifications
You must be signed in to change notification settings - Fork 770
/
update-stencil-core-import.ts
89 lines (81 loc) · 2.61 KB
/
update-stencil-core-import.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ts from 'typescript';
import { STENCIL_CORE_ID } from '../bundle/entry-alias-ids';
export const updateStencilCoreImports = (updatedCoreImportPath: string): ts.TransformerFactory<ts.SourceFile> => {
return () => {
return (tsSourceFile) => {
if (STENCIL_CORE_ID === updatedCoreImportPath) {
return tsSourceFile;
}
let madeChanges = false;
const newStatements: ts.Statement[] = [];
tsSourceFile.statements.forEach((s) => {
if (ts.isImportDeclaration(s)) {
if (s.moduleSpecifier != null && ts.isStringLiteral(s.moduleSpecifier)) {
if (s.moduleSpecifier.text === STENCIL_CORE_ID) {
if (
s.importClause &&
s.importClause.namedBindings &&
s.importClause.namedBindings.kind === ts.SyntaxKind.NamedImports
) {
const origImports = s.importClause.namedBindings.elements;
const keepImports = origImports.map((e) => e.getText()).filter((name) => KEEP_IMPORTS.has(name));
if (keepImports.length > 0) {
const newImport = ts.factory.updateImportDeclaration(
s,
undefined,
undefined,
ts.factory.createImportClause(
false,
undefined,
ts.factory.createNamedImports(
keepImports.map((name) =>
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(name))
)
)
),
ts.factory.createStringLiteral(updatedCoreImportPath),
undefined
);
newStatements.push(newImport);
}
}
madeChanges = true;
return;
}
}
}
newStatements.push(s);
});
if (madeChanges) {
return ts.updateSourceFileNode(
tsSourceFile,
newStatements,
tsSourceFile.isDeclarationFile,
tsSourceFile.referencedFiles,
tsSourceFile.typeReferenceDirectives,
tsSourceFile.hasNoDefaultLib,
tsSourceFile.libReferenceDirectives
);
}
return tsSourceFile;
};
};
};
const KEEP_IMPORTS = new Set([
'h',
'setMode',
'getMode',
'setPlatformHelpers',
'Build',
'Env',
'Host',
'Fragment',
'getAssetPath',
'writeTask',
'readTask',
'getElement',
'forceUpdate',
'getRenderingRef',
'forceModeUpdate',
'setErrorHandler',
]);