Skip to content

Polyfill node: imports #170

@ogonkov

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

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