@nx/rollup 18.1.0-beta.1 → 18.1.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/rollup",
3
- "version": "18.1.0-beta.1",
3
+ "version": "18.1.0-beta.3",
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": {
@@ -45,12 +45,20 @@
45
45
  "rollup-plugin-typescript2": "0.36.0",
46
46
  "rxjs": "^7.8.0",
47
47
  "tslib": "^2.3.0",
48
- "@nx/devkit": "18.1.0-beta.1",
49
- "@nx/js": "18.1.0-beta.1",
50
- "@nrwl/rollup": "18.1.0-beta.1"
48
+ "@nx/devkit": "18.1.0-beta.3",
49
+ "@nx/js": "18.1.0-beta.3",
50
+ "@nrwl/rollup": "18.1.0-beta.3"
51
51
  },
52
52
  "publishConfig": {
53
53
  "access": "public"
54
54
  },
55
+ "exports": {
56
+ "./package.json": "./package.json",
57
+ ".": "./index.js",
58
+ "./executors.json": "./executors.json",
59
+ "./generators.json": "./generators.json",
60
+ "./migrations.json": "./migrations.json",
61
+ "./plugin": "./plugin.js"
62
+ },
55
63
  "type": "commonjs"
56
64
  }
package/plugin.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { createNodes, RollupPluginOptions } from './src/plugins/plugin';
package/plugin.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createNodes = void 0;
4
+ var plugin_1 = require("./src/plugins/plugin");
5
+ Object.defineProperty(exports, "createNodes", { enumerable: true, get: function () { return plugin_1.createNodes; } });
@@ -9,5 +9,5 @@ export type RollupExecutorEvent = {
9
9
  outfile?: string;
10
10
  };
11
11
  export declare function rollupExecutor(rawOptions: RollupExecutorOptions, context: ExecutorContext): AsyncGenerator<RollupExecutorEvent, any, undefined>;
12
- export declare function createRollupOptions(options: NormalizedRollupExecutorOptions, dependencies: DependentBuildableProjectNode[], context: ExecutorContext, packageJson: PackageJson, sourceRoot: string, npmDeps: string[]): rollup.InputOptions[];
12
+ export declare function createRollupOptions(options: NormalizedRollupExecutorOptions, dependencies: DependentBuildableProjectNode[], context: ExecutorContext, packageJson: PackageJson, sourceRoot: string, npmDeps: string[]): Promise<rollup.InputOptions[]>;
13
13
  export default rollupExecutor;
@@ -20,6 +20,7 @@ const analyze_plugin_1 = require("./lib/analyze-plugin");
20
20
  const fs_1 = require("../../utils/fs");
21
21
  const swc_plugin_1 = require("./lib/swc-plugin");
22
22
  const update_package_json_1 = require("./lib/update-package-json");
23
+ const config_utils_1 = require("@nx/devkit/src/utils/config-utils");
23
24
  // These use require because the ES import isn't correct.
24
25
  const commonjs = require('@rollup/plugin-commonjs');
25
26
  const image = require('@rollup/plugin-image');
@@ -37,7 +38,7 @@ async function* rollupExecutor(rawOptions, context) {
37
38
  const npmDeps = (context.projectGraph.dependencies[context.projectName] ?? [])
38
39
  .filter((d) => d.target.startsWith('npm:'))
39
40
  .map((d) => d.target.slice(4));
40
- const rollupOptions = createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps);
41
+ const rollupOptions = await createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps);
41
42
  const outfile = resolveOutfile(context, options);
42
43
  if (options.watch) {
43
44
  const watcher = rollup.watch(rollupOptions);
@@ -93,7 +94,7 @@ async function* rollupExecutor(rawOptions, context) {
93
94
  }
94
95
  exports.rollupExecutor = rollupExecutor;
95
96
  // -----------------------------------------------------------------------------
96
- function createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps) {
97
+ async function createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps) {
97
98
  const useBabel = options.compiler === 'babel';
98
99
  const useTsc = options.compiler === 'tsc';
99
100
  const useSwc = options.compiler === 'swc';
@@ -115,7 +116,7 @@ function createRollupOptions(options, dependencies, context, packageJson, source
115
116
  }
116
117
  options.format = ['cjs'];
117
118
  }
