From 6ecb5bfcb2a3525e5558eac345b5a777e982bc24 Mon Sep 17 00:00:00 2001 From: joshwooding <12938082+joshwooding@users.noreply.github.com> Date: Mon, 1 Nov 2021 21:56:08 +0000 Subject: [PATCH] Tidy code and implement configEntries fix --- .../code-generator-plugin.js | 16 ++------------- .../codegen-modern-iframe-script.js | 9 +++++---- .../codegen-virtual-imports-script.js | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 packages/storybook-builder-vite/codegen-virtual-imports-script.js diff --git a/packages/storybook-builder-vite/code-generator-plugin.js b/packages/storybook-builder-vite/code-generator-plugin.js index 33a6b5ed..d3b3d0ba 100644 --- a/packages/storybook-builder-vite/code-generator-plugin.js +++ b/packages/storybook-builder-vite/code-generator-plugin.js @@ -4,8 +4,7 @@ const { transformIframeHtml } = require('./transform-iframe-html'); const { generateIframeScriptCode } = require('./codegen-iframe-script'); const { generateModernIframeScriptCode } = require('./codegen-modern-iframe-script'); const { generateImportFnScriptCode } = require('./codegen-importfn-script'); -const glob = require('glob-promise'); -const { normalizePath } = require('vite'); +const { generateVirtualImportsScriptCode } = require('./codegen-virtual-imports-script'); module.exports.codeGeneratorPlugin = function codeGeneratorPlugin(options) { const virtualFileId = '/virtual:/@storybook/builder-vite/vite-app.js'; @@ -84,18 +83,7 @@ module.exports.codeGeneratorPlugin = function codeGeneratorPlugin(options) { } if (id === virtualStoryImportsFile) { - const files = ( - await Promise.all( - ( - await options.presets.apply('stories', [], options) - ).map((g) => - glob(path.isAbsolute(g) ? g : path.join(options.configDir, g)) - ) - ) - ).reduce((carry, stories) => carry.concat(stories), []); - const objectEntries = files - .map(file => ` './${normalizePath(path.relative(process.cwd(),file))}': async () => import('${file}')`); - return `export default {\n${objectEntries.join(',\n')}\n};`; + return generateVirtualImportsScriptCode(options); } }, async transformIndexHtml(html, ctx) { diff --git a/packages/storybook-builder-vite/codegen-modern-iframe-script.js b/packages/storybook-builder-vite/codegen-modern-iframe-script.js index ab3c75bb..77b9a468 100644 --- a/packages/storybook-builder-vite/codegen-modern-iframe-script.js +++ b/packages/storybook-builder-vite/codegen-modern-iframe-script.js @@ -5,10 +5,11 @@ module.exports.generateModernIframeScriptCode = async function generateModernIframeScriptCode(options, { storiesFilename }) { const { presets, configDir } = options; - const configEntries = [loadPreviewOrConfigFile({ configDir })] - .concat(await presets.apply('config', [], options)) - .filter(Boolean) - .map(configEntry => `/@fs/${normalizePath(configEntry)}`); + const previewOrConfigFile = loadPreviewOrConfigFile({ configDir }); + const presetEntries = await presets.apply('config', [], options); + const configEntries = [previewOrConfigFile, ...presetEntries].filter( + Boolean + ).map(configEntry => `/@fs/${normalizePath(configEntry)}`); const code = ` import fetch from 'unfetch'; diff --git a/packages/storybook-builder-vite/codegen-virtual-imports-script.js b/packages/storybook-builder-vite/codegen-virtual-imports-script.js new file mode 100644 index 00000000..1d401ffb --- /dev/null +++ b/packages/storybook-builder-vite/codegen-virtual-imports-script.js @@ -0,0 +1,20 @@ +const glob = require('glob-promise'); +const path = require('path'); +const { normalizePath } = require('vite'); + +module.exports.generateVirtualImportsScriptCode = + async function generateVirtualImportsScriptCode(options ) { + const files = ( + await Promise.all( + ( + await options.presets.apply('stories', [], options) + ).map((g) => + glob(path.isAbsolute(g) ? g : path.join(options.configDir, g)) + ) + ) + ).reduce((carry, stories) => carry.concat(stories), []); + const objectEntries = files + .map(file => ` './${normalizePath(path.relative(process.cwd(),file))}': async () => import('${file}')`); + + return `export default {\n${objectEntries.join(',\n')}\n};`; + };