-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathpathTransform.ts
More file actions
233 lines (209 loc) · 9.16 KB
/
Copy pathpathTransform.ts
File metadata and controls
233 lines (209 loc) · 9.16 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import * as ts from "typescript";
import * as path from "path";
import * as fs from "fs";
import {
type BuildType,
type PublicPackageVariable,
getDevPackagesByBuildType,
getPublicPackageName,
isValidDevPackageName,
declarationsOnlyPackages,
bundledESPackages,
} from "./packageMapping.js";
const AddJS = (to: string, forceAppend?: boolean | string): string => (forceAppend && !to.endsWith(".js") ? to + (forceAppend === true ? ".js" : forceAppend) : to);
// This function was adjusted for generated/src process
const GetPathForComputed = (computedPath: string, sourceFilename: string) => {
let p = computedPath;
const generatedIndex = sourceFilename.indexOf("src");
const srcIndex = sourceFilename.indexOf("src");
if (generatedIndex !== -1) {
p = sourceFilename.substring(0, generatedIndex) + "src/" + p;
} else if (srcIndex !== -1) {
p = p.substring(0, srcIndex) + "src/" + p;
}
return p;
};
const GetRelativePath = (computedPath: string, sourceFilename: string) => {
let p = path.relative(path.dirname(sourceFilename), computedPath).split(path.sep).join(path.posix.sep);
p = p[0] === "." ? p : "./" + p;
return p;
};
/**
* Transform the source location to the right location according to build type.
* Used mainly for publishing and generating LTS versions.
* The idea is to convert 'import { Something } from "location/something";' to 'import { Something } from "package/something";'
* @param location the source's location
* @param options the transformer options
* @param sourceFilename the optional source filename
* @returns the new location
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export const transformPackageLocation = (location: string, options: ITransformerOptions, sourceFilename?: string) => {
const directoryParts = location.split("/");
const basePackage = directoryParts[0] === "@" ? `${directoryParts.shift()}/${directoryParts.shift()}` : directoryParts.shift();
if (basePackage === "tslib" && sourceFilename && options.buildType === "es6") {
let computedPath = "./tslib.es6.js";
const result = GetPathForComputed(computedPath, sourceFilename);
if (options.basePackage === "@babylonjs/core") {
storeTsLib();
computedPath = GetRelativePath(result, sourceFilename);
} else {
computedPath = "@babylonjs/core/tslib.es6";
}
return AddJS(computedPath, options.appendJS);
}
if (!basePackage || !isValidDevPackageName(basePackage) || declarationsOnlyPackages.indexOf(basePackage) !== -1) {
return;
}
// local file?
if (basePackage.startsWith(".")) {
return AddJS(location, options.appendJS);
}
const returnPackageVariable: PublicPackageVariable = getDevPackagesByBuildType(options.buildType)[basePackage];
const returnPackage = getPublicPackageName(returnPackageVariable);
// not found? probably an external library. return the same location
if (!returnPackage) {
return location;
}
if (returnPackage === options.basePackage) {
if (options.keepDev) {
return location;
}
let computedPath = "./" + directoryParts.join("/");
if (sourceFilename) {
const result = GetPathForComputed(computedPath, sourceFilename);
computedPath = GetRelativePath(result, sourceFilename);
}
return AddJS(computedPath, options.appendJS);
} else {
if (directoryParts.length === 0) {
// Do not add .js to imports that reference the root of a package
return returnPackage;
}
// For bundled packages, always return just the package name without sub-paths
if (bundledESPackages.indexOf(basePackage) !== -1) {
return returnPackage;
}
return AddJS(options.packageOnly ? returnPackage : `${returnPackage}/${directoryParts.join("/")}`, options.appendJS);
}
};
type TransformerNode = ts.Bundle | ts.SourceFile;
/**
* Options to pass for the transform function
*/
export interface ITransformerOptions {
/**
* can be lts, esm, umd and es6
*/
buildType: BuildType;
/**
* the current package being processed. Whether abstract (core, gui) or concrete (@babylonjs/core, babylonjs and so on)
*/
basePackage: string;
/**
* do not return full path but only the package
*/
packageOnly: boolean;
/**
* Should we append ".js" to the end of the import
* can either be a boolean or the actual extension to add (like ".mjs")
*/
appendJS?: boolean | string;
keepDev?: boolean;
}
// inspired by https://github.com/OniVe/ts-transform-paths
// eslint-disable-next-line @typescript-eslint/naming-convention
export default function transformer(_program: ts.Program, options: ITransformerOptions) {
function optionsFactory<T extends TransformerNode>(context: ts.TransformationContext): ts.Transformer<T> {
return TransformerFactory(context, options);
}
return optionsFactory;
}
function ChainBundle<T extends ts.SourceFile | ts.Bundle>(transformSourceFile: (x: ts.SourceFile) => ts.SourceFile): (x: T) => T {
function transformBundle(node: ts.Bundle) {
return ts.factory.createBundle(node.sourceFiles.map(transformSourceFile));
}
return function transformSourceFileOrBundle(node: T) {
return ts.isSourceFile(node) ? (transformSourceFile(node) as T) : (transformBundle(node) as T);
};
}
function IsImportCall(node: ts.Node): node is ts.CallExpression {
return ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
}
function TransformerFactory<T extends TransformerNode>(context: ts.TransformationContext, options: ITransformerOptions): ts.Transformer<T> {
// const aliasResolver = new AliasResolver(context.getCompilerOptions());
function transformSourceFile(sourceFile: ts.SourceFile) {
function getResolvedPathNode(node: ts.StringLiteral) {
const resolvedPath = transformPackageLocation(/*sourceFile.fileName*/ node.text, options, sourceFile.fileName);
return resolvedPath && resolvedPath !== node.text ? ts.factory.createStringLiteral(resolvedPath) : null;
}
function pathReplacer(node: ts.Node): ts.Node {
if (ts.isStringLiteral(node)) {
return getResolvedPathNode(node) || node;
}
// Skip type literals - they can't contain dynamic imports and their
// get/set accessor signatures cause lexical environment issues in TS 5.9
if (ts.isTypeLiteralNode(node)) {
return node;
}
return ts.visitEachChild(node, pathReplacer, context);
}
function visitor(node: ts.Node): ts.Node {
/**
* e.g.
* - const x = require('path');
* - const x = import('path');
*/
if (IsImportCall(node)) {
return ts.visitEachChild(node, pathReplacer, context);
}
/**
* e.g.
* - type Foo = import('path').Foo;
*/
if (ts.isImportTypeNode(node)) {
return ts.visitEachChild(node, pathReplacer, context);
}
/**
* e.g.
* - import * as x from 'path';
* - import { x } from 'path';
*/
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
return ts.visitEachChild(node, pathReplacer, context);
}
/**
* e.g.
* - export { x } from 'path';
*/
if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
return ts.visitEachChild(node, pathReplacer, context);
}
/**
* e.g.
* - declare module "core/path";
*/
if (ts.isModuleDeclaration(node)) {
return ts.visitEachChild(node, pathReplacer, context);
}
// Skip type literals - they can't contain dynamic imports and their
// get/set accessor signatures cause lexical environment issues in TS 5.9
if (ts.isTypeLiteralNode(node)) {
return node;
}
return ts.visitEachChild(node, visitor, context);
}
return ts.visitEachChild(sourceFile, visitor, context);
}
return ChainBundle(transformSourceFile);
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export const storeTsLib = () => {
const tsLibPath = path.resolve(path.resolve(".", "tslib.es6.js"));
if (!fs.existsSync(tsLibPath)) {
// Read from the installed tslib package instead of using a hardcoded copy,
// so that the helpers stay in sync with the TypeScript version.
const tslibSource = require.resolve("tslib/tslib.es6.mjs");
fs.copyFileSync(tslibSource, tsLibPath);
}
};