Skip to content

Commit a6d93fb

Browse files
author
Franck Freiburger
committed
fix(core): missing bindingMetadata in scriptSetup cache
1 parent a6b6e02 commit a6d93fb

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/createVue3SFCModule.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
131131

132132
// TBD: handle <script setup src="...
133133

134-
const [depsList, transformedScriptSource] =
134+
const [bindingMetadata, depsList, transformedScriptSource] =
135135
await withCache(
136136
compiledCache,
137137
[
@@ -167,18 +167,17 @@ export async function createSFCModule(source : string, filename : AbstractPath,
167167
templateOptions: compileTemplateOptions,
168168
});
169169

170-
// see https://github.com/vuejs/vue-loader/blob/12aaf2ea77add8654c50c8751bad135f1881e53f/src/templateLoader.ts#L54
171-
if ( compileTemplateOptions?.compilerOptions !== undefined )
172-
compileTemplateOptions.compilerOptions.bindingMetadata = scriptBlock.bindings;
173-
174-
175170
// note:
176171
// scriptBlock.content is the script code after vue transformations
177172
// scriptBlock.scriptAst is the script AST before vue transformations
178-
return await transformJSCode(scriptBlock.content, true, strFilename, [ ...contextBabelParserPlugins, ...additionalBabelParserPlugins ], { ...contextBabelPlugins, ...additionalBabelPlugins }, log, devMode);
173+
return [scriptBlock.bindings, ...await transformJSCode(scriptBlock.content, true, strFilename, [ ...contextBabelParserPlugins, ...additionalBabelParserPlugins ], { ...contextBabelPlugins, ...additionalBabelPlugins }, log, devMode)];
179174

180175
});
181176

177+
// see https://github.com/vuejs/vue-loader/blob/12aaf2ea77add8654c50c8751bad135f1881e53f/src/templateLoader.ts#L54
178+
if ( compileTemplateOptions?.compilerOptions !== undefined )
179+
compileTemplateOptions.compilerOptions.bindingMetadata = bindingMetadata;
180+
182181
await loadDeps(filename, depsList, options);
183182
Object.assign(component, interopRequireDefault(createCJSModule(filename, transformedScriptSource, options).exports).default);
184183
}
@@ -196,6 +195,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
196195
compileTemplateOptions.compilerOptions.delimiters,
197196
compileTemplateOptions.compilerOptions.whitespace,
198197
compileTemplateOptions.compilerOptions.scopeId,
198+
compileTemplateOptions.compilerOptions.bindingMetadata ? Object.entries(compileTemplateOptions.compilerOptions.bindingMetadata) : '',
199199
],
200200
async ({ preventCache }) => {
201201

tests/basic.test.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,77 @@ const { defaultFilesFactory, createPage } = require('./testsTools.js');
11981198
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('hello');
11991199
});
12001200

1201-
});
12021201

1202+
if ( vueTarget === 3 ) { //
1203+
1204+
test('fix bindingMetadata missing in cache data', async () => {
1205+
1206+
const { page, output } = await createPage({
1207+
files: {
1208+
...files,
1209+
'/index.html': `
1210+
<!DOCTYPE html>
1211+
<html><body>
1212+
<script src="vue"></script>
1213+
<script src="vue${ vueTarget }-sfc-loader.js"></script>
1214+
<div id="app"></div>
1215+
<script>
1216+
1217+
;(async () => {
1218+
1219+
const { loadModule } = window['vue3-sfc-loader'];
1220+
1221+
/* <!-- */
1222+
const config = {
1223+
files: {
1224+
'/comp.vue': '<template>comp_1</template>',
1225+
'/main.vue': '<script setup> import comp from "./comp.vue" </script><template>main_comp <comp/></template>',
1226+
}
1227+
};
1228+
/* --> */
1229+
1230+
const options = {
1231+
getFile: async url => config.files[url],
1232+
addStyle() {},
1233+
}
1234+
1235+
const cache = {}
1236+
const compiledCache = {
1237+
get: key => cache[key],
1238+
set: (key, value) => cache[key] = value,
1239+
}
1240+
1241+
let moduleCache = {
1242+
vue: Vue,
1243+
}
1244+
1245+
// first run
1246+
await loadModule('/main.vue', { moduleCache, compiledCache, ...options });
12031247
1248+
// second run, reset moduleCache
1249+
moduleCache = {
1250+
vue: Vue,
1251+
}
1252+
1253+
// get the cache item of the main.vue script (find "main_comp" string)
1254+
const mainKey = Object.entries(cache).find(([k,v]) => v.includes('main_comp')).at(0);
1255+
1256+
// delete only main.vue script cache item
1257+
delete cache[mainKey];
1258+
1259+
const app = Vue.createApp(await loadModule('/main.vue', { moduleCache, compiledCache, ...options }));
1260+
1261+
app.mount('#app');
1262+
1263+
})().catch(ex => console.error(ex));
1264+
1265+
</script>
1266+
</body></html>
1267+
`}
1268+
});
1269+
1270+
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('main_comp comp_1');
1271+
});
1272+
}
1273+
});
12041274
})

0 commit comments

Comments
 (0)