@nx/rollup 19.3.0 → 19.4.0-beta.1

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/generators.json CHANGED
@@ -14,6 +14,11 @@
14
14
  "factory": "./src/generators/configuration/configuration",
15
15
  "schema": "./src/generators/configuration/schema.json",
16
16
  "description": "Add rollup configuration to a project."
17
+ },
18
+ "convert-to-inferred": {
19
+ "factory": "./src/generators/convert-to-inferred/convert-to-inferred",
20
+ "schema": "./src/generators/convert-to-inferred/schema.json",
21
+ "description": "Convert existing Rollup project(s) using `@nx/rollup:*` executors to use `@nx/rollup/plugin`."
17
22
  }
18
23
  }
19
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/rollup",
3
- "version": "19.3.0",
3
+ "version": "19.4.0-beta.1",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for Rollup contains executors and generators that support building applications using Rollup.",
6
6
  "repository": {
@@ -43,9 +43,9 @@
43
43
  "rollup-plugin-postcss": "^4.0.2",
44
44
  "rollup-plugin-typescript2": "^0.36.0",
45
45
  "tslib": "^2.3.0",
46
- "@nx/devkit": "19.3.0",
47
- "@nx/js": "19.3.0",
48
- "@nrwl/rollup": "19.3.0"
46
+ "@nx/devkit": "19.4.0-beta.1",
47
+ "@nx/js": "19.4.0-beta.1",
48
+ "@nrwl/rollup": "19.4.0-beta.1"
49
49
  },
