diff --git a/packages/catalog-model/package.json b/packages/catalog-model/package.json index ffd8fdba22..2d6df2e34e 100644 --- a/packages/catalog-model/package.json +++ b/packages/catalog-model/package.json @@ -1,7 +1,8 @@ { "name": "@backstage/catalog-model", "version": "0.1.1-alpha.6", - "main": "dist/index.esm.js", + "main": "dist/index.cjs.js", + "module": "dist/index.esm.js", "main:src": "src/index.ts", "types": "src/index.ts", "license": "Apache-2.0", @@ -10,7 +11,7 @@ "access": "public" }, "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts new file mode 100644 index 0000000000..19e4b17352 --- /dev/null +++ b/packages/cli/src/commands/build.ts @@ -0,0 +1,37 @@ +/* + * 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, Output } from '../lib/packager'; +import { Command } from 'commander'; + +export default async (cmd: Command) => { + let outputs = new Set(); + + const { outputs: outputsStr } = cmd as { outputs?: string }; + if (outputsStr) { + for (const output of outputsStr.split(',') as (keyof typeof Output)[]) { + if (output in Output) { + outputs.add(Output[output]); + } else { + throw new Error(`Unknown output format: ${output}`); + } + } + } else { + outputs = new Set([Output.types, Output.esm, Output.cjs]); + } + + await buildPackage({ outputs }); +}; diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index 561d79ed55..7e4e5cd36a 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -14,8 +14,10 @@ * limitations under the License. */ -import { buildPackage } from '../../lib/packager'; +import { buildPackage, Output } from '../../lib/packager'; export default async () => { - await buildPackage(); + await buildPackage({ + outputs: new Set([Output.esm, Output.types]), + }); }; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index bdd0d3e1f2..8288f776e1 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -78,6 +78,12 @@ 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') + .option('--outputs ', 'List of formats to output [types,cjs,esm]') + .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..aaecd4a148 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, Output } 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(Output.cjs) || options.outputs.has(Output.esm)) { + const output = new Array(); + + if (options.outputs.has(Output.cjs)) { + output.push({ + file: 'dist/index.cjs.js', + format: 'commonjs', + }); + } + if (options.outputs.has(Output.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(Output.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..17aae25ec4 100644 --- a/packages/cli/src/lib/packager/index.ts +++ b/packages/cli/src/lib/packager/index.ts @@ -15,3 +15,5 @@ */ export { buildPackage } from './packager'; +export { Output } from './types'; +export type { BuildOptions } 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..853789df59 --- /dev/null +++ b/packages/cli/src/lib/packager/types.ts @@ -0,0 +1,25 @@ +/* + * 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 enum Output { + esm, + cjs, + types, +} + +export type BuildOptions = { + outputs: Set; +}; diff --git a/packages/core-api/package.json b/packages/core-api/package.json index a8c2b939ac..8139f2c54a 100644 --- a/packages/core-api/package.json +++ b/packages/core-api/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/core/package.json b/packages/core/package.json index 05986a6b1e..818634238b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index fc77298371..ab595ea9a7 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/test-utils-core/package.json b/packages/test-utils-core/package.json index f629877a65..1ebe1bf072 100644 --- a/packages/test-utils-core/package.json +++ b/packages/test-utils-core/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 1f7afc9bbb..136b6d6854 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "test": "backstage-cli test", "prepack": "backstage-cli prepack", diff --git a/packages/theme/package.json b/packages/theme/package.json index 1704299e3c..a5d552a660 100644 --- a/packages/theme/package.json +++ b/packages/theme/package.json @@ -20,7 +20,7 @@ "main:src": "src/index.ts", "types": "src/index.ts", "scripts": { - "build": "backstage-cli plugin:build", + "build": "backstage-cli build --outputs types,esm", "lint": "backstage-cli lint", "prepack": "backstage-cli prepack", "postpack": "backstage-cli postpack",