Skip to content

Commit

Permalink
update tsup config
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensacks committed Nov 27, 2023
1 parent 3692f76 commit da3b8c0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
18 changes: 0 additions & 18 deletions tsup.config.js

This file was deleted.

94 changes: 94 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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;
});

0 comments on commit da3b8c0

Please sign in to comment.