Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Vite setup so we use the correct working directory #28

Merged
merged 3 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions packages/storybook-builder-vite/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@ const { build: viteBuild } = require('vite');
module.exports.build = async function build(options) {
const config = {
configFile: false,
// We create a kind of "custom" source root inside this project (yes, inside the node_modules folder)
// so that "iframe.html" resolves to a correct path. (Otherwise, Vite will fail.)
root: path.resolve(__dirname, 'input'),
root: path.resolve(options.configDir, '..'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what is a better assumption, this, or cwd. 🤔. Probably this.

build: {
outDir: options.outputDir,
rollupOptions: {
input: {
'iframe.html': path.resolve(
__dirname,
'input',
'iframe.html'
),
},
},
emptyOutDir: false, // do not clean before running Vite build - Storybook has already added assets in there!
sourcemap: true,
},
resolve: {
Expand Down
22 changes: 21 additions & 1 deletion packages/storybook-builder-vite/code-generator-plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs');
const path = require('path');
const { transformIframeHtml } = require('./transform-iframe-html');
const { generateIframeScriptCode } = require('./codegen-iframe-script');

Expand All @@ -17,18 +19,36 @@ module.exports.codeGeneratorPlugin = function codeGeneratorPlugin(options) {
}
});
},
config(config, { command }) {
// If we are building the static distribution, add iframe.html as an entry.
// In development mode, it's not an entry - instead, we use an express middleware
// to serve iframe.html. The reason is that Vite's dev server (at the time of writing)
// does not support virtual files as entry points.
if (command === 'build') {
config.build.rollupOptions = {
input: {
'iframe.html': 'iframe.html'
}
};
}
},
resolveId(source) {
if (source === virtualFileId) {
return virtualFileId;
} else if (source === 'iframe.html') {
return 'iframe.html';
}
},
async load(id) {
if (id === virtualFileId) {
return generateIframeScriptCode(options);
}
if (id === 'iframe.html') {
return fs.readFileSync(path.resolve(__dirname, 'input', 'iframe.html'), 'utf-8');
}
},
async transformIndexHtml(html, ctx) {
if (ctx.path !== '/iframe.html') {
if (!ctx.path.endsWith('/iframe.html')) {
return;
}
return transformIframeHtml(html, options);
Expand Down
6 changes: 5 additions & 1 deletion packages/storybook-builder-vite/vite-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ module.exports.createViteServer = async function createViteServer(
devServer
) {
const { port, presets } = options;
const root = path.resolve(options.configDir, '..');

const defaultConfig = {
configFile: false,
root: path.resolve(__dirname, 'input'),
root,
server: {
middlewareMode: true,
hmr: {
port,
server: devServer,
},
fsServe: {
strict: true
}
},
resolve: {
alias: {
Expand Down