From da3b8c0afd4a5db28650487c154c81c1317209c8 Mon Sep 17 00:00:00 2001 From: Steven Sacks Date: Mon, 27 Nov 2023 13:31:14 +0900 Subject: [PATCH] update tsup config --- tsup.config.js | 18 ---------- tsup.config.ts | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 18 deletions(-) delete mode 100644 tsup.config.js create mode 100644 tsup.config.ts diff --git a/tsup.config.js b/tsup.config.js deleted file mode 100644 index 3691293..0000000 --- a/tsup.config.js +++ /dev/null @@ -1,18 +0,0 @@ -import {defineConfig} from 'tsup'; - -export default defineConfig((options) => ({ - entry: ['src/index.ts', 'src/preview.ts', 'src/manager.ts'], - splitting: false, - minify: !options.watch, - format: ['cjs', 'esm'], - dts: { - resolve: true, - }, - treeshake: true, - sourcemap: true, - clean: true, - platform: 'browser', - esbuildOptions(options) { - options.conditions = ['module']; - }, -})); diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 0000000..8e0dd0d --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,94 @@ +import { defineConfig, type Options } from "tsup"; +import { readFile } from "fs/promises"; +import { globalPackages as globalManagerPackages } from "@storybook/manager/globals"; +import { globalPackages as globalPreviewPackages } from "@storybook/preview/globals"; + +// The current browsers supported by Storybook v7 +const BROWSER_TARGET: Options['target'] = ["chrome100", "safari15", "firefox91"]; +const NODE_TARGET: Options['target'] = ["node16"]; + +type BundlerConfig = { + bundler?: { + exportEntries?: string[]; + nodeEntries?: string[]; + managerEntries?: string[]; + previewEntries?: string[]; + }; +}; + +export default defineConfig(async (options) => { + // reading the three types of entries from package.json, which has the following structure: + // { + // ... + // "bundler": { + // "exportEntries": ["./src/index.ts"], + // "managerEntries": ["./src/manager.ts"], + // "previewEntries": ["./src/preview.ts"] + // } + // } + const packageJson = await readFile('./package.json', 'utf8').then(JSON.parse) as BundlerConfig; + const { + bundler: { + exportEntries = [], + managerEntries = [], + previewEntries = [], + } = {}, + } = packageJson; + + const commonConfig: Options = { + splitting: false, + minify: !options.watch, + treeshake: true, + sourcemap: true, + clean: true, + }; + + const configs: Options[] = []; + + // export entries are entries meant to be manually imported by the user + // they are not meant to be loaded by the manager or preview + // they'll be usable in both node and browser environments, depending on which features and modules they depend on + if (exportEntries.length) { + configs.push({ + ...commonConfig, + entry: exportEntries, + dts: { + resolve: true, + }, + format: ["esm", 'cjs'], + target: [...BROWSER_TARGET, ...NODE_TARGET], + platform: "neutral", + external: [...globalManagerPackages, ...globalPreviewPackages], + }); + } + + // manager entries are entries meant to be loaded into the manager UI + // they'll have manager-specific packages externalized and they won't be usable in node + // they won't have types generated for them as they're usually loaded automatically by Storybook + if (managerEntries.length) { + configs.push({ + ...commonConfig, + entry: managerEntries, + format: ["esm"], + target: BROWSER_TARGET, + platform: "browser", + external: globalManagerPackages, + }); + } + + // preview entries are entries meant to be loaded into the preview iframe + // they'll have preview-specific packages externalized and they won't be usable in node + // they won't have types generated for them as they're usually loaded automatically by Storybook + if (previewEntries.length) { + configs.push({ + ...commonConfig, + entry: previewEntries, + format: ["esm"], + target: BROWSER_TARGET, + platform: "browser", + external: globalPreviewPackages, + }); + } + + return configs; +}); \ No newline at end of file