118
- return options.format.map((format, idx) => {
119
+ const _rollupOptions = options.format.map(async (format, idx) => {
119
120
  // Either we're generating only one format, so we should bundle types
120
121
  // OR we are generating dual formats, so only bundle types for CJS.
121
122
  const shouldBundleTypes = options.format.length === 1 || format === 'cjs';
@@ -217,10 +218,31 @@ function createRollupOptions(options, dependencies, context, packageJson, source
217
218
  },
218
219
  plugins,
219
220
  };
220
- return options.rollupConfig.reduce((currentConfig, plugin) => {
221
- return require(plugin)(currentConfig, options);
222
- }, rollupConfig);
221
+ const userDefinedRollupConfigs = options.rollupConfig.map((plugin) => (0, config_utils_1.loadConfigFile)(plugin));
222
+ let finalConfig = rollupConfig;
223
+ for (const _config of userDefinedRollupConfigs) {
224
+ const config = await _config;
225
+ if (typeof config === 'function') {
226
+ finalConfig = config(finalConfig, options);
227
+ }
228
+ else {
229
+ finalConfig = {
230
+ ...finalConfig,
231
+ ...config,
232
+ plugins: [
233
+ ...(finalConfig.plugins?.length > 0 ? finalConfig.plugins : []),
234
+ ...(config.plugins?.length > 0 ? config.plugins : []),
235
+ ],
236
+ };
237
+ }
238
+ }
239
+ return finalConfig;
223
240
  });
241
+ const rollupOptions = [];
242
+ for (const rollupOption of _rollupOptions) {
243
+ rollupOptions.push(await rollupOption);
244
+ }
245
+ return rollupOptions;
224
246
  }
225
247
  exports.createRollupOptions = createRollupOptions;
226
248
  function createTsCompilerOptions(config, dependencies, options) {
@@ -3,11 +3,38 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rollupInitGenerator = void 0;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const versions_1 = require("../../utils/versions");
6
+ const update_package_scripts_1 = require("@nx/devkit/src/utils/update-package-scripts");
7
+ const plugin_1 = require("../../plugins/plugin");
8
+ function addPlugin(tree) {
9
+ const nxJson = (0, devkit_1.readNxJson)(tree);
10
+ nxJson.plugins ??= [];
11
+ for (const plugin of nxJson.plugins) {
12
+ if (typeof plugin === 'string'
13
+ ? plugin === '@nx/rollup/plugin'
14
+ : plugin.plugin === '@nx/rollup/plugin') {
15
+ return;
16
+ }
17
+ }
18
+ nxJson.plugins.push({
19
+ plugin: '@nx/rollup/plugin',
20
+ options: {
21
+ buildTargetName: 'build',
22
+ },
23
+ });
24
+ (0, devkit_1.updateNxJson)(tree, nxJson);
25
+ }
6
26
  async function rollupInitGenerator(tree, schema) {
7
27
  let task = () => { };
8
28
  if (!schema.skipPackageJson) {
9
29
  task = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { '@nx/rollup': versions_1.nxVersion }, undefined, schema.keepExistingVersions);
10
30
  }
31
+ schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';
32
+ if (schema.addPlugin) {
33
+ addPlugin(tree);
34
+ }
35
+ if (schema.updatePackageScripts) {
36
+ await (0, update_package_scripts_1.updatePackageScripts)(tree, plugin_1.createNodes);
37
+ }
11
38
  if (!schema.skipFormat) {
12
39
  await (0, devkit_1.formatFiles)(tree);
13
40
  }
@@ -2,4 +2,6 @@ export interface Schema {
2
2
  skipFormat?: boolean;
3
3
  skipPackageJson?: boolean;
4
4
  keepExistingVersions?: boolean;
5
+ updatePackageScripts?: boolean;
6
+ addPlugin?: boolean;
5
7
  }
@@ -21,6 +21,12 @@
21
21
  "x-priority": "internal",
22
22
  "description": "Keep existing dependencies versions",
23
23
  "default": false
24
+ },
25
+ "updatePackageScripts": {
26
+ "type": "boolean",
27
+ "x-priority": "internal",
28
+ "description": "Update `package.json` scripts with inferred targets",
29
+ "default": false
24
30
  }
25
31
  },
26
32
  "required": []
