Skip to content

Commit

Permalink
Tidy code and implement configEntries fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding committed Nov 1, 2021
1 parent afffe82 commit 6ecb5bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
16 changes: 2 additions & 14 deletions packages/storybook-builder-vite/code-generator-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 20 additions & 0 deletions packages/storybook-builder-vite/codegen-virtual-imports-script.js
Original file line number Diff line number Diff line change
@@ -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};`;
};

0 comments on commit 6ecb5bf

Please sign in to comment.