-
-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathbabel-hook.js
More file actions
60 lines (55 loc) · 2.05 KB
/
Copy pathbabel-hook.js
File metadata and controls
60 lines (55 loc) · 2.05 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
// Synchronous replacement for @babel/register.
//
// @babel/register@8 compiles in a Node worker thread, but Electron renderer
// processes cannot create Node workers ("The V8 platform used by this instance
// of Node does not support creating Workers"), and poi installs the require
// hook in renderers too (views/env.ts, assets/js/plugin-preload.js). This hook
// does the same job in-process: pirates + @babel/core transformSync, with
// source-map-support wired up for readable stack traces.
//
// Accepts the options object from babel-register.config.js.
const sourceMapSupport = require('@cspotcode/source-map-support')
const { addHook } = require('pirates')
const sourceMaps = new Map()
let installed = false
module.exports = ({ configFile, babelrc, extensions, only }) => {
if (installed) return
installed = true
const { transformSync } = require('@babel/core')
sourceMapSupport.install({
handleUncaughtExceptions: false,
environment: 'node',
retrieveSourceMap: (source) =>
sourceMaps.has(source) ? { url: null, map: sourceMaps.get(source) } : null,
})
addHook(
(code, filename) => {
const result = transformSync(code, {
filename,
configFile,
babelrc,
sourceMaps: 'both',
caller: {
name: 'poi-babel-hook',
// poi keeps import() native so ESM-only packages stay loadable
// (see the transform-dynamic-import handling in babel.config.js).
supportsDynamicImport: true,
supportsStaticESM: false,
supportsExportNamespaceFrom: false,
supportsTopLevelAwait: false,
},
})
if (!result) return code
if (result.map) sourceMaps.set(filename, result.map)
return result.code
},
{
exts: extensions,
// Third-party plugins live in a node_modules directory outside poi's
// root (ignore: [] in the old @babel/register options), so node_modules
// must not be excluded here.
ignoreNodeModules: false,
matcher: (filename) => only.some((re) => re.test(filename)),
},
)
}