@@ -0,0 +1,6 @@
1
+ import { type CreateDependencies, type CreateNodes } from '@nx/devkit';
2
+ export declare const createDependencies: CreateDependencies;
3
+ export interface RollupPluginOptions {
4
+ buildTargetName?: string;
5
+ }
6
+ export declare const createNodes: CreateNodes<RollupPluginOptions>;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createNodes = exports.createDependencies = void 0;
4
+ const cache_directory_1 = require("nx/src/utils/cache-directory");
5
+ const path_1 = require("path");
6
+ const fs_1 = require("fs");
7
+ const devkit_1 = require("@nx/devkit");
8
+ const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
9
+ const js_1 = require("@nx/js");
10
+ const get_named_inputs_1 = require("@nx/devkit/src/utils/get-named-inputs");
11
+ const loadConfigFile = require("rollup/dist/loadConfigFile");
12
+ const cachePath = (0, path_1.join)(cache_directory_1.projectGraphCacheDirectory, 'rollup.hash');
13
+ const targetsCache = (0, fs_1.existsSync)(cachePath) ? readTargetsCache() : {};
14
+ const calculatedTargets = {};
15
+ function readTargetsCache() {
16
+ return (0, devkit_1.readJsonFile)(cachePath);
17
+ }
18
+ function writeTargetsToCache(targets) {
19
+ (0, devkit_1.writeJsonFile)(cachePath, targets);
20
+ }
21
+ const createDependencies = () => {
22
+ writeTargetsToCache(calculatedTargets);
23
+ return [];
24
+ };
25
+ exports.createDependencies = createDependencies;
26
+ exports.createNodes = [
27
+ '**/rollup.config.{js,cjs,mjs}',
28
+ async (configFilePath, options, context) => {
29
+ const projectRoot = (0, path_1.dirname)(configFilePath);
30
+ const fullyQualifiedProjectRoot = (0, path_1.join)(context.workspaceRoot, projectRoot);
31
+ // Do not create a project if package.json and project.json do not exist
32
+ const siblingFiles = (0, fs_1.readdirSync)(fullyQualifiedProjectRoot);
33
+ if (!siblingFiles.includes('package.json') &&
34
+ !siblingFiles.includes('project.json')) {
35
+ return {};
36
+ }
37
+ options = normalizeOptions(options);
38
+ const hash = (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context, [
39
+ (0, js_1.getLockFileName)((0, devkit_1.detectPackageManager)(context.workspaceRoot)),
40
+ ]);
41
+ const targets = targetsCache[hash]
42
+ ? targetsCache[hash]
43
+ : await buildRollupTarget(configFilePath, projectRoot, options, context);
44
+ calculatedTargets[hash] = targets;
45
+ return {
46
+ projects: {
47
+ [projectRoot]: {
48
+ root: projectRoot,
49
+ targets,
50
+ },
51
+ },
52
+ };
53
+ },
54
+ ];
55
+ async function buildRollupTarget(configFilePath, projectRoot, options, context) {
56
+ const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
57
+ const rollupConfig = (await loadConfigFile((0, devkit_1.joinPathFragments)(context.workspaceRoot, configFilePath))).options;
58
+ const outputs = getOutputs(rollupConfig, projectRoot);
59
+ const targets = {};
60
+ targets[options.buildTargetName] = {
61
+ command: `rollup -c ${(0, path_1.basename)(configFilePath)}`,
62
+ options: { cwd: projectRoot },
63
+ cache: true,
64
+ dependsOn: [`^${options.buildTargetName}`],
65
+ inputs: [
66
+ ...('production' in namedInputs
67
+ ? ['production', '^production']
68
+ : ['default', '^default']),
69
+ ],
70
+ outputs,
71
+ };
72
+ return targets;
73
+ }
74
+ function getOutputs(rollupConfigs, projectRoot) {
75
+ const outputs = new Set();
76
+ for (const rollupConfig of rollupConfigs) {
77
+ if (rollupConfig.output) {
78
+ const rollupConfigOutputs = [];
79
+ if (Array.isArray(rollupConfig.output)) {
80
+ rollupConfigOutputs.push(...rollupConfig.output);
81
+ }
82
+ else {
83
+ rollupConfigOutputs.push(rollupConfig.output);
84
+ }
85
+ for (const output of rollupConfigOutputs) {
86
+ const outputPathFromConfig = output.dir
87
+ ? output.dir
88
+ : output.file
89
+ ? (0, path_1.dirname)(output.file)
90
+ : 'dist';
91
+ const outputPath = projectRoot === '.'
92
+ ? (0, devkit_1.joinPathFragments)(`{workspaceRoot}`, outputPathFromConfig)
93
+ : (0, devkit_1.joinPathFragments)(`{workspaceRoot}`, projectRoot, outputPathFromConfig);
94
+ outputs.add(outputPath);
95
+ }
96
+ }
97
+ }
98
+ return Array.from(outputs);
99
+ }
100
+ function normalizeOptions(options) {
101
+ options ??= {};
102
+ options.buildTargetName ??= 'build';
103
+ return options;
104
+ }