mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-16 05:03:11 +08:00
# Conflicts: # addons/a11y/package.json # addons/actions/package.json # addons/backgrounds/package.json # addons/controls/package.json # addons/docs/package.json # addons/essentials/package.json # addons/interactions/package.json # addons/jest/package.json # addons/links/package.json # addons/measure/package.json # addons/outline/package.json # addons/storyshots/storyshots-core/package.json # addons/storysource/package.json # addons/toolbars/package.json # addons/viewport/package.json # docs/faq.md # examples/angular-cli/.storybook/main.js # examples/angular-cli/package.json # examples/cra-kitchen-sink/.storybook/main.js # examples/cra-kitchen-sink/package.json # examples/cra-react15/.storybook/main.js # examples/cra-react15/package.json # examples/cra-ts-essentials/.storybook/main.ts # examples/cra-ts-essentials/package.json # examples/cra-ts-kitchen-sink/.storybook/main.ts # examples/cra-ts-kitchen-sink/package.json # examples/ember-cli/.storybook/main.js # examples/ember-cli/package.json # examples/external-docs/package.json # examples/html-kitchen-sink/.storybook/main.js # examples/html-kitchen-sink/package.json # examples/official-storybook/main.ts # examples/official-storybook/package.json # examples/preact-kitchen-sink/.storybook/main.js # examples/preact-kitchen-sink/package.json # examples/react-ts-webpack4/package.json # examples/react-ts/package.json # examples/server-kitchen-sink/package.json # examples/standalone-preview/package.json # examples/svelte-kitchen-sink/.storybook/main.js # examples/svelte-kitchen-sink/package.json # examples/vue-3-cli/.storybook/main.js # examples/vue-3-cli/package.json # examples/vue-cli/.storybook/main.js # examples/vue-cli/package.json # examples/vue-kitchen-sink/.storybook/main.js # examples/vue-kitchen-sink/package.json # examples/web-components-kitchen-sink/.storybook/main.js # frameworks/angular/package.json # frameworks/ember/package.json # frameworks/preact-webpack5/package.json # frameworks/vue-webpack5/package.json # frameworks/vue3-webpack5/package.json # lib/addons/package.json # lib/api/package.json # lib/builder-webpack4/package.json # lib/builder-webpack5/package.json # lib/channel-postmessage/package.json # lib/channel-websocket/package.json # lib/channels/package.json # lib/cli/package.json # lib/cli/src/automigrate/fixes/angular12.test.ts # lib/cli/src/automigrate/index.ts # lib/cli/src/versions.ts # lib/client-api/package.json # lib/client-logger/package.json # lib/codemod/package.json # lib/components/package.json # lib/core-client/package.json # lib/core-common/package.json # lib/core-events/package.json # lib/core-server/package.json # lib/core-server/src/__snapshots__/vue-3-cli_preview-dev-posix # lib/core-server/src/__snapshots__/vue-3-cli_preview-prod-posix # lib/core-vite/package.json # lib/csf-tools/package.json # lib/docs-tools/package.json # lib/instrumenter/package.json # lib/manager-webpack4/package.json # lib/manager-webpack5/package.json # lib/node-logger/package.json # lib/postinstall/package.json # lib/preview-web/package.json # lib/router/package.json # lib/source-loader/package.json # lib/store/package.json # lib/telemetry/package.json # lib/theming/package.json # lib/ui/package.json # presets/server-webpack/package.json # renderers/html/package.json # renderers/react/package.json # renderers/svelte/package.json # renderers/web-components/package.json # scripts/build-package.js # scripts/bundle-package.ts # yarn.lock
202 lines
5.3 KiB
TypeScript
202 lines
5.3 KiB
TypeScript
import path, { resolve } from 'path';
|
|
import chalk from 'chalk';
|
|
import { rollup, OutputOptions, watch, RollupOptions } from 'rollup';
|
|
import readPkgUp from 'read-pkg-up';
|
|
import fs from 'fs-extra';
|
|
import rollupTypescript from '@rollup/plugin-typescript';
|
|
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
import commonjs from '@rollup/plugin-commonjs';
|
|
import json from '@rollup/plugin-json';
|
|
import { babel, getBabelOutputPlugin } from '@rollup/plugin-babel';
|
|
import { terser } from 'rollup-plugin-terser';
|
|
import { generateDtsBundle } from 'dts-bundle-generator';
|
|
import * as dtsLozalize from './dts-localize';
|
|
|
|
const { bold, gray, greenBright } = chalk;
|
|
|
|
interface Options {
|
|
input: string;
|
|
externals: string[];
|
|
cwd: string;
|
|
optimized?: boolean;
|
|
watch?: boolean;
|
|
}
|
|
|
|
async function dts({ input, externals, cwd, ...options }: Options) {
|
|
if (options.watch) {
|
|
try {
|
|
const [out] = await generateDtsBundle(
|
|
[
|
|
{
|
|
filePath: input,
|
|
output: { inlineDeclareGlobals: false, sortNodes: true, noBanner: true },
|
|
},
|
|
],
|
|
{ followSymlinks: false }
|
|
);
|
|
|
|
await fs.outputFile('dist/types/index.d.ts', out);
|
|
} catch (e) {
|
|
console.log(e.message);
|
|
}
|
|
} else {
|
|
const [out] = await generateDtsBundle(
|
|
[
|
|
{
|
|
filePath: input,
|
|
output: { inlineDeclareGlobals: false, sortNodes: true, noBanner: true },
|
|
},
|
|
],
|
|
{ followSymlinks: false }
|
|
);
|
|
|
|
const bundledDTSfile = path.join(cwd, 'dist/ts-tmp/index.d.ts');
|
|
const localizedDTSout = path.join(cwd, 'dist/types');
|
|
await fs.outputFile(bundledDTSfile, out);
|
|
|
|
await dtsLozalize.run([bundledDTSfile], localizedDTSout, { externals, cwd });
|
|
}
|
|
}
|
|
|
|
async function removeDist() {
|
|
await fs.remove('dist');
|
|
}
|
|
|
|
async function mapper() {
|
|
await fs.emptyDir(path.join(process.cwd(), 'dist', 'types'));
|
|
await fs.writeFile(
|
|
path.join(process.cwd(), 'dist', 'types', 'index.d.ts'),
|
|
`export * from '../../src/index';`
|
|
);
|
|
}
|
|
const makeExternalPredicate = (externals: string[]) => {
|
|
if (externals.length === 0) {
|
|
return () => false;
|
|
}
|
|
const pattern = new RegExp(`^(${externals.join('|')})($|/)`);
|
|
return (id: string) => pattern.test(id);
|
|
};
|
|
|
|
async function build(options: Options) {
|
|
const { input, externals, cwd, optimized } = options;
|
|
const setting: RollupOptions = {
|
|
input,
|
|
external: makeExternalPredicate(externals),
|
|
plugins: [
|
|
nodeResolve({
|
|
preferBuiltins: true,
|
|
}),
|
|
commonjs(),
|
|
babel({
|
|
babelHelpers: 'external',
|
|
skipPreflightCheck: true,
|
|
compact: false,
|
|
}),
|
|
json(),
|
|
rollupTypescript({ lib: ['es2015', 'dom', 'esnext'], target: 'es6' }),
|
|
],
|
|
};
|
|
|
|
const outputs: OutputOptions[] = [
|
|
{
|
|
dir: resolve(cwd, './dist/esm'),
|
|
format: 'es',
|
|
sourcemap: optimized,
|
|
preferConst: true,
|
|
plugins: [
|
|
getBabelOutputPlugin({
|
|
compact: false,
|
|
presets: [
|
|
[
|
|
'@babel/preset-env',
|
|
{
|
|
shippedProposals: true,
|
|
useBuiltIns: 'usage',
|
|
corejs: '3',
|
|
modules: false,
|
|
targets: { chrome: '100' },
|
|
},
|
|
],
|
|
],
|
|
}),
|
|
optimized ? terser({ output: { comments: false }, module: true }) : null,
|
|
].filter(Boolean),
|
|
},
|
|
{
|
|
dir: resolve(cwd, './dist/cjs'),
|
|
format: 'commonjs',
|
|
plugins: [
|
|
getBabelOutputPlugin({
|
|
compact: false,
|
|
presets: [
|
|
[
|
|
'@babel/preset-env',
|
|
{
|
|
shippedProposals: true,
|
|
useBuiltIns: 'usage',
|
|
corejs: '3',
|
|
modules: false,
|
|
targets: { node: '14' },
|
|
},
|
|
],
|
|
],
|
|
}),
|
|
optimized ? terser({ output: { comments: false }, module: true }) : null,
|
|
].filter(Boolean),
|
|
},
|
|
];
|
|
|
|
if (options.watch) {
|
|
const watcher = watch({ ...setting, output: outputs });
|
|
|
|
watcher.on('change', (event) => {
|
|
console.log(`${greenBright('changed')}: ${event.replace(path.resolve(cwd, '../..'), '.')}`);
|
|
});
|
|
} else {
|
|
const bundler = await rollup(setting);
|
|
|
|
await Promise.all(outputs.map((config) => bundler.write(config)));
|
|
|
|
await bundler.close();
|
|
}
|
|
}
|
|
|
|
const hasFlag = (flags: string[], name: string) => !!flags.find((s) => s.startsWith(`--${name}`));
|
|
|
|
export async function run({ cwd, flags }: { cwd: string; flags: string[] }) {
|
|
const { packageJson: pkg } = await readPkgUp({ cwd });
|
|
const message = gray(`Built: ${bold(`${pkg.name}@${pkg.version}`)}`);
|
|
console.time(message);
|
|
|
|
const reset = hasFlag(flags, 'reset');
|
|
const watch = hasFlag(flags, 'watch');
|
|
const optimized = hasFlag(flags, 'optimized');
|
|
|
|
if (reset) {
|
|
await removeDist();
|
|
}
|
|
|
|
const input = path.join(cwd, pkg.bundlerEntrypoint);
|
|
const externals = Object.keys({ ...pkg.dependencies, ...pkg.peerDependencies });
|
|
|
|
const options: Options = {
|
|
cwd,
|
|
externals,
|
|
input,
|
|
optimized,
|
|
watch,
|
|
};
|
|
|
|
if (!optimized) {
|
|
console.log(`skipping generating types for ${process.cwd()}`);
|
|
}
|
|
|
|
await Promise.all([
|
|
//
|
|
build(options),
|
|
...(options.optimized ? [dts(options)] : [mapper()]),
|
|
]);
|
|
|
|
console.timeEnd(message);
|
|
}
|