Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bundling): crystalize rollup #22045

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/generated/packages/rollup/generators/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
},
"updatePackageScripts": {
"type": "boolean",
"x-priority": "internal",
"description": "Update `package.json` scripts with inferred targets",
"default": false
}
},
"required": [],
Expand Down
49 changes: 49 additions & 0 deletions e2e/rollup/src/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,53 @@ describe('Rollup Plugin', () => {
expect(() => runCLI(`build ${jsLib}`)).not.toThrow();
checkFilesExist(`dist/test/index.mjs.js`);
});

it('should build correctly with crystal', () => {
// ARRANGE
updateFile(
`libs/test/src/index.ts`,
`export function helloWorld() {
console.log("hello world");
}`
);
updateFile(`libs/test/package.json`, JSON.stringify({ name: 'test' }));
updateFile(
`libs/test/rollup.config.js`,
`import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import typescript2 from 'rollup-plugin-typescript2';
const config = {
input: 'src/index.ts',
output: [
{
file: 'dist/bundle.js',
format: 'cjs',
sourcemap: true
},
{
file: 'dist/bundle.es.js',
format: 'es',
sourcemap: true
}
],
plugins: [
typescript2(),
babel({ babelHelpers: 'bundled' }),
commonjs(),
]
};
export default config;
`
);
// ACT
runCLI(`generate @nx/rollup:init --no-interactive`);
const output = runCLI(`build test`);

// ASSERT
expect(output).toContain('Successfully ran target build for project test');
checkFilesExist(`libs/test/dist/bundle.js`);
checkFilesExist(`libs/test/dist/bundle.es.js`);
});
});
1 change: 1 addition & 0 deletions packages/nx/src/command-line/init/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const npmPackageToPluginMap: Record<string, string> = {
vite: '@nx/vite',
vitest: '@nx/vite',
webpack: '@nx/webpack',
rollup: '@nx/rollup',
// Testing tools
jest: '@nx/jest',
cypress: '@nx/cypress',
Expand Down
1 change: 1 addition & 0 deletions packages/rollup/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createNodes, RollupPluginOptions } from './src/plugins/plugin';
5 changes: 5 additions & 0 deletions packages/rollup/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"build-base": {
"executor": "@nx/js:tsc",
"options": {
"generateExportsField": true,
"additionalEntryPoints": [
"{projectRoot}/{executors,generators,migrations}.json",
"{projectRoot}/plugin.ts"
],
"assets": [
{
"input": "packages/rollup",
Expand Down
37 changes: 37 additions & 0 deletions packages/rollup/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,37 @@ import {
formatFiles,
GeneratorCallback,
Tree,
readNxJson,
updateNxJson,
} from '@nx/devkit';
import { nxVersion } from '../../utils/versions';
import { Schema } from './schema';
import { updatePackageScripts } from '@nx/devkit/src/utils/update-package-scripts';
import { createNodes } from '../../plugins/plugin';

function addPlugin(tree: Tree) {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];

for (const plugin of nxJson.plugins) {
if (
typeof plugin === 'string'
? plugin === '@nx/rollup/plugin'
: plugin.plugin === '@nx/rollup/plugin'
) {
return;
}
}

nxJson.plugins.push({
plugin: '@nx/rollup/plugin',
options: {
buildTargetName: 'build',
},
});

updateNxJson(tree, nxJson);
}

export async function rollupInitGenerator(tree: Tree, schema: Schema) {
let task: GeneratorCallback = () => {};
Expand All @@ -20,6 +48,15 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) {
);
}

schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';
if (schema.addPlugin) {
addPlugin(tree);
}

if (schema.updatePackageScripts) {
await updatePackageScripts(tree, createNodes);
}

if (!schema.skipFormat) {
await formatFiles(tree);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rollup/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export interface Schema {
skipFormat?: boolean;
skipPackageJson?: boolean;
keepExistingVersions?: boolean;
updatePackageScripts?: boolean;
addPlugin?: boolean;
}
6 changes: 6 additions & 0 deletions packages/rollup/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
},
"updatePackageScripts": {
"type": "boolean",
"x-priority": "internal",
"description": "Update `package.json` scripts with inferred targets",
"default": false
}
},
"required": []
Expand Down
60 changes: 60 additions & 0 deletions packages/rollup/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@nx/rollup/plugin non-root project should create nodes 1`] = `
{
"projects": {
"mylib": {
"root": "mylib",
"targets": {
"build": {
"cache": true,
"command": "rollup -c rollup.config.js",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
],
"options": {
"cwd": "mylib",
},
"outputs": [
"{workspaceRoot}/mylib/build",
"{workspaceRoot}/mylib/dist",
],
},
},
},
},
}
`;

exports[`@nx/rollup/plugin root project should create nodes 1`] = `
{
"projects": {
".": {
"root": ".",
"targets": {
"build": {
"cache": true,
"command": "rollup -c rollup.config.js",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
],
"options": {
"cwd": ".",
},
"outputs": [
"{workspaceRoot}/dist",
],
},
},
},
},
}
`;
155 changes: 155 additions & 0 deletions packages/rollup/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { type CreateNodesContext, joinPathFragments } from '@nx/devkit';
import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';

describe('@nx/rollup/plugin', () => {
let createNodesFunction = createNodes[1];
let context: CreateNodesContext;
let cwd = process.cwd();

describe('root project', () => {
const tempFs = new TempFs('test');

beforeEach(() => {
context = {
nxJsonConfiguration: {
targetDefaults: {
build: {
cache: false,
inputs: ['foo', '^foo'],
},
},
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: tempFs.tempDir,
};

tempFs.createFileSync('package.json', JSON.stringify({ name: 'mylib' }));
tempFs.createFileSync(
'src/index.js',
`export function main() {
console.log("hello world");
}`
);
tempFs.createFileSync(
'rollup.config.js',
`
const config = {
input: 'src/index.js',
output: [
{
file: 'dist/bundle.js',
format: 'cjs',
sourcemap: true
},
{
file: 'dist/bundle.es.js',
format: 'es',
sourcemap: true
}
],
plugins: [],
};
module.exports = config;
`
);

process.chdir(tempFs.tempDir);
});

afterEach(() => {
jest.resetModules();
tempFs.cleanup();
process.chdir(cwd);
});

it('should create nodes', async () => {
// ACT
const nodes = await createNodesFunction(
'rollup.config.js',
{
buildTargetName: 'build',
},
context
);

// ASSERT
expect(nodes).toMatchSnapshot();
});
});
describe('non-root project', () => {
const tempFs = new TempFs('test');

beforeEach(() => {
context = {
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: tempFs.tempDir,
};

tempFs.createFileSync(
'mylib/package.json',
JSON.stringify({ name: 'mylib' })
);
tempFs.createFileSync(
'mylib/src/index.js',
`export function main() {
console.log("hello world");
}`
);
tempFs.createFileSync(
'mylib/rollup.config.js',
`
const config = {
input: 'src/index.js',
output: [
{
file: 'build/bundle.js',
format: 'cjs',
sourcemap: true
},
{
file: 'dist/bundle.es.js',
format: 'es',
sourcemap: true
}
],
plugins: [],
};
module.exports = config;
`
);

process.chdir(tempFs.tempDir);
});

afterEach(() => {
jest.resetModules();
tempFs.cleanup();
process.chdir(cwd);
});

it('should create nodes', async () => {
// ACT
const nodes = await createNodesFunction(
'mylib/rollup.config.js',
{
buildTargetName: 'build',
},
context
);

// ASSERT
expect(nodes).toMatchSnapshot();
});
});
});
Loading