diff --git a/packages/config/package.json b/packages/config/package.json index eafaeb87e5..6126b20a18 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -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", diff --git a/packages/config/src/load.js b/packages/config/src/load.js index ac094b4b5a..b780b024d9 100644 --- a/packages/config/src/load.js +++ b/packages/config/src/load.js @@ -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 = '.', diff --git a/packages/config/src/options.js b/packages/config/src/options.js index 7b2f42bb79..58a0981f80 100644 --- a/packages/config/src/options.js +++ b/packages/config/src/options.js @@ -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", ` diff --git a/packages/core/package.json b/packages/core/package.json index 7a245a631a..51f2976307 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/resolver.js b/packages/core/src/resolver.js index c34fc2cb25..50adf7dd7d 100644 --- a/packages/core/src/resolver.js +++ b/packages/core/src/resolver.js @@ -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, diff --git a/packages/core/test/resolver.test.js b/packages/core/test/resolver.test.js index 032e4b436c..c16b49954a 100644 --- a/packages/core/test/resolver.test.js +++ b/packages/core/test/resolver.test.js @@ -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') diff --git a/packages/utils/package.json b/packages/utils/package.json index 2c33bb7290..ea87bfc82f 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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", diff --git a/packages/utils/src/cjs.js b/packages/utils/src/cjs.js index cc6f32a103..de79bf15ac 100644 --- a/packages/utils/src/cjs.js +++ b/packages/utils/src/cjs.js @@ -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()