@nx/react 16.4.0-beta.1 → 16.4.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/react",
3
- "version": "16.4.0-beta.1",
3
+ "version": "16.4.0-beta.3",
4
4
  "private": false,
5
5
  "description": "The React plugin for Nx contains executors and generators for managing React applications and libraries within an Nx workspace. It provides:\n\n\n- Integration with libraries such as Jest, Cypress, and Storybook.\n\n- Generators for applications, libraries, components, hooks, and more.\n\n- Library build support for publishing packages to npm or other registries.\n\n- Utilities for automatic workspace refactoring.",
6
6
  "repository": {
@@ -31,11 +31,11 @@
31
31
  "migrations": "./migrations.json"
32
32
  },
33
33
  "dependencies": {
34
- "@nrwl/react": "16.4.0-beta.1",
35
- "@nx/devkit": "16.4.0-beta.1",
36
- "@nx/js": "16.4.0-beta.1",
37
- "@nx/linter": "16.4.0-beta.1",
38
- "@nx/web": "16.4.0-beta.1",
34
+ "@nrwl/react": "16.4.0-beta.3",
35
+ "@nx/devkit": "16.4.0-beta.3",
36
+ "@nx/js": "16.4.0-beta.3",
37
+ "@nx/linter": "16.4.0-beta.3",
38
+ "@nx/web": "16.4.0-beta.3",
39
39
  "@phenomnomnominal/tsquery": "~5.0.1",
40
40
  "@svgr/webpack": "^8.0.1",
41
41
  "chalk": "^4.1.0",
@@ -46,5 +46,5 @@
46
46
  "access": "public"
47
47
  },
48
48
  "types": "./index.d.ts",
49
- "gitHead": "1c4fb934b607f0d93bb23bff2cf0f39b31a8fa43"
49
+ "gitHead": "d7f500985ba56117bf17c133bf70d22e42a7af27"
50
50
  }
@@ -8,6 +8,7 @@ const path_1 = require("path");
8
8
  const webpack_1 = require("webpack");
9
9
  const merge_plugins_1 = require("./merge-plugins");
10
10
  const with_react_1 = require("../with-react");
11
+ const fs_1 = require("fs");
11
12
  // This is shamelessly taken from CRA and modified for NX use
12
13
  // https://github.com/facebook/create-react-app/blob/4784997f0682e75eb32a897b4ffe34d735912e6c/packages/react-scripts/config/env.js#L71
