mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
8f3b6a245c
Co-authored-by: Daniel Roe <daniel@roe.dev>
34 lines
855 B
JavaScript
34 lines
855 B
JavaScript
import { transformSync } from 'esbuild'
|
|
|
|
// https://jestjs.io/docs/next/code-transformation
|
|
export default {
|
|
process (src, path, _opts) {
|
|
const r = transformSync(src, {
|
|
target: 'node14',
|
|
format: 'cjs',
|
|
sourcefile: path,
|
|
loader: path.endsWith('.ts') ? 'ts' : 'default'
|
|
})
|
|
r.code = r.code.replace(/import(\(.*\))/g, (_, id) => {
|
|
let openBrackets = 0
|
|
|
|
for (let pos = 0; pos < id.length; pos++) {
|
|
const char = id[pos]
|
|
switch (char) {
|
|
case '(':
|
|
openBrackets++
|
|
break
|
|
case ')':
|
|
openBrackets--
|
|
if (!openBrackets) {
|
|
return 'Promise.resolve(require' + id.slice(0, pos) + ')' + id.slice(pos)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return 'Promise.resolve(require' + id + ')'
|
|
})
|
|
return r
|
|
}
|
|
}
|