feat(bundling): add createNodesV2 for rollup plugin (#28090)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> There is no implementation for `createNodesV2`. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> There should be an implementation for `createNodesV2`. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
This commit is contained in:
parent
7ba7f3e798
commit
38448da34f
@ -1 +1,5 @@
|
|||||||
export { createNodes, RollupPluginOptions } from './src/plugins/plugin';
|
export {
|
||||||
|
createNodes,
|
||||||
|
createNodesV2,
|
||||||
|
RollupPluginOptions,
|
||||||
|
} from './src/plugins/plugin';
|
||||||
|
|||||||
@ -1,70 +1,110 @@
|
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`@nx/rollup/plugin non-root project should create nodes 1`] = `
|
exports[`@nx/rollup/plugin non-root project should create nodes 1`] = `
|
||||||
{
|
[
|
||||||
"projects": {
|
[
|
||||||
"mylib": {
|
"mylib/rollup.config.js",
|
||||||
"root": "mylib",
|
{
|
||||||
"targets": {
|
"projects": {
|
||||||
"build": {
|
"mylib": {
|
||||||
"cache": true,
|
"root": "mylib",
|
||||||
"command": "rollup -c rollup.config.js",
|
"targets": {
|
||||||
"dependsOn": [
|
"build": {
|
||||||
"^build",
|
"cache": true,
|
||||||
],
|
"command": "rollup -c rollup.config.js",
|
||||||
"inputs": [
|
"dependsOn": [
|
||||||
"production",
|
"^build",
|
||||||
"^production",
|
],
|
||||||
{
|
"inputs": [
|
||||||
"externalDependencies": [
|
"production",
|
||||||
"rollup",
|
"^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",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
|
||||||
"options": {
|
|
||||||
"cwd": "mylib",
|
|
||||||
},
|
},
|
||||||
"outputs": [
|
|
||||||
"{workspaceRoot}/mylib/build",
|
|
||||||
"{workspaceRoot}/mylib/dist",
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
}
|
]
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`@nx/rollup/plugin root project should create nodes 1`] = `
|
exports[`@nx/rollup/plugin root project should create nodes 1`] = `
|
||||||
{
|
[
|
||||||
"projects": {
|
[
|
||||||
".": {
|
"rollup.config.js",
|
||||||
"root": ".",
|
{
|
||||||
"targets": {
|
"projects": {
|
||||||
"build": {
|
".": {
|
||||||
"cache": true,
|
"root": ".",
|
||||||
"command": "rollup -c rollup.config.js",
|
"targets": {
|
||||||
"dependsOn": [
|
"build": {
|
||||||
"^build",
|
"cache": true,
|
||||||
],
|
"command": "rollup -c rollup.config.js",
|
||||||
"inputs": [
|
"dependsOn": [
|
||||||
"production",
|
"^build",
|
||||||
"^production",
|
],
|
||||||
{
|
"inputs": [
|
||||||
"externalDependencies": [
|
"production",
|
||||||
"rollup",
|
"^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",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
|
||||||
"options": {
|
|
||||||
"cwd": ".",
|
|
||||||
},
|
},
|
||||||
"outputs": [
|
|
||||||
"{workspaceRoot}/dist",
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
}
|
]
|
||||||
`;
|
`;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { type CreateNodesContext } from '@nx/devkit';
|
import { type CreateNodesContext } from '@nx/devkit';
|
||||||
import { createNodes } from './plugin';
|
import { createNodesV2 } from './plugin';
|
||||||
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
|
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
|
||||||
|
|
||||||
// Jest 29 does not support dynamic import() unless --experimental-vm-modules is set.
|
// Jest 29 does not support dynamic import() unless --experimental-vm-modules is set.
|
||||||
@ -11,7 +11,7 @@ jest.mock('rollup/loadConfigFile', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('@nx/rollup/plugin', () => {
|
describe('@nx/rollup/plugin', () => {
|
||||||
let createNodesFunction = createNodes[1];
|
let createNodesFunction = createNodesV2[1];
|
||||||
let context: CreateNodesContext;
|
let context: CreateNodesContext;
|
||||||
let cwd = process.cwd();
|
let cwd = process.cwd();
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ describe('@nx/rollup/plugin', () => {
|
|||||||
it('should create nodes', async () => {
|
it('should create nodes', async () => {
|
||||||
// ACT
|
// ACT
|
||||||
const nodes = await createNodesFunction(
|
const nodes = await createNodesFunction(
|
||||||
'rollup.config.js',
|
['rollup.config.js'],
|
||||||
{
|
{
|
||||||
buildTargetName: 'build',
|
buildTargetName: 'build',
|
||||||
},
|
},
|
||||||
@ -154,7 +154,7 @@ describe('@nx/rollup/plugin', () => {
|
|||||||
it('should create nodes', async () => {
|
it('should create nodes', async () => {
|
||||||
// ACT
|
// ACT
|
||||||
const nodes = await createNodesFunction(
|
const nodes = await createNodesFunction(
|
||||||
'mylib/rollup.config.js',
|
['mylib/rollup.config.js'],
|
||||||
{
|
{
|
||||||
buildTargetName: 'build',
|
buildTargetName: 'build',
|
||||||
},
|
},
|
||||||
|
|||||||
@ -5,8 +5,12 @@ import {
|
|||||||
type CreateDependencies,
|
type CreateDependencies,
|
||||||
type CreateNodes,
|
type CreateNodes,
|
||||||
CreateNodesContext,
|
CreateNodesContext,
|
||||||
|
createNodesFromFiles,
|
||||||
|
CreateNodesV2,
|
||||||
detectPackageManager,
|
detectPackageManager,
|
||||||
|
getPackageManagerCommand,
|
||||||
joinPathFragments,
|
joinPathFragments,
|
||||||
|
logger,
|
||||||
readJsonFile,
|
readJsonFile,
|
||||||
type TargetConfiguration,
|
type TargetConfiguration,
|
||||||
writeJsonFile,
|
writeJsonFile,
|
||||||
@ -15,27 +19,27 @@ import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash
|
|||||||
import { getLockFileName } from '@nx/js';
|
import { getLockFileName } from '@nx/js';
|
||||||
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
|
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
|
||||||
import { type RollupOptions } from 'rollup';
|
import { type RollupOptions } from 'rollup';
|
||||||
|
import { hashObject } from 'nx/src/hasher/file-hasher';
|
||||||
|
|
||||||
const cachePath = join(workspaceDataDirectory, 'rollup.hash');
|
const pmc = getPackageManagerCommand();
|
||||||
const targetsCache = readTargetsCache();
|
|
||||||
|
|
||||||
function readTargetsCache(): Record<
|
function readTargetsCache(
|
||||||
string,
|
cachePath: string
|
||||||
Record<string, TargetConfiguration>
|
): Record<string, Record<string, TargetConfiguration>> {
|
||||||
> {
|
|
||||||
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
|
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeTargetsToCache() {
|
function writeTargetsToCache(
|
||||||
const oldCache = readTargetsCache();
|
cachePath: string,
|
||||||
writeJsonFile(cachePath, {
|
results: Record<string, Record<string, TargetConfiguration>>
|
||||||
...oldCache,
|
) {
|
||||||
...targetsCache,
|
writeJsonFile(cachePath, results);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated The 'createDependencies' function is now a no-op. This functionality is included in 'createNodesV2'.
|
||||||
|
*/
|
||||||
export const createDependencies: CreateDependencies = () => {
|
export const createDependencies: CreateDependencies = () => {
|
||||||
writeTargetsToCache();
|
|
||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,47 +47,95 @@ export interface RollupPluginOptions {
|
|||||||
buildTargetName?: string;
|
buildTargetName?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const rollupConfigGlob = '**/rollup.config.{js,cjs,mjs}';
|
||||||
|
|
||||||
export const createNodes: CreateNodes<RollupPluginOptions> = [
|
export const createNodes: CreateNodes<RollupPluginOptions> = [
|
||||||
'**/rollup.config.{js,cjs,mjs}',
|
rollupConfigGlob,
|
||||||
async (configFilePath, options, context) => {
|
async (configFilePath, options, context) => {
|
||||||
const projectRoot = dirname(configFilePath);
|
logger.warn(
|
||||||
const fullyQualifiedProjectRoot = join(context.workspaceRoot, projectRoot);
|
'`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.'
|
||||||
// Do not create a project if package.json and project.json do not exist
|
|
||||||
const siblingFiles = readdirSync(fullyQualifiedProjectRoot);
|
|
||||||
if (
|
|
||||||
!siblingFiles.includes('package.json') &&
|
|
||||||
!siblingFiles.includes('project.json')
|
|
||||||
) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
options = normalizeOptions(options);
|
|
||||||
|
|
||||||
const hash = await calculateHashForCreateNodes(
|
|
||||||
projectRoot,
|
|
||||||
options,
|
|
||||||
context,
|
|
||||||
[getLockFileName(detectPackageManager(context.workspaceRoot))]
|
|
||||||
);
|
);
|
||||||
|
return createNodesInternal(
|
||||||
targetsCache[hash] ??= await buildRollupTarget(
|
|
||||||
configFilePath,
|
configFilePath,
|
||||||
projectRoot,
|
normalizeOptions(options),
|
||||||
options,
|
context,
|
||||||
context
|
{}
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
|
||||||
projects: {
|
|
||||||
[projectRoot]: {
|
|
||||||
root: projectRoot,
|
|
||||||
targets: targetsCache[hash],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const createNodesV2: CreateNodesV2<RollupPluginOptions> = [
|
||||||
|
rollupConfigGlob,
|
||||||
|
async (configFilePaths, options, context) => {
|
||||||
|
const normalizedOptions = normalizeOptions(options);
|
||||||
|
const optionsHash = hashObject(normalizedOptions);
|
||||||
|
const cachePath = join(
|
||||||
|
workspaceDataDirectory,
|
||||||
|
`rollup-${optionsHash}.hash`
|
||||||
|
);
|
||||||
|
const targetsCache = readTargetsCache(cachePath);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await createNodesFromFiles(
|
||||||
|
(configFile, _, context) =>
|
||||||
|
createNodesInternal(
|
||||||
|
configFile,
|
||||||
|
normalizedOptions,
|
||||||
|
context,
|
||||||
|
targetsCache
|
||||||
|
),
|
||||||
|
configFilePaths,
|
||||||
|
normalizedOptions,
|
||||||
|
context
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
writeTargetsToCache(cachePath, targetsCache);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
async function createNodesInternal(
|
||||||
|
configFilePath: string,
|
||||||
|
options: Required<RollupPluginOptions>,
|
||||||
|
context: CreateNodesContext,
|
||||||
|
targetsCache: Record<string, Record<string, TargetConfiguration>>
|
||||||
|
) {
|
||||||
|
const projectRoot = dirname(configFilePath);
|
||||||
|
const fullyQualifiedProjectRoot = join(context.workspaceRoot, projectRoot);
|
||||||
|
|
||||||
|
// Do not create a project if package.json and project.json do not exist
|
||||||
|
const siblingFiles = readdirSync(fullyQualifiedProjectRoot);
|
||||||
|
if (
|
||||||
|
!siblingFiles.includes('package.json') &&
|
||||||
|
!siblingFiles.includes('project.json')
|
||||||
|
) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const hash = await calculateHashForCreateNodes(
|
||||||
|
projectRoot,
|
||||||
|
options,
|
||||||
|
context,
|
||||||
|
[getLockFileName(detectPackageManager(context.workspaceRoot))]
|
||||||
|
);
|
||||||
|
|
||||||
|
targetsCache[hash] ??= await buildRollupTarget(
|
||||||
|
configFilePath,
|
||||||
|
projectRoot,
|
||||||
|
options,
|
||||||
|
context
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
projects: {
|
||||||
|
[projectRoot]: {
|
||||||
|
root: projectRoot,
|
||||||
|
targets: targetsCache[hash],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function buildRollupTarget(
|
async function buildRollupTarget(
|
||||||
configFilePath: string,
|
configFilePath: string,
|
||||||
projectRoot: string,
|
projectRoot: string,
|
||||||
@ -134,6 +186,19 @@ async function buildRollupTarget(
|
|||||||
{ externalDependencies: ['rollup'] },
|
{ externalDependencies: ['rollup'] },
|
||||||
],
|
],
|
||||||
outputs,
|
outputs,
|
||||||
|
metadata: {
|
||||||
|
technologies: ['rollup'],
|
||||||
|
description: 'Run Rollup',
|
||||||
|
help: {
|
||||||
|
command: `${pmc.exec} rollup --help`,
|
||||||
|
example: {
|
||||||
|
options: {
|
||||||
|
sourcemap: true,
|
||||||
|
watch: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
return targets;
|
return targets;
|
||||||
}
|
}
|
||||||
@ -173,9 +238,10 @@ function getOutputs(
|
|||||||
return Array.from(outputs);
|
return Array.from(outputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeOptions(options: RollupPluginOptions) {
|
function normalizeOptions(
|
||||||
options ??= {};
|
options: RollupPluginOptions
|
||||||
options.buildTargetName ??= 'build';
|
): Required<RollupPluginOptions> {
|
||||||
|
return {
|
||||||
return options;
|
buildTargetName: options.buildTargetName ?? 'build',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user