Skip to content

Commit

Permalink
fix(bundling): rename aliases for @nx/rollup:convert-to-inferred gene…
Browse files Browse the repository at this point in the history
…rator
  • Loading branch information
jaysoo committed Jun 24, 2024
1 parent dece9af commit d2c863b
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function createProject(tree: Tree, opts: Partial<CreateProjectOptions> = {}) {
projectOpts.targetOptions.outputPath ??= `dist/${projectOpts.root}`;
projectOpts.targetOptions.tsConfig ??= `${projectOpts.root}/tsconfig.lib.json`;
projectOpts.targetOptions.compiler ??= 'babel';
projectOpts.targetOptions.format ??= ['esm'];
projectOpts.targetOptions.format ??= projectOpts.targetOptions.f ?? ['esm'];
projectOpts.targetOptions.external ??= [];
projectOpts.targetOptions.assets ??= [];
}
Expand Down Expand Up @@ -516,6 +516,57 @@ describe('Rollup - Convert Executors To Plugin', () => {
"
`);
});

it('should rename aliases to the original option name', async () => {
const project = createProject(tree, {
name: 'mypkg',
root: 'mypkg',
targetOptions: {
entryFile: 'mypkg/src/foo.ts',
f: ['cjs'],
exports: true,
},
});

await convertToInferred(tree, { project: project.name });

expect(readNxJson(tree).plugins).toEqual([
{
options: {
targetName: 'build',
},
plugin: '@nx/rollup/plugin',
},
]);
expect(tree.read('mypkg/rollup.config.js', 'utf-8'))
.toMatchInlineSnapshot(`
"const { withNx } = require('@nx/rollup/with-nx');
// These options were migrated by @nx/rollup:convert-to-inferred from project.json
const options = {
main: './src/index.ts',
format: ['cjs'],
generateExportsField: true,
outputPath: '../dist/mypkg',
tsConfig: './tsconfig.lib.json',
compiler: 'babel',
external: [],
assets: [],
};
const config = withNx(options, {
// Provide additional rollup configuration here. See: https://rollupjs.org/configuration-options
// e.g.
// output: { sourcemap: true },
});
module.exports = config;
"
`);
expect(tree.exists('otherpkg1/rollup.config.js')).toBe(false);
expect(tree.exists('otherpkg2/rollup.config.js')).toBe(false);
expect(readProjectConfiguration(tree, project.name).targets).toEqual({});
});
});

describe('all projects', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { joinPathFragments, stripIndents, Tree } from '@nx/devkit';
import { RollupExecutorOptions } from '../../../executors/rollup/schema';
import { normalizePathOptions } from './normalize-path-options';

const aliases = {
entryFile: 'main',
exports: 'generateExportsField',
f: 'format',
};

export function extractRollupConfigFromExecutorOptions(
tree: Tree,
options: RollupExecutorOptions,
Expand Down Expand Up @@ -36,7 +42,11 @@ export function extractRollupConfigFromExecutorOptions(
for (const [key, value] of Object.entries(options)) {
if (key === 'watch') continue;
delete options[key];
defaultOptions[key] = value;
if (aliases[key]) {
defaultOptions[aliases[key]] = value;
} else {
defaultOptions[key] = value;
}
}
let configurationOptions: Record<string, Record<string, unknown>>;
if (hasConfigurations) {
Expand Down
58 changes: 55 additions & 3 deletions packages/rollup/src/plugins/with-nx/with-nx-options.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,76 @@
// TODO: Add TSDoc
export interface RollupWithNxPluginOptions {
/**
* Additional entry-points to add to exports field in the package.json file.
* */
additionalEntryPoints?: string[];
/**
* Allow JavaScript files to be compiled.
*/
allowJs?: boolean;
/**
* List of static assets.
*/
assets?: any[];
/**
* Whether to set rootmode to upward. See https://babeljs.io/docs/en/options#rootmode
*/
babelUpwardRootMode?: boolean;
/**
* Which compiler to use.
*/
compiler?: 'babel' | 'tsc' | 'swc';
/**
* Delete the output path before building. Defaults to true.
*/
deleteOutputPath?: boolean;
/**
* A list of external modules that will not be bundled (`react`, `react-dom`, etc.). Can also be set to `all` (bundle nothing) or `none` (bundle everything).
*/
external?: string[] | 'all' | 'none';
/**
* CSS files will be extracted to the output folder. Alternatively custom filename can be provided (e.g. styles.css)
*/
extractCss?: boolean | string;
/**
* List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).
*/
format?: ('cjs' | 'esm')[];
/**
* Update the output package.json file's 'exports' field. This field is used by Node and bundles.
*/
generateExportsField?: boolean;
/**
* Sets `javascriptEnabled` option for less loader
*/
javascriptEnabled?: boolean;
/**
* The path to the entry file, relative to project.
*/
main: string;
/** @deprecated Do not set this. The package.json file in project root is detected automatically. */
/**
* The path to package.json file.
* @deprecated Do not set this. The package.json file in project root is detected automatically.
*/
project?: string;
/**
* Name of the main output file. Defaults same basename as 'main' file.
*/
outputFileName?: string;
/**
* The output path of the generated files.
*/
outputPath: string;
rollupConfig?: string | string[];
/**
* Whether to skip TypeScript type checking.
*/
skipTypeCheck?: boolean;
/**
* Prevents 'type' field from being added to compiled package.json file. Use this if you are having an issue with this field.
*/
skipTypeField?: boolean;
/**
* The path to tsconfig file.
*/
tsConfig: string;
}

Expand Down

0 comments on commit d2c863b

Please sign in to comment.