Path: blob/main/src/babel-plugins/strip-node-prefix.mjs
7085 views
/**1* @license2* Copyright 2026 The Emscripten Authors3* SPDX-License-Identifier: MIT4*/56// A babel plugin to remove the leading `node:` prefix from all imports.78export default function ({ types: t, targets }) {9// Skip this plugin for Node.js >= 1610if (+targets().node.split('.')[0] >= 16) {11return {};12}1314return {15name: 'strip-node-prefix',16visitor: {17// e.g. `import fs from 'node:fs'`18ImportDeclaration({ node }) {19if (node.source.value.startsWith('node:')) {20node.source.value = node.source.value.slice(5);21}22},2324// e.g. `await import('node:fs')`25// Note: only here for reference, it's mangled with EMSCRIPTEN$AWAIT$IMPORT below.26ImportExpression({ node }) {27if (t.isStringLiteral(node.source) && node.source.value.startsWith('node:')) {28node.source.value = node.source.value.slice(5);29}30},3132// e.g. `require('node:fs')` or EMSCRIPTEN$AWAIT$IMPORT('node:fs')33CallExpression({ node }) {34if (35(t.isIdentifier(node.callee, { name: 'require' }) ||36// Special placeholder for `await import`37// FIXME: Remove after PR https://github.com/emscripten-core/emscripten/pull/23730 is landed.38t.isIdentifier(node.callee, { name: 'EMSCRIPTEN$AWAIT$IMPORT' })) &&39t.isStringLiteral(node.arguments[0]) &&40node.arguments[0].value.startsWith('node:')41) {42node.arguments[0].value = node.arguments[0].value.slice(5);43}44},45},46};47}484950