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

feat: add ssrRequire in ssrloadmodule for #5424 #5425

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Expand Up @@ -4,6 +4,15 @@ import fetch from 'node-fetch'

const url = `http://localhost:${port}`

test('require syntax can be used natural in server side', async () => {
await page.goto(url + '/require')
expect(await page.textContent('h1')).toMatch('foo')

// raw http request
const requireHtml = await (await fetch(url + '/require')).text()
expect(requireHtml).toMatch('foo')
})

test('vuex can be import succeed by named import', async () => {
await page.goto(url + '/store')
expect(await page.textContent('h1')).toMatch('bar')
Expand All @@ -13,7 +22,6 @@ test('vuex can be import succeed by named import', async () => {
expect(storeHtml).toMatch('bar')
})


test('/about', async () => {
await page.goto(url + '/about')
expect(await page.textContent('h1')).toMatch('About')
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/ssr-vue/config.js
@@ -0,0 +1,3 @@
module.exports = {
foo: 'foo'
}
28 changes: 28 additions & 0 deletions packages/playground/ssr-vue/src/pages/Require.vue
@@ -0,0 +1,28 @@
<template>
<h1>{{foo}}</h1>
</template>

<script>
export default {
async setup() {
if (typeof window === 'undefined') {
// code execute in server side
const { resolve } = require('path')
const cwd = process.env.JEST_WORKER_ID !== undefined ? resolve(process.cwd(), './packages/playground/ssr-vue') : process.cwd()
const { foo } = require(resolve(cwd, './config'))
return {
foo: foo
}
}
return {
foo: 'foo'
}
}
}
</script>

<style scoped>
h1 {
color: red;
}
</style>
7 changes: 7 additions & 0 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -139,6 +139,11 @@ async function instantiateModule(
}
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
}
const ssrRequire = (dep: string) => {
return require(require.resolve(dep, {
paths: [path.dirname(mod.file!)]
}))
}

const ssrDynamicImport = (dep: string) => {
// #3087 dynamic import vars is ignored at rewrite import path,
Expand Down Expand Up @@ -168,6 +173,7 @@ async function instantiateModule(
const AsyncFunction = async function () {}.constructor as typeof Function
const initModule = new AsyncFunction(
`global`,
`require`,
ssrModuleExportsKey,
ssrImportMetaKey,
ssrImportKey,
Expand All @@ -177,6 +183,7 @@ async function instantiateModule(
)
await initModule(
context.global,
ssrRequire,
ssrModule,
ssrImportMeta,
ssrImport,
Expand Down