Skip to content

Commit

Permalink
feat(bundling): make rollup executor return outfile for node executor (
Browse files Browse the repository at this point in the history
…#14989)

(cherry picked from commit bd2a1ae)
  • Loading branch information
cm-ayf authored and FrozenPandaz committed Feb 15, 2023
1 parent a276bcd commit ed0e6c7
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ts from 'typescript';
import * as rollup from 'rollup';
import * as peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { getBabelInputPlugin } from '@rollup/plugin-babel';
import { dirname, join, parse } from 'path';
import { dirname, join, parse, resolve } from 'path';
import { from, Observable, of } from 'rxjs';
import { catchError, concatMap, last, scan, tap } from 'rxjs/operators';
import { eachValueFrom } from '@nrwl/devkit/src/utils/rxjs-for-await';
Expand All @@ -15,7 +15,7 @@ import {
computeCompilerOptionsPaths,
DependentBuildableProjectNode,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import resolve from '@rollup/plugin-node-resolve';
import nodeResolve from '@rollup/plugin-node-resolve';

import { AssetGlobPattern, RollupExecutorOptions } from './schema';
import { runRollup } from './lib/run-rollup';
Expand All @@ -29,6 +29,11 @@ import { swc } from './lib/swc-plugin';
import { validateTypes } from './lib/validate-types';
import { updatePackageJson } from './lib/update-package-json';

export type RollupExecutorEvent = {
success: boolean;
outfile?: string;
};

// These use require because the ES import isn't correct.
const commonjs = require('@rollup/plugin-commonjs');
const image = require('@rollup/plugin-image');
Expand Down Expand Up @@ -77,6 +82,8 @@ export async function* rollupExecutor(
npmDeps
);

const outfile = resolveOutfile(context, options);

if (options.compiler === 'swc') {
try {
await validateTypes({
Expand All @@ -92,7 +99,7 @@ export async function* rollupExecutor(
if (options.watch) {
const watcher = rollup.watch(rollupOptions);
return yield* eachValueFrom(
new Observable<{ success: boolean }>((obs) => {
new Observable<RollupExecutorEvent>((obs) => {
watcher.on('event', (data) => {
if (data.code === 'START') {
logger.info(`Bundling ${context.projectName}...`);
Expand All @@ -105,7 +112,7 @@ export async function* rollupExecutor(
packageJson
);
logger.info('Bundle complete. Watching for file changes...');
obs.next({ success: true });
obs.next({ success: true, outfile });
} else if (data.code === 'ERROR') {
logger.error(`Error during bundle: ${data.error.message}`);
obs.next({ success: false });
Expand Down Expand Up @@ -135,12 +142,12 @@ export async function* rollupExecutor(
})
)
),
scan(
scan<RollupExecutorEvent>(
(acc, result) => {
if (!acc.success) return acc;
return result;
},
{ success: true }
{ success: true, outfile }
),
last(),
tap({
Expand Down Expand Up @@ -231,7 +238,7 @@ export function createRollupOptions(
},
},
}),
resolve({
nodeResolve({
preferBuiltins: true,
extensions: fileExtensions,
}),
Expand Down Expand Up @@ -341,4 +348,13 @@ function readCompatibleFormats(config: ts.ParsedCommandLine) {
}
}

function resolveOutfile(
context: ExecutorContext,
options: NormalizedRollupExecutorOptions
) {
if (!options.format?.includes('cjs')) return undefined;
const { name } = parse(options.outputFileName ?? options.main);
return resolve(context.root, options.outputPath, `${name}.cjs`);
}

export default rollupExecutor;

0 comments on commit ed0e6c7

Please sign in to comment.