Skip to content

Commit

Permalink
feat(rollup): use .cjs file extension for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Dec 4, 2024
1 parent 1dbddb1 commit ffff293
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 85 deletions.
2 changes: 1 addition & 1 deletion e2e/react/src/react-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
10 changes: 5 additions & 5 deletions e2e/rollup/src/rollup-legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
});

Expand All @@ -219,15 +219,15 @@ 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
plugins: config.plugins.filter((p) => p.name !== 'rollup-plugin-nx-generate-package-json'),
})`
);
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;
});

Expand Down
8 changes: 4 additions & 4 deletions e2e/rollup/src/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/generators/setup-build/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/plugins/bundle-rollup.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 31 additions & 2 deletions packages/react/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
],
});
"
`);
});
});

Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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({});
});

Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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({});
});
});
Expand All @@ -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');
Expand All @@ -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');
Expand Down
Loading

0 comments on commit ffff293

Please sign in to comment.