-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
163 lines (133 loc) · 4.42 KB
/
index.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
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
import { readFile, existsSync } from 'fs';
import { dirname, resolve } from 'path';
import * as esbuild from 'esbuild';
import { promisify } from 'util';
const read = promisify(readFile);
import type { TransformOptions } from 'esbuild';
import type { PreprocessorGroup, Processed } from 'svelte/types/compiler/preprocess';
export type Definitions = {
[find: string]: string;
}
type Allow = Pick<TransformOptions, 'banner'|'charset'|'define'|'footer'|'keepNames'|'pure'|'target'|'treeShaking'|'tsconfigRaw'>;
export interface Options extends Allow {
/** @default 'tsconfig.json' */
tsconfig?: string;
/** @default 'error' */
loglevel?: TransformOptions['logLevel'];
/** @default true */
sourcemap?: boolean | 'inline';
/** @default {} */
define?: Definitions;
/** @default 'utf8' */
charset?: TransformOptions['charset'];
}
interface ProcessorInput {
content: string;
attributes: Record<string, string | boolean>;
filename?: string;
}
type Attributes = Record<string, string | boolean>;
type Negate<U, T> = U extends T ? never : U;
type TSConfig = Negate<TransformOptions['tsconfigRaw'], string> & { extends?: boolean };
// ---
const isExternal = /^(https?:)?\/\//;
const isString = (x: unknown): x is string => typeof x === 'string';
function isTypescript(attrs: Attributes): boolean | void {
if (isString(attrs.lang)) return /^(ts|typescript)$/.test(attrs.lang);
if (isString(attrs.type)) return /^(text|application)[/](ts|typescript)$/.test(attrs.type);
if (isString(attrs.src) && !isExternal.test(attrs.src)) return /\.ts$/.test(attrs.src);
}
function bail(err: Error, ...args: (string|number)[]): never {
console.error('[esbuild]', ...args);
console.error(err.stack || err);
process.exit(1);
}
async function transform(input: ProcessorInput, options: TransformOptions): Promise<Processed> {
let config = options;
let deps: string[] = [];
if (input.filename) {
let src = input.attributes.src;
config = { ...config, sourcefile: input.filename };
if (isString(src) && !isExternal.test(src)) {
src = resolve(dirname(input.filename), src);
if (existsSync(src)) {
input.content = await read(src, 'utf8');
deps.push(src);
} else {
console.warn('[esbuild] Could not find "%s" file', src);
}
}
}
let output = await esbuild.transform(input.content, config);
// TODO: format output.warnings
if (output.warnings.length > 0) {
console.log(output.warnings);
}
return {
code: output.code,
dependencies: deps,
map: output.map,
};
}
/** @note Use `options.define` for replacements */
export function typescript(options: Partial<Options> = {}): PreprocessorGroup {
let { tsconfig, loglevel='error', ...config } = options as Options & TransformOptions;
config = {
charset: 'utf8',
logLevel: loglevel,
sourcemap: true,
...config,
loader: 'ts',
format: 'esm',
minify: false,
logLimit: 0,
};
let contents: TSConfig;
if (config.tsconfigRaw) {
contents = config.tsconfigRaw as TSConfig;
} else {
let file = resolve(tsconfig || 'tsconfig.json');
try {
contents = require(file);
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
return bail(err, 'Error while parsing "tsconfig" file:', file);
}
if (tsconfig) {
return bail(err, 'Unable to load `tsconfig` file:', file);
}
console.warn('[esbuild] Attempted to autoload "tsconfig.json" – failed!');
contents = { extends: true }; // ignore "no compilerOptions" warning
}
}
if (!contents.compilerOptions && !contents.extends) {
console.warn('[esbuild] Missing `compilerOptions` configuration – skip!');
}
let compilerOptions = { ...contents.compilerOptions };
// @see https://github.com/evanw/esbuild/releases/tag/v0.14.0
compilerOptions.importsNotUsedAsValues = 'preserve';
compilerOptions.preserveValueImports = true;
config.tsconfigRaw = { compilerOptions };
const define = config.define;
return {
async script(input) {
let bool = !!isTypescript(input.attributes);
if (!bool && !!define) return transform(input, { define, loader: 'js' });
if (!bool) return { code: input.content };
return transform(input, config);
}
};
}
/** @important Only works with JavaScript! */
export function replace(define: Definitions = {}): PreprocessorGroup {
for (let key in define) {
define[key] = String(define[key]);
}
return {
async script(input) {
let bool = !!isTypescript(input.attributes);
if (bool) return { code: input.content };
return transform(input, { define, loader: 'js' });
}
};
}