Skip to content

Commit

Permalink
feat(webpack): change plugin import paths to speed up config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Apr 26, 2024
1 parent aa1be0f commit 100f96e
Show file tree
Hide file tree
Showing 28 changed files with 85 additions and 70 deletions.
6 changes: 3 additions & 3 deletions docs/shared/packages/webpack/webpack-config-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Currently, Nx module federation requires an enhanced Webpack configuration file
A basic Webpack configuration was introduced in Nx 18, and it looks like this:

```js {% fileName="apps/demo/webpack.config.js" %}
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
Expand Down Expand Up @@ -132,8 +132,8 @@ React projects use the `@nx/react` package to build their apps. This package pro
{% tab label="Basic Webpack configuration" %}

```js {% fileName="apps/demo/app/webpack.config.js" %}
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions docs/shared/packages/webpack/webpack-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Watch for file changes.
#### Example

```js
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
Expand Down Expand Up @@ -322,7 +322,7 @@ Enables or disables [React SVGR](https://react-svgr.com/). Default is `true`.
#### Example

```js
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion docs/shared/recipes/ci-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The [`NxWebpackPlugin`](/recipes/webpack/webpack-plugins#nxwebpackplugin) plugin
To generate a `package.json` we would declare it in the plugin options.

```js {% fileName="apps/acme/app/webpack.config.js" %}
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('Node Applications', () => {
updateFile(
`apps/${nodeapp}/webpack.config.js`,
`
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Webpack Plugin', () => {
`libs/${myPkg}/webpack.config.js`,
`
const path = require('path');
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
class DebugPlugin {
apply(compiler) {
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('Webpack Plugin', () => {
`apps/${appName}/webpack.config.js`,
`
const path = require('path');
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
module.exports = {
target: 'node',
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('Webpack Plugin', () => {
updateFile(
`apps/${appName}/webpack.config.js`,
`
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
output: {
Expand Down
4 changes: 2 additions & 2 deletions graph/client/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require('node:path');
// nx-ignore-next-line
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack');
// nx-ignore-next-line
const { NxReactWebpackPlugin } = require('@nx/react');

Expand All @@ -23,7 +23,7 @@ module.exports = {
},
},
plugins: [
new NxWebpackPlugin({
new NxAppWebpackPlugin({
index: './src/index.html',
compiler: 'babel',
main: './src/main.tsx',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if (webpackPluginOptions) { %>
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if (webpackPluginOptions) { %>
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');

module.exports = {
resolve: {
Expand Down
7 changes: 6 additions & 1 deletion packages/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { NxReactWebpackPlugin as _NxReactWebpackPlugin } from './plugins/nx-react-webpack-plugin/nx-react-webpack-plugin';

// TODO(v20): Remove this in favor of deep imports in order to load configs faster (150-200ms faster).
/** @deprecated Use '@nx/react/webpack-plugin' instead. */
export const NxReactWebpackPlugin = _NxReactWebpackPlugin;

export {
extraEslintDependencies,
extendReactEslintJson,
Expand All @@ -22,4 +28,3 @@ export { componentTestGenerator } from './src/generators/component-test/componen
export { setupTailwindGenerator } from './src/generators/setup-tailwind/setup-tailwind';
export type { SupportedStyles } from './typings/style';
export * from './plugins/with-react';
export { NxReactWebpackPlugin } from './plugins/nx-react-webpack-plugin/nx-react-webpack-plugin';
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ it('should have a greeting as the title', () => {

exports[`app --style @emotion/styled should exclude styles 1`] = `
"
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');
module.exports = {
Expand Down Expand Up @@ -145,8 +145,8 @@ nxViteTsPaths()],

exports[`app --style none should exclude styles 1`] = `
"
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');
module.exports = {
Expand Down Expand Up @@ -256,8 +256,8 @@ exports[`app setup React app with --bundler=vite should setup targets with vite

exports[`app should add custom webpack config 1`] = `
"
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');
module.exports = {
Expand Down Expand Up @@ -374,8 +374,8 @@ nxViteTsPaths()],

exports[`app should setup webpack 1`] = `
"
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');
module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if (webpackPluginOptions) { %>
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { NxReactWebpackPlugin } = require('@nx/react/webpack-plugin');
const { join } = require('path');

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions packages/react/webpack-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NxReactWebpackPlugin } from './plugins/nx-react-webpack-plugin/nx-react-webpack-plugin';
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports[`app should setup eslint 1`] = `
`;

exports[`app should setup the web build builder 1`] = `
"const { NxWebpackPlugin } = require('@nx/webpack');
"const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = {
`;

exports[`app should setup the web dev server 1`] = `
"const { NxWebpackPlugin } = require('@nx/webpack');
"const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if (webpackPluginOptions) { %>
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack/app-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { NxAppWebpackPlugin } from './src/plugins/nx-webpack-plugin/nx-app-webpack-plugin';
export type { NxAppWebpackPluginOptions } from './src/plugins/nx-webpack-plugin/nx-app-webpack-plugin-options';
10 changes: 8 additions & 2 deletions packages/webpack/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { configurationGenerator } from './src/generators/configuration/configuration';
import { NxAppWebpackPlugin } from './src/plugins/nx-webpack-plugin/nx-app-webpack-plugin';
import { NxTsconfigPathsWebpackPlugin as _NxTsconfigPathsWebpackPlugin } from './src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin';

export { configurationGenerator };

// Exported for backwards compatibility in case a plugin is using the old name.
/** @deprecated Use `configurationGenerator` instead. */
export const webpackProjectGenerator = configurationGenerator;

// TODO(v20): Remove this in favor of deep imports in order to load configs faster (150-200ms faster).
/** @deprecated Use NxAppWebpackPlugin from `@nx/webpack/app-plugin` instead. */
export const NxWebpackPlugin = NxAppWebpackPlugin;
/** @deprecated Use NxTsconfigPathsWebpackPlugin from `@nx/webpack/tsconfig-paths-plugin` instead. */
export const NxTsconfigPathsWebpackPlugin = _NxTsconfigPathsWebpackPlugin;

export * from './src/utils/create-copy-plugin';
export * from './src/utils/config';
export { webpackInitGenerator } from './src/generators/init/init';
Expand All @@ -23,5 +31,3 @@ export * from './src/utils/get-css-module-local-ident';
export * from './src/utils/with-nx';
export * from './src/utils/with-web';
export * from './src/utils/module-federation/public-api';
export { NxWebpackPlugin } from './src/plugins/nx-webpack-plugin/nx-webpack-plugin';
export { NxTsconfigPathsWebpackPlugin } from './src/plugins/nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin';
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function createWebpackConfig(
joinPathFragments(project.root, 'webpack.config.js'),
hasPlugin(tree)
? `
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
Expand Down Expand Up @@ -142,7 +142,7 @@ module.exports = composePlugins(withNx(), withWeb(), (config) => {
joinPathFragments(project.root, 'webpack.config.js'),
hasPlugin(tree)
? `
const { NxWebpackPlugin } = require('@nx/webpack');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getOutputHashFormat } from '../../../utils/hash-format';
import { NxTsconfigPathsWebpackPlugin } from '../../nx-typescript-webpack-plugin/nx-tsconfig-paths-webpack-plugin';
import { getTerserEcmaVersion } from './get-terser-ecma-version';
import { createLoaderFromCompiler } from './compiler-loaders';
import { NormalizedNxWebpackPluginOptions } from '../nx-webpack-plugin-options';
import { NormalizedNxAppWebpackPluginOptions } from '../nx-app-webpack-plugin-options';
import TerserPlugin = require('terser-webpack-plugin');
import nodeExternals = require('webpack-node-externals');

Expand All @@ -29,7 +29,7 @@ const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
const mainFields = ['module', 'main'];

export function applyBaseConfig(
options: NormalizedNxWebpackPluginOptions,
options: NormalizedNxAppWebpackPluginOptions,
config: Partial<WebpackOptionsNormalized | Configuration> = {},
{
useNormalizedEntry,
Expand Down Expand Up @@ -57,7 +57,7 @@ export function applyBaseConfig(
}

function applyNxIndependentConfig(
options: NormalizedNxWebpackPluginOptions,
options: NormalizedNxAppWebpackPluginOptions,
config: Partial<WebpackOptionsNormalized | Configuration>
): void {
const hashFormat = getOutputHashFormat(options.outputHashing as string);
Expand Down Expand Up @@ -205,7 +205,7 @@ function applyNxIndependentConfig(
}

function applyNxDependentConfig(
options: NormalizedNxWebpackPluginOptions,
options: NormalizedNxAppWebpackPluginOptions,
config: Partial<WebpackOptionsNormalized | Configuration>,
{ useNormalizedEntry }: { useNormalizedEntry?: boolean } = {}
): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'webpack';

import { WriteIndexHtmlPlugin } from '../../write-index-html-plugin';
import { NormalizedNxWebpackPluginOptions } from '../nx-webpack-plugin-options';
import { NormalizedNxAppWebpackPluginOptions } from '../nx-app-webpack-plugin-options';
import { getOutputHashFormat } from '../../../utils/hash-format';
import { getClientEnvironment } from '../../../utils/get-client-environment';
import { normalizeExtraEntryPoints } from '../../../utils/webpack/normalize-entry';
Expand All @@ -24,7 +24,7 @@ import CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
import MiniCssExtractPlugin = require('mini-css-extract-plugin');

export function applyWebConfig(
options: NormalizedNxWebpackPluginOptions,
options: NormalizedNxAppWebpackPluginOptions,
config: Partial<WebpackOptionsNormalized | Configuration> = {},
{
useNormalizedEntry,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from 'path';
import { readTsConfig } from '@nx/js';

import { NormalizedNxWebpackPluginOptions } from '../nx-webpack-plugin-options';
import { NormalizedNxAppWebpackPluginOptions } from '../nx-app-webpack-plugin-options';

export function createLoaderFromCompiler(
options: NormalizedNxWebpackPluginOptions
options: NormalizedNxAppWebpackPluginOptions
) {
switch (options.compiler) {
case 'swc':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { WebpackPluginInstance } from 'webpack';
import { getOutputHashFormat } from '../../../utils/hash-format';
import { ScriptsWebpackPlugin } from '../../../utils/webpack/plugins/scripts-webpack-plugin';
import { normalizeExtraEntryPoints } from '../../../utils/webpack/normalize-entry';
import { NormalizedNxWebpackPluginOptions } from '../nx-webpack-plugin-options';
import { NormalizedNxAppWebpackPluginOptions } from '../nx-app-webpack-plugin-options';

export function instantiateScriptPlugins(
options: NormalizedNxWebpackPluginOptions
options: NormalizedNxAppWebpackPluginOptions
): WebpackPluginInstance[] {
// process global scripts
const globalScriptsByBundleName = normalizeExtraEntryPoints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
import {
AssetGlobPattern,
FileReplacement,
NormalizedNxWebpackPluginOptions,
NxWebpackPluginOptions,
} from '../nx-webpack-plugin-options';
NormalizedNxAppWebpackPluginOptions,
NxAppWebpackPluginOptions,
} from '../nx-app-webpack-plugin-options';

export function normalizeOptions(
options: NxWebpackPluginOptions
): NormalizedNxWebpackPluginOptions {
const combinedPluginAndMaybeExecutorOptions: Partial<NormalizedNxWebpackPluginOptions> =
options: NxAppWebpackPluginOptions
): NormalizedNxAppWebpackPluginOptions {
const combinedPluginAndMaybeExecutorOptions: Partial<NormalizedNxAppWebpackPluginOptions> =
{};
const isProd = process.env.NODE_ENV === 'production';
const projectName = process.env.NX_TASK_TARGET_PROJECT;
Expand Down Expand Up @@ -187,7 +187,7 @@ export function normalizeFileReplacements(

function normalizeRelativePaths(
projectRoot: string,
options: NxWebpackPluginOptions
options: NxAppWebpackPluginOptions
): void {
for (const [fieldName, fieldValue] of Object.entries(options)) {
if (isRelativePath(fieldValue)) {
Expand Down
Loading

0 comments on commit 100f96e

Please sign in to comment.