mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(dev): resolve module required by create-require in jiti (#8556)
* fix(dev): resolve module required by create-require in jiti * fix test * add useJITI to create require * refactor default options.createRequire
This commit is contained in:
parent
d4380bc0f7
commit
6b4d451b66
@ -12,11 +12,9 @@
|
||||
"dependencies": {
|
||||
"@nuxt/utils": "2.14.12",
|
||||
"consola": "^2.15.0",
|
||||
"create-require": "^1.1.1",
|
||||
"defu": "^3.2.2",
|
||||
"destr": "^1.0.1",
|
||||
"dotenv": "^8.2.0",
|
||||
"jiti": "^0.1.18",
|
||||
"lodash": "^4.17.20",
|
||||
"rc9": "^1.2.0",
|
||||
"std-env": "^2.2.1",
|
||||
|
@ -3,15 +3,12 @@ import fs from 'fs'
|
||||
import defu from 'defu'
|
||||
import consola from 'consola'
|
||||
import dotenv from 'dotenv'
|
||||
import { clearRequireCache, scanRequireTree } from '@nuxt/utils'
|
||||
import jiti from 'jiti'
|
||||
import _createRequire from 'create-require'
|
||||
import { clearRequireCache, createRequire, scanRequireTree } from '@nuxt/utils'
|
||||
import destr from 'destr'
|
||||
import * as rc from 'rc9'
|
||||
import { defaultNuxtConfigFile } from './config'
|
||||
|
||||
const isJest = typeof jest !== 'undefined'
|
||||
const _require = isJest ? _createRequire(__filename) : jiti(__filename)
|
||||
const _require = createRequire(__filename)
|
||||
|
||||
export async function loadNuxtConfig ({
|
||||
rootDir = '.',
|
||||
|
@ -4,7 +4,7 @@ import { defaultsDeep, pick, uniq } from 'lodash'
|
||||
import defu from 'defu'
|
||||
import consola from 'consola'
|
||||
import destr from 'destr'
|
||||
import { TARGETS, MODES, guardDir, isNonEmptyString, isPureObject, isUrl, getMainModule, getPKG } from '@nuxt/utils'
|
||||
import { TARGETS, MODES, createRequire, guardDir, isNonEmptyString, isPureObject, isUrl, getMainModule, getPKG } from '@nuxt/utils'
|
||||
import { joinURL, normalizeURL, withTrailingSlash } from 'ufo'
|
||||
import { defaultNuxtConfigFile, getDefaultNuxtConfig } from './config'
|
||||
|
||||
@ -479,12 +479,9 @@ export function getNuxtConfig (_options) {
|
||||
|
||||
options.createRequire = process.env.NUXT_CREATE_REQUIRE || options.createRequire || defaultCreateRequire
|
||||
|
||||
if (options.createRequire === 'native') {
|
||||
const createRequire = require('create-require')
|
||||
options.createRequire = p => createRequire(typeof p === 'string' ? p : p.filename)
|
||||
} else if (options.createRequire === 'jiti') {
|
||||
const jiti = require('jiti')
|
||||
options.createRequire = p => jiti(typeof p === 'string' ? p : p.filename)
|
||||
if (options.createRequire === 'native' || options.createRequire === 'jiti') {
|
||||
const useJiti = options.createRequire === 'jiti'
|
||||
options.createRequire = p => createRequire(typeof p === 'string' ? p : p.filename, useJiti)
|
||||
} else if (typeof options.createRequire !== 'function') {
|
||||
throw new TypeError(
|
||||
`Unsupported createRequire value ${options.createRequire}! Possible values: "native", "jiti", <Function>`
|
||||
|
@ -12,7 +12,6 @@
|
||||
"@nuxt/server": "2.14.12",
|
||||
"@nuxt/utils": "2.14.12",
|
||||
"consola": "^2.15.0",
|
||||
"create-require": "^1.1.1",
|
||||
"fs-extra": "^9.0.1",
|
||||
"hable": "^3.0.0",
|
||||
"hash-sum": "^2.0.0",
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { resolve, join } from 'path'
|
||||
import fs from 'fs-extra'
|
||||
import consola from 'consola'
|
||||
import createRequire from 'create-require'
|
||||
|
||||
import {
|
||||
createRequire,
|
||||
startsWithRootAlias,
|
||||
startsWithSrcAlias,
|
||||
isExternalDependency,
|
||||
|
@ -6,7 +6,11 @@ import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/utils'
|
||||
import Resolver from '../src/resolver'
|
||||
|
||||
jest.mock('fs-extra')
|
||||
jest.mock('@nuxt/utils')
|
||||
jest.mock('@nuxt/utils', () => ({
|
||||
...jest.requireActual('@nuxt/utils'),
|
||||
startsWithRootAlias: jest.fn(),
|
||||
startsWithSrcAlias: jest.fn()
|
||||
}))
|
||||
|
||||
jest.spyOn(path, 'join')
|
||||
jest.spyOn(path, 'resolve')
|
||||
|
@ -12,6 +12,7 @@
|
||||
"create-require": "^1.1.1",
|
||||
"fs-extra": "^9.0.1",
|
||||
"hash-sum": "^2.0.0",
|
||||
"jiti": "^0.1.18",
|
||||
"lodash": "^4.17.20",
|
||||
"proper-lockfile": "^4.1.1",
|
||||
"semver": "^7.3.4",
|
||||
|
@ -1,5 +1,17 @@
|
||||
import { join } from 'path'
|
||||
import createRequire from 'create-require'
|
||||
import _createRequire from 'create-require'
|
||||
import jiti from 'jiti'
|
||||
|
||||
export const createRequire = (
|
||||
filename,
|
||||
useJiti = global.__NUXT_DEV__ && typeof jest === 'undefined'
|
||||
) => {
|
||||
if (useJiti) {
|
||||
return jiti(filename)
|
||||
}
|
||||
|
||||
return _createRequire(filename)
|
||||
}
|
||||
|
||||
const _require = createRequire()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user