From ffff29300e4515c1b5561cb6923e193c2cfa8529 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 4 Dec 2024 10:11:27 -0500 Subject: [PATCH] feat(rollup): use .cjs file extension for config files --- e2e/react/src/react-package.test.ts | 2 +- e2e/rollup/src/rollup-legacy.test.ts | 10 +-- e2e/rollup/src/rollup.test.ts | 8 +- .../generators/setup-build/generator.spec.ts | 2 +- packages/react/plugins/bundle-rollup.ts | 4 +- .../library/lib/add-rollup-build-target.ts | 64 +++++++------- .../src/generators/library/library.spec.ts | 33 +++++++- .../configuration/configuration.spec.ts | 4 +- .../generators/configuration/configuration.ts | 2 +- .../convert-to-inferred.spec.ts | 36 ++++---- .../convert-to-inferred.ts | 4 +- ...ollup-config-from-executor-options.spec.ts | 83 +++++++++++++++++-- ...act-rollup-config-from-executor-options.ts | 11 ++- packages/rollup/src/plugins/plugin.spec.ts | 8 +- 14 files changed, 186 insertions(+), 85 deletions(-) diff --git a/e2e/react/src/react-package.test.ts b/e2e/react/src/react-package.test.ts index f5b72a9af6b20..f89dafec04696 100644 --- a/e2e/react/src/react-package.test.ts +++ b/e2e/react/src/react-package.test.ts @@ -95,7 +95,7 @@ describe('Build React libraries and apps', () => { // Add assets to child lib updateFile( - join('libs', childLib, 'rollup.config.js'), + join('libs', childLib, 'rollup.config.cjs'), `const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx( { diff --git a/e2e/rollup/src/rollup-legacy.test.ts b/e2e/rollup/src/rollup-legacy.test.ts index 1ec847001e24d..b5fcd121cec6b 100644 --- a/e2e/rollup/src/rollup-legacy.test.ts +++ b/e2e/rollup/src/rollup-legacy.test.ts @@ -185,13 +185,13 @@ describe('Rollup Plugin', () => { checkFilesExist(`dist/test/index.mjs.js`); }); - it('should support array config from rollup.config.js', () => { + it('should support array config from rollup.config.cjs', () => { const jsLib = uniq('jslib'); runCLI( `generate @nx/js:lib ${jsLib} --directory=libs/${jsLib} --bundler rollup --verbose` ); updateFile( - `libs/${jsLib}/rollup.config.js`, + `libs/${jsLib}/rollup.config.cjs`, `module.exports = (config) => [{ ...config, output: { @@ -204,7 +204,7 @@ describe('Rollup Plugin', () => { }]` ); updateJson(join('libs', jsLib, 'project.json'), (config) => { - config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.js`; + config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.cjs`; return config; }); @@ -219,7 +219,7 @@ describe('Rollup Plugin', () => { `generate @nx/js:lib ${jsLib} --directory=libs/${jsLib} --bundler rollup --verbose` ); updateFile( - `libs/${jsLib}/rollup.config.js`, + `libs/${jsLib}/rollup.config.cjs`, `module.exports = (config) => ({ ...config, // Filter out the plugin, but the @nx/rollup:rollup executor should add it back @@ -227,7 +227,7 @@ describe('Rollup Plugin', () => { })` ); updateJson(join('libs', jsLib, 'project.json'), (config) => { - config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.js`; + config.targets.build.options.rollupConfig = `libs/${jsLib}/rollup.config.cjs`; return config; }); diff --git a/e2e/rollup/src/rollup.test.ts b/e2e/rollup/src/rollup.test.ts index 2d8056a9ec3d7..bce8c2ca4ccec 100644 --- a/e2e/rollup/src/rollup.test.ts +++ b/e2e/rollup/src/rollup.test.ts @@ -27,7 +27,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --tsConfig=./tsconfig.lib.json --main=./src/index.ts` ); updateFile( - `libs/${myPkg}/rollup.config.js`, + `libs/${myPkg}/rollup.config.cjs`, ` const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx({ @@ -61,7 +61,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --tsConfig=./tsconfig.lib.json --main=./src/index.ts --compiler=swc` ); updateFile( - `libs/${myPkg}/rollup.config.js`, + `libs/${myPkg}/rollup.config.cjs`, ` const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx({ @@ -85,7 +85,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --tsConfig=./tsconfig.lib.json --main=./src/index.ts --compiler=tsc` ); updateFile( - `libs/${myPkg}/rollup.config.js`, + `libs/${myPkg}/rollup.config.cjs`, ` const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx({ @@ -114,7 +114,7 @@ describe('Rollup Plugin', () => { `generate @nx/rollup:configuration ${myPkg} --tsConfig=./tsconfig.lib.json --main=./src/index.ts --compiler=tsc` ); updateFile( - `libs/${myPkg}/rollup.config.js`, + `libs/${myPkg}/rollup.config.cjs`, ` const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx({ diff --git a/packages/js/src/generators/setup-build/generator.spec.ts b/packages/js/src/generators/setup-build/generator.spec.ts index aec3504f8447a..24993382ec27f 100644 --- a/packages/js/src/generators/setup-build/generator.spec.ts +++ b/packages/js/src/generators/setup-build/generator.spec.ts @@ -138,7 +138,7 @@ describe('setup-build generator', () => { bundler: 'rollup', }); - expect(tree.exists('packages/mypkg/rollup.config.js')).toBe(true); + expect(tree.exists('packages/mypkg/rollup.config.cjs')).toBe(true); }); it('should support --bundler=esbuild', async () => { diff --git a/packages/react/plugins/bundle-rollup.ts b/packages/react/plugins/bundle-rollup.ts index eae13e6e8702b..4461bf328d016 100644 --- a/packages/react/plugins/bundle-rollup.ts +++ b/packages/react/plugins/bundle-rollup.ts @@ -1,8 +1,8 @@ import * as rollup from 'rollup'; -// TODO(v22): Remove this in Nx 22 and migrate to explicit rollup.config.js files. +// TODO(v22): Remove this in Nx 22 and migrate to explicit rollup.config.cjs files. /** - * @deprecated Use `withNx` function from `@nx/rollup/with-nx` in your rollup.config.js file instead. Use `nx g @nx/rollup:convert-to-inferred` to generate the rollup.config.js file if it does not exist. + * @deprecated Use `withNx` function from `@nx/rollup/with-nx` in your rollup.config.cjs file instead. Use `nx g @nx/rollup:convert-to-inferred` to generate the rollup.config.cjs file if it does not exist. */ function getRollupOptions(options: rollup.RollupOptions) { const extraGlobals = { diff --git a/packages/react/src/generators/library/lib/add-rollup-build-target.ts b/packages/react/src/generators/library/lib/add-rollup-build-target.ts index a7cb9ff47bebc..bdcba77207167 100644 --- a/packages/react/src/generators/library/lib/add-rollup-build-target.ts +++ b/packages/react/src/generators/library/lib/add-rollup-build-target.ts @@ -69,38 +69,38 @@ export async function addRollupBuildTarget( if (hasRollupPlugin) { // New behavior, using rollup config file and inferred target. host.write( - joinPathFragments(options.projectRoot, 'rollup.config.js'), - stripIndents` - const { withNx } = require('@nx/rollup/with-nx'); - const url = require('@rollup/plugin-url'); - const svg = require('@svgr/rollup'); - - module.exports = withNx({ - main: '${maybeJs(options, './src/index.ts')}', - outputPath: '${joinPathFragments( - offsetFromRoot(options.projectRoot), - 'dist', - options.projectRoot - )}', - tsConfig: './tsconfig.lib.json', - compiler: '${options.compiler ?? 'babel'}', - external: ${JSON.stringify(external)}, - format: ['esm'], - assets:[{ input: '.', output: '.', glob: 'README.md'}], - }, { - // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options - plugins: [ - svg({ - svgo: false, - titleProp: true, - ref: true, - }), - url({ - limit: 10000, // 10kB - }), - ], - }); - ` + joinPathFragments(options.projectRoot, 'rollup.config.cjs'), + ` +const { withNx } = require('@nx/rollup/with-nx'); +const url = require('@rollup/plugin-url'); +const svg = require('@svgr/rollup'); + +module.exports = withNx({ + main: '${maybeJs(options, './src/index.ts')}', + outputPath: '${joinPathFragments( + offsetFromRoot(options.projectRoot), + 'dist', + options.projectRoot + )}', + tsConfig: './tsconfig.lib.json', + compiler: '${options.compiler ?? 'babel'}', + external: ${JSON.stringify(external)}, + format: ['esm'], + assets:[{ input: '.', output: '.', glob: 'README.md'}], +}, { + // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options + plugins: [ + svg({ + svgo: false, + titleProp: true, + ref: true, + }), + url({ + limit: 10000, // 10kB + }), + ], +}); +` ); } else { // Legacy behavior, there is a target in project.json using rollup executor. diff --git a/packages/react/src/generators/library/library.spec.ts b/packages/react/src/generators/library/library.spec.ts index 60ad9f8216236..a9e08564a054d 100644 --- a/packages/react/src/generators/library/library.spec.ts +++ b/packages/react/src/generators/library/library.spec.ts @@ -517,7 +517,36 @@ describe('lib', () => { buildable: true, }); - expect(tree.exists('my-lib/rollup.config.js')).toBeTruthy(); + expect(tree.read('my-lib/rollup.config.cjs', 'utf-8')) + .toMatchInlineSnapshot(` + " + const { withNx } = require('@nx/rollup/with-nx'); + const url = require('@rollup/plugin-url'); + const svg = require('@svgr/rollup'); + + module.exports = withNx({ + main: './src/index.ts', + outputPath: '../dist/my-lib', + tsConfig: './tsconfig.lib.json', + compiler: 'babel', + external: ["react","react-dom","react/jsx-runtime"], + format: ['esm'], + assets:[{ input: '.', output: '.', glob: 'README.md'}], + }, { + // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options + plugins: [ + svg({ + svgo: false, + titleProp: true, + ref: true, + }), + url({ + limit: 10000, // 10kB + }), + ], + }); + " + `); }); }); @@ -558,7 +587,7 @@ describe('lib', () => { importPath: '@proj/my-lib', }); - expect(tree.read('my-lib/rollup.config.js', 'utf-8')) + expect(tree.read('my-lib/rollup.config.cjs', 'utf-8')) .toEqual(`const { withNx } = require('@nx/rollup/with-nx'); const url = require('@rollup/plugin-url'); const svg = require('@svgr/rollup'); diff --git a/packages/rollup/src/generators/configuration/configuration.spec.ts b/packages/rollup/src/generators/configuration/configuration.spec.ts index e68d286da1523..370241aad389f 100644 --- a/packages/rollup/src/generators/configuration/configuration.spec.ts +++ b/packages/rollup/src/generators/configuration/configuration.spec.ts @@ -59,7 +59,7 @@ describe('configurationGenerator', () => { main: './libs/mypkg/src/index.ts', }); - const rollupConfig = tree.read('libs/mypkg/rollup.config.js', 'utf-8'); + const rollupConfig = tree.read('libs/mypkg/rollup.config.cjs', 'utf-8'); expect(rollupConfig) .toEqual(`const { withNx } = require('@nx/rollup/with-nx'); @@ -88,7 +88,7 @@ module.exports = withNx( tsConfig: 'libs/mypkg/tsconfig.custom.json', }); - const rollupConfig = tree.read('libs/mypkg/rollup.config.js', 'utf-8'); + const rollupConfig = tree.read('libs/mypkg/rollup.config.cjs', 'utf-8'); expect(rollupConfig) .toEqual(`const { withNx } = require('@nx/rollup/with-nx'); diff --git a/packages/rollup/src/generators/configuration/configuration.ts b/packages/rollup/src/generators/configuration/configuration.ts index df1096bf57d3a..53a6a79dcad49 100644 --- a/packages/rollup/src/generators/configuration/configuration.ts +++ b/packages/rollup/src/generators/configuration/configuration.ts @@ -99,7 +99,7 @@ function createRollupConfig( }; tree.write( - joinPathFragments(project.root, 'rollup.config.js'), + joinPathFragments(project.root, 'rollup.config.cjs'), `const { withNx } = require('@nx/rollup/with-nx'); module.exports = withNx( diff --git a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts index 4e200052fce05..5839570a95c9d 100644 --- a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts +++ b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.spec.ts @@ -100,7 +100,7 @@ describe('Rollup - Convert Executors To Plugin', () => { include: [`${project.root}/**/*`], }, ]); - expect(tree.read('mypkg/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -124,8 +124,8 @@ describe('Rollup - Convert Executors To Plugin', () => { module.exports = config; " `); - expect(tree.exists('otherpkg1/rollup.config.js')).toBe(false); - expect(tree.exists('otherpkg2/rollup.config.js')).toBe(false); + expect(tree.exists('otherpkg1/rollup.config.cjs')).toBe(false); + expect(tree.exists('otherpkg2/rollup.config.cjs')).toBe(false); expect(readProjectConfiguration(tree, project.name).targets).toEqual({}); }); @@ -187,7 +187,7 @@ describe('Rollup - Convert Executors To Plugin', () => { project: projectWithMultipleConfigsAndEntries.name, }); - expect(tree.read('mypkg1/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg1/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -213,7 +213,7 @@ describe('Rollup - Convert Executors To Plugin', () => { module.exports = config; " `); - expect(tree.read('mypkg2/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg2/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -254,31 +254,31 @@ describe('Rollup - Convert Executors To Plugin', () => { ).toEqual({}); }); - it('should handle conflicts with existing rollup.config.js file', async () => { + it('should handle conflicts with existing rollup.config.cjs file', async () => { const project = createProject(tree, { name: 'mypkg1', root: 'mypkg1', targetOptions: { rollupConfig: [ - 'mypkg1/rollup.config.js', + 'mypkg1/rollup.config.cjs', 'mypkg1/rollup.other.config.js', ], }, }); tree.write( - 'mypkg1/rollup.config.js', + 'mypkg1/rollup.config.cjs', '// existing config\nmodule.exports = {};' ); await convertToInferred(tree, { project: project.name }); - expect(tree.read('mypkg1/rollup.migrated.config.js', 'utf-8')) + expect(tree.read('mypkg1/rollup.migrated.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "// existing config module.exports = {}; " `); - expect(tree.read('mypkg1/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg1/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -299,7 +299,7 @@ describe('Rollup - Convert Executors To Plugin', () => { // output: { sourcemap: true }, }); - config = require('./rollup.migrated.config.js')(config, options); + config = require('./rollup.migrated.config.cjs')(config, options); config = require('./rollup.other.config.js')(config, options); module.exports = config; @@ -422,7 +422,7 @@ describe('Rollup - Convert Executors To Plugin', () => { await convertToInferred(tree, { project: project.name }); - expect(tree.read('mypkg/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -521,7 +521,7 @@ describe('Rollup - Convert Executors To Plugin', () => { readProjectConfiguration(tree, project.name).targets.build.cache ).toBe(true); // Plugin options are read from targetDefaults since they were missing in project.json - expect(tree.read('mypkg1/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg1/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -574,7 +574,7 @@ describe('Rollup - Convert Executors To Plugin', () => { plugin: '@nx/rollup/plugin', }, ]); - expect(tree.read('mypkg/rollup.config.js', 'utf-8')) + expect(tree.read('mypkg/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -599,8 +599,8 @@ describe('Rollup - Convert Executors To Plugin', () => { module.exports = config; " `); - expect(tree.exists('otherpkg1/rollup.config.js')).toBe(false); - expect(tree.exists('otherpkg2/rollup.config.js')).toBe(false); + expect(tree.exists('otherpkg1/rollup.config.cjs')).toBe(false); + expect(tree.exists('otherpkg2/rollup.config.cjs')).toBe(false); expect(readProjectConfiguration(tree, project.name).targets).toEqual({}); }); }); @@ -623,7 +623,7 @@ describe('Rollup - Convert Executors To Plugin', () => { await convertToInferred(tree, {}); - expect(tree.read('pkg1/rollup.config.js', 'utf-8')) + expect(tree.read('pkg1/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -647,7 +647,7 @@ describe('Rollup - Convert Executors To Plugin', () => { module.exports = config; " `); - expect(tree.read('pkg2/rollup.config.js', 'utf-8')) + expect(tree.read('pkg2/rollup.config.cjs', 'utf-8')) .toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); diff --git a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.ts b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.ts index 18487dc7c9778..b5e87dcc3cb42 100644 --- a/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.ts +++ b/packages/rollup/src/generators/convert-to-inferred/convert-to-inferred.ts @@ -79,10 +79,10 @@ export async function convertToInferred(tree: Tree, options: Schema) { (target.outputs[0] === '{options.outputPath}' || target.outputs[0] === '{workspaceRoot}/{options.outputPath}') ) { - // If only the default `options.outputPath` is set as output, remove it and use path inferred from `rollup.config.js`. + // If only the default `options.outputPath` is set as output, remove it and use path inferred from `rollup.config.cjs`. delete target.outputs; } else { - // Otherwise, replace `options.outputPath` with what is inferred from `rollup.config.js`. + // Otherwise, replace `options.outputPath` with what is inferred from `rollup.config.cjs`. target.outputs = target.outputs.map((output) => // Again, "{projectRoot}/{options.outputPath}" is an invalid output for Rollup. output === '{options.outputPath}' || diff --git a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.spec.ts b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.spec.ts index a55fc4b3ecac8..550e30da06e63 100644 --- a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.spec.ts +++ b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.spec.ts @@ -4,6 +4,71 @@ import { extractRollupConfigFromExecutorOptions } from './extract-rollup-config- describe('extract-rollup-config-from-executor-options', () => { it('should extract the options correctly', () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + tree.write( + `apps/myapp/rollup.config.cjs`, + `export default (config) => {return config;}` + ); + // ACT + const defaultValues = extractRollupConfigFromExecutorOptions( + tree, + { + outputPath: 'dist/apps/myapp', + tsConfig: 'apps/myapp/tsconfig.json', + project: '', + main: 'src/lib/index.ts', + rollupConfig: 'apps/myapp/rollup.config.cjs', + watch: true, + format: ['esm', 'cjs'], + }, + {}, + 'apps/myapp' + ); + + // ASSERT + const configFile = tree.read('apps/myapp/rollup.config.cjs', 'utf-8'); + expect(configFile).toMatchInlineSnapshot(` + "const { withNx } = require('@nx/rollup/with-nx'); + + // These options were migrated by @nx/rollup:convert-to-inferred from project.json + const options = { + "outputPath": "../../dist/apps/myapp", + "tsConfig": "./tsconfig.json", + "project": "", + "main": "../../src/lib/index.ts", + "format": [ + "esm", + "cjs" + ] + }; + + let config = withNx(options, { + // Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options + // e.g. + // output: { sourcemap: true }, + }); + + config = require('./rollup.migrated.config.cjs')(config, options); + + module.exports = config;" + `); + expect(tree.exists('apps/myapp/rollup.migrated.config.cjs')).toBeTruthy(); + expect(defaultValues).toMatchInlineSnapshot(` + { + "format": [ + "esm", + "cjs", + ], + "main": "../../src/lib/index.ts", + "outputPath": "../../dist/apps/myapp", + "project": "", + "tsConfig": "./tsconfig.json", + } + `); + }); + + it('should extract the options correctly (rollup.config.js)', () => { // ARRANGE const tree = createTreeWithEmptyWorkspace(); tree.write( @@ -27,7 +92,7 @@ describe('extract-rollup-config-from-executor-options', () => { ); // ASSERT - const configFile = tree.read('apps/myapp/rollup.config.js', 'utf-8'); + const configFile = tree.read('apps/myapp/rollup.config.cjs', 'utf-8'); expect(configFile).toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -68,11 +133,11 @@ describe('extract-rollup-config-from-executor-options', () => { `); }); - it('should extract configurations that do not defined a rollupConfig into the rollup.config.js file', () => { + it('should extract configurations that do not defined a rollupConfig into the rollup.config.cjs file', () => { // ARRANGE const tree = createTreeWithEmptyWorkspace(); tree.write( - `apps/myapp/rollup.config.js`, + `apps/myapp/rollup.config.cjs`, `export default (config) => {return config;}` ); tree.write( @@ -87,7 +152,7 @@ describe('extract-rollup-config-from-executor-options', () => { tsConfig: 'apps/myapp/tsconfig.json', project: '', main: 'src/lib/index.ts', - rollupConfig: 'apps/myapp/rollup.config.js', + rollupConfig: 'apps/myapp/rollup.config.cjs', watch: true, format: ['esm', 'cjs'], }, @@ -100,7 +165,7 @@ describe('extract-rollup-config-from-executor-options', () => { ); // ASSERT - const configFile = tree.read('apps/myapp/rollup.config.js', 'utf-8'); + const configFile = tree.read('apps/myapp/rollup.config.cjs', 'utf-8'); expect(configFile).toMatchInlineSnapshot(` "const { withNx } = require('@nx/rollup/with-nx'); @@ -137,11 +202,11 @@ describe('extract-rollup-config-from-executor-options', () => { // output: { sourcemap: true }, }); - config = require('./rollup.migrated.config.js')(config, options); + config = require('./rollup.migrated.config.cjs')(config, options); module.exports = config;" `); - expect(tree.exists('apps/myapp/rollup.migrated.config.js')).toBeTruthy(); + expect(tree.exists('apps/myapp/rollup.migrated.config.cjs')).toBeTruthy(); expect(defaultValues).toMatchInlineSnapshot(` { "format": [ @@ -160,7 +225,7 @@ describe('extract-rollup-config-from-executor-options', () => { // ARRANGE const tree = createTreeWithEmptyWorkspace(); tree.write( - `apps/myapp/rollup.config.js`, + `apps/myapp/rollup.config.cjs`, `export default (config) => {return config;}` ); tree.write( @@ -175,7 +240,7 @@ describe('extract-rollup-config-from-executor-options', () => { tsConfig: 'apps/myapp/tsconfig.json', project: '', main: 'src/lib/index.ts', - rollupConfig: 'apps/myapp/rollup.config.js', + rollupConfig: 'apps/myapp/rollup.config.cjs', watch: true, format: ['esm', 'cjs'], }, diff --git a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts index df9694f4630d8..b5e0bedaf5c5a 100644 --- a/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts +++ b/packages/rollup/src/generators/convert-to-inferred/lib/extract-rollup-config-from-executor-options.ts @@ -26,9 +26,16 @@ export function extractRollupConfigFromExecutorOptions( : []; delete options.rollupConfig; - // Resolve conflict with rollup.config.js if it exists. + // Resolve conflict with rollup.config.cjs or rollup.config.js if they exist. for (let i = 0; i < oldRollupConfig.length; i++) { const file = oldRollupConfig[i]; + if (file === './rollup.config.cjs') { + tree.rename( + joinPathFragments(projectRoot, 'rollup.config.cjs'), + joinPathFragments(projectRoot, `rollup.migrated.config.cjs`) + ); + oldRollupConfig.splice(i, 1, './rollup.migrated.config.cjs'); + } if (file === './rollup.config.js') { tree.rename( joinPathFragments(projectRoot, 'rollup.config.js'), @@ -116,7 +123,7 @@ export function extractRollupConfigFromExecutorOptions( } tree.write( - joinPathFragments(projectRoot, `rollup.config.js`), + joinPathFragments(projectRoot, `rollup.config.cjs`), createNewRollupConfig(oldRollupConfig, defaultOptions, configurationOptions) ); diff --git a/packages/rollup/src/plugins/plugin.spec.ts b/packages/rollup/src/plugins/plugin.spec.ts index 67c7b6e8202ae..4c18f9c54bb50 100644 --- a/packages/rollup/src/plugins/plugin.spec.ts +++ b/packages/rollup/src/plugins/plugin.spec.ts @@ -51,7 +51,7 @@ describe('@nx/rollup/plugin', () => { // is that the hash is different after updating the // config file. The actual config read is mocked below. tempFs.createFileSync( - 'rollup.config.js', + 'rollup.config.cjs', JSON.stringify(rollupConfigOptions) ); tempFs.createFileSync('package.json', JSON.stringify({ name: 'mylib' })); @@ -77,7 +77,7 @@ describe('@nx/rollup/plugin', () => { it('should create nodes', async () => { // ACT const nodes = await createNodesFunction( - ['rollup.config.js'], + ['rollup.config.cjs'], { buildTargetName: 'build', }, @@ -125,7 +125,7 @@ describe('@nx/rollup/plugin', () => { // is that the hash is different after updating the // config file. The actual config read is mocked below. tempFs.createFileSync( - 'mylib/rollup.config.js', + 'mylib/rollup.config.cjs', JSON.stringify(rollupConfigOptions) ); tempFs.createFileSync( @@ -154,7 +154,7 @@ describe('@nx/rollup/plugin', () => { it('should create nodes', async () => { // ACT const nodes = await createNodesFunction( - ['mylib/rollup.config.js'], + ['mylib/rollup.config.cjs'], { buildTargetName: 'build', },