@nx/rollup 0.0.0-pr-22179-271588f → 0.0.0-pr-26482-ebe2dba

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.
Files changed (43) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/migrations.json +24 -7
  4. package/package.json +15 -25
  5. package/src/executors/rollup/lib/normalize.d.ts +2 -5
  6. package/src/executors/rollup/lib/normalize.js +3 -60
  7. package/src/executors/rollup/rollup.impl.d.ts +3 -9
  8. package/src/executors/rollup/rollup.impl.js +70 -246
  9. package/src/generators/configuration/configuration.d.ts +1 -1
  10. package/src/generators/configuration/configuration.js +44 -16
  11. package/src/generators/configuration/schema.d.ts +1 -0
  12. package/src/generators/init/init.js +11 -25
  13. package/src/plugins/delete-output.d.ts +5 -0
  14. package/src/plugins/delete-output.js +11 -0
  15. package/src/plugins/package-json/generate-package-json.d.ts +12 -0
  16. package/src/plugins/package-json/generate-package-json.js +13 -0
  17. package/src/plugins/package-json/update-package-json.d.ts +10 -0
  18. package/src/{executors/rollup/lib → plugins/package-json}/update-package-json.js +12 -11
  19. package/src/plugins/plugin.js +32 -17
  20. package/src/plugins/with-nx/get-project-node.d.ts +2 -0
  21. package/src/plugins/with-nx/get-project-node.js +22 -0
  22. package/src/plugins/with-nx/normalize-options.d.ts +2 -0
  23. package/src/plugins/with-nx/normalize-options.js +83 -0
  24. package/src/plugins/with-nx/with-nx-options.d.ts +33 -0
  25. package/src/plugins/with-nx/with-nx-options.js +2 -0
  26. package/src/plugins/with-nx/with-nx.d.ts +4 -0
  27. package/src/plugins/with-nx/with-nx.js +233 -0
  28. package/src/utils/ensure-dependencies.js +14 -7
  29. package/src/utils/has-plugin.d.ts +2 -0
  30. package/src/utils/has-plugin.js +11 -0
  31. package/src/utils/versions.d.ts +2 -0
  32. package/src/utils/versions.js +3 -1
  33. package/with-nx.d.ts +2 -0
  34. package/with-nx.js +5 -0
  35. package/src/executors/rollup/lib/run-rollup.d.ts +0 -4
  36. package/src/executors/rollup/lib/run-rollup.js +0 -15
  37. package/src/executors/rollup/lib/update-package-json.d.ts +0 -3
  38. package/src/migrations/update-15-0-0/add-babel-inputs.d.ts +0 -2
  39. package/src/migrations/update-15-0-0/add-babel-inputs.js +0 -9
  40. /package/src/{executors/rollup/lib/analyze-plugin.d.ts → plugins/analyze.d.ts} +0 -0
  41. /package/src/{executors/rollup/lib/analyze-plugin.js → plugins/analyze.js} +0 -0
  42. /package/src/{executors/rollup/lib/swc-plugin.d.ts → plugins/swc.d.ts} +0 -0
  43. /package/src/{executors/rollup/lib/swc-plugin.js → plugins/swc.js} +0 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2017-2023 Narwhal Technologies Inc.
3
+ Copyright (c) 2017-2024 Narwhal Technologies Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
package/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  Nx is a build system with built-in tooling and advanced CI capabilities. It helps you maintain and scale monorepos, both locally and on CI.
26
26
 
