-
Notifications
You must be signed in to change notification settings - Fork 170
/
mod.ts
184 lines (166 loc) · 4.66 KB
/
mod.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { join } from 'https://deno.land/std@0.100.0/path/mod.ts'
import { ensureDir } from 'https://deno.land/std@0.100.0/fs/ensure_dir.ts'
import { existsFile } from '../shared/fs.ts'
import { Measure } from '../shared/log.ts'
import type { ImportMap } from '../types.d.ts'
import { getDenoDir } from '../server/helper.ts'
import { checksum } from './dist/checksum.js'
import init, { parseExportNamesSync, stripSsrCodeSync, transformSync } from './dist/compiler.js'
export enum SourceType {
JS = 'js',
JSX = 'jsx',
TS = 'ts',
TSX = 'tsx',
CSS = 'css',
Unknown = '??',
}
export type SWCOptions = {
sourceType?: SourceType
jsxFactory?: string
jsxFragmentFactory?: string
}
export type ReactOptions = {
version: string,
esmShBuildVersion: number
}
export type TransformOptions = {
swcOptions?: SWCOptions
workingDir?: string
alephPkgUri?: string
importMap?: ImportMap
react?: ReactOptions
sourceMap?: boolean
isDev?: boolean
httpExternal?: boolean
bundleMode?: boolean
bundleExternals?: string[]
inlineStylePreprocess?(key: string, type: string, tpl: string): Promise<string>
}
export type TransformResult = {
code: string
deps?: DependencyDescriptor[]
ssrPropsFn?: string
ssgPathsFn?: boolean
denoHooks?: string[]
starExports?: string[]
jsxStaticClassNames?: string[]
map?: string
}
type DependencyDescriptor = {
specifier: string
resolved: string
isDynamic: boolean
}
type InlineStyle = {
type: string,
quasis: string[],
exprs: string[]
}
type InlineStyles = Record<string, InlineStyle>
let wasmReady: Promise<void> | boolean = false
async function initWasm() {
const cacheDir = join(await getDenoDir(), `deps/https/deno.land/aleph`)
const cachePath = `${cacheDir}/compiler.${checksum}.wasm`
if (await existsFile(cachePath)) {
const wasmData = await Deno.readFile(cachePath)
await init(wasmData)
} else {
const { default: getWasmData } = await import('./dist/wasm.js')
const wasmData = getWasmData()
await init(wasmData)
await ensureDir(cacheDir)
await Deno.writeFile(cachePath, wasmData)
}
}
async function checkWasmReady() {
let ms: Measure | null = null
if (wasmReady === false) {
ms = new Measure()
wasmReady = initWasm()
}
if (wasmReady instanceof Promise) {
await wasmReady
wasmReady = true
}
if (ms !== null) {
ms.stop('init compiler wasm')
}
}
/**
* transform module by swc.
*
* ```tsx
* transform(
* '/app.tsx',
* `
* export default App() {
* return <h1>Hello World</h1>
* }
* `
* )
* ```
*/
export async function transform(specifier: string, code: string, options: TransformOptions = {}): Promise<TransformResult> {
await checkWasmReady()
const { inlineStylePreprocess, ...transformOptions } = options
let {
code: jsContent,
deps,
ssrPropsFn,
ssgPathsFn,
inlineStyles,
denoHooks,
starExports,
jsxStaticClassNames,
map,
} = transformSync(specifier, code, transformOptions)
// resolve inline-style
if (inlineStyles) {
await Promise.all(Object.entries(inlineStyles as InlineStyles).map(async ([key, style]) => {
let tpl = style.quasis.reduce((tpl, quais, i, a) => {
tpl += quais
if (i < a.length - 1) {
tpl += `%%aleph-inline-style-expr-${i}%%`
}
return tpl
}, '')
.replace(/\:\s*%%aleph-inline-style-expr-(\d+)%%/g, (_, id) => `: var(--aleph-inline-style-expr-${id})`)
.replace(/%%aleph-inline-style-expr-(\d+)%%/g, (_, id) => `/*%%aleph-inline-style-expr-${id}%%*/`)
if (inlineStylePreprocess !== undefined) {
tpl = await inlineStylePreprocess('#' + key, style.type, tpl)
}
tpl = tpl.replace(
/\:\s*var\(--aleph-inline-style-expr-(\d+)\)/g,
(_, id) => ': ${' + style.exprs[parseInt(id)] + '}'
).replace(
/\/\*%%aleph-inline-style-expr-(\d+)%%\*\//g,
(_, id) => '${' + style.exprs[parseInt(id)] + '}'
)
jsContent = jsContent.replace(`"%%${key}-placeholder%%"`, '`' + tpl + '`')
}))
}
return {
code: jsContent,
deps,
ssrPropsFn,
ssgPathsFn,
denoHooks,
starExports,
jsxStaticClassNames,
map
}
}
/* strip SSR code. */
export async function stripSsrCode(specifier: string, code: string, options: TransformOptions = {}): Promise<TransformResult> {
await checkWasmReady()
return stripSsrCodeSync(specifier, code, options)
}
/* parse export names of the module. */
export async function parseExportNames(specifier: string, code: string, options: SWCOptions = {}): Promise<string[]> {
await checkWasmReady()
return parseExportNamesSync(specifier, code, options)
}
/**
* The wasm checksum.
*/
export const wasmChecksum = checksum