From 11695b87f3d32f01d65000dc5a645df78daed9d4 Mon Sep 17 00:00:00 2001 From: xeho91 Date: Tue, 9 Jul 2024 18:05:00 +0800 Subject: [PATCH] resolve post-plugin accessing pre-transform code --- src/compiler/plugins.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/compiler/plugins.ts b/src/compiler/plugins.ts index 0164367..2cd1ac8 100644 --- a/src/compiler/plugins.ts +++ b/src/compiler/plugins.ts @@ -40,9 +40,14 @@ export async function preTransformPlugin(): Promise { magicCode.overwrite(0, code.length - 1, print(transformedSvelteAST)); + const stringifiedMagicCode = magicCode.toString(); + return { - code: magicCode.toString(), + code: stringifiedMagicCode, map: magicCode.generateMap({ hires: true, source: id }), + meta: { + _storybook_csf_pre_transform: stringifiedMagicCode, + }, }; }, }; @@ -66,14 +71,9 @@ export async function postTransformPlugin(): Promise { const compiledAST = this.parse(compiledCode); let magicCompiledCode = new MagicString(compiledCode); - - // FIXME: This needs to change. - // we need to access the code - results from `pre-transform` plugin - 'storybook:addon-svelte-csf-plugin-post'. - // But how? - - // @ts-expect-error FIXME: `this.originalCode` exists at runtime in the development mode only. - // Need to research if its documented somewhere - let rawCode = this.originalCode ?? fs.readFileSync(id).toString(); + let rawCode = + (this.getModuleInfo(id)?.meta._storybook_csf_pre_transform as string | undefined) ?? + fs.readFileSync(id).toString(); if (svelteConfig?.preprocess) { const processed = await preprocess(rawCode, svelteConfig.preprocess, {