27
- This package is a [Rollup plugin for Nx](https://nx.dev/packages/rollup).
27
+ This package is a [Rollup plugin for Nx](https://nx.dev/nx-api/rollup).
28
28
 
29
29
  ## Getting Started
30
30
 
package/migrations.json CHANGED
@@ -1,11 +1,5 @@
1
1
  {
2
2
  "generators": {
3
- "add-babel-inputs": {
4
- "cli": "nx",
5
- "version": "15.0.0-beta.0",
6
- "description": "Adds babel.config.json to the hash of all tasks",
7
- "factory": "./src/migrations/update-15-0-0/add-babel-inputs"
8
- },
9
3
  "update-16-0-0-add-nx-packages": {
10
4
  "cli": "nx",
11
5
  "version": "16.0.0-beta.1",
@@ -25,5 +19,28 @@
25
19
  "factory": "./src/migrations/update-16-6-0/explicitly-set-projects-to-update-buildable-deps"
26
20
  }
27
21
  },
28
- "packageJsonUpdates": {}
22
+ "packageJsonUpdates": {
23
+ "18.2.0": {
24
+ "version": "18.2.0-beta.1",
25
+ "packages": {
26
+ "core-js": {
27
+ "version": "3.36.1",
28
+ "alwaysAddToPackageJson": false
29
+ }
30
+ }
31
+ },
32
+ "19.0.0": {
33
+ "version": "19.0.0-beta.0",
34
+ "packages": {
35
+ "@rollup/plugin-url": {
36
+ "version": "^8.0.2",
37
+ "alwaysAddToPackageJson": false
38
+ },
39
+ "@svgr/rollup": {
40
+ "version": "^8.1.0",
41
+ "alwaysAddToPackageJson": false
42
+ }
43
+ }
44
+ }
45
+ }
29
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/rollup",
3
- "version": "0.0.0-pr-22179-271588f",
3
+ "version": "0.0.0-pr-26482-ebe2dba",
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": {
@@ -14,7 +14,7 @@
14
14
  "Web",
15
15
  "CLI"
16
16
  ],
17
- "main": "./index.js",
17
+ "main": "./index",
18
18
  "typings": "./index.d.ts",
19
19
  "author": "Victor Savkin",
20
20
  "license": "MIT",
@@ -29,36 +29,26 @@
29
29
  "migrations": "./migrations.json"
30
30
  },
31
31
  "dependencies": {
32
- "@rollup/plugin-babel": "^5.3.0",
33
- "@rollup/plugin-commonjs": "^20.0.0",
34
- "@rollup/plugin-image": "^2.1.0",
35
- "@rollup/plugin-json": "^4.1.0",
36
- "@rollup/plugin-node-resolve": "^13.0.4",
32
+ "@rollup/plugin-babel": "^6.0.4",
33
+ "@rollup/plugin-commonjs": "^25.0.7",
34
+ "@rollup/plugin-image": "^3.0.3",
35
+ "@rollup/plugin-json": "^6.1.0",
36
+ "@rollup/plugin-node-resolve": "^15.2.3",
37
37
  "autoprefixer": "^10.4.9",
38
38
  "babel-plugin-transform-async-to-promises": "^0.8.15",
39
39
  "chalk": "^4.1.0",
40
- "postcss": "^8.4.14",
41
- "rollup": "^2.56.2",
42
- "rollup-plugin-copy": "^3.4.0",
43
- "rollup-plugin-peer-deps-external": "^2.2.4",
44
- "rollup-plugin-postcss": "^4.0.1",
45
- "rollup-plugin-typescript2": "0.36.0",
46
- "rxjs": "^7.8.0",
40
+ "postcss": "^8.4.38",
41
+ "rollup": "^4.14.0",
42
+ "rollup-plugin-copy": "^3.5.0",
43
+ "rollup-plugin-postcss": "^4.0.2",
44
+ "rollup-plugin-typescript2": "^0.36.0",
47
45
  "tslib": "^2.3.0",
48
- "@nx/devkit": "0.0.0-pr-22179-271588f",
49
- "@nx/js": "0.0.0-pr-22179-271588f",
50
- "@nrwl/rollup": "0.0.0-pr-22179-271588f"
46
+ "@nx/devkit": "0.0.0-pr-26482-ebe2dba",
47
+ "@nx/js": "0.0.0-pr-26482-ebe2dba",
48
+ "@nrwl/rollup": "0.0.0-pr-26482-ebe2dba"
51
49
  },
52
50
  "publishConfig": {
53
51
  "access": "public"
54
52
  },
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
- },
63
53
  "type": "commonjs"
64
54
  }
@@ -1,11 +1,8 @@
1
1
  import { ExecutorContext } from '@nx/devkit';
2
- import type { AssetGlobPattern, RollupExecutorOptions } from '../schema';
2
+ import type { RollupExecutorOptions } from '../schema';
3
3
  export interface NormalizedRollupExecutorOptions extends RollupExecutorOptions {
4
- entryRoot: string;
5
4
  projectRoot: string;
6
- assets: AssetGlobPattern[];
7
5
  rollupConfig: string[];
8
6
  }
9
- export declare function normalizeRollupExecutorOptions(options: RollupExecutorOptions, context: ExecutorContext, sourceRoot: string): NormalizedRollupExecutorOptions;
7
+ export declare function normalizeRollupExecutorOptions(options: RollupExecutorOptions, context: ExecutorContext): NormalizedRollupExecutorOptions;
10
8
  export declare function normalizePluginPath(pluginPath: void | string, root: string): string;
11
- export declare function normalizeAssets(assets: any[], root: string, sourceRoot: string): AssetGlobPattern[];
@@ -1,37 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeAssets = exports.normalizePluginPath = exports.normalizeRollupExecutorOptions = void 0;
3
+ exports.normalizePluginPath = exports.normalizeRollupExecutorOptions = void 0;
4
4
  const path_1 = require("path");
5
- const fs_1 = require("fs");
6
- const devkit_1 = require("@nx/devkit");
7
- const js_1 = require("@nx/js");
8
- function normalizeRollupExecutorOptions(options, context, sourceRoot) {
5
+ function normalizeRollupExecutorOptions(options, context) {
9
6
  const { root } = context;
10
- const main = `${root}/${options.main}`;
11
- const entryRoot = (0, path_1.dirname)(main);
12
- const project = options.project
13
- ? `${root}/${options.project}`
14
- : (0, path_1.join)(root, 'package.json');
15
- const projectRoot = (0, path_1.dirname)(project);
16
- const outputPath = `${root}/${options.outputPath}`;
17
7
  return {
18
8
  ...options,
19
- // de-dupe formats
20
- format: Array.from(new Set(options.format)),
21
9
  rollupConfig: []
22
10
  .concat(options.rollupConfig)
23
11
  .filter(Boolean)
24
12
  .map((p) => normalizePluginPath(p, root)),
25
- assets: options.assets
26
- ? normalizeAssets(options.assets, root, sourceRoot)
27
- : undefined,
28
- main,
29
- entryRoot,
30
- project,
31
- projectRoot,
32
- outputPath,
13
+ projectRoot: context.projectGraph.nodes[context.projectName].data.root,
33
14
  skipTypeCheck: options.skipTypeCheck || false,
34
- additionalEntryPoints: (0, js_1.createEntryPoints)(options.additionalEntryPoints, context.root),
35
15
  };
36
16
  }
37
17
  exports.normalizeRollupExecutorOptions = normalizeRollupExecutorOptions;
@@ -47,40 +27,3 @@ function normalizePluginPath(pluginPath, root) {
47
27
  }
48
28
  }
49
29
  exports.normalizePluginPath = normalizePluginPath;
50
- function normalizeAssets(assets, root, sourceRoot) {
51
- return assets.map((asset) => {
52
- if (typeof asset === 'string') {
53
- const assetPath = (0, devkit_1.normalizePath)(asset);
54
- const resolvedAssetPath = (0, path_1.resolve)(root, assetPath);
55
- const resolvedSourceRoot = (0, path_1.resolve)(root, sourceRoot);
56
- if (!resolvedAssetPath.startsWith(resolvedSourceRoot)) {
57
- throw new Error(`The ${resolvedAssetPath} asset path must start with the project source root: ${sourceRoot}`);
58
- }
59
- const isDirectory = (0, fs_1.statSync)(resolvedAssetPath).isDirectory();
60
- const input = isDirectory
61
- ? resolvedAssetPath
62
- : (0, path_1.dirname)(resolvedAssetPath);
63
- const output = (0, path_1.relative)(resolvedSourceRoot, (0, path_1.resolve)(root, input));
64
- const glob = isDirectory ? '**/*' : (0, path_1.basename)(resolvedAssetPath);
65
- return {
66
- input,
67
- output,
68
- glob,
69
- };
70
- }
71
- else {
72
- if (asset.output.startsWith('..')) {
73
- throw new Error('An asset cannot be written to a location outside of the output path.');
74
- }
75
- const assetPath = (0, devkit_1.normalizePath)(asset.input);
76
- const resolvedAssetPath = (0, path_1.resolve)(root, assetPath);
77
- return {
78
- ...asset,
79
- input: resolvedAssetPath,
80
- // Now we remove starting slash to make Webpack place it from the output root.
81
- output: asset.output.replace(/^\//, ''),
82
- };
83
- }
84
- });
85
- }
86
- exports.normalizeAssets = normalizeAssets;
@@ -1,13 +1,7 @@
1
1
  import * as rollup from 'rollup';
2
- import type { ExecutorContext } from '@nx/devkit';
3
- import { DependentBuildableProjectNode } from '@nx/js/src/utils/buildable-libs-utils';
4
- import type { PackageJson } from 'nx/src/utils/package-json';
2
+ import { type ExecutorContext } from '@nx/devkit';
5
3
  import { RollupExecutorOptions } from './schema';
6
4
  import { NormalizedRollupExecutorOptions } from './lib/normalize';
7
- export type RollupExecutorEvent = {
8
- success: boolean;
9
- outfile?: string;
10
- };
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[]): Promise<rollup.InputOptions[]>;
5
+ export declare function rollupExecutor(rawOptions: RollupExecutorOptions, context: ExecutorContext): AsyncGenerator<unknown, any, undefined>;
6
+ export declare function createRollupOptions(options: NormalizedRollupExecutorOptions, context: ExecutorContext): Promise<rollup.RollupOptions | rollup.RollupOptions[]>;
13
7
  export default rollupExecutor;
@@ -1,284 +1,108 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRollupOptions = exports.rollupExecutor = void 0;
4
- const ts = require("typescript");
5
4
  const rollup = require("rollup");
6
- const peerDepsExternal = require("rollup-plugin-peer-deps-external");
7
- const plugin_babel_1 = require("@rollup/plugin-babel");
8
5
  const path_1 = require("path");
9
- const rxjs_1 = require("rxjs");
10
- const operators_1 = require("rxjs/operators");
11
- const rxjs_for_await_1 = require("@nx/devkit/src/utils/rxjs-for-await");
12
- const autoprefixer = require("autoprefixer");
13
6
  const devkit_1 = require("@nx/devkit");
14
- const buildable_libs_utils_1 = require("@nx/js/src/utils/buildable-libs-utils");
15
- const plugin_node_resolve_1 = require("@rollup/plugin-node-resolve");
16
- const type_definitions_1 = require("@nx/js/src/plugins/rollup/type-definitions");
17
- const run_rollup_1 = require("./lib/run-rollup");
18
7
  const normalize_1 = require("./lib/normalize");
19
- const analyze_plugin_1 = require("./lib/analyze-plugin");
20
- const fs_1 = require("../../utils/fs");
21
- const swc_plugin_1 = require("./lib/swc-plugin");
22
- const update_package_json_1 = require("./lib/update-package-json");
23
8
  const config_utils_1 = require("@nx/devkit/src/utils/config-utils");
24
- // These use require because the ES import isn't correct.
25
- const commonjs = require('@rollup/plugin-commonjs');
26
- const image = require('@rollup/plugin-image');
27
- const json = require('@rollup/plugin-json');
28
- const copy = require('rollup-plugin-copy');
29
- const postcss = require('rollup-plugin-postcss');
30
- const fileExtensions = ['.js', '.jsx', '.ts', '.tsx'];
9
+ const async_iterable_1 = require("@nx/devkit/src/utils/async-iterable");
10
+ const with_nx_1 = require("../../plugins/with-nx/with-nx");
11
+ const buildable_libs_utils_1 = require("@nx/js/src/utils/buildable-libs-utils");
31
12
  async function* rollupExecutor(rawOptions, context) {
32
13
  process.env.NODE_ENV ??= 'production';
33
- const project = context.projectsConfigurations.projects[context.projectName];
34
- const sourceRoot = project.sourceRoot;
35
- const { target, dependencies } = (0, buildable_libs_utils_1.calculateProjectBuildableDependencies)(context.taskGraph, context.projectGraph, context.root, context.projectName, context.targetName, context.configurationName, true);
36
- const options = (0, normalize_1.normalizeRollupExecutorOptions)(rawOptions, context, sourceRoot);
37
- const packageJson = (0, devkit_1.readJsonFile)(options.project);
38
- const npmDeps = (context.projectGraph.dependencies[context.projectName] ?? [])
39
- .filter((d) => d.target.startsWith('npm:'))
40
- .map((d) => d.target.slice(4));
41
- const rollupOptions = await createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps);
14
+ const options = (0, normalize_1.normalizeRollupExecutorOptions)(rawOptions, context);
15
+ const rollupOptions = await createRollupOptions(options, context);
42
16
  const outfile = resolveOutfile(context, options);
