From d4f1b185a348cdb0b091284bb32e73610053f936 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 30 Oct 2022 11:33:37 +0000 Subject: [PATCH] feat: support vue plugin in pipeline --- patches/@storybook__builder-vite@0.2.3.patch | 13 ---------- src/index.ts | 26 +++++++++++++++++++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/patches/@storybook__builder-vite@0.2.3.patch b/patches/@storybook__builder-vite@0.2.3.patch index dc0785bc..9699f7f5 100644 --- a/patches/@storybook__builder-vite@0.2.3.patch +++ b/patches/@storybook__builder-vite@0.2.3.patch @@ -37,16 +37,3 @@ index 6a87782c2c4d88d0e7fa39c904b2eff78d591d1e..10b91b2b24d9c71300f6288eed697509 const storySourcePattern = /var __STORY__ = "(.*)"/; const storySourceReplacement = '--STORY_SOURCE_REPLACEMENT--'; const mockClassLoader = (id) => ({ emitWarning: (message) => console.warn(message), resourcePath: id }); -diff --git a/dist/vite-config.js b/dist/vite-config.js -index 37735b8e79b6c62f0ce66e53e48273e7ca175f49..bbf539d14a831019a6395a9b2ba3d830176d722e 100644 ---- a/dist/vite-config.js -+++ b/dist/vite-config.js -@@ -132,7 +132,7 @@ async function pluginConfig(options, _type) { - if (framework === 'vue3') { - try { - const vuePlugin = require('@vitejs/plugin-vue'); -- plugins.push(vuePlugin()); -+ plugins.push(vuePlugin({exclude: [/\.stories\.vue$/]})); - } - catch (err) { - if (err.code === 'MODULE_NOT_FOUND') { \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f97fca64..8070ec4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,13 +2,37 @@ import { createUnplugin } from 'unplugin' import { transform as transformVue } from './core/transform' import type { Options } from './types' +// We a custom type so that the vue plugin is ignoring the "main" import +const STORIES_INTERNAL_SUFFIX = '?vue&type=stories' +const STORIES_PUBLIC_SUFFIX = '.stories.vue' + export default createUnplugin(_options => ({ name: 'unplugin-starter', enforce: 'pre', + async resolveId(source, importer, options) { + if (source.endsWith(STORIES_PUBLIC_SUFFIX)) { + // Determine what the actual entry would have been. We need "skipSelf" + // to avoid an infinite loop. + const resolution = await this.resolve(source, importer, { skipSelf: true, ...options }) + + // If it cannot be resolved or is external, just return it so that + // Rollup can display an error + if (!resolution || resolution.external) + return resolution + + console.log('resolveIdStory', resolution) + return { + ...resolution, + id: resolution.id + STORIES_INTERNAL_SUFFIX, + } + } + }, transformInclude(id) { - return id.endsWith('.stories.vue') + console.log('transformInclude', id) + return id.endsWith(STORIES_INTERNAL_SUFFIX) }, transform(code) { + console.log('transform', code) return transformVue(code) }, }))