Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webpack): add plugin to automatically configure build and serve targets #20243

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

jaysoo
Copy link
Member

@jaysoo jaysoo commented Nov 14, 2023

This PR adds support for projects using webpack.config.js files to have their build, serve, and preview targets automatically configured by @nx/webpack/plugin.

Just need to add to your nx.json file:

{
  //...
  plugins: ['@nx/webpack/plugin']
}

The plugin will support both composePlugins and NxWebpackPlugin usages. The former requires a new isNxWebpackComposablePlugin function that checks if a nxWebpackComposablePlugin exists on the exported function. Otherwise, the function could be a normal webpack config function.

When NX_PCV3=true, Wwe'll also generate projects with options set in webpack config files using NxWebpackPlugin, rather than in project.json.

e.g.

const { NxWebpackPlugin } = require('@nx/webpack');
const { NxReactWebpackPlugin } = require('@nx/react');

// Standard webpack config format, no Nx-specific logic
module.exports =  {
  plugins: [
    new NxWebpackPlugin({
      compiler: 'babel',
      index: './src/index.html',
      main: './src/main.tsx',
      tsConfig: './tsconfig.app.json',
      baseHref: '/',
      assets: ['./src/favicon.ico', './src/assets'],
      styles: ['./src/styles.css'],
    }),
    NxReactWebpackPlugin({
      // Uncomment this line if you don't want to use SVGR
      // See: https://react-svgr.com/
      // svgr: false
    }),
  ],
};

If NX_PCV3=true is not set, then everything behaves as normal.

Note: We use relative paths (e.g. main: './src/main.tsx'), or absolute paths (e.g. output: { path: join(__dirname, '...') }). Previously inside targets, we used paths relative to workspace root, which doesn't make sense in the new setup. You can still use paths from the workspace root to make migration easier.

Summary of changes

  • Add @nx/webpack/plugin that uses CreateNodes to infer build, serve, and preview targets
  • Support standard webpack configuration, even the functional format that exports a (env, args) -> config function
  • Option defaults are moved from schema.json of webpack build executor to plugins (e.g. withNx and NxWebpackPlugin)
  • Our webpack plugins can load in both the "reading" phase of Nx (e.g. during CreateNodes), and during actual task run (e.g. webpack build) -- need to read out options like config.output.path to determine what's cached
  • Our composable webpack plugins (e.g. withNx) works with normal webpack CLI
  • Add webpack-cli as a workspace dep in @nx/webpack:init generator
  • withWeb is merged into withNx, and a deprecation message is show to user when used -- this should not impact most users, since withWeb is not used for React
  • Generated apps will have options, such as main and tsConfig, configured in webpack.config.js rather than project.json -- existing apps will continue to function as before

TODO

  • Cache targets so we don't slow down project graph generation
  • e2e tests to cover PCv3 cases

Copy link

vercel bot commented Nov 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview Nov 28, 2023 3:36pm

@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from c1b3180 to d09d1a5 Compare November 15, 2023 13:35
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from d09d1a5 to 7249510 Compare November 16, 2023 19:29
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from 7249510 to 26e204e Compare November 21, 2023 18:56
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from 26e204e to 230ebcf Compare November 21, 2023 19:25
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch 3 times, most recently from 1ba95e7 to 9e0f2d8 Compare November 21, 2023 19:59
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch 2 times, most recently from 907ebd1 to a4d1f1d Compare November 21, 2023 21:19
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from a4d1f1d to b3bf3b1 Compare November 21, 2023 21:36
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from b3bf3b1 to 51f5a9b Compare November 21, 2023 21:47
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from 51f5a9b to 4fbd1f3 Compare November 21, 2023 22:01
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from 4fbd1f3 to fd3afd6 Compare November 22, 2023 13:23
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from fd3afd6 to 5c48f59 Compare November 22, 2023 14:03
@jaysoo jaysoo force-pushed the feat/webpack-create-nodes branch from 5c48f59 to f1cc57b Compare November 22, 2023 17:13
@FrozenPandaz FrozenPandaz merged commit 507fe42 into nrwl:master Nov 28, 2023
4 of 6 checks passed
Copy link

github-actions bot commented Dec 4, 2023

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants