Skip to content

Commit

Permalink
fix: resolve addons using nodeResolve() (#5809)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Nov 23, 2021
1 parent 4c17252 commit d288772
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 22 deletions.
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

0 comments on commit d288772

Please sign in to comment.