liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

How to alias externaled module path? #66

Open ycjcl868 opened 5 years ago

ycjcl868 commented 5 years ago

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 commented 5 years ago

@liady

Pines-Cheng commented 4 years ago

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

ycjcl868 commented 4 years ago

SSR bundle all not set alias in umi 3.2.0

liady commented 4 years ago

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...

daniel-nagy commented 3 years ago

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.