packages/cli: make build output formats configurable and add separate build command

This commit is contained in:
Patrik Oldsberg
2020-06-04 15:24:51 +02:00
parent 187d6c9103
commit a9cb9a9467
7 changed files with 89 additions and 14 deletions
+23
View File
@@ -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']),
});
};
+3 -1
View File
@@ -17,5 +17,7 @@
import { buildPackage } from '../../lib/packager';
export default async () => {
await buildPackage();
await buildPackage({
outputs: new Set(['esm', 'types']),
});
};
+5
View File
@@ -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')
+33 -11
View File
@@ -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<RollupOptions[]> => {
export const makeConfigs = async (
options: BuildOptions,
): Promise<RollupOptions[]> => {
const typesInput = paths.resolveTargetRoot(
'dist',
relativePath(paths.targetRoot, paths.targetDir),
@@ -43,13 +46,27 @@ export const makeConfigs = async (): Promise<RollupOptions[]> => {
);
}
return [
{
input: 'src/index.ts',
output: {
const configs = new Array<RollupOptions>();
if (options.outputs.has('cjs') || options.outputs.has('esm')) {
const output = new Array<OutputOptions>();
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<RollupOptions[]> => {
target: 'es2019',
}),
],
},
{
});
}
if (options.outputs.has('types')) {
configs.push({
input: typesInput,
output: {
file: 'dist/index.d.ts',
format: 'es',
},
plugins: [dts()],
},
];
});
}
return configs;
};
+1
View File
@@ -15,3 +15,4 @@
*/
export { buildPackage } from './packager';
export type { BuildOptions, OutputFormat } from './types';
+3 -2
View File
@@ -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));
};
+21
View File
@@ -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<OutputFormat>;
};