Repository navigation
Expand file tree
/
Copy pathplugin.ts
More file actions
167 lines (128 loc) · 5.4 KB
/
Copy pathplugin.ts
File metadata and controls
167 lines (128 loc) · 5.4 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
namespace tsp {
const path = require('path');
const fs = require('fs');
const requireStack: string[] = [];
/* ****************************************************** */
// region: Types
/* ****************************************************** */
export namespace TspPlugin {
export interface CreateOptions {
resolveBaseDir: string
}
export type Kind = 'SourceTransformer' | 'ProgramTransformer'
}
// endregion
/* ****************************************************** */
// region: Helpers
/* ****************************************************** */
// Could likely fail if the plugin is in a symlinked directory or the package's main file is in a
// directory above the package.json – however, I believe that the walking up method used here is the common
// approach, so we'll consider these acceptable edge cases for now.
function getPackagePath(entryFilePath: string): string | undefined {
let currentDir = path.dirname(entryFilePath);
const seenPaths = new Set<string>();
while (currentDir !== path.parse(currentDir).root) {
if (seenPaths.has(currentDir)) return undefined;
seenPaths.add(currentDir);
const potentialPkgPath = path.join(currentDir, 'package.json');
if (fs.existsSync(potentialPkgPath)) return potentialPkgPath;
currentDir = path.resolve(currentDir, '..');
}
return undefined;
}
// endregion
/* ****************************************************** */
// region: TspPlugin
/* ****************************************************** */
export class TspPlugin {
public readonly config: PluginConfig;
public readonly tsConfigPath: string | undefined;
public readonly entryFilePath: string;
public readonly importKey: string;
public readonly packageConfig: PluginPackageConfig | undefined;
public readonly kind: TspPlugin.Kind;
private readonly _createOptions: TspPlugin.CreateOptions;
constructor(config: PluginConfig, createOptions: TspPlugin.CreateOptions) {
this.config = { ...config };
this.validateConfig();
this._createOptions = createOptions;
this.importKey = config.import || 'default';
this.kind = config.transformProgram === true ? 'ProgramTransformer' : 'SourceTransformer';
const { resolveBaseDir } = createOptions;
const configTransformValue = config.transform!;
/* Resolve paths */
this.tsConfigPath = config.tsConfig && path.resolve(resolveBaseDir, config.tsConfig);
const entryFilePath = require.resolve(configTransformValue, { paths: [ resolveBaseDir ] });
this.entryFilePath = entryFilePath;
/* Get module PluginPackageConfig */
let pluginPackageConfig: PluginPackageConfig | undefined;
const modulePackagePath = getPackagePath(entryFilePath);
if (modulePackagePath) {
const modulePkgJsonContent = fs.readFileSync(modulePackagePath, 'utf8');
const modulePkgJson = JSON.parse(modulePkgJsonContent) as { tsp?: PluginPackageConfig };
pluginPackageConfig = modulePkgJson.tsp;
if (pluginPackageConfig === null || typeof pluginPackageConfig !== 'object') pluginPackageConfig = undefined;
}
this.packageConfig = pluginPackageConfig;
}
private validateConfig() {
const { config } = this;
const configTransformValue = config.transform;
if (!configTransformValue) throw new TsPatchError(`Invalid plugin config: missing "transform" value`);
if (config.resolvePathAliases && !config.tsConfig) {
console.warn(`[ts-patch] Warning: resolvePathAliases needs a tsConfig value pointing to a tsconfig.json for transformer" ${configTransformValue}.`);
}
}
createFactory() {
const { entryFilePath, config, tsConfigPath, importKey } = this;
const configTransformValue = config.transform!;
/* Prevent circular require */
if (requireStack.includes(entryFilePath)) return;
requireStack.push(entryFilePath);
const isTs = configTransformValue.match(/\.[mc]?ts$/) != null;
const registerConfig: RegisterConfig = {
isTs,
tsConfig: tsConfigPath,
pluginConfig: config
};
registerPlugin(registerConfig);
try {
/* Load plugin */
const commonjsModule = loadEntryFile();
const factoryModule = (typeof commonjsModule === 'function') ? { default: commonjsModule } : commonjsModule;
const factory = factoryModule[importKey];
if (!factory)
throw new TsPatchError(
`tsconfig.json > plugins: "${configTransformValue}" does not have an export "${importKey}": ` +
require('util').inspect(factoryModule)
);
if (typeof factory !== 'function') {
throw new TsPatchError(
`tsconfig.json > plugins: "${configTransformValue}" export "${importKey}" is not a plugin: ` +
require('util').inspect(factory)
);
}
return {
factory,
registerConfig: registerConfig
};
}
finally {
requireStack.pop();
unregisterPlugin();
}
function loadEntryFile(): PluginFactory | { [key: string]: PluginFactory } {
/* Load plugin */
try {
return require(entryFilePath);
} catch (e) {
const hint = describeEsmInCjsError(e);
if (hint) throw new TsPatchError(hint);
throw e;
}
}
}
}
// endregion
}
// endregion