packages/cli: move rollup build to lib/packager
This commit is contained in:
@@ -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();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
}),
|
||||
],
|
||||
};
|
||||
};
|
||||
@@ -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';
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user