chore(core): move webpack utils into web and node packages (#7367)
This commit is contained in:
parent
39c290d6b6
commit
8779a22be3
@ -6,11 +6,10 @@ import buildExecutor from './build.impl';
|
||||
import { BuildNodeBuilderOptions } from '../../utils/types';
|
||||
|
||||
jest.mock('tsconfig-paths-webpack-plugin');
|
||||
jest.mock('@nrwl/workspace/src/utilities/run-webpack', () => ({
|
||||
jest.mock('../../utils/run-webpack', () => ({
|
||||
runWebpack: jest.fn(),
|
||||
}));
|
||||
import { runWebpack } from '@nrwl/workspace/src/utilities/run-webpack';
|
||||
import objectContaining = jasmine.objectContaining;
|
||||
import { runWebpack } from '../../utils/run-webpack';
|
||||
|
||||
describe('Node Build Executor', () => {
|
||||
let context: ExecutorContext;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import * as webpack from 'webpack';
|
||||
import 'dotenv/config';
|
||||
import { ExecutorContext } from '@nrwl/devkit';
|
||||
|
||||
@ -8,7 +7,6 @@ import {
|
||||
checkDependentProjectsHaveBeenBuilt,
|
||||
createTmpTsConfig,
|
||||
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
|
||||
import { runWebpack } from '@nrwl/workspace/src/utilities/run-webpack';
|
||||
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
import { eachValueFrom } from 'rxjs-for-await';
|
||||
@ -19,6 +17,7 @@ import { OUT_FILENAME } from '../../utils/config';
|
||||
import { BuildNodeBuilderOptions } from '../../utils/types';
|
||||
import { normalizeBuildOptions } from '../../utils/normalize';
|
||||
import { generatePackageJson } from '../../utils/generate-package-json';
|
||||
import { runWebpack } from '../../utils/run-webpack';
|
||||
|
||||
export type NodeBuildEvent = {
|
||||
outfile: string;
|
||||
|
||||
33
packages/node/src/utils/run-webpack.ts
Normal file
33
packages/node/src/utils/run-webpack.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import * as webpack from 'webpack';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
export function runWebpack(config: webpack.Configuration): Observable<any> {
|
||||
return new Observable((subscriber) => {
|
||||
// Passing `watch` option here will result in a warning due to missing callback.
|
||||
// We manually call `.watch` or `.run` later so this option isn't needed here.
|
||||
const { watch, ...normalizedConfig } = config;
|
||||
const webpackCompiler = webpack(normalizedConfig);
|
||||
|
||||
const callback = (err: Error, stats: webpack.Stats) => {
|
||||
if (err) {
|
||||
subscriber.error(err);
|
||||
}
|
||||
subscriber.next(stats);
|
||||
};
|
||||
|
||||
if (config.watch) {
|
||||
const watchOptions = config.watchOptions || {};
|
||||
const watching = webpackCompiler.watch(watchOptions, callback);
|
||||
|
||||
return () => watching.close(() => subscriber.complete());
|
||||
} else {
|
||||
webpackCompiler.run((err, stats) => {
|
||||
callback(err, stats);
|
||||
webpackCompiler.close((closeErr) => {
|
||||
if (closeErr) subscriber.error(closeErr);
|
||||
subscriber.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
import type { ExecutorContext } from '@nrwl/devkit';
|
||||
import type { Configuration, Stats } from 'webpack';
|
||||
import * as webpack from 'webpack';
|
||||
import { from, of } from 'rxjs';
|
||||
import { bufferCount, mergeScan, switchMap, tap } from 'rxjs/operators';
|
||||
import { eachValueFrom } from 'rxjs-for-await';
|
||||
@ -14,10 +13,6 @@ import {
|
||||
checkDependentProjectsHaveBeenBuilt,
|
||||
createTmpTsConfig,
|
||||
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
|
||||
import {
|
||||
getEmittedFiles,
|
||||
runWebpack,
|
||||
} from '@nrwl/workspace/src/utilities/run-webpack';
|
||||
import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript';
|
||||
|
||||
import { writeIndexHtml } from '../../utils/third-party/cli-files/utilities/index-file/write-index-html';
|
||||
@ -29,6 +24,7 @@ import { getWebConfig } from '../../utils/web.config';
|
||||
import type { BuildBuilderOptions } from '../../utils/types';
|
||||
import { deleteOutputDir } from '../../utils/delete-output-dir';
|
||||
import type { ExtraEntryPoint } from '../../utils/third-party/browser/schema';
|
||||
import { getEmittedFiles, runWebpack } from '../../utils/run-webpack';
|
||||
|
||||
export interface WebBuildBuilderOptions extends BuildBuilderOptions {
|
||||
index: string;
|
||||
|
||||
@ -9,10 +9,6 @@ import {
|
||||
import { eachValueFrom } from 'rxjs-for-await';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
import * as WebpackDevServer from 'webpack-dev-server';
|
||||
import {
|
||||
getEmittedFiles,
|
||||
runWebpackDevServer,
|
||||
} from '@nrwl/workspace/src/utilities/run-webpack';
|
||||
|
||||
import { normalizeWebBuildOptions } from '../../utils/normalize';
|
||||
import { WebBuildBuilderOptions } from '../build/build.impl';
|
||||
@ -22,6 +18,7 @@ import {
|
||||
createTmpTsConfig,
|
||||
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
|
||||
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
|
||||
import { getEmittedFiles, runWebpackDevServer } from '../../utils/run-webpack';
|
||||
|
||||
export interface WebDevServerOptions {
|
||||
host: string;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import * as webpack from 'webpack';
|
||||
import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { Observable } from 'rxjs';
|
||||
import { extname } from 'path';
|
||||
|
||||
export function runWebpack(config: webpack.Configuration): Observable<any> {
|
||||
@ -7,7 +7,6 @@
|
||||
*/
|
||||
|
||||
import { dirname, join } from 'path';
|
||||
import { EmittedFile } from '@nrwl/workspace/src/utilities/run-webpack';
|
||||
import { ExtraEntryPoint } from '../../../browser/schema';
|
||||
import { generateEntryPoints } from '../package-chunk-sort';
|
||||
import { stripBom } from '../strip-bom';
|
||||
@ -18,6 +17,7 @@ import {
|
||||
} from './augment-index-html';
|
||||
import { readFileSync, writeFileSync } from 'fs';
|
||||
import { interpolateEnvironmentVariablesToIndex } from '../../../../interpolate-env-variables-to-index';
|
||||
import { EmittedFile } from '../../../../run-webpack';
|
||||
|
||||
type ExtensionFilter = '.js' | '.css';
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user