From a9cb9a94679ba2adb3deecc36bdfdfb2acccadbe Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 4 Jun 2020 15:24:51 +0200 Subject: [PATCH] packages/cli: make build output formats configurable and add separate build command --- packages/cli/src/commands/build.ts | 23 ++++++++++++ packages/cli/src/commands/plugin/build.ts | 4 ++- packages/cli/src/index.ts | 5 +++ packages/cli/src/lib/packager/config.ts | 44 +++++++++++++++++------ packages/cli/src/lib/packager/index.ts | 1 + packages/cli/src/lib/packager/packager.ts | 5 +-- packages/cli/src/lib/packager/types.ts | 21 +++++++++++ 7 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 packages/cli/src/commands/build.ts create mode 100644 packages/cli/src/lib/packager/types.ts diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts new file mode 100644 index 0000000000..2bc661af34 --- /dev/null +++ b/packages/cli/src/commands/build.ts @@ -0,0 +1,23 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { buildPackage } from '../lib/packager'; + +export default async () => { + await buildPackage({ + outputs: new Set(['types', 'esm', 'cjs']), + }); +}; diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index 561d79ed55..5fa9827116 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -17,5 +17,7 @@ import { buildPackage } from '../../lib/packager'; export default async () => { - await buildPackage(); + await buildPackage({ + outputs: new Set(['esm', 'types']), + }); }; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index bdd0d3e1f2..7eee21c8da 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -78,6 +78,11 @@ const main = (argv: string[]) => { .description('Diff an existing plugin with the creation template') .action(actionHandler(() => require('./commands/plugin/diff'))); + program + .command('build') + .description('Build a package for publishing') + .action(actionHandler(() => require('./commands/build'))); + program .command('lint') .option('--fix', 'Attempt to automatically fix violations') diff --git a/packages/cli/src/lib/packager/config.ts b/packages/cli/src/lib/packager/config.ts index df91f8067a..ab59bab413 100644 --- a/packages/cli/src/lib/packager/config.ts +++ b/packages/cli/src/lib/packager/config.ts @@ -24,11 +24,14 @@ import esbuild from 'rollup-plugin-esbuild'; import imageFiles from 'rollup-plugin-image-files'; import dts from 'rollup-plugin-dts'; import json from '@rollup/plugin-json'; -import { RollupOptions } from 'rollup'; +import { RollupOptions, OutputOptions } from 'rollup'; +import { BuildOptions } from './types'; import { paths } from '../paths'; -export const makeConfigs = async (): Promise => { +export const makeConfigs = async ( + options: BuildOptions, +): Promise => { const typesInput = paths.resolveTargetRoot( 'dist', relativePath(paths.targetRoot, paths.targetDir), @@ -43,13 +46,27 @@ export const makeConfigs = async (): Promise => { ); } - return [ - { - input: 'src/index.ts', - output: { + const configs = new Array(); + + if (options.outputs.has('cjs') || options.outputs.has('esm')) { + const output = new Array(); + + if (options.outputs.has('cjs')) { + output.push({ + file: 'dist/index.cjs.js', + format: 'commonjs', + }); + } + if (options.outputs.has('esm')) { + output.push({ file: 'dist/index.esm.js', format: 'module', - }, + }); + } + + configs.push({ + input: 'src/index.ts', + output, plugins: [ peerDepsExternal({ includeDependencies: true, @@ -68,14 +85,19 @@ export const makeConfigs = async (): Promise => { target: 'es2019', }), ], - }, - { + }); + } + + if (options.outputs.has('types')) { + configs.push({ input: typesInput, output: { file: 'dist/index.d.ts', format: 'es', }, plugins: [dts()], - }, - ]; + }); + } + + return configs; }; diff --git a/packages/cli/src/lib/packager/index.ts b/packages/cli/src/lib/packager/index.ts index e639599af9..5a179b9ddc 100644 --- a/packages/cli/src/lib/packager/index.ts +++ b/packages/cli/src/lib/packager/index.ts @@ -15,3 +15,4 @@ */ export { buildPackage } from './packager'; +export type { BuildOptions, OutputFormat } from './types'; diff --git a/packages/cli/src/lib/packager/packager.ts b/packages/cli/src/lib/packager/packager.ts index d3e88d8c3d..0c41d0f767 100644 --- a/packages/cli/src/lib/packager/packager.ts +++ b/packages/cli/src/lib/packager/packager.ts @@ -19,6 +19,7 @@ import chalk from 'chalk'; import { relative as relativePath } from 'path'; import { paths } from '../paths'; import { makeConfigs } from './config'; +import { BuildOptions } from './types'; function formatErrorMessage(error: any) { let msg = ''; @@ -80,7 +81,7 @@ async function build(config: RollupOptions) { } } -export const buildPackage = async () => { - const configs = await makeConfigs(); +export const buildPackage = async (options: BuildOptions) => { + const configs = await makeConfigs(options); await Promise.all(configs.map(build)); }; diff --git a/packages/cli/src/lib/packager/types.ts b/packages/cli/src/lib/packager/types.ts new file mode 100644 index 0000000000..49b697c1c6 --- /dev/null +++ b/packages/cli/src/lib/packager/types.ts @@ -0,0 +1,21 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export type OutputFormat = 'esm' | 'cjs' | 'types'; + +export type BuildOptions = { + outputs: Set; +};