Skip to content

Commit

Permalink
feat(core): support rollup.config.ts (#28240)
Browse files Browse the repository at this point in the history
I would like to be able to use rollup.config.ts. Same way it's used by
rollup and vite teams too.

## Current Behavior

Currently nx plugin does not reconize project containing
rollup.config.ts. It expects only {c|m}js files

## Expected Behavior
When project has rollup.config.ts rollup plugin infers build target
automatically. This build target should be able to find the
rollup.config.ts. For that purpose a special [configPlugin
](https://rollupjs.org/command-line-interface/#configplugin-plugin)CLI
parameter should be used

## Related Issue(s)

Feature request [#28226](#28226)

---------

Co-authored-by: Colum Ferry <[email protected]>
  • Loading branch information
ThePlenkov and Coly010 authored Dec 12, 2024
1 parent 0d1c960 commit f96fda9
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/rollup/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"nx",
"typescript",
"@swc/core", // Installed to workspace and only used in swc() plugin
"postcss"
"postcss",
"@rollup/plugin-typescript"
]
}
]
Expand Down
7 changes: 4 additions & 3 deletions packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-image": "^3.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^12.1.0",
"autoprefixer": "^10.4.9",
"minimatch": "9.0.3",
"picocolors": "^1.1.0",
Expand All @@ -42,9 +45,7 @@
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-typescript2": "^0.36.0",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js"
"tslib": "^2.3.0"
},
"publishConfig": {
"access": "public"
Expand Down
109 changes: 109 additions & 0 deletions packages/rollup/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,61 @@ exports[`@nx/rollup/plugin non-root project should create nodes 1`] = `
]
`;

exports[`@nx/rollup/plugin non-root project should create nodes 2`] = `
[
[
"mylib/rollup.config.cts",
{
"projects": {
"mylib": {
"root": "mylib",
"targets": {
"build": {
"cache": true,
"command": "rollup -c rollup.config.cts --configPlugin @rollup/plugin-typescript",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"rollup",
],
},
],
"metadata": {
"description": "Run Rollup",
"help": {
"command": "npx rollup --help",
"example": {
"options": {
"sourcemap": true,
"watch": true,
},
},
},
"technologies": [
"rollup",
],
},
"options": {
"cwd": "mylib",
},
"outputs": [
"{workspaceRoot}/mylib/build",
"{workspaceRoot}/mylib/dist",
],
},
},
},
},
},
],
]
`;

exports[`@nx/rollup/plugin root project should create nodes 1`] = `
[
[
Expand Down Expand Up @@ -108,3 +163,57 @@ exports[`@nx/rollup/plugin root project should create nodes 1`] = `
],
]
`;

exports[`@nx/rollup/plugin root project should create nodes 2`] = `
[
[
"rollup.config.cts",
{
"projects": {
".": {
"root": ".",
"targets": {
"build": {
"cache": true,
"command": "rollup -c rollup.config.cts --configPlugin @rollup/plugin-typescript",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"rollup",
],
},
],
"metadata": {
"description": "Run Rollup",
"help": {
"command": "npx rollup --help",
"example": {
"options": {
"sourcemap": true,
"watch": true,
},
},
},
"technologies": [
"rollup",
],
},
"options": {
"cwd": ".",
},
"outputs": [
"{workspaceRoot}/dist",
],
},
},
},
},
},
],
]
`;
12 changes: 6 additions & 6 deletions packages/rollup/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('@nx/rollup/plugin', () => {
let context: CreateNodesContext;
let cwd = process.cwd();

describe('root project', () => {
describe.each(['js', 'ts'])('root project', (extname) => {
const tempFs = new TempFs('test');

beforeEach(() => {
Expand Down Expand Up @@ -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.cjs',
`rollup.config.c${extname}`,
JSON.stringify(rollupConfigOptions)
);
tempFs.createFileSync('package.json', JSON.stringify({ name: 'mylib' }));
Expand All @@ -77,7 +77,7 @@ describe('@nx/rollup/plugin', () => {
it('should create nodes', async () => {
// ACT
const nodes = await createNodesFunction(
['rollup.config.cjs'],
[`rollup.config.c${extname}`],
{
buildTargetName: 'build',
},
Expand All @@ -89,7 +89,7 @@ describe('@nx/rollup/plugin', () => {
});
});

describe('non-root project', () => {
describe.each(['js', 'ts'])('non-root project', (extname) => {
const tempFs = new TempFs('test');

beforeEach(() => {
Expand Down Expand Up @@ -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.cjs',
`mylib/rollup.config.c${extname}`,
JSON.stringify(rollupConfigOptions)
);
tempFs.createFileSync(
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('@nx/rollup/plugin', () => {
it('should create nodes', async () => {
// ACT
const nodes = await createNodesFunction(
['mylib/rollup.config.cjs'],
[`mylib/rollup.config.c${extname}`],
{
buildTargetName: 'build',
},
Expand Down
8 changes: 6 additions & 2 deletions packages/rollup/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface RollupPluginOptions {
buildTargetName?: string;
}

const rollupConfigGlob = '**/rollup.config.{js,cjs,mjs}';
const rollupConfigGlob = '**/rollup.config.{js,cjs,mjs,ts,cts,mts}';

export const createNodes: CreateNodes<RollupPluginOptions> = [
rollupConfigGlob,
Expand Down Expand Up @@ -182,7 +182,11 @@ async function buildRollupTarget(

const targets: Record<string, TargetConfiguration> = {};
targets[options.buildTargetName] = {
command: `rollup -c ${basename(configFilePath)}`,
command: `rollup -c ${basename(configFilePath)}${
configFilePath.endsWith('ts')
? ' --configPlugin @rollup/plugin-typescript'
: ''
}`,
options: { cwd: projectRoot },
cache: true,
dependsOn: [`^${options.buildTargetName}`],
Expand Down

0 comments on commit f96fda9

Please sign in to comment.