Skip to content

Commit

Permalink
Merge pull request #981 from ckeditor/ck/16798
Browse files Browse the repository at this point in the history
Fix (build-tools): Ability to pass `globals` parameter if necessary for external imports in `umd` bundles. See ckeditor/ckeditor5#16798.

BREAKING CHANGE: Ability to pass `globals` parameter if necessary for external imports in `umd` bundles.
  • Loading branch information
pszczesniak authored Jul 26, 2024
2 parents 6fea367 + 6e851a3 commit 74f4571
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
24 changes: 24 additions & 0 deletions packages/ckeditor5-dev-build-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ The name of the UMD bundle. This name will be used as the global variable name w

This option is required if the `browser' option is enabled.

#### `globals` / `--globals=[moduleID:Global]`

**Type:** `{ [ name: string ]: string } | ( ( name: string ) => string )` | `string[]`

**Default value:** `{}`

Pairs of external package names and associated global variables used in the `umd` build.

The list already contains `ckeditor5` and `ckeditor5-premium-features` which are mapped to `CKEDITOR` and `CKEDITOR_PREMIUM_FEATURES`.

When using the CLI, this option can be used multiple times.

**Example value:** `--globals=external-id:variableName --globals=another-external-id:anotherVariableName`

When using the JavaScript API, the option must be an object.

**Example value:**
```js
globals: {
'external-id': 'variableName',
'another-external-id': 'anotherVariableName'
}
```

#### `external` / `--external=[path]`

**Type:** `string[]` | `string`
Expand Down
39 changes: 33 additions & 6 deletions packages/ckeditor5-dev-build-tools/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import url from 'url';
import util from 'util';
import chalk from 'chalk';
import path from 'upath';
import { rollup, type RollupOutput } from 'rollup';
import { rollup, type RollupOutput, type GlobalsOption } from 'rollup';
import { getRollupConfig } from './config.js';
import { getCwdPath, camelizeObjectKeys, removeWhitespace } from './utils.js';

Expand All @@ -17,6 +17,7 @@ export interface BuildOptions {
output: string;
tsconfig: string;
name: string;
globals: GlobalsOption | Array<string>;
banner: string;
external: Array<string>;
rewrite: Array<[string, string]>;
Expand All @@ -33,6 +34,7 @@ export const defaultOptions: BuildOptions = {
output: 'dist/index.js',
tsconfig: 'tsconfig.json',
name: '',
globals: {},
banner: '',
external: [],
rewrite: [],
Expand All @@ -44,6 +46,14 @@ export const defaultOptions: BuildOptions = {
browser: false
};

/**
* `ckeditor5` and `ckeditor5-premium-features` globals.
*/
const CKEDITOR_GLOBALS: GlobalsOption = {
ckeditor5: 'CKEDITOR',
'ckeditor5-premium-features': 'CKEDITOR_PREMIUM_FEATURES'
};

/**
* Reads CLI arguments and turn the keys into camelcase.
*/
Expand All @@ -61,7 +71,8 @@ function getCliArguments(): Partial<BuildOptions> {
'minify': { type: 'boolean' },
'clean': { type: 'boolean' },
'browser': { type: 'boolean' },
'name': { type: 'string' }
'name': { type: 'string' },
'globals': { type: 'string', multiple: true }
},

// Skip `node ckeditor5-build-package`.
Expand All @@ -74,6 +85,17 @@ function getCliArguments(): Partial<BuildOptions> {
return camelizeObjectKeys( values );
}

/**
* Convert `globals` parameter to object when it's passed via CLI as `<external-id:variableName,another-external-id:anotherVariableName, >`
*/
function normalizeGlobalsParameter( globals: GlobalsOption | Array<string> ): GlobalsOption | Array<string> {
if ( Array.isArray( globals ) ) {
return Object.fromEntries( globals.map( item => item.split( ':' ) ) );
}

return globals;
}

/**
* Generates `UMD` build based on previous `ESM` build.
*/
Expand All @@ -83,17 +105,18 @@ async function generateUmdBuild( args: BuildOptions, bundle: RollupOutput ): Pro
const { dir, name } = path.parse( args.output );
const { plugins, ...config } = await getRollupConfig( args );
const build = await rollup( config );
const globals = {
...CKEDITOR_GLOBALS,
...args.globals as GlobalsOption
};

const umdBundle = await build.write( {
format: 'umd',
file: path.join( dir, `${ name }.umd.js` ),
assetFileNames: '[name][extname]',
sourcemap: args.sourceMap,
name: args.name,
globals: {
ckeditor5: 'ckeditor5',
'ckeditor5-premium-features': 'ckeditor5-premium-features'
}
globals
} );

return {
Expand Down Expand Up @@ -137,6 +160,10 @@ async function normalizeOptions( options: Partial<BuildOptions> ): Promise<Build
normalized.banner = banner;
}

if ( normalized.globals ) {
normalized.globals = normalizeGlobalsParameter( normalized.globals );
}

return normalized;
}

Expand Down
25 changes: 25 additions & 0 deletions packages/ckeditor5-dev-build-tools/tests/build/arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ test( '--external', async () => {
expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { external: [ 'foo', 'bar' ] } ) );
} );

test( '--globals', async () => {
const spy = getConfigMock();

mockCliArgs( '--globals=foo:bar', '--globals=baz:faz' );
await build();

expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { globals: { foo: 'bar', baz: 'faz' } } ) );
} );

test( '--declarations', async () => {
const spy = getConfigMock();

Expand Down Expand Up @@ -212,6 +221,22 @@ test( '.external', async () => {
expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { external: [ 'foo', 'bar' ] } ) );
} );

test( '.globals', async () => {
const spy = getConfigMock();

await build( { globals: { foo: 'bar' } } );

expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { globals: { foo: 'bar' } } ) );
} );

test( '.globals (empty object)', async () => {
const spy = getConfigMock();

await build( { globals: {} } );

expect( spy ).toHaveBeenCalledWith( expect.objectContaining( { globals: {} } ) );
} );

test( '.rewrite', async () => {
const spy = getConfigMock();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ test( 'Minify', async () => {
expect( output[ 0 ].code ).toContain( 'export{' );
} );

test( 'Minification doesnt remove banner', async () => {
test( 'Minification doesn\'t remove banner', async () => {
const { output } = await build( {
input: 'src/input.js',
banner: 'src/banner.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaults: Options = {
banner: '',
external: [],
rewrite: [],
globals: [],
declarations: false,
translations: '',
sourceMap: false,
Expand Down

0 comments on commit 74f4571

Please sign in to comment.