Skip to content

Commit 2de885b

Browse files
authored
fix(nuxt): reduce usage of cjs utilities (#27642)
1 parent 44cada9 commit 2de885b

File tree

13 files changed

+37
-20
lines changed

13 files changed

+37
-20
lines changed

packages/kit/src/plugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ export function normalizePlugin (plugin: NuxtPlugin | string): NuxtPlugin {
4343
* Note: By default plugin is prepended to the plugins array. You can use second argument to append (push) instead.
4444
* @example
4545
* ```js
46+
* import { createResolver } from '@nuxt/kit'
47+
* const resolver = createResolver(import.meta.url)
48+
*
4649
* addPlugin({
47-
* src: path.resolve(__dirname, 'templates/foo.js'),
50+
* src: resolver.resolve('templates/foo.js'),
4851
* filename: 'foo.server.js' // [optional] only include in server bundle
4952
* })
5053
* ```

packages/nuxt/src/core/nitro.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
355355
nitroConfig.rollupConfig!.plugins!.push(
356356
ImportProtectionPlugin.rollup({
357357
rootDir: nuxt.options.rootDir,
358+
modulesDir: nuxt.options.modulesDir,
358359
patterns: nuxtImportProtections(nuxt, { isNitro: true }),
359360
exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/],
360361
}),

packages/nuxt/src/core/nuxt.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ async function initNuxt (nuxt: Nuxt) {
167167
// Exclude top-level resolutions by plugins
168168
exclude: [join(nuxt.options.srcDir, 'index.html')],
169169
patterns: nuxtImportProtections(nuxt),
170+
modulesDir: nuxt.options.modulesDir,
170171
}
171172
addVitePlugin(() => ImportProtectionPlugin.vite(config))
172173
addWebpackPlugin(() => ImportProtectionPlugin.webpack(config))

packages/nuxt/src/core/plugins/import-protection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { createRequire } from 'node:module'
21
import { createUnplugin } from 'unplugin'
32
import { logger } from '@nuxt/kit'
3+
import { resolvePath } from 'mlly'
44
import { isAbsolute, join, relative, resolve } from 'pathe'
55
import escapeRE from 'escape-string-regexp'
66
import type { NuxtOptions } from 'nuxt/schema'
77

8-
const _require = createRequire(import.meta.url)
9-
108
interface ImportProtectionOptions {
119
rootDir: string
10+
modulesDir: string[]
1211
patterns: [importPattern: string | RegExp, warning?: string][]
1312
exclude?: Array<RegExp | string>
1413
}
@@ -58,6 +57,7 @@ export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: {
5857
export const ImportProtectionPlugin = createUnplugin(function (options: ImportProtectionOptions) {
5958
const cache: Record<string, Map<string | RegExp, boolean>> = {}
6059
const importersToExclude = options?.exclude || []
60+
const proxy = resolvePath('unenv/runtime/mock/proxy', { url: options.modulesDir })
6161
return {
6262
name: 'nuxt:import-protection',
6363
enforce: 'pre',
@@ -85,7 +85,7 @@ export const ImportProtectionPlugin = createUnplugin(function (options: ImportPr
8585
matched = true
8686
}
8787
if (matched) {
88-
return _require.resolve('unenv/runtime/mock/proxy')
88+
return proxy
8989
}
9090
return null
9191
},

packages/nuxt/test/auto-imports.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync } from 'node:fs'
2+
import { fileURLToPath } from 'node:url'
23
import { describe, expect, it } from 'vitest'
3-
import { join } from 'pathe'
4-
import { createCommonJS, findExports } from 'mlly'
4+
import { findExports } from 'mlly'
55
import * as VueFunctions from 'vue'
66
import type { Import } from 'unimport'
77
import { createUnimport } from 'unimport'
@@ -59,8 +59,8 @@ const excludedNuxtHelpers = ['useHydration', 'useHead', 'useSeoMeta', 'useServer
5959

6060
describe('imports:nuxt', () => {
6161
try {
62-
const { __dirname } = createCommonJS(import.meta.url)
63-
const entrypointContents = readFileSync(join(__dirname, '../src/app/composables/index.ts'), 'utf8')
62+
const entrypointPath = fileURLToPath(new URL('../src/app/composables/index.ts', import.meta.url))
63+
const entrypointContents = readFileSync(entrypointPath, 'utf8')
6464

6565
const names = findExports(entrypointContents).flatMap(i => i.names || i.name)
6666
for (let name of names) {

packages/nuxt/test/import-protection.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fileURLToPath } from 'node:url'
12
import { normalize } from 'pathe'
23
import { describe, expect, it } from 'vitest'
34
import { ImportProtectionPlugin, nuxtImportProtections } from '../src/core/plugins/import-protection'
@@ -40,6 +41,7 @@ describe('import protection', () => {
4041
const transformWithImportProtection = (id: string, importer: string) => {
4142
const plugin = ImportProtectionPlugin.rollup({
4243
rootDir: '/root',
44+
modulesDir: [fileURLToPath(new URL('..', import.meta.url))],
4345
patterns: nuxtImportProtections({
4446
options: {
4547
modules: ['some-nuxt-module'],

packages/nuxt/test/scan-components.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { fileURLToPath } from 'node:url'
12
import { resolve } from 'pathe'
23
import { expect, it, vi } from 'vitest'
34
import type { ComponentsDir } from 'nuxt/schema'
45

56
import { scanComponents } from '../src/components/scan'
67

7-
const fixtureDir = resolve(__dirname, 'fixture')
8+
const fixtureDir = fileURLToPath(new URL('fixture', import.meta.url))
89
const rFixture = (...p: string[]) => resolve(fixtureDir, ...p)
910

1011
vi.mock('@nuxt/kit', () => ({

packages/nuxt/test/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { resolve } from 'pathe'
1+
import { fileURLToPath } from 'node:url'
22

3-
export const fixtureDir = resolve(__dirname, 'fixture')
3+
export const fixtureDir = fileURLToPath(new URL('fixture', import.meta.url))
44

55
export function normalizeLineEndings (str: string, normalized = '\n') {
66
return str.replace(/\r?\n/g, normalized)

packages/ui-templates/lib/dev.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { join, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
23
import { promises as fsp } from 'node:fs'
34
import type { Plugin } from 'vite'
45
import { template } from 'lodash-es'
56
import genericMessages from '../templates/messages.json'
67

7-
const r = (...path: string[]) => resolve(join(__dirname, '..', ...path))
8+
const templatesRoot = fileURLToPath(new URL('..', import.meta.url))
9+
10+
const r = (...path: string[]) => resolve(join(templatesRoot, ...path))
811

912
export const DevRenderingPlugin = () => {
1013
return <Plugin>{

packages/ui-templates/lib/prerender.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { join, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
23
import { promises as fsp } from 'node:fs'
34
import { globby } from 'globby'
45

5-
const r = (...path: string[]) => resolve(join(__dirname, '..', ...path))
6+
const templatesRoot = fileURLToPath(new URL('..', import.meta.url))
7+
8+
const r = (...path: string[]) => resolve(join(templatesRoot, ...path))
69

710
async function main () {
811
const templates = await globby(r('dist/templates/*.js'))

0 commit comments

Comments
 (0)