50
50
  "publishConfig": {
51
51
  "access": "public"
@@ -0,0 +1,7 @@
1
+ import { type Tree } from '@nx/devkit';
2
+ interface Schema {
3
+ project?: string;
4
+ skipFormat?: boolean;
5
+ }
6
+ export declare function convertToInferred(tree: Tree, options: Schema): Promise<void>;
7
+ export default convertToInferred;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToInferred = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
6
+ const extract_rollup_config_from_executor_options_1 = require("./lib/extract-rollup-config-from-executor-options");
7
+ async function convertToInferred(tree, options) {
8
+ let migrated = 0;
9
+ const projects = (0, devkit_1.getProjects)(tree);
10
+ (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nx/rollup:rollup', (_, projectName, targetName, configurationName) => {
11
+ if (options.project && projectName !== options.project)
12
+ return;
13
+ const project = projects.get(projectName);
14
+ const target = project.targets[targetName];
15
+ // We'll handle configurations when dealing with default options.
16
+ if (configurationName)
17
+ return;
18
+ // Since targetDefaults for '@nx/rollup:rollup' will no longer apply, we want to copy them to the target options.
19
+ const nxJson = (0, devkit_1.readNxJson)(tree);
20
+ const defaults = nxJson.targetDefaults['@nx/rollup:rollup'];
21
+ if (defaults) {
22
+ for (const [key, value] of Object.entries(defaults)) {
23
+ target[key] ??= value;
24
+ }
25
+ }
26
+ const extractedPluginOptions = (0, extract_rollup_config_from_executor_options_1.extractRollupConfigFromExecutorOptions)(tree, target.options, target.configurations, project.root);
27
+ // If rollup is not an external dependency, add it
28
+ if (target.inputs &&
29
+ !target.inputs.some((i) => Array.isArray(i['externalDependencies']) &&
30
+ i['externalDependencies'].includes('rollup'))) {
31
+ const idx = target.inputs.findIndex((i) => Array.isArray(i['externalDependencies']));
32
+ if (idx === -1) {
33
+ target.inputs.push({ externalDependencies: ['rollup'] });
34
+ }
35
+ else {
36
+ target.inputs[idx]['externalDependencies'].push('rollup');
37
+ }
38
+ }
39
+ // Clean up the target now that it is inferred
40
+ delete target.executor;
41
+ if (target.outputs &&
42
+ target.outputs.length === 1 &&
43
+ // "{projectRoot}/{options.outputPath}" is an invalid output for Rollup since
44
+ // there would be a mismatch between where the executor outputs to and where Nx caches.
45
+ // If users have this set erroneously, then it will continue to not work.
46
+ (target.outputs[0] === '{options.outputPath}' ||
47
+ target.outputs[0] === '{workspaceRoot}/{options.outputPath}')) {
48
+ // If only the default `options.outputPath` is set as output, remove it and use path inferred from `rollup.config.js`.
49
+ delete target.outputs;
50
+ }
51
+ else {
52
+ // Otherwise, replace `options.outputPath` with what is inferred from `rollup.config.js`.
53
+ target.outputs = target.outputs.map((output) =>
54
+ // Again, "{projectRoot}/{options.outputPath}" is an invalid output for Rollup.
55
+ output === '{options.outputPath}' ||
56
+ output === '{workspaceRoot}/{options.outputPath}'
57
+ ? `{projectRoot}/${extractedPluginOptions.outputPath}`
58
+ : output);
59
+ }
60
+ if (Object.keys(target.options).length === 0)
61
+ delete target.options;
62
+ if (Object.keys(target).length === 0)
63
+ delete project.targets[targetName];
64
+ (0, devkit_1.updateProjectConfiguration)(tree, projectName, project);
65
+ nxJson.plugins ??= [];
66
+ if (!nxJson.plugins.some((p) => typeof p === 'string'
67
+ ? p === '@nx/rollup/plugin'
68
+ : p.plugin === '@nx/rollup/plugin')) {
69
+ nxJson.plugins.push({
70
+ plugin: '@nx/rollup/plugin',
71
+ options: {
72
+ targetName: 'build',
73
+ },
74
+ });
75
+ }
76
+ (0, devkit_1.updateNxJson)(tree, nxJson);
77
+ migrated++;
78
+ });
79
+ if (migrated === 0) {
80
+ throw new Error('Could not find any targets to migrate.');
81
+ }
82
+ if (!options.skipFormat) {
83
+ await (0, devkit_1.formatFiles)(tree);
84
+ }
85
+ }
86
+ exports.convertToInferred = convertToInferred;
87
+ exports.default = convertToInferred;
@@ -0,0 +1,3 @@
1
+ import { Tree } from '@nx/devkit';
2
+ import { RollupExecutorOptions } from '../../../executors/rollup/schema';
3
+ export declare function extractRollupConfigFromExecutorOptions(tree: Tree, options: RollupExecutorOptions, configurations: Record<string, Partial<RollupExecutorOptions>>, projectRoot: string): Record<string, unknown>;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractRollupConfigFromExecutorOptions = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const normalize_path_options_1 = require("./normalize-path-options");
6
+ function extractRollupConfigFromExecutorOptions(tree, options, configurations, projectRoot) {
7
+ (0, normalize_path_options_1.normalizePathOptions)(projectRoot, options);
8
+ let newRollupConfigContent;
9
+ const hasConfigurations = !!configurations && Object.keys(configurations).length > 0;
10
+ const oldRollupConfig = Array.isArray(options.rollupConfig)
11
+ ? options.rollupConfig
12
+ : options.rollupConfig
13
+ ? [options.rollupConfig]
14
+ : [];
15
+ delete options.rollupConfig;
16
+ // Resolve conflict with rollup.config.js if it exists.
17
+ for (let i = 0; i < oldRollupConfig.length; i++) {
18
+ const file = oldRollupConfig[i];
19
+ if (file === './rollup.config.js') {
20
+ tree.rename((0, devkit_1.joinPathFragments)(projectRoot, 'rollup.config.js'), (0, devkit_1.joinPathFragments)(projectRoot, `rollup.migrated.config.js`));
21
+ oldRollupConfig.splice(i, 1, './rollup.migrated.config.js');
22
+ }
23
+ }
24
+ const defaultOptions = {};
25
+ for (const [key, value] of Object.entries(options)) {
26
+ if (key === 'watch')
27
+ continue;
28
+ delete options[key];
29
+ defaultOptions[key] = value;
30
+ }
31
+ if (hasConfigurations) {
32
+ const configurationOptions = {};
33
+ for (const [key, value] of Object.entries(configurations)) {
34
+ for (const [optionKey, optionValue] of Object.entries(value)) {
35
+ if (optionKey === 'watch')
36
+ continue;
37
+ delete value[optionKey];
38
+ configurationOptions[key] ??= {};
39
+ configurationOptions[key][optionKey] = optionValue;
40
+ }
41
+ }
42
+ newRollupConfigContent = (0, devkit_1.stripIndents) `
43
+ const { withNx } = require('@nx/rollup/with-nx');
44
+
45
+ // These options were migrated by @nx/rollup:convert-to-inferred from project.json
46
+ const configValues = ${JSON.stringify({
47
+ default: defaultOptions,
48
+ ...configurationOptions,
49
+ }, null, 2)};
50
+
51
+ // Determine the correct configValue to use based on the configuration
52
+ const nxConfiguration = process.env.NX_TASK_TARGET_CONFIGURATION ?? 'default';
53
+
54
+ const options = {
55
+ ...configValues.default,
56
+ ...configValues[nxConfiguration],
57
+ };
58
+
59
+ ${oldRollupConfig.length > 0 ? 'let' : 'const'} config = withNx(options, {
60
+ // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
61
+ // e.g.
62
+ // output: { sourcemap: true },
63
+ });
64
+
65
+ ${oldRollupConfig
66
+ // Normalize path
67
+ .map((s) => `config = require('${s}')(config, options);`)
68
+ .join('\n')}
69
+
70
+ module.exports = config;
71
+ `;
72
+ }
73
+ else {
74
+ newRollupConfigContent = (0, devkit_1.stripIndents) `
75
+ const { withNx } = require('@nx/rollup/with-nx');
76
+
77
+ // These options were migrated by @nx/rollup:convert-to-inferred from project.json
78
+ const options = ${JSON.stringify(defaultOptions, null, 2)};
79
+
80
+ ${oldRollupConfig.length > 0 ? 'let' : 'const'} config = withNx(options, {
81
+ // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
82
+ // e.g.
83
+ // output: { sourcemap: true },
84
+ });
85
+
86
+ ${oldRollupConfig
87
+ // Normalize path
88
+ .map((s) => `config = require('${s}')(config, options);`)
89
+ .join('\n')}
90
+
91
+ module.exports = config;
92
+ `;
93
+ }
94
+ tree.write((0, devkit_1.joinPathFragments)(projectRoot, `rollup.config.js`), newRollupConfigContent);
95
+ return defaultOptions;
96
+ }
97
+ exports.extractRollupConfigFromExecutorOptions = extractRollupConfigFromExecutorOptions;
@@ -0,0 +1,2 @@
1
+ import { RollupExecutorOptions } from '../../../executors/rollup/schema';
2
+ export declare function normalizePathOptions(projectRoot: string, options: RollupExecutorOptions): void;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizePathOptions = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const executorPathFieldsToMigrate = [
6
+ 'tsConfig',
7
+ 'project',
8
+ 'main',
9
+ 'outputPath',
10
+ 'rollupConfig',
11
+ 'additionalEntryPoints',
12
+ ];
13
+ function normalizePathOptions(projectRoot, options) {
14
+ for (const [key, value] of Object.entries(options)) {
15
+ if (!executorPathFieldsToMigrate.includes(key))
16
+ continue;
17
+ if (Array.isArray(value)) {
18
+ options[key] = value.map((v) => normalizeValue(projectRoot, key, v));
19
+ }
20
+ else {
21
+ options[key] = normalizeValue(projectRoot, key, value);
22
+ }
23
+ }
24
+ }
25
+ exports.normalizePathOptions = normalizePathOptions;
26
+ function normalizeValue(projectRoot, key, value) {
27
+ // Logic matches `@nx/rollup:rollup` in `normalizePluginPath` function.
28
+ if (!value)
29
+ return value;
30
+ if (key === 'rollupConfig') {
31
+ try {
32
+ // If this can load as npm module, keep as is.
33
+ require.resolve(value);
34
+ return value;
35
+ }
36
+ catch {
37
+ // Otherwise continue below convert to relative path from project.
38
+ }
39
+ }
40
+ if (value.startsWith(`${projectRoot}/`)) {
41
+ return value.replace(new RegExp(`^${projectRoot}/`), './');
42
+ }
43
+ else {
44
+ return (0, devkit_1.joinPathFragments)((0, devkit_1.offsetFromRoot)(projectRoot), value);
45
+ }
46
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "$id": "NxRollupConvertToInferred",
4
+ "description": "Convert existing Rollup project(s) using `@nx/rollup:rollup` executor to use `@nx/rollup/plugin`. Defaults to migrating all projects. Pass '--project' to migrate only one target.",
5
+ "title": "Convert Rollup project from executor to plugin",
6
+ "type": "object",
7
+ "properties": {
8
+ "project": {
9
+ "type": "string",
10
+ "description": "The project to convert from using the `@nx/rollup:rollup` executor to use `@nx/rollup/plugin`.",
11
+ "x-priority": "important"
12
+ },
13
+ "skipFormat": {
14
+ "type": "boolean",
15
+ "description": "Whether to format files at the end of the migration.",
16
+ "default": false
17
+ }
18
+ }
19
+ }
@@ -189,6 +189,10 @@ dependencies) {
189
189
  }
190
190
  exports.withNx = withNx;
191
191
  function createInput(options) {
192
+ // During graph creation, these input entries don't affect target configuration, so we can skip them.
193
+ // If convert-to-inferred generator is used, and project uses configurations, some options like main might be missing from default options.
194
+ if (global.NX_GRAPH_CREATION)
195
+ return {};
192
196
  const mainEntryFileName = options.outputFileName || options.main;
193
197
  const input = {};
194
198
  input[(0, node_path_1.parse)(mainEntryFileName).name] = (0, node_path_1.join)(devkit_1.workspaceRoot, options.main);