webpack-node-externals icon indicating copy to clipboard operation
webpack-node-externals copied to clipboard

How to alias externaled module path?

Open ycjcl868 opened this issue 6 years ago • 5 comments
trafficstars

There's a problem for me to do SSR building, it's our framework export alias module, and use resolve.alias to alias the module , like React, we use

import React from 'bar/lib/sdk/react';

// not use
import React from 'react';

so now, I only add bar/lib/sdk/react into whiteList, but it's unconscionable to bundle all React library.

How could I solve the problem?

ycjcl868 avatar Jul 10 '19 05:07 ycjcl868

@liady

ycjcl868 avatar Jul 10 '19 06:07 ycjcl868

@ycjcl868 same question, looks like that you had avoided this by giving up external。

Pines-Cheng avatar May 29 '20 02:05 Pines-Cheng

SSR bundle all not set alias in umi 3.2.0

ycjcl868 avatar May 29 '20 03:05 ycjcl868

There's a problem for me to do SSR building, it's our framework export alias module, and use resolve.alias to alias the module , like React, we use

import React from 'bar/lib/sdk/react';

// not use
import React from 'react';

so now, I only add bar/lib/sdk/react into whiteList, but it's unconscionable to bundle all React library.

How could I solve the problem?

@ycjcl868 just to make sure i understood - do you need to bundle bar/lib/sdk/react? Since this is what adding it to allowlist will do...

liady avatar Jul 12 '20 19:07 liady

I think they had a resolve alias for react but Webpack was requiring react as an external dependency instead of their aliased module. I just ran into a similar issue with lodash-es, which doesn't work in a node runtime. I tried to rewrite the import to lodash

resolve: {
  alias: {
    "lodash-es": "lodash"
  }
}

but Webpack was still requiring lodash-es. I was able to work around it with the following.

externals: [
  nodeExternals({ allowlist: ["lodash-es"] }),
  { "lodash-es": "commonjs lodash" }
]

Note that the resolve alias is not necessary in this case. This seems to be an issue with Webpack but it might be nice to add an API to this module to rewrite external modules.

daniel-nagy avatar Jan 18 '21 03:01 daniel-nagy