Skip to content

Polyfill node: imports #170

Open
Open

Description

@ogonkov

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}`);
      }
    }));
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions