From 590276c67779ed9df8d7ec9aa97a2530c115f8dc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 17 May 2020 11:06:39 +0200 Subject: [PATCH] packages/cli: move rollup build to lib/packager --- packages/cli/src/commands/plugin/build.ts | 52 +------------- .../cli/src/commands/plugin/rollup.config.ts | 63 ----------------- packages/cli/src/lib/packager/config.ts | 65 ++++++++++++++++++ packages/cli/src/lib/packager/index.ts | 17 +++++ packages/cli/src/lib/packager/packager.ts | 67 +++++++++++++++++++ 5 files changed, 151 insertions(+), 113 deletions(-) delete mode 100644 packages/cli/src/commands/plugin/rollup.config.ts create mode 100644 packages/cli/src/lib/packager/config.ts create mode 100644 packages/cli/src/lib/packager/index.ts create mode 100644 packages/cli/src/lib/packager/packager.ts diff --git a/packages/cli/src/commands/plugin/build.ts b/packages/cli/src/commands/plugin/build.ts index 45379ba757..9af79028f4 100644 --- a/packages/cli/src/commands/plugin/build.ts +++ b/packages/cli/src/commands/plugin/build.ts @@ -14,59 +14,11 @@ * limitations under the License. */ -import { rollup, OutputOptions } from 'rollup'; -import chalk from 'chalk'; import { withCache, getDefaultCacheOptions } from '../../lib/buildCache'; -import { paths } from '../../lib/paths'; -import conf from './rollup.config'; - -function logError(error: any) { - console.log(''); - - if (error.code === 'PLUGIN_ERROR') { - // typescript2 plugin has a complete message with all codeframes - if (error.plugin === 'rpt2') { - console.log(error.message); - } else { - // Log which plugin is causing errors to make it easier to identity. - // If we see these in logs we likely want to provide some custom error - // output for those plugins too. - console.log(`(plugin ${error.plugin}) ${error}`); - } - } else { - // Generic rollup errors, log what's available - if (error.loc) { - const file = `${paths.resolveTarget((error.loc.file || error.id)!)}`; - const pos = `${error.loc.line}:${error.loc.column}`; - console.log(`${file} [${pos}]`); - } else if (error.id) { - console.log(paths.resolveTarget(error.id)); - } - - console.log(String(error)); - - if (error.url) { - console.log(chalk.cyan(error.url)); - } - - if (error.frame) { - console.log(chalk.dim(error.frame)); - } - } -} +import { buildPackage } from '../../lib/packager'; export default async () => { await withCache(getDefaultCacheOptions(), async () => { - try { - const bundle = await rollup({ - input: conf.input, - plugins: conf.plugins, - }); - await bundle.generate(conf.output as OutputOptions); - await bundle.write(conf.output as OutputOptions); - } catch (error) { - logError(error); - process.exit(1); - } + await buildPackage(); }); }; diff --git a/packages/cli/src/commands/plugin/rollup.config.ts b/packages/cli/src/commands/plugin/rollup.config.ts deleted file mode 100644 index 36b43bf5a7..0000000000 --- a/packages/cli/src/commands/plugin/rollup.config.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 peerDepsExternal from 'rollup-plugin-peer-deps-external'; -import typescript from 'rollup-plugin-typescript2'; -import commonjs from '@rollup/plugin-commonjs'; -import resolve from '@rollup/plugin-node-resolve'; -import postcss from 'rollup-plugin-postcss'; -import imageFiles from 'rollup-plugin-image-files'; -import json from '@rollup/plugin-json'; -import { RollupWatchOptions } from 'rollup'; -import { paths } from '../../lib/paths'; - -export default { - input: 'src/index.ts', - output: { - file: 'dist/index.esm.js', - format: 'module', - }, - plugins: [ - peerDepsExternal({ - includeDependencies: true, - }), - resolve({ - mainFields: ['browser', 'module', 'main'], - }), - commonjs({ - include: ['node_modules/**', '../../node_modules/**'], - exclude: ['**/*.stories.*', '**/*.test.*'], - }), - postcss(), - imageFiles(), - json(), - typescript({ - include: `${paths.resolveTarget('src')}/**/*.{js,jsx,ts,tsx}`, - tsconfigOverride: { - // The dev folder is for the local plugin serve, ignore it in the build - // If we don't do this we get a folder structure similar to dist/{src,dev}/... - exclude: ['dev'], - compilerOptions: { - // Use absolute path to src dir as root for declarations, relying on the default - // seems to produce declaration maps that are relative to dist/ instead of src/ - // Using a relative path like ../src doesn't work either becaus it will be used as is in subdirs. - sourceRoot: paths.resolveTarget('src'), - }, - }, - clean: true, - }), - ], -} as RollupWatchOptions; diff --git a/packages/cli/src/lib/packager/config.ts b/packages/cli/src/lib/packager/config.ts new file mode 100644 index 0000000000..602b662f5c --- /dev/null +++ b/packages/cli/src/lib/packager/config.ts @@ -0,0 +1,65 @@ +/* + * 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 peerDepsExternal from 'rollup-plugin-peer-deps-external'; +import typescript from 'rollup-plugin-typescript2'; +import commonjs from '@rollup/plugin-commonjs'; +import resolve from '@rollup/plugin-node-resolve'; +import postcss from 'rollup-plugin-postcss'; +import imageFiles from 'rollup-plugin-image-files'; +import json from '@rollup/plugin-json'; +import { RollupWatchOptions } from 'rollup'; +import { paths } from '../paths'; + +export const makeConfig = (): RollupWatchOptions => { + return { + input: 'src/index.ts', + output: { + file: 'dist/index.esm.js', + format: 'module', + }, + plugins: [ + peerDepsExternal({ + includeDependencies: true, + }), + resolve({ + mainFields: ['browser', 'module', 'main'], + }), + commonjs({ + include: ['node_modules/**', '../../node_modules/**'], + exclude: ['**/*.stories.*', '**/*.test.*'], + }), + postcss(), + imageFiles(), + json(), + typescript({ + include: `${paths.resolveTarget('src')}/**/*.{js,jsx,ts,tsx}`, + tsconfigOverride: { + // The dev folder is for the local plugin serve, ignore it in the build + // If we don't do this we get a folder structure similar to dist/{src,dev}/... + exclude: ['dev'], + compilerOptions: { + // Use absolute path to src dir as root for declarations, relying on the default + // seems to produce declaration maps that are relative to dist/ instead of src/ + // Using a relative path like ../src doesn't work either becaus it will be used as is in subdirs. + sourceRoot: paths.resolveTarget('src'), + }, + }, + clean: true, + }), + ], + }; +}; diff --git a/packages/cli/src/lib/packager/index.ts b/packages/cli/src/lib/packager/index.ts new file mode 100644 index 0000000000..e639599af9 --- /dev/null +++ b/packages/cli/src/lib/packager/index.ts @@ -0,0 +1,17 @@ +/* + * 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 { buildPackage } from './packager'; diff --git a/packages/cli/src/lib/packager/packager.ts b/packages/cli/src/lib/packager/packager.ts new file mode 100644 index 0000000000..cf12b517eb --- /dev/null +++ b/packages/cli/src/lib/packager/packager.ts @@ -0,0 +1,67 @@ +/* + * 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 { rollup, OutputOptions } from 'rollup'; +import chalk from 'chalk'; +import { paths } from '../paths'; +import { makeConfig } from './config'; + +function logError(error: any) { + console.log(''); + + if (error.code === 'PLUGIN_ERROR') { + // typescript2 plugin has a complete message with all codeframes + if (error.plugin === 'rpt2') { + console.log(error.message); + } else { + // Log which plugin is causing errors to make it easier to identity. + // If we see these in logs we likely want to provide some custom error + // output for those plugins too. + console.log(`(plugin ${error.plugin}) ${error}`); + } + } else { + // Generic rollup errors, log what's available + if (error.loc) { + const file = `${paths.resolveTarget((error.loc.file || error.id)!)}`; + const pos = `${error.loc.line}:${error.loc.column}`; + console.log(`${file} [${pos}]`); + } else if (error.id) { + console.log(paths.resolveTarget(error.id)); + } + + console.log(String(error)); + + if (error.url) { + console.log(chalk.cyan(error.url)); + } + + if (error.frame) { + console.log(chalk.dim(error.frame)); + } + } +} + +export const buildPackage = async () => { + try { + const config = makeConfig(); + const bundle = await rollup(config); + await bundle.generate(config.output as OutputOptions); + await bundle.write(config.output as OutputOptions); + } catch (error) { + logError(error); + process.exit(1); + } +};