fix(vite): dev bundler for cjs support (#4371)

This commit is contained in:
Anthony Fu 2022-04-15 15:57:36 +08:00 committed by GitHub
parent 2917af595b
commit 51ae426d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -79,7 +79,7 @@ async function transformRequest (opts: TransformOptions, id: string) {
? withoutVersionQuery ? withoutVersionQuery
: pathToFileURL(withoutVersionQuery).href : pathToFileURL(withoutVersionQuery).href
return { return {
code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => code: `(global, module, _, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) =>
${genDynamicImport(path, { wrapper: false })} ${genDynamicImport(path, { wrapper: false })}
.then(r => { .then(r => {
if (r.default && r.default.__esModule) if (r.default && r.default.__esModule)
@ -104,7 +104,7 @@ ${genDynamicImport(path, { wrapper: false })}
}) as SSRTransformResult || { code: '', map: {}, deps: [], dynamicDeps: [] } }) as SSRTransformResult || { code: '', map: {}, deps: [], dynamicDeps: [] }
// Wrap into a vite module // Wrap into a vite module
const code = `async function (global, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) { const code = `async function (global, module, exports, __vite_ssr_exports__, __vite_ssr_import_meta__, __vite_ssr_import__, __vite_ssr_dynamic_import__, __vite_ssr_exportAll__) {
${res.code || '/* empty */'}; ${res.code || '/* empty */'};
}` }`
return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] } return { code, deps: res.deps || [], dynamicDeps: res.dynamicDeps || [] }
@ -209,8 +209,19 @@ async function __instantiateModule__(url, urlStack) {
} }
} }
const cjsModule = {
get exports () {
return stubModule.default
},
set exports (v) {
stubModule.default = v
},
}
await mod( await mod(
__ssrContext__.global, __ssrContext__.global,
cjsModule,
stubModule.default,
stubModule, stubModule,
importMeta, importMeta,
ssrImport, ssrImport,