Skip to content

Commit

Permalink
Allow user-provided rewrites and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsobol committed Apr 14, 2024
1 parent 6d18cac commit e035fe5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
3 changes: 2 additions & 1 deletion packages/ckeditor5-dev-build-tools/src/build.ts
Expand Up @@ -16,6 +16,7 @@ export interface BuildOptions {
tsconfig: string;
banner: string;
external: Array<string>;
rewrite: Array<[string, string]>;
declarations: boolean;
translations: string;
sourceMap: boolean;
Expand All @@ -29,6 +30,7 @@ export const defaultOptions: BuildOptions = {
tsconfig: 'tsconfig.json',
banner: '',
external: [],
rewrite: [],
declarations: false,
translations: '',
sourceMap: false,
Expand All @@ -50,7 +52,6 @@ function getCliArguments(): Partial<BuildOptions> {
'declarations': { type: 'boolean' },
'translations': { type: 'string' },
'source-map': { type: 'boolean' },
'bundle': { type: 'boolean' },
'minify': { type: 'boolean' },
'clean': { type: 'boolean' }
},
Expand Down
12 changes: 9 additions & 3 deletions packages/ckeditor5-dev-build-tools/src/config.ts
Expand Up @@ -46,6 +46,7 @@ export async function getRollupConfig( options: BuildOptions ) {
tsconfig,
banner,
external,
rewrite,
declarations,
translations,
sourceMap,
Expand All @@ -65,12 +66,12 @@ export async function getRollupConfig( options: BuildOptions ) {
*
* This mapping can be removed when old installation methods are deprecated.
*/
const rewrites = [
const autoRewrites = [
...( external.includes( 'ckeditor5' ) ? await getPackageDependencies( 'ckeditor5' ) : [] ),
...( external.includes( 'ckeditor5-premium-features' ) ? await getPackageDependencies( 'ckeditor5-premium-features' ) : [] )
];

external.push( ...rewrites );
external.push( ...autoRewrites );

/**
* Get the name of the output CSS file based on the name of the "output" file.
Expand Down Expand Up @@ -217,7 +218,12 @@ export async function getRollupConfig( options: BuildOptions ) {
* [ '@ckeditor/ckeditor5-ai', 'ckeditor5-premium-features' ],
* [ '@ckeditor/ckeditor5-case-change', 'ckeditor5-premium-features' ],
*/
...rewrites.map( pkg => [ pkg, `${ pkg }/dist/index.js` ] as [ string, string ] )
...autoRewrites.map( pkg => [ pkg, `${ pkg }/dist/index.js` ] as [ string, string ] ),

/**
* Rewrites provided in the config.
*/
...rewrite
]
} ),

Expand Down
20 changes: 7 additions & 13 deletions packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import { test, expect, beforeEach, vi } from 'vitest';
import { test, expect, vi } from 'vitest';
import fs from 'fs';
import * as rollup from 'rollup';
import * as config from '../../src/config.js';
Expand Down Expand Up @@ -47,11 +47,6 @@ function getCwdPath( fileName: string ) {
return process.cwd() + fileName;
}

// eslint-disable-next-line mocha/no-top-level-hooks
beforeEach( () => {
vi.clearAllMocks();
} );

test( 'paths are normalized', async () => {
await build();

Expand Down Expand Up @@ -114,13 +109,6 @@ test( '--source-map', async () => {
expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { sourceMap: true } ) );
} );

test( '--bundle', async () => {
mockCliArgs( '--bundle' );
await build();

expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { bundle: true } ) );
} );

test( '--minify', async () => {
mockCliArgs( '--minify' );
await build();
Expand Down Expand Up @@ -170,6 +158,12 @@ test( '.external', async () => {
expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { external: [ 'foo', 'bar' ] } ) );
} );

test( '.rewrite', async () => {
await build( { rewrite: [ [ 'foo', 'bar' ] ] } );

expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { rewrite: [ [ 'foo', 'bar' ] ] } ) );
} );

test( '.declarations', async () => {
await build( { declarations: true } );

Expand Down
7 changes: 1 addition & 6 deletions packages/ckeditor5-dev-build-tools/tests/build/build.test.ts
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import { test, expect, beforeEach, vi } from 'vitest';
import { test, expect, vi } from 'vitest';
import * as rollup from 'rollup';
import { build } from '../../src/build.js';

Expand All @@ -30,11 +30,6 @@ vi
} as any;
} );

// eslint-disable-next-line mocha/no-top-level-hooks
beforeEach( () => {
vi.clearAllMocks();
} );

/**
* Input
*/
Expand Down
31 changes: 30 additions & 1 deletion packages/ckeditor5-dev-build-tools/tests/config/config.test.ts
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md.
*/

import { test, expect } from 'vitest';
import { test, expect, vi } from 'vitest';
import { getRollupConfig } from '../../src/config.js';

type Options = Parameters<typeof getRollupConfig>[0];
Expand All @@ -14,6 +14,7 @@ const defaults: Options = {
tsconfig: '',
banner: '',
external: [],
rewrite: [],
declarations: false,
translations: '',
sourceMap: false,
Expand Down Expand Up @@ -82,6 +83,34 @@ test( '--external automatically adds packages that make up the "ckeditor5-premiu
expect( config.external( '@ckeditor/ckeditor5-real-time-collaboration/theme/usermarkers.css' ) ).toBe( false );
} );

test( '--external doesnt fail when "ckeditor5-premium-features" is not installed', async () => {
vi.doMock( 'ckeditor5-premium-features/package.json', () => {
throw new Error( 'Module doesn\'t exist' );
} );

const config = await getConfig( {
external: [ 'ckeditor5-premium-features' ]
} );

expect( config.external( 'ckeditor5-premium-features' ) ).toBe( true );
expect( config.external( 'ckeditor5-collaboration/src/collaboration-core.js' ) ).toBe( false );
expect( config.external( '@ckeditor/ckeditor5-case-change' ) ).toBe( false );
} );

test( '--external doesnt fail when "ckeditor5-premium-features" doesnt have any dependencies', async () => {
vi.doMock( 'ckeditor5-premium-features/package.json', () => ( {
default: JSON.stringify( { name: 'mocked' } )
} ) );

const config = await getConfig( {
external: [ 'ckeditor5-premium-features' ]
} );

expect( config.external( 'ckeditor5-premium-features' ) ).toBe( true );
expect( config.external( 'ckeditor5-collaboration/src/collaboration-core.js' ) ).toBe( false );
expect( config.external( '@ckeditor/ckeditor5-case-change' ) ).toBe( false );
} );

test( '--translations', async () => {
const withoutTranslations = await getConfig();
const withTranslations = await getConfig( {
Expand Down

0 comments on commit e035fe5

Please sign in to comment.