Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/babel-plugins/strip-node-prefix.mjs
7085 views
1
/**
2
* @license
3
* Copyright 2026 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
// A babel plugin to remove the leading `node:` prefix from all imports.
8
9
export default function ({ types: t, targets }) {
10
// Skip this plugin for Node.js >= 16
11
if (+targets().node.split('.')[0] >= 16) {
12
return {};
13
}
14
15
return {
16
name: 'strip-node-prefix',
17
visitor: {
18
// e.g. `import fs from 'node:fs'`
19
ImportDeclaration({ node }) {
20
if (node.source.value.startsWith('node:')) {
21
node.source.value = node.source.value.slice(5);
22
}
23
},
24
25
// e.g. `await import('node:fs')`
26
// Note: only here for reference, it's mangled with EMSCRIPTEN$AWAIT$IMPORT below.
27
ImportExpression({ node }) {
28
if (t.isStringLiteral(node.source) && node.source.value.startsWith('node:')) {
29
node.source.value = node.source.value.slice(5);
30
}
31
},
32
33
// e.g. `require('node:fs')` or EMSCRIPTEN$AWAIT$IMPORT('node:fs')
34
CallExpression({ node }) {
35
if (
36
(t.isIdentifier(node.callee, { name: 'require' }) ||
37
// Special placeholder for `await import`
38
// FIXME: Remove after PR https://github.com/emscripten-core/emscripten/pull/23730 is landed.
39
t.isIdentifier(node.callee, { name: 'EMSCRIPTEN$AWAIT$IMPORT' })) &&
40
t.isStringLiteral(node.arguments[0]) &&
41
node.arguments[0].value.startsWith('node:')
42
) {
43
node.arguments[0].value = node.arguments[0].value.slice(5);
44
}
45
},
46
},
47
};
48
}
49
50