Skip to content

[Bug]: @babel/plugin-proposal-json-module may output some ESM code (or even call undefined as if it's a function) to CJS outputs #16534

@BinToss

Description

@BinToss

💻

  • Would you like to work on a fix?

How are you using Babel?

@rollup/plugin-babel

Input code

https://babel.dev/repl#?browsers=&build=&builtIns=false&corejs=false&spec=false&loose=false&code_lz=JYWwDg9gTgLgBAcwDYQEYEMkGc4DMoQhwBEyamWA9GRtgHQBWWEAdsXAO7AwAWcA3jACeYAKYAuEk1bEAvgCh5ogB6RYcACajc6AK5J4NCkA&debug=false&forceAllTransforms=false&modules=commonjs&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=unambiguous&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=Node-20.9&version=7.24.6&externalPlugins=%40babel%2Fplugin-proposal-json-modules%407.24.6&assumptions=%7B%7D

Configuration file name

No response

Configuration

{
  "caller": {
    "name": "packemon"
  },
  "comments": true,
  "parserOpts": {
    "sourceType": "unambiguous",
    "strictMode": true
  },
  "plugins": [
    "C:\\Repos\\HaloSPV3\\hce.shared-config\\node_modules\\@babel\\plugin-syntax-import-attributes\\lib\\index.js",
    "@babel/plugin-proposal-json-modules"
  ],
  "presets": [
    [
      "C:\\Repos\\HaloSPV3\\hce.shared-config\\node_modules\\@babel\\preset-typescript\\lib\\index.js",
      {
        "allowDeclareFields": true,
        "onlyRemoveTypeImports": false,
        "optimizeConstEnums": true
      }
    ]
  ],
  "configFile": false,
  "babelrc": false
}

This is incomplete. Packemon conditionally adds more plugins and presets (including @babel/preset-env) before passing the config to rollup. The SHOW_CONFIG_FOR environment variable had no effect.

Current and expected behavior

@babel/plugin-proposal-json-module may output some ESM code to CJS outputs, resulting in build errors.

  • Top-level awaits in CommonJS
  • import.meta in CommonJS

Fiddling with config values eventually caused the top-level await to be removed and the const was moved to the bottom of my imports group (as expected), but a import.meta.resolve(...) replace with undefined(...).

Environment

System:
OS: Windows 11 10.0.22621
Binaries:
Node: 22.2.0 - ~\scoop\apps\nodejs\current\node.EXE
Yarn: 1.22.22 - ~\scoop\apps\nodejs\current\bin\yarn.CMD
npm: 10.8.0 - ~\scoop\apps\nodejs\current\bin\npm.CMD
pnpm: 9.1.2 - ~\scoop\apps\nodejs\current\bin\pnpm.CMD
npmPackages:
@babel/cli: ^7.24.6 => 7.24.6
@babel/core: ^7.24.6 => 7.24.6
@babel/eslint-parser: ^7.24.6 => 7.24.6
@babel/helper-import-to-platform-api: ^7.24.6 => 7.24.6
@babel/plugin-proposal-json-modules: ^7.24.6 => 7.24.6
@babel/plugin-syntax-import-attributes: ^7.24.6 => 7.24.6
eslint: ^8.57.0 => 8.57.0


Additionally, packemon's dependencies (

"node_modules/packemon": {
  "version": "4.1.1",
  "resolved": "https://registry.npmjs.org/packemon/-/packemon-4.1.1.tgz",
  "integrity": "sha512-HfT+CBM0LS+hMUS8ncvmtEz2G378AJuyBLnXWbJ0NxfkKXwKXEjHPVX64XoTDIhOupUYIiU5nH2LdOD1bi4ulw==",
  "dev": true,
  "license": "MIT",
  "dependencies": {
    "@babel/core": "^7.24.6",
    "@babel/plugin-external-helpers": "^7.24.6",
    "@babel/plugin-proposal-decorators": "^7.24.6",
    "@babel/plugin-proposal-json-modules": "^7.24.6",
    "@babel/plugin-syntax-import-attributes": "^7.24.6",
    "@babel/plugin-transform-runtime": "^7.24.6",
    "@babel/preset-env": "^7.24.6",
    "@babel/preset-flow": "^7.24.6",
    "@babel/preset-react": "^7.24.6",
    "@babel/preset-typescript": "^7.24.6",
    "@boost/cli": "^5.0.0",
    "@boost/common": "^5.0.0",
    "@boost/config": "^5.0.0",
    "@boost/debug": "^5.0.0",
    "@boost/event": "^5.0.0",
    "@boost/pipeline": "^5.0.0",
    "@boost/terminal": "^5.0.0",
    "@rollup/plugin-babel": "^6.0.4",
    "@rollup/plugin-commonjs": "^25.0.8",
    "@rollup/plugin-json": "^6.1.0",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "@swc/core": "^1.5.7",
    "@swc/helpers": "^0.5.11",
    "babel-plugin-cjs-esm-interop": "^4.0.2",
    "babel-plugin-conditional-invariant": "^4.0.1",
    "babel-plugin-env-constants": "^4.0.1",
    "babel-preset-solid": "^1.8.17",
    "browserslist": "^4.23.0",
    "debug": "^4.3.4",
    "execa": "^9.1.0",
    "fast-glob": "^3.3.2",
    "filesize": "^10.1.2",
    "ink": "^5.0.0",
    "ink-progress-bar": "^3.0.0",
    "ink-spinner": "^5.0.0",
    "magic-string": "^0.30.10",
    "micromatch": "^4.0.7",
    "react": "^18.3.1",
    "resolve": "^1.22.8",
    "rollup": "^4.18.0",
    "rollup-plugin-node-externals": "^7.1.2",
    "rollup-plugin-polyfill-node": "^0.13.0",
    "semver": "^7.6.2",
    "spdx-license-list": "^6.9.0"
  },

Possible solution

No response

Additional context

This issue was previously noted and investigated at milesj/packemon#251

Additionally, assigning a version string to node with a patch version will result in unexpected behavior.

import globals from "globals/globals.json" with {type: "json"}
const globals = JSON.parse(node_fs.readFileSync(new URL(undefined("globals/globals.json"))));
// TypeError: undefined is not a function

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions