Skip to content

Commit

Permalink
feat(node): add support for async webpack configuration (nrwl#9641)
Browse files Browse the repository at this point in the history
  • Loading branch information
exocom authored and sidmonta committed Apr 2, 2022
1 parent 0b9ad5b commit 028752b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
14 changes: 14 additions & 0 deletions docs/shared/guides/customize-webpack.md
Expand Up @@ -21,6 +21,20 @@ module.exports = (config, context) => {
};
```

Also supports promises

```typescript
// Utility function for sleeping
const sleep = (n) => new Promise((resolve) => setTimeout(resolve, n));

module.exports = async (config, context) => {
await sleep(10);
return merge(config, {
// overwrite values here
});
};
```

## Add a Loader

To add the `css-loader` to your config, install it and add the rule.
Expand Down
84 changes: 42 additions & 42 deletions packages/node/src/executors/webpack/webpack.impl.ts
@@ -1,72 +1,72 @@
import 'dotenv/config';
import { ExecutorContext } from '@nrwl/devkit';
import 'dotenv/config'
import { ExecutorContext } from '@nrwl/devkit'

import { readCachedProjectGraph } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/devkit'
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript';
} from '@nrwl/workspace/src/utilities/buildable-libs-utils'
import { getRootTsConfigPath } from '@nrwl/workspace/src/utilities/typescript'

import { map, mergeMap, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import { resolve } from 'path';
import { register } from 'ts-node';
import { map, mergeMap, tap } from 'rxjs/operators'
import { eachValueFrom } from 'rxjs-for-await'
import { resolve } from 'path'
import { register } from 'ts-node'

import { getNodeWebpackConfig } from '../../utils/node.config';
import { BuildNodeBuilderOptions } from '../../utils/types';
import { normalizeBuildOptions } from '../../utils/normalize';
import { generatePackageJson } from '../../utils/generate-package-json';
import { runWebpack } from '../../utils/run-webpack';
import { from, of } from 'rxjs';
import { Configuration } from 'webpack';
import { getNodeWebpackConfig } from '../../utils/node.config'
import { BuildNodeBuilderOptions } from '../../utils/types'
import { normalizeBuildOptions } from '../../utils/normalize'
import { generatePackageJson } from '../../utils/generate-package-json'
import { runWebpack } from '../../utils/run-webpack'
import { from, of } from 'rxjs'
import { Configuration } from 'webpack'

export type NodeBuildEvent = {
outfile: string;
success: boolean;
};
outfile: string
success: boolean
}

export async function* webpackExecutor(
rawOptions: BuildNodeBuilderOptions,
context: ExecutorContext
) {
const { sourceRoot, root } = context.workspace.projects[context.projectName];
const { sourceRoot, root } = context.workspace.projects[context.projectName]

if (!sourceRoot) {
throw new Error(`${context.projectName} does not have a sourceRoot.`);
throw new Error(`${context.projectName} does not have a sourceRoot.`)
}

if (!root) {
throw new Error(`${context.projectName} does not have a root.`);
throw new Error(`${context.projectName} does not have a root.`)
}

const options = normalizeBuildOptions(
rawOptions,
context.root,
sourceRoot,
root
);
)

if (options.webpackConfig.some((x) => x.endsWith('.ts'))) {
registerTsNode();
registerTsNode()
}

const projGraph = readCachedProjectGraph();
const projGraph = readCachedProjectGraph()
if (!options.buildLibsFromSource) {
const { target, dependencies } = calculateProjectDependencies(
projGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
)
options.tsConfig = createTmpTsConfig(
options.tsConfig,
context.root,
target.data.root,
dependencies
);
)

if (
!checkDependentProjectsHaveBeenBuilt(
Expand All @@ -76,31 +76,31 @@ export async function* webpackExecutor(
dependencies
)
) {
return { success: false } as any;
return { success: false } as any
}
}

if (options.generatePackageJson) {
generatePackageJson(context.projectName, projGraph, options);
generatePackageJson(context.projectName, projGraph, options)
}
const config: Configuration | Configuration[] = options.webpackConfig.reduce(
(currentConfig, plugin) => {
return require(plugin)(currentConfig, {
const config: Configuration | Configuration[] = await options.webpackConfig.reduce(
async (currentConfig, plugin) => {
return require(plugin)(await currentConfig, {
options,
configuration: context.configurationName,
});
})
},
getNodeWebpackConfig(options)
);
Promise.resolve(getNodeWebpackConfig(options))
)

return yield* eachValueFrom(
(Array.isArray(config) ? from(config) : of(config)).pipe(
mergeMap((conf) => {
return runWebpack(conf).pipe(
tap((stats) => {
console.info(stats.toString(conf.stats));
console.info(stats.toString(conf.stats))
})
);
)
}),
map((stats) => {
return {
Expand All @@ -110,17 +110,17 @@ export async function* webpackExecutor(
options.outputPath,
options.outputFileName
),
} as NodeBuildEvent;
} as NodeBuildEvent
})
)
);
)
}

function registerTsNode() {
const rootTsConfig = getRootTsConfigPath();
const rootTsConfig = getRootTsConfigPath()
register({
...(rootTsConfig ? { project: rootTsConfig } : null),
});
})
}

export default webpackExecutor;
export default webpackExecutor

0 comments on commit 028752b

Please sign in to comment.