diff --git a/code/lib/builder-vite/package.json b/code/lib/builder-vite/package.json index 965b38180643..2d620ddd9f67 100644 --- a/code/lib/builder-vite/package.json +++ b/code/lib/builder-vite/package.json @@ -3,19 +3,42 @@ "version": "7.0.0-alpha.52", "description": "A plugin to run and build Storybooks with Vite", "homepage": "https://github.com/storybookjs/storybook/tree/main/code/lib/builder-vite/#readme", + "bugs": { + "url": "https://github.com/storybookjs/storybook/issues" + }, "repository": { "type": "git", "url": "https://github.com/storybookjs/storybook.git", "directory": "code/lib/builder-vite" }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/storybook" + }, "license": "MIT", "author": "Eirik Sletteberg", - "main": "dist/cjs/index.js", - "module": "dist/esm/index.js", - "types": "dist/types/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs", + "types": "./dist/index.d.ts" + }, + "./input/iframe.html": "./input/iframe.html", + "./input/react-dom-client-placeholder.js": "./input/react-dom-client-placeholder.js", + "./package.json": "./package.json" + }, + "main": "dist/index.js", + "module": "dist/index.mjs", + "types": "dist/index.d.ts", + "files": [ + "dist/**/*", + "input/**/*", + "*.js", + "*.d.ts" + ], "scripts": { "check": "../../../scripts/node_modules/.bin/tsc --noEmit", - "prep": "node ../../../scripts/prepare.js" + "prep": "../../../scripts/prepare/bundle.ts" }, "dependencies": { "@joshwooding/vite-plugin-react-docgen-typescript": "0.0.5", @@ -42,8 +65,26 @@ "typescript": "^4.9.3", "vite": "^3.1.3" }, + "peerDependencies": { + "@preact/preset-vite": "*", + "vite-plugin-glimmerx": "*" + }, + "peerDependenciesMeta": { + "@preact/preset-vite": { + "optional": true + }, + "vite-plugin-glimmerx": { + "optional": true + } + }, "publishConfig": { "access": "public" }, + "bundler": { + "entries": [ + "./src/index.ts" + ], + "platform": "node" + }, "gitHead": "d2494e3f51ce0f55bcb1ef693a6477c669fbe666" } diff --git a/code/lib/builder-vite/src/index.ts b/code/lib/builder-vite/src/index.ts index fccf5694b8f8..8f4aa2570b7f 100644 --- a/code/lib/builder-vite/src/index.ts +++ b/code/lib/builder-vite/src/index.ts @@ -1,7 +1,6 @@ // noinspection JSUnusedGlobalSymbols import * as fs from 'fs'; -import * as path from 'path'; import type { Builder, StorybookConfig as StorybookBaseConfig, Options } from '@storybook/types'; import type { RequestHandler, Request, Response } from 'express'; import type { InlineConfig, UserConfig, ViteDevServer } from 'vite'; @@ -46,7 +45,7 @@ function iframeMiddleware(options: ExtendedOptions, server: ViteDevServer): Requ } const indexHtml = fs.readFileSync( - path.resolve(__dirname, '../..', 'input', 'iframe.html'), + require.resolve('@storybook/builder-vite/input/iframe.html'), 'utf-8' ); const generated = await transformIframeHtml(indexHtml, options); diff --git a/code/lib/builder-vite/src/plugins/code-generator-plugin.ts b/code/lib/builder-vite/src/plugins/code-generator-plugin.ts index 6efa3d6ae4a2..e4460515a45c 100644 --- a/code/lib/builder-vite/src/plugins/code-generator-plugin.ts +++ b/code/lib/builder-vite/src/plugins/code-generator-plugin.ts @@ -1,7 +1,6 @@ /* eslint-disable no-param-reassign */ import * as fs from 'fs'; -import * as path from 'path'; import { mergeConfig } from 'vite'; import type { Plugin } from 'vite'; import { transformIframeHtml } from '../transform-iframe-html'; @@ -21,7 +20,7 @@ import { } from '../virtual-file-names'; export function codeGeneratorPlugin(options: ExtendedOptions): Plugin { - const iframePath = path.resolve(__dirname, '../../..', 'input', 'iframe.html'); + const iframePath = require.resolve('@storybook/builder-vite/input/iframe.html'); let iframeId: string; // noinspection JSUnusedGlobalSymbols @@ -44,7 +43,7 @@ export function codeGeneratorPlugin(options: ExtendedOptions): Plugin { // Adding new story files is not covered by the change event above. So we need to detect this and trigger // HMR to update the importFn. - // eslint-disable-next-line @typescript-eslint/no-shadow + server.watcher.on('add', (path) => { // TODO maybe use the stories declaration in main if (/\.stories\.([tj])sx?$/.test(path) || /\.(story|stories).mdx$/.test(path)) { @@ -75,11 +74,8 @@ export function codeGeneratorPlugin(options: ExtendedOptions): Plugin { if (isNodeError(e) && e.code === 'MODULE_NOT_FOUND') { config.resolve = mergeConfig(config.resolve ?? {}, { alias: { - 'react-dom/client': path.resolve( - __dirname, - '../../..', - 'input', - 'react-dom-client-placeholder.js' + 'react-dom/client': require.resolve( + '@storybook/builder-vite/input/react-dom-client-placeholder.js' ), }, }); @@ -133,7 +129,7 @@ export function codeGeneratorPlugin(options: ExtendedOptions): Plugin { if (id === iframeId) { return fs.readFileSync( - path.resolve(__dirname, '../../..', 'input', 'iframe.html'), + require.resolve('@storybook/builder-vite/input/iframe.html'), 'utf-8' ); } diff --git a/code/yarn.lock b/code/yarn.lock index 4cb3c05d1407..99a203e14381 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5862,6 +5862,14 @@ __metadata: slash: ^3.0.0 typescript: ^4.9.3 vite: ^3.1.3 + peerDependencies: + "@preact/preset-vite": "*" + vite-plugin-glimmerx: "*" + peerDependenciesMeta: + "@preact/preset-vite": + optional: true + vite-plugin-glimmerx: + optional: true languageName: unknown linkType: soft