Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip node: prefix when converting to CJS and target < node14.18 #1760

Closed
mattfysh opened this issue Nov 9, 2021 · 3 comments
Closed

Strip node: prefix when converting to CJS and target < node14.18 #1760

mattfysh opened this issue Nov 9, 2021 · 3 comments

Comments

@mattfysh
Copy link

mattfysh commented Nov 9, 2021

A recent release of node-fetch exposed an issue where node:* prefix imports compiled into require() calls are not supported if < node v14.18

node-fetch/node-fetch#1367

Is there a way to check the compile target, and if the target is CJS without node:* prefix support, strip away the prefix?

Thanks Evan! :)

@mattfysh
Copy link
Author

mattfysh commented Nov 9, 2021

@hyrious
Copy link

hyrious commented Nov 10, 2021

Simple redirect and mark it as external in onResolve callback:

var stripNodeColonPlugin = {
  name: 'strip-node-colon',
  setup({ onResolve, onLoad }) {
    onResolve({ filter: /^node:/ }, args => {
      return { path: args.path.slice('node:'.length), external: true }
    })
  }
}

@jimmywarting
Copy link

jimmywarting commented Nov 10, 2021

i was just thinking... what if you transpile:

import fs from 'node:fs'
export const foo = 'bar'

into something like:

// main.cjs

(async () => {
  const { default: fs } = await import('node:fs')
  return { foo: 'bar' }
})()

Everything about ESM is async from the ground up. All imports, top-level await.
This is valid cjs. And cjs can load esm modules too, this node prefix works from 12.20 as long as you use dynamic import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants