Skip to content

Commit

Permalink
fix(web): support custom webpack config written in TypeScript for web…
Browse files Browse the repository at this point in the history
…:build (#8939)
  • Loading branch information
owen26 committed Feb 11, 2022
1 parent 48117ac commit 3c25a1c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
14 changes: 1 addition & 13 deletions packages/web/src/executors/dev-server/dev-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import { getEmittedFiles, runWebpackDevServer } from '../../utils/run-webpack';
import { tsNodeRegister } from '../../utils/webpack/tsNodeRegister';
import { resolveCustomWebpackConfig } from '../../utils/webpack/custom-webpack';

export interface WebDevServerOptions {
host: string;
Expand Down Expand Up @@ -128,15 +128,3 @@ function getBuildOptions(
...overrides,
};
}

function resolveCustomWebpackConfig(path: string, tsConfig: string) {
tsNodeRegister(path, tsConfig);

const customWebpackConfig = require(path);
// If the user provides a configuration in TS file
// then there are 2 cases for exporing an object. The first one is:
// `module.exports = { ... }`. And the second one is:
// `export default { ... }`. The ESM format is compiled into:
// `{ default: { ... } }`
return customWebpackConfig.default || customWebpackConfig;
}
23 changes: 15 additions & 8 deletions packages/web/src/executors/webpack/webpack.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CrossOriginValue,
writeIndexHtml,
} from '../../utils/webpack/write-index-html';
import { resolveCustomWebpackConfig } from '../../utils/webpack/custom-webpack';

export interface WebWebpackExecutorOptions extends BuildBuilderOptions {
index: string;
Expand Down Expand Up @@ -106,14 +107,20 @@ function getWebpackConfigs(
: undefined,
]
.filter(Boolean)
.map((config) =>
options.webpackConfig
? require(options.webpackConfig)(config, {
options,
configuration: context.configurationName,
})
: config
);
.map((config) => {
if (options.webpackConfig) {
const customWebpack = resolveCustomWebpackConfig(
options.webpackConfig,
options.tsConfig
);
return customWebpack(config, {
options,
configuration: context.configurationName,
});
} else {
return config;
}
});
}

export async function* run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ export function tsNodeRegister(file: string = '', tsConfig?: string) {
tsconfigPaths.register({ baseUrl, paths });
}
}

export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
tsNodeRegister(path, tsConfig);

const customWebpackConfig = require(path);
// If the user provides a configuration in TS file
// then there are 2 cases for exporing an object. The first one is:
// `module.exports = { ... }`. And the second one is:
// `export default { ... }`. The ESM format is compiled into:
// `{ default: { ... } }`
return customWebpackConfig.default || customWebpackConfig;
}

0 comments on commit 3c25a1c

Please sign in to comment.