43
17
  if (options.watch) {
44
- const watcher = rollup.watch(rollupOptions);
45
- return yield* (0, rxjs_for_await_1.eachValueFrom)(new rxjs_1.Observable((obs) => {
18
+ // region Watch build
19
+ return yield* (0, async_iterable_1.createAsyncIterable)(({ next }) => {
20
+ const watcher = rollup.watch(rollupOptions);
46
21
  watcher.on('event', (data) => {
47
22
  if (data.code === 'START') {
48
23
  devkit_1.logger.info(`Bundling ${context.projectName}...`);
49
24
  }
50
25
  else if (data.code === 'END') {
51
- (0, update_package_json_1.updatePackageJson)(options, packageJson);
52
26
  devkit_1.logger.info('Bundle complete. Watching for file changes...');
53
- obs.next({ success: true, outfile });
27
+ next({ success: true, outfile });
54
28
  }
55
29
  else if (data.code === 'ERROR') {
56
30
  devkit_1.logger.error(`Error during bundle: ${data.error.message}`);
57
- obs.next({ success: false });
31
+ next({ success: false });
58
32
  }
59
33
  });
60
- // Teardown logic. Close watcher when unsubscribed.
61
- return () => watcher.close();
62
- }));
34
+ const processExitListener = (signal) => () => {
35
+ watcher.close();
36
+ };
37
+ process.once('SIGTERM', processExitListener);
38
+ process.once('SIGINT', processExitListener);
39
+ process.once('SIGQUIT', processExitListener);
40
+ });
41
+ // endregion
63
42
  }
64
43
  else {
65
- devkit_1.logger.info(`Bundling ${context.projectName}...`);
66
- // Delete output path before bundling
67
- if (options.deleteOutputPath) {
68
- (0, fs_1.deleteOutputDir)(context.root, options.outputPath);
69
- }
70
- const start = process.hrtime.bigint();
71
- return (0, rxjs_1.from)(rollupOptions)
72
- .pipe((0, operators_1.concatMap)((opts) => (0, run_rollup_1.runRollup)(opts).pipe((0, operators_1.catchError)((e) => {
73
- devkit_1.logger.error(`Error during bundle: ${e}`);
74
- return (0, rxjs_1.of)({ success: false });
75
- }))), (0, operators_1.scan)((acc, result) => {
76
- if (!acc.success)
77
- return acc;
78
- return result;
79
- }, { success: true, outfile }), (0, operators_1.last)(), (0, operators_1.tap)({
80
- next: (result) => {
81
- if (result.success) {
82
- const end = process.hrtime.bigint();
83
- const duration = `${(Number(end - start) / 1000000000).toFixed(2)}s`;
84
- (0, update_package_json_1.updatePackageJson)(options, packageJson);
85
- devkit_1.logger.info(`⚡ Done in ${duration}`);
44
+ // region Single build
45
+ try {
46
+ devkit_1.logger.info(`Bundling ${context.projectName}...`);
47
+ const start = process.hrtime.bigint();
48
+ const allRollupOptions = Array.isArray(rollupOptions)
49
+ ? rollupOptions
50
+ : [rollupOptions];
51
+ for (const opts of allRollupOptions) {
52
+ const bundle = await rollup.rollup(opts);
53
+ const output = Array.isArray(opts.output) ? opts.output : [opts.output];
54
+ for (const o of output) {
55
+ await bundle.write(o);
86
56
  }
87
- else {
88
- devkit_1.logger.error(`Bundle failed: ${context.projectName}`);
89
- }
90
- },
91
- }))
92
- .toPromise();
57
+ }
58
+ const end = process.hrtime.bigint();
59
+ const duration = `${(Number(end - start) / 1_000_000_000).toFixed(2)}s`;
60
+ devkit_1.logger.info(`⚡ Done in ${duration}`);
61
+ return { success: true, outfile };
62
+ }
63
+ catch (e) {
64
+ if (e.formatted) {
65
+ devkit_1.logger.info(e.formatted);
66
+ }
67
+ else if (e.message) {
68
+ devkit_1.logger.info(e.message);
69
+ }
70
+ devkit_1.logger.error(e);
71
+ devkit_1.logger.error(`Bundle failed: ${context.projectName}`);
72
+ return { success: false };
73
+ }
74
+ // endregion
93
75
  }
94
76
  }
95
77
  exports.rollupExecutor = rollupExecutor;
96
78
  // -----------------------------------------------------------------------------
