-
Notifications
You must be signed in to change notification settings - Fork 3
/
strategy.system.js
43 lines (33 loc) · 1.32 KB
/
strategy.system.js
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
// This tests the evaluation order of TypeScript's SystemJS transform + SystemJS's top-level await support
import * as fs from 'fs'
import * as path from 'path'
import * as url from 'url'
import ts from 'typescript'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
export async function systemJSStrategy(files) {
let lastName
let js = ''
js += fs.readFileSync(require.resolve('systemjs/dist/system.js'), 'utf8')
js += fs.readFileSync(require.resolve('systemjs/dist/extras/named-register.js'), 'utf8')
for (const name in files) {
lastName = name
// Use TypeScript to transform the JavaScript into SystemJS format
let code = ts.transpileModule(files[name], {
compilerOptions: {
module: ts.ModuleKind.System,
},
}).outputText
// Use named registration since there are multiple modules in the same file
code = code.replace(
'System.register(',
`System.register(${JSON.stringify('./' + name)}, `,
)
js += code
}
js += `await System.import(${JSON.stringify('./' + lastName)}, '.')\n`
return js
}
const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
const packageJSON = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'))
systemJSStrategy.version = `SystemJS ${packageJSON.dependencies.systemjs}`