Skip to content

Commit ccb3318

Browse files
committed
transformer identify jsx
1 parent 107eb82 commit ccb3318

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

subsystems/sidecar/lib/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ module.exports = class Http extends ReadyResource {
162162
res.end(out)
163163
} else {
164164
if (protocol === 'app' && (link.filename.endsWith('.html') || link.filename.endsWith('.htm'))) {
165-
const mods = await linker.warmup(link.filename)
165+
const mods = await linker.warmup(link.filename, { identify: transformer.identify.bind(transformer) })
166166
const batch = []
167167
for (const [filename, mod] of mods) {
168168
if (mod.type === 'module') continue

transformer.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = class Transformer extends ReadyResource {
1414
worker = null
1515
pipe = null
1616
stream = null
17+
transforms = null
1718

1819
constructor (app, link, args) {
1920
super()
@@ -22,6 +23,7 @@ module.exports = class Transformer extends ReadyResource {
2223
this.app = app
2324
this.link = link
2425
this.args = args
26+
this.transforms = app.state?.transforms
2527

2628
app.transformer = this
2729
}
@@ -56,7 +58,7 @@ module.exports = class Transformer extends ReadyResource {
5658

5759
async transform (buffer, filename) {
5860
const transforms = []
59-
const patterns = this.app.state?.transforms
61+
const patterns = this.transforms
6062
for (const ptn in patterns) {
6163
const isMatch = picomatch(ptn)
6264
if (isMatch(filename)) {
@@ -124,6 +126,31 @@ module.exports = class Transformer extends ReadyResource {
124126
return b.toBuffer()
125127
}
126128

129+
identify (doubles, singles) {
130+
const patterns = this.transforms
131+
const jsx = Object.keys(patterns).some(pattern => pattern.includes('jsx'))
132+
const double = jsx ? /\.(m|c)?jsx?"$/ : /\.(m|c)?js"$/
133+
const single = jsx ? /\.(m|c)?jsx?'$/ : /\.(m|c)?js'$/
134+
135+
const entries = []
136+
if (doubles) {
137+
for (const s of doubles) {
138+
if (double.test(s)) {
139+
entries.push(s.slice(1, -1))
140+
}
141+
}
142+
}
143+
144+
if (singles) {
145+
for (const s of singles) {
146+
if (single.test(s)) {
147+
entries.push(s.slice(1, -1))
148+
}
149+
}
150+
}
151+
return entries
152+
}
153+
127154
static validate (transforms) {
128155
if (!transforms) return null
129156

0 commit comments

Comments
 (0)