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

fix: resolve addons using nodeResolve() #5809

Merged
merged 1 commit into from Nov 23, 2021
Merged
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
6 changes: 6 additions & 0 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Expand Up @@ -163,3 +163,9 @@ test('deep import built-in module', async () => {
await page.goto(url)
expect(await page.textContent('.file-message')).toMatch('fs/promises')
})

test('msg should encrypted', async () => {
// raw http request
const homeHtml = await (await fetch(url + '/')).text()
expect(homeHtml).not.toMatch('Secret Message!')
})
1 change: 1 addition & 0 deletions packages/playground/ssr-vue/package.json
Expand Up @@ -14,6 +14,7 @@
"debug": "node --inspect-brk server"
},
"dependencies": {
"bcrypt": "^5.0.1",
"example-external-component": "file:example-external-component",
"vue": "^3.2.21",
"vue-router": "^4.0.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/playground/ssr-vue/src/pages/Home.vue
Expand Up @@ -9,6 +9,7 @@
<p class="inter">this will be styled with a font-face</p>
<p class="import-meta-url">{{ state.url }}</p>
<p class="protocol">{{ state.protocol }}</p>
<div>encrypted message: <p class="encrypted-msg">{{ encryptedMsg }}</p></div>

<ImportType />
</template>
Expand All @@ -27,10 +28,15 @@ const url = import.meta.env.SSR
? import.meta.url
: document.querySelector('.import-meta-url').textContent
const protocol = new URL(url).protocol
const encryptedMsg = import.meta.env.SSR
? await (await import('bcrypt')).hash('Secret Message!', 10)
: document.querySelector('.encrypted-msg').textContent

const state = reactive({
count: 0,
protocol,
url
url,
encryptedMsg
})
</script>

Expand Down
3 changes: 3 additions & 0 deletions packages/playground/ssr-vue/vite.config.js
Expand Up @@ -5,6 +5,9 @@ const vueJsx = require('@vitejs/plugin-vue-jsx')
* @type {import('vite').UserConfig}
*/
module.exports = {
optimizeDeps: {
exclude: ['bcrypt']
},
plugins: [
vuePlugin(),
vueJsx(),
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -228,7 +228,9 @@ async function nodeImport(
// When an ESM module imports an ESM dependency, this hook is *not* used.
const unhookNodeResolve = hookNodeResolve(
(nodeResolve) => (id, parent, isMain, options) => {
if (id[0] === '.' || isBuiltin(id)) {
// Fix #5709, use require to resolve files with the '.node' file extension.
// See detail, https://nodejs.org/api/addons.html#addons_loading_addons_using_require
if (id[0] === '.' || isBuiltin(id) || id.endsWith('.node')) {
return nodeResolve(id, parent, isMain, options)
}
if (parent) {
Expand Down