mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +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": {
|
"dependencies": {
|
||||||
"@nuxt/utils": "2.14.12",
|
"@nuxt/utils": "2.14.12",
|
||||||
"consola": "^2.15.0",
|
"consola": "^2.15.0",
|
||||||
"create-require": "^1.1.1",
|
|
||||||
"defu": "^3.2.2",
|
"defu": "^3.2.2",
|
||||||
"destr": "^1.0.1",
|
"destr": "^1.0.1",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"jiti": "^0.1.18",
|
|
||||||
"lodash": "^4.17.20",
|
"lodash": "^4.17.20",
|
||||||
"rc9": "^1.2.0",
|
"rc9": "^1.2.0",
|
||||||
"std-env": "^2.2.1",
|
"std-env": "^2.2.1",
|
||||||
|
@ -3,15 +3,12 @@ import fs from 'fs'
|
|||||||
import defu from 'defu'
|
import defu from 'defu'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import { clearRequireCache, scanRequireTree } from '@nuxt/utils'
|
import { clearRequireCache, createRequire, scanRequireTree } from '@nuxt/utils'
|
||||||
import jiti from 'jiti'
|
|
||||||
import _createRequire from 'create-require'
|
|
||||||
import destr from 'destr'
|
import destr from 'destr'
|
||||||
import * as rc from 'rc9'
|
import * as rc from 'rc9'
|
||||||
import { defaultNuxtConfigFile } from './config'
|
import { defaultNuxtConfigFile } from './config'
|
||||||
|
|
||||||
const isJest = typeof jest !== 'undefined'
|
const _require = createRequire(__filename)
|
||||||
const _require = isJest ? _createRequire(__filename) : jiti(__filename)
|
|
||||||
|
|
||||||
export async function loadNuxtConfig ({
|
export async function loadNuxtConfig ({
|
||||||
rootDir = '.',
|
rootDir = '.',
|
||||||
|
@ -4,7 +4,7 @@ import { defaultsDeep, pick, uniq } from 'lodash'
|
|||||||
import defu from 'defu'
|
import defu from 'defu'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import destr from 'destr'
|
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 { joinURL, normalizeURL, withTrailingSlash } from 'ufo'
|
||||||
import { defaultNuxtConfigFile, getDefaultNuxtConfig } from './config'
|
import { defaultNuxtConfigFile, getDefaultNuxtConfig } from './config'
|
||||||
|
|
||||||
@ -479,12 +479,9 @@ export function getNuxtConfig (_options) {
|
|||||||
|
|
||||||
options.createRequire = process.env.NUXT_CREATE_REQUIRE || options.createRequire || defaultCreateRequire
|
options.createRequire = process.env.NUXT_CREATE_REQUIRE || options.createRequire || defaultCreateRequire
|
||||||
|
|
||||||
if (options.createRequire === 'native') {
|
if (options.createRequire === 'native' || options.createRequire === 'jiti') {
|
||||||
const createRequire = require('create-require')
|
const useJiti = options.createRequire === 'jiti'
|
||||||
options.createRequire = p => createRequire(typeof p === 'string' ? p : p.filename)
|
options.createRequire = p => createRequire(typeof p === 'string' ? p : p.filename, useJiti)
|
||||||
} else if (options.createRequire === 'jiti') {
|
|
||||||
const jiti = require('jiti')
|
|
||||||
options.createRequire = p => jiti(typeof p === 'string' ? p : p.filename)
|
|
||||||
} else if (typeof options.createRequire !== 'function') {
|
} else if (typeof options.createRequire !== 'function') {
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
`Unsupported createRequire value ${options.createRequire}! Possible values: "native", "jiti", <Function>`
|
`Unsupported createRequire value ${options.createRequire}! Possible values: "native", "jiti", <Function>`
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
"@nuxt/server": "2.14.12",
|
"@nuxt/server": "2.14.12",
|
||||||
"@nuxt/utils": "2.14.12",
|
"@nuxt/utils": "2.14.12",
|
||||||
"consola": "^2.15.0",
|
"consola": "^2.15.0",
|
||||||
"create-require": "^1.1.1",
|
|
||||||
"fs-extra": "^9.0.1",
|
"fs-extra": "^9.0.1",
|
||||||
"hable": "^3.0.0",
|
"hable": "^3.0.0",
|
||||||
"hash-sum": "^2.0.0",
|
"hash-sum": "^2.0.0",
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { resolve, join } from 'path'
|
import { resolve, join } from 'path'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import createRequire from 'create-require'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
createRequire,
|
||||||
startsWithRootAlias,
|
startsWithRootAlias,
|
||||||
startsWithSrcAlias,
|
startsWithSrcAlias,
|
||||||
isExternalDependency,
|
isExternalDependency,
|
||||||
|
@ -6,7 +6,11 @@ import { startsWithRootAlias, startsWithSrcAlias } from '@nuxt/utils'
|
|||||||
import Resolver from '../src/resolver'
|
import Resolver from '../src/resolver'
|
||||||
|
|
||||||
jest.mock('fs-extra')
|
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, 'join')
|
||||||
jest.spyOn(path, 'resolve')
|
jest.spyOn(path, 'resolve')
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
"create-require": "^1.1.1",
|
"create-require": "^1.1.1",
|
||||||
"fs-extra": "^9.0.1",
|
"fs-extra": "^9.0.1",
|
||||||
"hash-sum": "^2.0.0",
|
"hash-sum": "^2.0.0",
|
||||||
|
"jiti": "^0.1.18",
|
||||||
"lodash": "^4.17.20",
|
"lodash": "^4.17.20",
|
||||||
"proper-lockfile": "^4.1.1",
|
"proper-lockfile": "^4.1.1",
|
||||||
"semver": "^7.3.4",
|
"semver": "^7.3.4",
|
||||||
|
@ -1,5 +1,17 @@
|
|||||||
import { join } from 'path'
|
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()
|
const _require = createRequire()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user