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

Proload doesn't work with esbuild as an external package. #31

Open
Alexandre-Fernandez opened this issue Nov 2, 2022 · 0 comments
Open

Comments

@Alexandre-Fernandez
Copy link

Alexandre-Fernandez commented Nov 2, 2022

When setting proload to external in esbuild, it fails to load.

Reproduction :

  • git clone https://github.com/Alexandre-Fernandez/esbuild-proload &&
  • cd esbuild-proload &&
  • npm i &&
  • npm run debug

Reason (evanw/esbuild#2650 (comment)) :

This is because the CJS wrapper in @proload/core does not re-export the same object (the load function) in ESM module.

// @proload/core/lib/cjs/index.cjs
function load(...args) {
    return import('../esm/index.mjs').then(({ default: loader }) => loader(...args));
    //                                                 ^ the real load function in ESM
}

The real one (in ESM module) does have the load.use() method, while the CJS one does not implement corresponding wrapper.

You can compile your script to ESM format with format: 'esm', which should work. (Don't forget to change the extension to .mjs for node.js to run correctly).

As for the package author, they can implement such wrapper instead,

// @proload/core/lib/cjs/index.cjs
let _use_plugins
async function load(...args) {
  const mod = await import('../esm/index.mjs')
  if (_use_plugins) {
    mod.default.use(_use_plugins)
    _use_plugins = undefined
  }
  return (0, mod.default)(...args)
}
load.use = function use(plugins) {
  (_use_plugins ||= []).push(...plugins)
  load.plugins.push(...plugins) // XXX not the same array in ESM!
}
// caveat: cannot wrap `load.plugins` correctly because it is sync
load.plugins = []
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

1 participant