97
- async function createRollupOptions(options, dependencies, context, packageJson, sourceRoot, npmDeps) {
98
- const useBabel = options.compiler === 'babel';
99
- const useTsc = options.compiler === 'tsc';
100
- const useSwc = options.compiler === 'swc';
101
- const tsConfigPath = (0, devkit_1.joinPathFragments)(context.root, options.tsConfig);
102
- const configFile = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
103
- const config = ts.parseJsonConfigFileContent(configFile.config, ts.sys, (0, path_1.dirname)(tsConfigPath));
104
- if (!options.format || !options.format.length) {
105
- options.format = readCompatibleFormats(config);
106
- }
107
- if (packageJson.type === 'module') {
108
- if (options.format.includes('cjs')) {
109
- devkit_1.logger.warn(`Package type is set to "module" but "cjs" format is included. Going to use "esm" format instead. You can change the package type to "commonjs" or remove type in the package.json file.`);
79
+ async function createRollupOptions(options, context) {
80
+ const { dependencies } = (0, buildable_libs_utils_1.calculateProjectBuildableDependencies)(context.taskGraph, context.projectGraph, context.root, context.projectName, context.targetName, context.configurationName, true);
81
+ const rollupConfig = (0, with_nx_1.withNx)(options, {}, dependencies);
82
+ const userDefinedRollupConfigs = options.rollupConfig.map((plugin) => (0, config_utils_1.loadConfigFile)(plugin));
83
+ let finalConfig = rollupConfig;
84
+ for (const _config of userDefinedRollupConfigs) {
85
+ const config = await _config;
86
+ if (typeof config === 'function') {
87
+ finalConfig = config(finalConfig, options);
110
88
  }
111
- options.format = ['esm'];
112
- }
113
- else if (packageJson.type === 'commonjs') {
114
- if (options.format.includes('esm')) {
115
- devkit_1.logger.warn(`Package type is set to "commonjs" but "esm" format is included. Going to use "cjs" format instead. You can change the package type to "module" or remove type in the package.json file.`);
89
+ else {
90
+ finalConfig = {
91
+ ...finalConfig,
92
+ ...config,
93
+ plugins: [
94
+ ...(Array.isArray(finalConfig.plugins) &&
95
+ finalConfig.plugins?.length > 0
96
+ ? finalConfig.plugins
97
+ : []),
98
+ ...(config.plugins?.length > 0 ? config.plugins : []),
99
+ ],
100
+ };
116
101
  }
117
- options.format = ['cjs'];
118
102
  }
119
- const _rollupOptions = options.format.map(async (format, idx) => {
120
- // Either we're generating only one format, so we should bundle types
121
- // OR we are generating dual formats, so only bundle types for CJS.
122
- const shouldBundleTypes = options.format.length === 1 || format === 'cjs';
123
- const plugins = [
124
- copy({
125
- targets: convertCopyAssetsToRollupOptions(options.outputPath, options.assets),
126
- }),
127
- image(),
128
- json(),
129
- (useTsc || shouldBundleTypes) &&
130
- require('rollup-plugin-typescript2')({
131
- check: !options.skipTypeCheck,
132
- tsconfig: options.tsConfig,
133
- tsconfigOverride: {
134
- compilerOptions: createTsCompilerOptions(config, dependencies, options),
135
- },
136
- }),
137
- shouldBundleTypes &&
138
- (0, type_definitions_1.typeDefinitions)({
139
- main: options.main,
140
- projectRoot: options.projectRoot,
141
- }),
142
- peerDepsExternal({
143
- packageJsonPath: options.project,
144
- }),
145
- postcss({
146
- inject: true,
147
- extract: options.extractCss,
148
- autoModules: true,
149
- plugins: [autoprefixer],
150
- use: {
151
- less: {
152
- javascriptEnabled: options.javascriptEnabled,
153
- },
154
- },
155
- }),
156
- (0, plugin_node_resolve_1.default)({
157
- preferBuiltins: true,
158
- extensions: fileExtensions,
159
- }),
160
- useSwc && (0, swc_plugin_1.swc)(),
161
- useBabel &&
162
- (0, plugin_babel_1.getBabelInputPlugin)({
163
- // Lets `@nx/js/babel` preset know that we are packaging.
164
- caller: {
165
- // @ts-ignore
166
- // Ignoring type checks for caller since we have custom attributes
167
- isNxPackage: true,
168
- // Always target esnext and let rollup handle cjs
169
- supportsStaticESM: true,
170
- isModern: true,
171
- },
172
- cwd: (0, path_1.join)(context.root, sourceRoot),
173
- rootMode: options.babelUpwardRootMode ? 'upward' : undefined,
174
- babelrc: true,
175
- extensions: fileExtensions,
176
- babelHelpers: 'bundled',
177
- skipPreflightCheck: true, // pre-flight check may yield false positives and also slows down the build
178
- exclude: /node_modules/,
179
- plugins: [
180
- format === 'esm'
181
- ? undefined
182
- : require.resolve('babel-plugin-transform-async-to-promises'),
183
- ].filter(Boolean),
184
- }),
185
- commonjs(),
186
- (0, analyze_plugin_1.analyze)(),
187
- ];
188
- let externalPackages = [
189
- ...Object.keys(packageJson.dependencies || {}),
190
- ...Object.keys(packageJson.peerDependencies || {}),
191
- ]; // If external is set to none, include all dependencies and peerDependencies in externalPackages
192
- if (options.external === 'all') {
193
- externalPackages = externalPackages
194
- .concat(dependencies.map((d) => d.name))
195
- .concat(npmDeps);
196
- }
197
- else if (Array.isArray(options.external) && options.external.length > 0) {
198
- externalPackages = externalPackages.concat(options.external);
199
- }
200
- externalPackages = [...new Set(externalPackages)];
201
- const mainEntryFileName = options.outputFileName || options.main;
202
- const input = {};
203
- input[(0, path_1.parse)(mainEntryFileName).name] = options.main;
204
- options.additionalEntryPoints.forEach((entry) => {
205
- input[(0, path_1.parse)(entry).name] = entry;
206
- });
207
- const rollupConfig = {
208
- input,
209
- output: {
210
- format,
211
- dir: `${options.outputPath}`,
212
- name: (0, devkit_1.names)(context.projectName).className,
213
- entryFileNames: `[name].${format}.js`,
214
- chunkFileNames: `[name].${format}.js`,
215
- },
216
- external: (id) => {
217
- return externalPackages.some((name) => id === name || id.startsWith(`${name}/`)); // Could be a deep import
218
- },
219
- plugins,
220
- };
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;
240
- });
241
- const rollupOptions = [];
242
- for (const rollupOption of _rollupOptions) {
243
- rollupOptions.push(await rollupOption);
244
- }
245
- return rollupOptions;
103
+ return finalConfig;
246
104
  }
247
105
  exports.createRollupOptions = createRollupOptions;
248
- function createTsCompilerOptions(config, dependencies, options) {
249
- const compilerOptionPaths = (0, buildable_libs_utils_1.computeCompilerOptionsPaths)(config, dependencies);
250
- const compilerOptions = {
251
- rootDir: options.projectRoot,
252
- allowJs: options.allowJs,
253
- declaration: true,
254
- paths: compilerOptionPaths,
255
- };
256
- if (config.options.module === ts.ModuleKind.CommonJS) {
257
- compilerOptions['module'] = 'ESNext';
258
- }
259
- if (options.compiler === 'swc') {
260
- compilerOptions['emitDeclarationOnly'] = true;
261
- }
262
- return compilerOptions;
263
- }
264
- function convertCopyAssetsToRollupOptions(outputPath, assets) {
265
- return assets
266
- ? assets.map((a) => ({
267
- src: (0, path_1.join)(a.input, a.glob).replace(/\\/g, '/'),
268
- dest: (0, path_1.join)(outputPath, a.output).replace(/\\/g, '/'),
269
- }))
270
- : undefined;
271
- }
272
- function readCompatibleFormats(config) {
273
- switch (config.options.module) {
274
- case ts.ModuleKind.CommonJS:
275
- case ts.ModuleKind.UMD:
276
- case ts.ModuleKind.AMD:
277
- return ['cjs'];
278
- default:
279
- return ['esm'];
280
- }
281
- }
282
106
  function resolveOutfile(context, options) {
283
107
  if (!options.format?.includes('cjs'))
284
108
  return undefined;
@@ -1,4 +1,4 @@
1
- import { Tree, GeneratorCallback } from '@nx/devkit';
1
+ import { GeneratorCallback, Tree } from '@nx/devkit';
2
2
  import { RollupProjectSchema } from './schema';
3
3
  export declare function configurationGenerator(tree: Tree, options: RollupProjectSchema): Promise<GeneratorCallback>;
4
4
  export default configurationGenerator;