Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/docs/rspack/node-externals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default {
};
```

### Option `importType`

Tells Rspack how to import external dependency. By default it is `module`.

### Option `allow`

To force add some dependencies to bundle you can use `allow` option:
Expand All @@ -42,4 +46,6 @@ nodeExternals({
});
```

By default it contains `css-loader` because it injects some code into runtime.
By default `css-loader` and `*.css` files allowed.
Files from `css-loader` allowed because it injects some code into runtime.
CSS files allowed to easy use styles from libraries like `overlay-scrollbars` or `@krutoo/showcase`.
5 changes: 1 addition & 4 deletions docs/rspack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export default [
module: true,
},
devtool: isProd ? false : undefined,
externals: utils.nodeExternals({
importType: 'module',
allow: [/css-loader/, /\.css$/i],
}),
externals: utils.nodeExternals(),
externalsPresets: {
node: true,
},
Expand Down
28 changes: 20 additions & 8 deletions src/rspack/node-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,36 @@ export type AllowlistItemFunction = (request: string) => boolean;
export type AllowlistItem = string | RegExp | AllowlistItemFunction;

export interface NodeExternalsOptions {
/** Method of importing requested module. */
/**
* Method of importing requested module.
* @default `module`
*/
importType?: ExternalsType;

/** Defines modules that should be included to bundle. */
/**
* Defines modules that should be included to bundle.
* By default css-loader and `*.css` files allowed.
*/
allow?: AllowlistItem | AllowlistItem[];
}

const NODE_EXTERNALS_DEFAULT_ALLOW: AllowlistItem[] = [
// `css-loader` allowed by default to prevent errors about css-loader' runtime code was not found
/css-loader/,

// css files allowed by default for using files from libraries like `overlay-scrollbars` or `@krutoo/showcase`
/\.css$/,
];

/**
* Simple analogue of `webpack-node-externals`.
* Marks all files from node_modules as externals.
* Simple modern analogue of `webpack-node-externals`.
* Marks all files from `node_modules` as externals.
* @param options Options.
* @returns Externals function.
*/
export function nodeExternals({
importType = 'commonjs',

// css-loader allowed by default to prevent errors about css-loader' runtime code was not found
allow = [/css-loader/],
importType = 'module',
allow = NODE_EXTERNALS_DEFAULT_ALLOW,
}: NodeExternalsOptions = {}): ExternalItem {
const allowPredicates = allowToPredicates(allow);

Expand Down
Loading