-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathregister-runtime-tuning.spec.ts
More file actions
161 lines (130 loc) · 4.22 KB
/
register-runtime-tuning.spec.ts
File metadata and controls
161 lines (130 loc) · 4.22 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
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
import test from 'ava'
import sinon from 'sinon'
import * as ts from 'typescript'
import * as swcCore from '@swc-node/core'
import { SourcemapMap } from '@swc-node/sourcemap-support'
import { compile } from '../register'
import { clearTransformCache } from '../transform-cache'
const originalEnv = { ...process.env }
const emptyMap = '{"version":3,"sources":[],"names":[],"mappings":""}'
function uniquePath(label: string, extension: string) {
return `/tmp/${label}-${Date.now()}-${Math.random().toString(16).slice(2)}.${extension}`
}
test.beforeEach(() => {
process.env.SWC_NODE_CACHE_DIR = `/tmp/swc-node-test-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
})
test.afterEach.always(() => {
sinon.restore()
SourcemapMap.clear()
clearTransformCache({ memory: true, disk: false })
process.env = { ...originalEnv }
})
test.serial('reuses transform cache for sync compile', (t) => {
const transformSyncStub = sinon.stub(swcCore, 'transformSync').returns({
code: 'console.log("cached")',
map: emptyMap,
})
const filename = uniquePath('cache-sync', 'ts')
const source = `const value: number = ${Date.now()}`
const first = compile(source, filename, {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
const second = compile(source, filename, {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
t.is(first, second)
t.is(transformSyncStub.callCount, 1)
})
test.serial('reuses transform cache for async compile', async (t) => {
const transformStub = sinon.stub(swcCore, 'transform').resolves({
code: 'console.log("cached-async")',
map: emptyMap,
})
const filename = uniquePath('cache-async', 'ts')
const source = `const value: number = ${Date.now()}`
const first = await compile(
source,
filename,
{
module: ts.ModuleKind.ESNext,
sourceMap: true,
},
true,
)
const second = await compile(
source,
filename,
{
module: ts.ModuleKind.ESNext,
sourceMap: true,
},
true,
)
t.is(first, second)
t.true(transformStub.callCount <= 1)
})
test.serial('supports sourcemap inline-only mode to reduce map-store memory', (t) => {
process.env.SWC_NODE_SOURCE_MAP_MODE = 'inline'
sinon.stub(swcCore, 'transformSync').returns({
code: 'console.log("inline-only")',
map: emptyMap,
})
const filename = uniquePath('sourcemap-inline', 'ts')
const output = compile('const x = 1', filename, {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
t.true(output.includes('sourceMappingURL'))
t.false(SourcemapMap.has(filename))
})
test.serial('supports sourcemap store-only mode to avoid inline map payload', (t) => {
process.env.SWC_NODE_SOURCE_MAP_MODE = 'store'
sinon.stub(swcCore, 'transformSync').returns({
code: 'console.log("store-only")',
map: emptyMap,
})
const filename = uniquePath('sourcemap-store', 'ts')
const output = compile('const x = 1', filename, {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
t.false(output.includes('sourceMappingURL'))
t.true(SourcemapMap.has(filename))
})
test.serial('skips transform for plain js in commonjs mode', (t) => {
const transformSyncStub = sinon.stub(swcCore, 'transformSync')
const output = compile('module.exports = 42', uniquePath('plain-cjs', 'js'), {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
t.is(output, 'module.exports = 42')
t.false(transformSyncStub.called)
})
test.serial('still transforms js with esm syntax in commonjs mode', (t) => {
const transformSyncStub = sinon.stub(swcCore, 'transformSync').returns({
code: 'exports.value = 42',
map: emptyMap,
})
const output = compile('export const value = 42', uniquePath('esm-in-js', 'js'), {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})
t.true(output.includes('exports.value = 42'))
t.true(transformSyncStub.calledOnce)
})
test.serial('skips transform for runtime js in esm mode', async (t) => {
const transformStub = sinon.stub(swcCore, 'transform')
const output = await compile(
'export const value = 42',
uniquePath('plain-esm', 'mjs'),
{
module: ts.ModuleKind.ESNext,
sourceMap: true,
},
true,
)
t.is(output, 'export const value = 42')
t.false(transformStub.called)
})