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

Polyfill node: imports #170

Open
ogonkov opened this issue Dec 20, 2024 · 0 comments
Open

Polyfill node: imports #170

ogonkov opened this issue Dec 20, 2024 · 0 comments

Comments

@ogonkov
Copy link
Contributor

ogonkov commented Dec 20, 2024

In order to polyfill node:path for example we need to use NormalModuleReplacementPlugin (webpack/webpack#13290 (comment))

I think that would be nice for app-builder to map config.fallback, and include polyfills from here

For example we have

{
  fallback: {
   path: require.resolve('path-browserify')
  }
}

My proposal to make mapping to final webpack config like:

if (config.fallback) {
  const polyfills: string[] = [];

  for (const fallback of config.fallback) {
    const polyfill = config.fallback[fallback];

    // We also could check that it is builtin node module polyfill
    if (typeof polyfill === 'string') {
      polyfills.push(polyfill);
    }
  }

  if (polyfills.length) {
    config.plugins.push(new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
      const mod = resource.request.replace(/^node:/, "");

      if (polyfills.includes(mod)) {
        resource.request = config.polyfills[mod];
      } else {
        throw new Error(`Not found ${mod}`);
      }
    }));
  }
}
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