13
14
  function getClientEnvironment(mode) {
@@ -44,6 +45,67 @@ function getClientEnvironment(mode) {
44
45
  }
45
46
  const core = (prev, options) => (Object.assign(Object.assign({}, prev), { disableWebpackDefaults: true }));
46
47
  exports.core = core;
48
+ const getProjectData = (storybookOptions) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
49
+ const fallbackData = {
50
+ workspaceRoot: storybookOptions.configDir,
51
+ projectRoot: '',
52
+ sourceRoot: '',
53
+ };
54
+ // Edge-case: not running from Nx
55
+ if (!process.env.NX_WORKSPACE_ROOT)
56
+ return fallbackData;
57
+ const projectGraph = yield (0, devkit_1.createProjectGraphAsync)();
58
+ const projectNode = projectGraph.nodes[process.env.NX_TASK_TARGET_PROJECT];
59
+ return projectNode
60
+ ? {
61
+ workspaceRoot: process.env.NX_WORKSPACE_ROOT,
62
+ projectRoot: projectNode.data.root,
63
+ sourceRoot: projectNode.data.sourceRoot,
64
+ projectNode,
65
+ }
66
+ : // Edge-case: missing project node
67
+ fallbackData;
68
+ });
69
+ const fixBabelConfigurationIfNeeded = (webpackConfig, projectData) => {
70
+ if (!projectData.projectNode)
71
+ return;
72
+ const isUsingBabelUpwardRootMode = Object.keys(projectData.projectNode.data.targets).find((k) => {
73
+ var _a;
74
+ const targetConfig = projectData.projectNode.data.targets[k];
75
+ return ((targetConfig.executor === '@nx/webpack:webpack' ||
76
+ targetConfig.executor === '@nrwl/webpack:webpack') &&
77
+ ((_a = targetConfig.options) === null || _a === void 0 ? void 0 : _a.babelUpwardRootMode));
78
+ });
79
+ if (isUsingBabelUpwardRootMode)
80
+ return;
81
+ let babelrcPath;
82
+ for (const ext of ['', '.json', '.js', '.cjs', '.mjs', '.cts']) {
83
+ const candidate = (0, path_1.join)(projectData.workspaceRoot, projectData.projectRoot, `.babelrc${ext}`);
84
+ if ((0, fs_1.existsSync)(candidate)) {
85
+ babelrcPath = candidate;
86
+ break;
87
+ }
88
+ }
89
+ // Unexpected setup, skip.
90
+ if (!babelrcPath)
91
+ return;
92
+ let babelRuleItem;
93
+ for (const rule of webpackConfig.module.rules) {
94
+ if (typeof rule === 'string')
95
+ continue;
96
+ if (!Array.isArray(rule.use))
97
+ continue;
98
+ for (const item of rule.use) {
99
+ if (typeof item !== 'string' && item['loader'].includes('babel-loader')) {
100
+ babelRuleItem = item;
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ if (babelRuleItem) {
106
+ babelRuleItem.options.configFile = babelrcPath;
107
+ }
108
+ };
47
109
  const webpack = (storybookWebpackConfig = {}, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
48
110
  var _a, _b, _c, _d, _e, _f;
49
111
  devkit_1.logger.info('=> Loading Nx React Storybook Addon from "@nx/react/plugins/storybook"');
@@ -57,9 +119,9 @@ const webpack = (storybookWebpackConfig = {}, options) => tslib_1.__awaiter(void
57
119
  }
58
120
  const { withNx, withWeb } = require('@nx/webpack');
59
121
  const tsconfigPath = (0, path_1.join)(options.configDir, 'tsconfig.json');
60
- const builderOptions = Object.assign(Object.assign({}, options), { root: options.configDir,
61
- // These are blank because root is the absolute path to .storybook folder
62
- projectRoot: '', sourceRoot: '', fileReplacements: [], sourceMap: true, styles: (_a = options.styles) !== null && _a !== void 0 ? _a : [], optimization: {}, tsConfig: tsconfigPath, extractCss: storybookWebpackConfig.mode === 'production', target: 'web' });
122
+ const projectData = yield getProjectData(options);
123
+ fixBabelConfigurationIfNeeded(storybookWebpackConfig, projectData);
124
+ const builderOptions = Object.assign(Object.assign({}, options), { root: projectData.workspaceRoot, projectRoot: projectData.projectRoot, sourceRoot: projectData.sourceRoot, fileReplacements: [], sourceMap: true, styles: (_a = options.styles) !== null && _a !== void 0 ? _a : [], optimization: {}, tsConfig: tsconfigPath, extractCss: storybookWebpackConfig.mode === 'production', target: 'web' });
63
125
  // ESM build for modern browsers.
64
126
  let baseWebpackConfig = {};
65
127
  const configure = (0, config_1.composePluginsSync)(withNx({ skipTypeChecking: true }), withWeb(), (0, with_react_1.withReact)());
@@ -21,6 +21,7 @@ const install_common_dependencies_1 = require("./lib/install-common-dependencies
21
21
  const create_ts_config_1 = require("../../utils/create-ts-config");
22
22
  const add_swc_dependencies_1 = require("@nx/js/src/utils/swc/add-swc-dependencies");
23
23
  const chalk = require("chalk");
24
+ const show_possible_warnings_1 = require("./lib/show-possible-warnings");
24
25
  function addLinting(host, options) {
25
26
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
26
27
  const tasks = [];
@@ -42,9 +43,7 @@ function addLinting(host, options) {
42
43
  tasks.push(lintTask);
43
44
  (0, devkit_1.updateJson)(host, (0, devkit_1.joinPathFragments)(options.appProjectRoot, '.eslintrc.json'), lint_1.extendReactEslintJson);
44
45
  if (!options.skipPackageJson) {
45
- const installTask = yield (0, devkit_1.addDependenciesToPackageJson)(host, lint_1.extraEslintDependencies.dependencies, Object.assign(Object.assign({}, lint_1.extraEslintDependencies.devDependencies), (options.compiler === 'swc'
46
- ? { 'swc-loader': versions_1.swcLoaderVersion }
47
- : {})));
46
+ const installTask = yield (0, devkit_1.addDependenciesToPackageJson)(host, lint_1.extraEslintDependencies.dependencies, lint_1.extraEslintDependencies.devDependencies);
48
47
  const addSwcTask = (0, add_swc_dependencies_1.addSwcDependencies)(host);
49
48
  tasks.push(installTask, addSwcTask);
50
49
  }
@@ -56,7 +55,8 @@ function applicationGenerator(host, schema) {
56
55
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
57
56
  const tasks = [];
58
57
  const options = (0, normalize_options_1.normalizeOptions)(host, schema);
59
- const initTask = yield (0, init_1.default)(host, Object.assign(Object.assign({}, options), { skipFormat: true, skipBabelConfig: options.bundler === 'vite', skipHelperLibs: options.bundler === 'vite' }));
58
+ (0, show_possible_warnings_1.showPossibleWarnings)(host, options);
59
+ const initTask = yield (0, init_1.default)(host, Object.assign(Object.assign({}, options), { skipFormat: true, skipHelperLibs: options.bundler === 'vite' }));
60
60
  tasks.push(initTask);
61
61
  if (!options.rootProject) {
62
62
  (0, create_ts_config_1.extractTsConfigBase)(host);
@@ -4,25 +4,32 @@ exports.installCommonDependencies = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const versions_1 = require("../../../utils/versions");
6
6
  function installCommonDependencies(host, options) {
7
- let devDependencies = null;
7
+ const devDependencies = {};
8
8
  // Vite requires style preprocessors to be installed manually.
9
9
  // `@nx/webpack` installs them automatically for now.
10
- // TODO(jack): Once we clean up webpack we can remove this check
11
10
  if (options.bundler === 'vite' || options.unitTestRunner === 'vitest') {
12
11
  switch (options.style) {
13
12
  case 'scss':
14
- devDependencies = { sass: versions_1.sassVersion };
13
+ devDependencies['sass'] = versions_1.sassVersion;
15
14
  break;
16
15
  case 'less':
17
- devDependencies = { less: versions_1.lessVersion };
16
+ devDependencies['less'] = versions_1.lessVersion;
18
17
  break;
19
18
  case 'styl': // @TODO(17): deprecated, going to be removed in Nx 17
20
- devDependencies = { stylus: versions_1.stylusVersion };
19
+ devDependencies['stylus'] = versions_1.stylusVersion;
21
20
  break;
22
21
  }
23
22
  }
24
- return devDependencies
25
- ? (0, devkit_1.addDependenciesToPackageJson)(host, {}, devDependencies)
26
- : function noop() { };
23
+ if (options.bundler === 'webpack') {
24
+ if (options.compiler === 'swc') {
25
+ devDependencies['swc-loader'] = versions_1.swcLoaderVersion;
26
+ }
27
+ else if (options.compiler === 'babel') {
28
+ // babel-loader is currently included in @nx/webpack
29
+ // TODO(jack): Install babel-loader and other babel packages only as needed
30
+ devDependencies['@babel/preset-react'] = versions_1.babelPresetReactVersion;
31
+ }
32
+ }
33
+ return (0, devkit_1.addDependenciesToPackageJson)(host, {}, devDependencies);
27
34
  }
28
35
  exports.installCommonDependencies = installCommonDependencies;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setDefaults = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  function setDefaults(host, options) {
6
- if (options.skipWorkspaceJson) {
6
+ if (options.skipNxJson) {
7
7
  return;
8
8
  }
9
9
  const nxJson = (0, devkit_1.readNxJson)(host);
@@ -0,0 +1,3 @@
1
+ import { Tree } from '@nx/devkit';
2
+ import { NormalizedSchema, Schema } from '../schema';
3
+ export declare function showPossibleWarnings(tree: Tree, options: NormalizedSchema<Schema>): void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.showPossibleWarnings = void 0;
4
+ const chalk = require("chalk");
5
+ const devkit_1 = require("@nx/devkit");
6
+ function showPossibleWarnings(tree, options) {
7
+ if (options.style === 'styled-jsx' && options.compiler === 'swc') {
8
+ devkit_1.logger.warn(`styled-jsx may not work with SWC. Try using ${chalk.bold('nx g @nx/react:app --compiler=babel')} instead.`);
9
+ }
10
+ }
11
+ exports.showPossibleWarnings = showPossibleWarnings;
@@ -14,7 +14,7 @@ export interface Schema {
14
14
  pascalCaseFiles?: boolean;
15
15
  classComponent?: boolean;
16
16
  routing?: boolean;
17
- skipWorkspaceJson?: boolean;
17
+ skipNxJson?: boolean;
18
18
  js?: boolean;
19
19
  globalCss?: boolean;
20
20
  strict?: boolean;
@@ -98,8 +98,8 @@
98
98
  "default": false,
99
99
  "x-priority": "internal"
100
100
  },
101
- "skipWorkspaceJson": {
102
- "description": "Skip updating `workspace.json` with default options based on values provided to this app (e.g. babel, style).",
101
+ "skipNxJson": {
102
+ "description": "Skip updating `nx.json` with default options based on values provided to this app.",
103
103
  "type": "boolean",
104
104
  "default": false,
105
105
  "x-priority": "internal"
@@ -15,7 +15,7 @@ export interface Schema {
15
15
  remotes?: string[];
16
16
  setParserOptionsProject?: boolean;
17
17
  skipFormat?: boolean;
18
- skipWorkspaceJson?: boolean;
18
+ skipNxJson?: boolean;
19
19
  ssr?: boolean;
20
20
  strict?: boolean;
21
21
  style: SupportedStyles;
@@ -79,7 +79,7 @@
79
79
  "default": false,
80
80
  "x-priority": "internal"
81
81
  },
82
- "skipWorkspaceJson": {
82
+ "skipNxJson": {
83
83
  "description": "Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style).",
84
84
  "type": "boolean",
85
85
  "default": false,
@@ -29,22 +29,6 @@ function updateDependencies(host, schema) {
29
29
  '@testing-library/react': versions_1.testingLibraryReactVersion,
30
30
  });
31
31
  }
32
- function initRootBabelConfig(tree, schema) {
33
- var _a;
34
- if (tree.exists('/babel.config.json') || tree.exists('/babel.config.js')) {
35
- return;
36
- }
37
- if (!schema.skipBabelConfig) {
38
- (0, devkit_1.writeJson)(tree, '/babel.config.json', {
39
- babelrcRoots: ['*'], // Make sure .babelrc files other than root can be loaded in a monorepo
40
- });
41
- }
42
- const nxJson = (0, devkit_1.readNxJson)(tree);
43
- if ((_a = nxJson.namedInputs) === null || _a === void 0 ? void 0 : _a.sharedGlobals) {
44
- nxJson.namedInputs.sharedGlobals.push('{workspaceRoot}/babel.config.json');
45
- }
46
- (0, devkit_1.updateNxJson)(tree, nxJson);
47
- }
48
32
  function reactInitGenerator(host, schema) {
49
33
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
50
34
  const tasks = [];
@@ -57,15 +41,6 @@ function reactInitGenerator(host, schema) {
57
41
  const cypressTask = yield cypressInitGenerator(host, {});
58
42
  tasks.push(cypressTask);
59
43
  }
60
- if (!schema.skipPackageJson && !schema.skipBabelConfig) {
61
- const installBabelTask = (0, devkit_1.addDependenciesToPackageJson)(host, {}, {
62
- '@babel/preset-react': versions_1.babelPresetReactVersion,
63
- });
64
- tasks.push(installBabelTask);
65
- }
66
- if (!schema.skipBabelConfig) {
67
- initRootBabelConfig(host, schema);
68
- }
69
44
  if (!schema.skipPackageJson) {
70
45
  const installTask = updateDependencies(host, schema);
71
46
  tasks.push(installTask);
@@ -1,7 +1,6 @@
1
1
  export interface InitSchema {
2
2
  unitTestRunner?: 'jest' | 'vitest' | 'none';
3
3
  e2eTestRunner?: 'cypress' | 'none';
4
- skipBabelConfig?: boolean;
5
4
  skipFormat?: boolean;
6
5
  skipPackageJson?: boolean;
7
6
  skipHelperLibs?: boolean;
@@ -28,11 +28,6 @@
28
28
  "type": "boolean",
29
29
  "default": false
30
30
  },
31
- "skipBabelConfig": {
32
- "description": "Do not generate a root babel.config.json (if babel is not needed).",
33
- "type": "boolean",
34
- "default": false
35
- },
36
31
  "skipHelperLibs": {
37
32
  "description": "Do not install tslib.",
38
33
  "type": "boolean",
@@ -10,9 +10,31 @@ function createFiles(host, options) {
10
10
  (0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, '../files/common'), options.projectRoot, substitutions);
11
11
  if (options.bundler === 'vite' || options.unitTestRunner === 'vitest') {
12
12
  (0, devkit_1.generateFiles)(host, (0, devkit_1.joinPathFragments)(__dirname, '../files/vite'), options.projectRoot, substitutions);
13
- if (host.exists((0, devkit_1.joinPathFragments)(options.projectRoot, '.babelrc'))) {
14
- host.delete((0, devkit_1.joinPathFragments)(options.projectRoot, '.babelrc'));
15
- }
13
+ }
14
+ if (options.compiler === 'babel') {
15
+ (0, devkit_1.writeJson)(host, (0, devkit_1.joinPathFragments)(options.projectRoot, '.babelrc'), {
16
+ presets: [
17
+ [
18
+ '@nx/react/babel',
19
+ {
20
+ runtime: 'automatic',
21
+ useBuiltIns: 'usage',
22
+ importSource: options.style === '@emotion/styled'
23
+ ? '@emotion/react'
24
+ : undefined,
25
+ },
26
+ ],
27
+ ],
28
+ plugins: [
29
+ options.style === 'styled-components'
30
+ ? ['styled-components', { pure: true, ssr: true }]
31
+ : undefined,
32
+ options.style === 'styled-jsx' ? 'styled-jsx/babel' : undefined,
33
+ options.style === '@emotion/styled'
34
+ ? '@emotion/babel-plugin'
35
+ : undefined,
36
+ ].filter(Boolean),
37
+ });
16
38
  }
17
39
  if (!options.publishable && !options.buildable) {
18
40
  host.delete(`${options.projectRoot}/package.json`);
@@ -31,6 +31,9 @@ function installCommonDependencies(host, options) {
31
31
  if (options.compiler === 'swc') {
32
32
  tasks.push((0, add_swc_dependencies_1.addSwcDependencies)(host));
33
33
  }
34
+ else if (options.compiler === 'babel') {
35
+ tasks.push((0, devkit_1.addDependenciesToPackageJson)(host, {}, { '@babel/preset-react': versions_1.babelPresetReactVersion }));
36
+ }
34
37
  return (0, devkit_1.runTasksInSerial)(...tasks);
35
38
  }
36
39
  exports.installCommonDependencies = installCommonDependencies;
@@ -26,7 +26,7 @@ function libraryGenerator(host, schema) {
26
26
  if (!options.component) {
27
27
  options.style = 'none';
28
28
  }
29
- const initTask = yield (0, init_1.default)(host, Object.assign(Object.assign({}, options), { e2eTestRunner: 'none', skipFormat: true, skipBabelConfig: options.bundler === 'vite' || options.compiler === 'swc', skipHelperLibs: options.bundler === 'vite' }));
29
+ const initTask = yield (0, init_1.default)(host, Object.assign(Object.assign({}, options), { e2eTestRunner: 'none', skipFormat: true, skipHelperLibs: options.bundler === 'vite' }));
30
30
  tasks.push(initTask);
31
31
  (0, devkit_1.addProjectConfiguration)(host, options.name, {
32
32
  root: options.projectRoot,
@@ -17,7 +17,7 @@ export interface Schema {
17
17
  routing?: boolean;
18
18
  setParserOptionsProject?: boolean;
19
19
  skipFormat: boolean;
20
- skipWorkspaceJson?: boolean;
20
+ skipNxJson?: boolean;
21
21
  ssr?: boolean;
22
22
  strict?: boolean;
23
23
  style: SupportedStyles;
@@ -84,7 +84,7 @@
84
84
  "default": false,
85
85
  "x-priority": "internal"
86
86
  },
87
- "skipWorkspaceJson": {
87
+ "skipNxJson": {
88
88
  "description": "Skip updating workspace.json with default options based on values provided to this app (e.g. babel, style).",
89
89
  "type": "boolean",
90
90
  "default": false,
@@ -1,16 +0,0 @@
1
- {
2
- "presets": [
3
- [
4
- "@nx/react/babel", {
5
- "runtime": "automatic",
6
- "useBuiltIns": "usage"
7
- <% if (style === '@emotion/styled') { %>,"importSource": "@emotion/react"<% } %>
8
- }
9
- ]
10
- ],
11
- "plugins": [
12
- <% if (style === 'styled-components') { %>["styled-components", { "pure": true, "ssr": true }]<% } %>
13
- <% if (style === 'styled-jsx') { %>"styled-jsx/babel"<% } %>
14
- <% if (style === '@emotion/styled') { %>"@emotion/babel-plugin"<% } %>
15
- ]
16
- }