From e035fe511834f9af866128ac3438899d067504a5 Mon Sep 17 00:00:00 2001 From: Filip Sobol Date: Sun, 14 Apr 2024 15:42:51 +0200 Subject: [PATCH] Allow user-provided rewrites and update tests --- .../ckeditor5-dev-build-tools/src/build.ts | 3 +- .../ckeditor5-dev-build-tools/src/config.ts | 12 +++++-- .../tests/build/arguments.test.ts | 20 +++++------- .../tests/build/build.test.ts | 7 +---- .../tests/config/config.test.ts | 31 ++++++++++++++++++- 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/packages/ckeditor5-dev-build-tools/src/build.ts b/packages/ckeditor5-dev-build-tools/src/build.ts index 86729cb02..be88692bc 100644 --- a/packages/ckeditor5-dev-build-tools/src/build.ts +++ b/packages/ckeditor5-dev-build-tools/src/build.ts @@ -16,6 +16,7 @@ export interface BuildOptions { tsconfig: string; banner: string; external: Array; + rewrite: Array<[string, string]>; declarations: boolean; translations: string; sourceMap: boolean; @@ -29,6 +30,7 @@ export const defaultOptions: BuildOptions = { tsconfig: 'tsconfig.json', banner: '', external: [], + rewrite: [], declarations: false, translations: '', sourceMap: false, @@ -50,7 +52,6 @@ function getCliArguments(): Partial { 'declarations': { type: 'boolean' }, 'translations': { type: 'string' }, 'source-map': { type: 'boolean' }, - 'bundle': { type: 'boolean' }, 'minify': { type: 'boolean' }, 'clean': { type: 'boolean' } }, diff --git a/packages/ckeditor5-dev-build-tools/src/config.ts b/packages/ckeditor5-dev-build-tools/src/config.ts index 62c124918..4886f8f51 100644 --- a/packages/ckeditor5-dev-build-tools/src/config.ts +++ b/packages/ckeditor5-dev-build-tools/src/config.ts @@ -46,6 +46,7 @@ export async function getRollupConfig( options: BuildOptions ) { tsconfig, banner, external, + rewrite, declarations, translations, sourceMap, @@ -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. @@ -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 ] } ), diff --git a/packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts b/packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts index 76242a33d..e9fa9ce4b 100644 --- a/packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts +++ b/packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts @@ -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'; @@ -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(); @@ -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(); @@ -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 } ); diff --git a/packages/ckeditor5-dev-build-tools/tests/build/build.test.ts b/packages/ckeditor5-dev-build-tools/tests/build/build.test.ts index bfa684f37..af91811e1 100644 --- a/packages/ckeditor5-dev-build-tools/tests/build/build.test.ts +++ b/packages/ckeditor5-dev-build-tools/tests/build/build.test.ts @@ -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'; @@ -30,11 +30,6 @@ vi } as any; } ); -// eslint-disable-next-line mocha/no-top-level-hooks -beforeEach( () => { - vi.clearAllMocks(); -} ); - /** * Input */ diff --git a/packages/ckeditor5-dev-build-tools/tests/config/config.test.ts b/packages/ckeditor5-dev-build-tools/tests/config/config.test.ts index 36f2b570a..d494d46a8 100644 --- a/packages/ckeditor5-dev-build-tools/tests/config/config.test.ts +++ b/packages/ckeditor5-dev-build-tools/tests/config/config.test.ts @@ -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[0]; @@ -14,6 +14,7 @@ const defaults: Options = { tsconfig: '', banner: '', external: [], + rewrite: [], declarations: false, translations: '', sourceMap: false, @@ -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( {