-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswc.js
More file actions
113 lines (106 loc) · 3.34 KB
/
swc.js
File metadata and controls
113 lines (106 loc) · 3.34 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
/*
- (-) its minifier does not make local names shorter.
- (-) external/remote resources are not supported. use dynamic imports to load them.
- (-) implicitly-imported JSX is not supported.
*/
let readLocalFile = (path) => {
return require('fs').openFile(path).readAll();
};
let readRemoteFile = (path) => {
return fetch(`https://esm.sh/gh/denoland/deno_emit@deno_registry/${path}?raw`).body.readAll();
};
let url = new URL('./deno_emit/emit.generated.js');
let source = readLocalFile(url).toString();
let transform = (source) => {
source = source.replaceAll('import.meta.url', `"${url.href}"`);
source = source.replace(/#(?=[a-zA-Z])/g, '_');
source += `WasmBuildLoader.prototype._instantiate = async function(url, decompress) {
const {imports, cache} = this._options;
return WebAssembly.instantiate(readFile(url), imports);
// return WebAssembly.instantiateStreaming(wasmResponse, imports);
};`;
let exports = [];
source = source.replace(/export\s+(?=(?:async\s+)?function(?:\s+\*)?\s+([^(\s]+)|(?:let|const)\s+([^=\s]+))/g, (all, functionName, variableName) => {
exports.push(functionName ?? variableName);
return ``;
});
source += `return {${exports}};`;
return source;
};
let start = async() => {
let emit = Function('readFile', transform(source))(readLocalFile);
let {instantiate, isInstantiated, bundle, transpile} = emit;
await instantiate({url: './deno_emit/emit_bg.wasm'});
let loader = (specifier, {isDynamic, cacheSetting, checksum}) => {
// cacheSetting: https://docs.rs/deno_emit/latest/deno_emit/enum.CacheSetting.html#variants
console.log(specifier);
if (0 && specifier.endsWith('runtime')) {
return {
kind: 'external',
specifier: specifier,
};
}
if (specifier.endsWith('a module without extension')) {
return {
kind: 'redirect',
specifier: specifier + '.js',
};
}
return {
kind: 'module',
specifier: specifier + (specifier.includes('.') ? '' : '.tsx'),
content: [
...Array.from(new TextEncoder().encode(`import '${compilerOptions.jsxImportSource}/jsx-runtime';`)),
...Array.from(readLocalFile(specifier.replace('file:///', '') + (specifier.includes('.') ? '' : '.ts')))
],
};
};
let processImportMap = (imports) => {
if (imports) {
return {
baseUrl: 'file:///' + process.cwd() + '/',
jsonString: JSON.stringify({
imports,
scopes: {}
}),
}
}
return undefined;
};
let compilerOptions = {
checkJs: true,
experimentalDecorators: false,
emitDecoratorMetadata: false,
// importsNotUsedAsValues: string, // one of "remove", "preserve", "error"
// inlineSourceMap: true,
// inlineSources: true,
sourceMap: true,
jsx: "react-jsx", // one of "preserve", "react-jsx", "react-jsxdev", "precompile", "react"
jsxImportSource: "fre", // implicitly import "BASE/jsx-runtime"
// jsxFactory: "React.createElement",
// jsxFragmentFactory: "Fragment",
};
let importMap = processImportMap({
'fre/jsx-runtime': './jsx-runtime.ts',
});
let root = 'file:///C:/Users/Mahdi/Desktop/main.tsx';
console.log(
await transpile(
root,
loader,
importMap, // maybe_import_map
compilerOptions, // maybe_compiler_options
)
);
console.log(
await bundle(
root,
loader,
'classic', // maybe_bundle_type; one of "module", "classic"
importMap, // maybe_import_map
compilerOptions, // maybe_compiler_options
true, // minify
)
);
};
start();