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: experimental Node.js imports compatibility for client-side #25028

Merged
merged 24 commits into from Jan 18, 2024
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
15 changes: 15 additions & 0 deletions docs/2.guide/3.going-further/1.experimental-features.md
Expand Up @@ -359,3 +359,18 @@ const { data } = await useAsyncData(async () => {
const { data } = await useAsyncData(route.params.slug, async () => {
return await $fetch(`/api/my-page/${route.params.slug}`)
})
```

## clientNodeCompat

With this feature, Nuxt will automatically polyfill Node.js imports in the client build using [`unenv`](https://github.com/unjs/unenv).

::alert{type=info}
To make globals like `Buffer` work in the browser, you need to manually inject them.

```ts
import { Buffer } from 'node:buffer'

globalThis.Buffer = globalThis.Buffer || Buffer
```
::
1 change: 1 addition & 0 deletions packages/schema/build.config.ts
Expand Up @@ -50,6 +50,7 @@ export default defineBuildConfig({
'pug',
'sass-loader',
'c12',
'unenv',
// Implicit
'@vue/compiler-core',
'@vue/shared',
Expand Down
1 change: 1 addition & 0 deletions packages/schema/package.json
Expand Up @@ -50,6 +50,7 @@
"ofetch": "1.3.3",
"unbuild": "latest",
"unctx": "2.3.1",
"unenv": "^1.9.0",
"vite": "5.0.11",
"vue": "3.4.14",
"vue-bundle-renderer": "2.0.0",
Expand Down
17 changes: 16 additions & 1 deletion packages/schema/src/config/experimental.ts
Expand Up @@ -307,6 +307,21 @@ export default defineUntypedSchema({
},
/** @type {Pick<typeof import('ofetch')['FetchOptions'], 'timeout' | 'retry' | 'retryDelay' | 'retryStatusCodes'>} */
useFetch: {}
}
},

/**
* Automatically polyfill Node.js imports in the client build using `unenv`.
* @see https://github.com/unjs/unenv
*
* **Note:** To make globals like `Buffer` work in the browser, you need to manually inject them.
*
* ```ts
* import { Buffer } from 'node:buffer'
*
* globalThis.Buffer = globalThis.Buffer || Buffer
* ```
* @type {boolean}
*/
clientNodeCompat: false,
}
})
1 change: 1 addition & 0 deletions packages/vite/package.json
Expand Up @@ -60,6 +60,7 @@
"std-env": "^3.7.0",
"strip-literal": "^2.0.0",
"ufo": "^1.3.2",
"unenv": "^1.8.0",
"unplugin": "^1.6.0",
"vite": "5.0.11",
"vite-node": "^1.1.1",
Expand Down
15 changes: 13 additions & 2 deletions packages/vite/src/client.ts
Expand Up @@ -8,6 +8,7 @@ import { logger } from '@nuxt/kit'
import { getPort } from 'get-port-please'
import { joinURL, withoutLeadingSlash } from 'ufo'
import { defu } from 'defu'
import { env, nodeless } from 'unenv'
import { appendCorsHeaders, appendCorsPreflightHeaders, defineEventHandler } from 'h3'
import type { ViteConfig } from '@nuxt/schema'
import { chunkErrorPlugin } from './plugins/chunk-error'
Expand All @@ -19,6 +20,13 @@ import { viteNodePlugin } from './vite-node'
import { createViteLogger } from './utils/logger'

export async function buildClient (ctx: ViteBuildContext) {
const nodeCompat = ctx.nuxt.options.experimental.clientNodeCompat ? {
alias: env(nodeless).alias,
define: {
global: 'globalThis',
}
} : { alias: {}, define: {} }

const clientConfig: ViteConfig = vite.mergeConfig(ctx.config, vite.mergeConfig({
configFile: false,
base: ctx.nuxt.options.dev
Expand Down Expand Up @@ -48,15 +56,18 @@ export async function buildClient (ctx: ViteBuildContext) {
'import.meta.browser': true,
'import.meta.nitro': false,
'import.meta.prerender': false,
'module.hot': false
'module.hot': false,
...nodeCompat.define
},
optimizeDeps: {
entries: [ctx.entry]
},
resolve: {
alias: {
...nodeCompat.alias,
...ctx.config.resolve?.alias,
'#build/plugins': resolve(ctx.nuxt.options.buildDir, 'plugins/client'),
'#internal/nitro': resolve(ctx.nuxt.options.buildDir, 'nitro.client.mjs')
'#internal/nitro': resolve(ctx.nuxt.options.buildDir, 'nitro.client.mjs'),
},
dedupe: [
'vue'
Expand Down
1 change: 1 addition & 0 deletions packages/webpack/package.json
Expand Up @@ -57,6 +57,7 @@
"std-env": "^3.7.0",
"time-fix-plugin": "^2.0.7",
"ufo": "^1.3.2",
"unenv": "^1.8.0",
"unplugin": "^1.6.0",
"url-loader": "^4.1.1",
"vue-bundle-renderer": "^2.0.0",
Expand Down
22 changes: 21 additions & 1 deletion packages/webpack/src/configs/client.ts
Expand Up @@ -5,6 +5,7 @@ import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import { logger } from '@nuxt/kit'
import { joinURL } from 'ufo'
import ForkTSCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import { env, nodeless } from 'unenv'

import type { WebpackConfigContext } from '../utils/config'
import { applyPresets } from '../utils/config'
Expand All @@ -20,7 +21,8 @@ export function client (ctx: WebpackConfigContext) {
clientOptimization,
clientDevtool,
clientPerformance,
clientHMR
clientHMR,
clientNodeCompat,
])
}

Expand Down Expand Up @@ -48,6 +50,24 @@ function clientPerformance (ctx: WebpackConfigContext) {
}
}

function clientNodeCompat(ctx: WebpackConfigContext) {
if (!ctx.nuxt.options.experimental.clientNodeCompat) {
return
}
ctx.config.plugins!.push(new webpack.DefinePlugin({ global: 'globalThis', }))

ctx.config.resolve = ctx.config.resolve || {}
ctx.config.resolve.fallback = {
...env(nodeless).alias,
...ctx.config.resolve.fallback,
}

// https://github.com/webpack/webpack/issues/13290#issuecomment-1188760779
ctx.config.plugins!.unshift(new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
resource.request = resource.request.replace(/^node:/, '');
}))
}

function clientHMR (ctx: WebpackConfigContext) {
if (!ctx.isDev) {
return
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/basic.test.ts
Expand Up @@ -2285,6 +2285,16 @@ describe('keepalive', () => {
})
})

describe('Node.js compatibility for client-side', () => {
it('should work', async () => {
const { page } = await renderPage('/node-compat')
const html = await page.innerHTML('body')
expect(html).toContain('Nuxt is Awesome!')
expect(html).toContain('CWD: [available]')
await page.close()
})
})

function normaliseIslandResult (result: NuxtIslandResponse) {
return {
...result,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/nuxt.config.ts
Expand Up @@ -200,6 +200,7 @@ export default defineNuxtConfig({
respectNoSSRHeader: true,
clientFallback: true,
restoreState: true,
clientNodeCompat: true,
componentIslands: {
selectiveClient: true
},
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/basic/pages/node-compat.vue
@@ -0,0 +1,20 @@
<script setup lang="ts">
import { Buffer } from 'node:buffer'
import process from 'node:process'

const base64 = atob(Buffer.from('Nuxt is Awesome!', 'utf8').toString('base64'))
const cwd = typeof process.cwd == 'function' && "[available]"
</script>

<template>
<div>
<ClientOnly>
<div>
{{ base64 }}
</div>
<div>
CWD: {{ cwd }}
</div>
</ClientOnly>
</div>
</template>