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: support import.meta.* for webpack #1298

Merged
merged 4 commits into from
Aug 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/bridge/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNuxtModule, installModule, checkNuxtCompatibility, logger, writeTypes, addTemplate } from '@nuxt/kit'
import { defineNuxtModule, installModule, checkNuxtCompatibility, logger, writeTypes, addTemplate, addWebpackPlugin } from '@nuxt/kit'
import type { NuxtModule, NuxtCompatibility } from '@nuxt/schema'
import type { NodeMiddleware } from 'h3'
import { fromNodeMiddleware } from 'h3'
Expand All @@ -13,6 +13,7 @@ import { setupMeta } from './meta'
import { setupTranspile } from './transpile'
import { generateWebpackBuildManifest } from './webpack/manifest'
import pageMetaModule from './page-meta/module'
import { ImportMetaPlugin } from './webpack/import-meta'

export default defineNuxtModule({
meta: {
Expand Down Expand Up @@ -173,5 +174,7 @@ export default defineNuxtModule({
getContents: () => 'export default false'
})
}

addWebpackPlugin(ImportMetaPlugin.webpack)
}
})
46 changes: 46 additions & 0 deletions packages/bridge/src/webpack/import-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { pathToFileURL } from 'node:url'
import { createUnplugin } from 'unplugin'
import { parseURL } from 'ufo'
import { normalize } from 'pathe'
import MagicString from 'magic-string'

const NODE_MODULES_RE = /[\\/]node_modules[\\/]/

// support for `import.meta` in webpack 4
export const ImportMetaPlugin = createUnplugin(() => {
return {
name: 'nuxt:import-meta-transform',
enforce: 'post',
transformInclude (id) {
const { pathname } = parseURL(
decodeURIComponent(pathToFileURL(id).href)
)
const extensions = ['.js', '.vue', '.ts', '.jsx', '.tsx', '.mjs']
if (extensions.some(ext => pathname.endsWith(ext))) {
return true
}
},
transform (code, id) {
id = normalize(id)
const isNodeModule = NODE_MODULES_RE.test(id)

if (isNodeModule) {
return
}

const s = new MagicString(code)
s.replace(/import\.meta\.client/g, 'process.client')
.replace(/import\.meta\.server/g, 'process.server')
.replace(/import\.meta\.browser/g, 'process.browser')
.replace(/import\.meta\.nitro/g, 'process.nitro')
.replace(/import\.meta\.prerender/g, 'process.prerender')

if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ hires: true })
}
}
}
}
})
4 changes: 2 additions & 2 deletions playground/pages/with-layout.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup>
let message
if (process.client) {
if (import.meta.client) {
message = 'Client'
} else if (process.server) {
} else if (import.meta.server) {
message = 'Server'
}

Expand Down