Skip to content

Commit 7a44528

Browse files
authored
feat(custom-webpack): pass target options to custom webpack config function (#721)
* closes #683
1 parent 53115f0 commit 7a44528

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

packages/custom-webpack/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ In this case, the behavior will be the same as when exporting a plain object —
291291
If `customWebpackConfig.path` file exports a function, the behaviour of the builder changes : no more automatic merge is applied, instead the function
292292
is called with the base Webpack configuration and must return the new configuration.
293293
294-
The function is called with the base config and the builder options as parameters.
294+
The function is called with the base config the builder options and the target options as parameters.
295+
`TargetOptions` follows `target` definition from [this](https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/architect/src/input-schema.json) schema
296+
and can be used to manipulate your build based on the build target.
295297
296298
In this case, `mergeStrategies` and `replaceDuplicatePlugins` options have no effect.
297299
@@ -301,7 +303,7 @@ In this case, `mergeStrategies` and `replaceDuplicatePlugins` options have no ef
301303
const webpack = require('webpack');
302304
const pkg = require('./package.json');
303305

304-
module.exports = (config, options) => {
306+
module.exports = (config, options, targetOptions) => {
305307
config.plugins.push(
306308
new webpack.DefinePlugin({
307309
APP_VERSION: JSON.stringify(pkg.version),
@@ -315,11 +317,11 @@ module.exports = (config, options) => {
315317
Alternatively, using TypeScript:
316318
317319
```ts
318-
import { CustomWebpackBrowserSchema } from '@angular-builders/custom-webpack';
320+
import { CustomWebpackBrowserSchema, TargetOptions } from '@angular-builders/custom-webpack';
319321
import * as webpack from 'webpack';
320322
import * as pkg from './package.json';
321323

322-
export default (config: webpack.Configuration, options: CustomWebpackBrowserSchema) => {
324+
export default (config: webpack.Configuration, options: CustomWebpackBrowserSchema, targetOptions: TargetOptions) => {
323325
config.plugins.push(
324326
new webpack.DefinePlugin({
325327
APP_VERSION: JSON.stringify(pkg.version),

packages/custom-webpack/examples/full-cycle-app/func-webpack.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Configuration } from 'webpack';
2-
import { CustomWebpackBrowserSchema } from '@angular-builders/custom-webpack';
2+
import { CustomWebpackBrowserSchema, TargetOptions } from '@angular-builders/custom-webpack';
33
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
44

55
/**
66
* This is where you define a function that modifies your webpack config
77
*/
8-
export default (cfg: Configuration, opts: CustomWebpackBrowserSchema) => {
8+
export default (cfg: Configuration, opts: CustomWebpackBrowserSchema, targetOptions: TargetOptions) => {
99
cfg.plugins.push(
1010
new HtmlWebpackPlugin({
1111
filename: 'footer.html',

packages/custom-webpack/src/common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import { tsNodeRegister } from './utils';
1212
export const customWebpackConfigTransformFactory: (
1313
options: CustomWebpackSchema,
1414
context: BuilderContext
15-
) => ExecutionTransformer<Configuration> = (options, { workspaceRoot }) => browserWebpackConfig => {
15+
) => ExecutionTransformer<Configuration> = (options, { workspaceRoot, target }) => browserWebpackConfig => {
1616
return CustomWebpackBuilder.buildWebpackConfig(
1717
normalize(workspaceRoot),
1818
options.customWebpackConfig,
1919
browserWebpackConfig,
20-
options //TODO: pass Target options as well (configuration option in particular)
20+
options,
21+
target
2122
);
2223
};
2324

packages/custom-webpack/src/custom-webpack-builder.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const buildOptions = {
1313
env: 'prod',
1414
};
1515

16+
const targetOptions = {
17+
project: 'application',
18+
configuration: 'production',
19+
target: 'serve'
20+
};
21+
1622
const customWebpackConfig = {
1723
module: {
1824
rules: [
@@ -62,6 +68,7 @@ describe('CustomWebpackBuilder', () => {
6268
__dirname as Path,
6369
null,
6470
baseWebpackConfig,
71+
{},
6572
{}
6673
);
6774

@@ -71,7 +78,7 @@ describe('CustomWebpackBuilder', () => {
7178
it('should load webpack.config.js if no path specified', async () => {
7279
const spy = jest.spyOn(webpackConfigMerger, 'mergeConfigs');
7380
createConfigFile(defaultWebpackConfigPath, customWebpackConfig);
74-
await CustomWebpackBuilder.buildWebpackConfig(__dirname as Path, {}, baseWebpackConfig, {});
81+
await CustomWebpackBuilder.buildWebpackConfig(__dirname as Path, {}, baseWebpackConfig, {}, {});
7582

7683
try {
7784
expect(spy).toHaveBeenCalledWith(
@@ -94,6 +101,7 @@ describe('CustomWebpackBuilder', () => {
94101
__dirname as Path,
95102
{ path: fileName },
96103
baseWebpackConfig,
104+
{},
97105
{}
98106
);
99107

@@ -118,6 +126,7 @@ describe('CustomWebpackBuilder', () => {
118126
__dirname as Path,
119127
{ mergeStrategies },
120128
baseWebpackConfig,
129+
{},
121130
{}
122131
);
123132

@@ -141,6 +150,7 @@ describe('CustomWebpackBuilder', () => {
141150
__dirname as Path,
142151
{ replaceDuplicatePlugins: true },
143152
baseWebpackConfig,
153+
{},
144154
{}
145155
);
146156

@@ -158,9 +168,10 @@ describe('CustomWebpackBuilder', () => {
158168
__dirname as Path,
159169
{},
160170
baseWebpackConfig,
161-
buildOptions
171+
buildOptions,
172+
targetOptions
162173
);
163-
expect(spy).toHaveBeenCalledWith(baseWebpackConfig, buildOptions);
174+
expect(spy).toHaveBeenCalledWith(baseWebpackConfig, buildOptions, targetOptions);
164175
});
165176

166177
it('should apply custom function on configuration', async () => {
@@ -170,6 +181,7 @@ describe('CustomWebpackBuilder', () => {
170181
__dirname as Path,
171182
{},
172183
baseWebpackConfig,
184+
{},
173185
{}
174186
);
175187

@@ -193,6 +205,7 @@ describe('CustomWebpackBuilder', () => {
193205
__dirname as Path,
194206
{},
195207
baseWebpackConfig,
208+
{},
196209
{}
197210
);
198211

@@ -221,6 +234,7 @@ describe('CustomWebpackBuilder', () => {
221234
__dirname as Path,
222235
{},
223236
baseWebpackConfig,
237+
{},
224238
{}
225239
);
226240

packages/custom-webpack/src/custom-webpack-builder.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import { Configuration } from 'webpack';
44
import { mergeConfigs } from './webpack-config-merger';
55
import { CustomWebpackBuilderConfig } from './custom-webpack-builder-config';
66
import { tsNodeRegister } from './utils';
7+
import {TargetOptions} from "./type-definition";
8+
import {CustomWebpackBrowserSchema} from "./browser";
79

810
export const defaultWebpackConfigPath = 'webpack.config.js';
911

1012
type CustomWebpackConfig =
1113
| Configuration
1214
| Promise<Configuration>
13-
| ((baseWebpackConfig: Configuration, buildOptions: any) => Configuration)
14-
| ((baseWebpackConfig: Configuration, buildOptions: any) => Promise<Configuration>);
15+
| ((baseWebpackConfig: Configuration, buildOptions: CustomWebpackBrowserSchema, targetOptions: TargetOptions) => Configuration)
16+
| ((baseWebpackConfig: Configuration, buildOptions: CustomWebpackBrowserSchema, targetOptions: TargetOptions) => Promise<Configuration>);
1517

1618
export class CustomWebpackBuilder {
1719
static async buildWebpackConfig(
1820
root: Path,
1921
config: CustomWebpackBuilderConfig,
2022
baseWebpackConfig: Configuration,
21-
buildOptions: any
23+
buildOptions: any,
24+
targetOptions: TargetOptions
2225
): Promise<Configuration> {
2326
if (!config) {
2427
return baseWebpackConfig;
@@ -32,7 +35,7 @@ export class CustomWebpackBuilder {
3235
// That exported function can be synchronous either
3336
// asynchronous. Given the following example:
3437
// `module.exports = async (config) => { ... }`
35-
return configOrFactoryOrPromise(baseWebpackConfig, buildOptions);
38+
return configOrFactoryOrPromise(baseWebpackConfig, buildOptions, targetOptions);
3639
}
3740

3841
// The user can also export a `Promise` that resolves `Configuration`

0 commit comments

Comments
 (0)