Skip to content

Commit

Permalink
Read rewrites from dependencies rather than packages-lists
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsobol committed Apr 14, 2024
1 parent 8518403 commit f1271df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
5 changes: 3 additions & 2 deletions packages/ckeditor5-dev-build-tools/package.json
Expand Up @@ -52,12 +52,13 @@
"upath": "^2.0.1"
},
"devDependencies": {
"@ckeditor/ckeditor5-utils": "alpha",
"@ckeditor/ckeditor5-utils": "^41.3.0",
"@types/css": "^0.0.37",
"@types/node": "^20.11.5",
"@types/postcss-mixins": "^9.0.5",
"@vitest/coverage-v8": "^1.2.1",
"ckeditor5": "alpha",
"ckeditor5": "^41.3.0",
"ckeditor5-premium-features": "^41.3.0",
"type-fest": "^4.10.2",
"typescript": "^5.3.3",
"vitest": "^1.2.1"
Expand Down
43 changes: 29 additions & 14 deletions packages/ckeditor5-dev-build-tools/src/config.ts
Expand Up @@ -6,6 +6,7 @@
import path from 'upath';
import { existsSync } from 'fs';
import { createRequire } from 'module';
import type { PackageJson } from 'type-fest';
import type { InputPluginOption, Plugin, RollupOptions } from 'rollup';
import type { BuildOptions } from './build.js';

Expand Down Expand Up @@ -64,13 +65,12 @@ export async function getRollupConfig( options: BuildOptions ) {
*
* This mapping can be removed when old installation methods are deprecated.
*/
const { default: rewrites }: { default: Record<string, Array<string>> } = await import(
resolveUserDependency( 'ckeditor5/packages-lists.json' ),
{ with: { type: 'json' } }
);
const mappedExternals = external.reduce( ( carry, pkg ) => carry.concat( rewrites[ pkg ] || [] ), external );
const ckeditor5Import = new RegExp( 'ckeditor5/src/([a-z-]+)(?:[a-z-/.]+)?' );
const collaborationImport = new RegExp( 'ckeditor5-collaboration/src/([a-z-]+)(?:[a-z-/.]+)?' );
const rewrites = [
...( external.includes( 'ckeditor5' ) ? await getPackageDependencies( 'ckeditor5' ) : [] ),
...( external.includes( 'ckeditor5-premium-features' ) ? await getPackageDependencies( 'ckeditor5-premium-features' ) : [] )
];

external.push( ...rewrites );

/**
* Get the name of the output CSS file based on the name of the "output" file.
Expand Down Expand Up @@ -100,7 +100,8 @@ export async function getRollupConfig( options: BuildOptions ) {

const extension = path.extname( id );

return mappedExternals.includes( packageName ) && ( !extension || extensions.includes( extension ) );
// Don't bundle, unless the import has non-JS or non-TS file extension (for example `.css`).
return external.includes( packageName ) && ( !extension || extensions.includes( extension ) );
},

plugins: [
Expand Down Expand Up @@ -204,8 +205,8 @@ export async function getRollupConfig( options: BuildOptions ) {
* - ckeditor5/src/XXX (optionally with `.js` or `.ts` extension).
* - ckeditor5-collaboration/src/XXX (optionally with `.js` or `.ts` extension).
*/
[ ckeditor5Import, '@ckeditor/ckeditor5-$1/dist/index.js' ],
[ collaborationImport, 'ckeditor5-collaboration/dist/index.js' ],
[ /ckeditor5\/src\/([a-z-]+)(?:[a-z-/.]+)?/, '@ckeditor/ckeditor5-$1/dist/index.js' ],
[ /ckeditor5-collaboration\/src\/([a-z-]+)(?:[a-z-/.]+)?/, 'ckeditor5-collaboration/dist/index.js' ],

/**
* Rewrite "old" imports to imports used in new installation methods.
Expand All @@ -216,10 +217,7 @@ export async function getRollupConfig( options: BuildOptions ) {
* [ '@ckeditor/ckeditor5-ai', 'ckeditor5-premium-features' ],
* [ '@ckeditor/ckeditor5-case-change', 'ckeditor5-premium-features' ],
*/
...Object
.values( rewrites )
.flat()
.map( pkg => [ pkg, `${ pkg }/dist/index.js` ] as [ string, string ] )
...rewrites.map( pkg => [ pkg, `${ pkg }/dist/index.js` ] as [ string, string ] )
]
} ),

Expand Down Expand Up @@ -262,6 +260,23 @@ function resolveUserDependency( dependencyName: string ): string {
);
}

/**
* Returns a list of keys in `package.json` file of a given dependency.
*/
async function getPackageDependencies( packageName: string ): Promise<Array<string>> {
try {
const { default: pkg }: { default: PackageJson } = await import(
resolveUserDependency( `${ packageName }/package.json` ),
{ with: { type: 'json' } }
);

return Object.keys( pkg.dependencies || {} );
} catch {
// Dependency is not installed, so return an empty array.
return Promise.resolve( [] );
}
}

/**
* Returns the TypeScript plugin if tsconfig file exists, otherwise doesn't return anything.
*/
Expand Down
Expand Up @@ -68,6 +68,7 @@ test( '--external automatically adds packages that make up the "ckeditor5"', asy
expect( config.external( 'ckeditor5' ) ).toBe( true );
expect( config.external( 'ckeditor5/src/ui.js' ) ).toBe( true );
expect( config.external( '@ckeditor/ckeditor5-core' ) ).toBe( true );
expect( config.external( '@ckeditor/ckeditor5-code-block/theme/codeblock.css' ) ).toBe( false );
} );

test( '--external automatically adds packages that make up the "ckeditor5-premium-features"', async () => {
Expand All @@ -76,7 +77,9 @@ test( '--external automatically adds packages that make up the "ckeditor5-premiu
} );

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

test( '--translations', async () => {
Expand Down

0 comments on commit f1271df

Please sign in to comment.