-
Notifications
You must be signed in to change notification settings - Fork 3
/
etsc-jsext.cjs
133 lines (108 loc) · 3.23 KB
/
etsc-jsext.cjs
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
const fs = require('node:fs');
/**
* @typedef {Object} JsExtPluginOptions
* @property {boolean} verbose
* @property {'load' | 'resolve'} method
*/
/**
* @param {import('esbuild').OnLoadArgs} args
* @param {JsExtPluginOptions} opts
* @return {Promise<string>}
*/
const transformImports = async (args, opts) => {
let changed = false;
let contents = await fs.promises.readFile(args.path, 'utf8');
for (const match of Array.from(contents.matchAll(/^\s*import.*\s(["'])(.*)\1\s*;?\s*$/gmi))) {
const importStr = match[0];
const quoteType = match[1];
const importName = match[2];
let transformed = importStr;
if (importName.startsWith('.') && !importName.endsWith('.js') && !importName.endsWith('.vue')) {
let newImportName = importName;
if (/\.ts$/i.test(newImportName))
newImportName = newImportName.slice(0, -3);
if (/\.(mts|cts|tsx)$/i.test(newImportName))
newImportName = newImportName.slice(0, -4);
newImportName += '.js';
transformed = transformed.replace(
`${quoteType}${importName}${quoteType}`,
`${quoteType}${newImportName}${quoteType}`
);
}
if (importStr === transformed) {
continue
}
changed = true;
contents = contents.replace(importStr, transformed)
if (opts.verbose) {
console.debug(`[js-ext] ${importStr} -> ${transformed}`);
}
}
if (changed && opts.verbose) {
console.debug(`[js-ext] in ${args.path}\n`);
}
return contents
};
/**
* @param {import('esbuild').OnResolveArgs} args
* @returns {import('esbuild').OnResolveResult|undefined}
*/
async function handleResolve(args) {
if (args.kind !== 'import-statement' && args.kind !== 'dynamic-import') {
return;
}
if (args.importer && args.path.startsWith('.')) {
const pathAlreadyHasExt = args.path.endsWith('.js');
let newImportName = args.path;
if (/\.ts$/i.test(newImportName))
newImportName = newImportName.slice(0, -3);
if (/\.(mts|cts|tsx)$/i.test(newImportName))
newImportName = newImportName.slice(0, -4);
newImportName += '.js';
if (!pathAlreadyHasExt) {
return {
path: newImportName,
external: true,
namespace: undefined
};
}
}
return {
path: args.path,
external: true,
namespace: undefined,
};
}
/**
* @param {JsExtPluginOptions} opts
* @return {import('esbuild').Plugin}
*/
const JsExtPlugin = (opts = {}) => {
return {
name: 'esbuild-jsext',
setup(_build) {
// noinspection JSValidateTypes
/** @type {import('esbuild').PluginBuild} */
const build = _build;
if (opts.verbose) {
console.debug('[js-ext] setup');
}
if (opts.method === 'load') {
build.onLoad({ filter: /\.(ts|tsx)$/ }, async (args) => {
let contents;
try {
contents = await transformImports(args, opts);
} catch (error) {
console.error(error);
return null;
}
return { contents, loader: args.path.toLowerCase().endsWith('.tsx') ? 'tsx' : 'ts' };
});
}
if (opts.method === 'resolve') {
build.onResolve({ filter: /.*/ }, async (args) => await handleResolve(args));
}
}
};
};
module.exports = JsExtPlugin;