mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
feat: improve esm handling
disables esm for nuxt modules
This commit is contained in:
parent
14a6cd1237
commit
e4ee624011
@ -1,9 +1,8 @@
|
|||||||
|
|
||||||
const { resolve } = require('path')
|
const { resolve } = require('path')
|
||||||
const { existsSync } = require('fs')
|
const { existsSync } = require('fs')
|
||||||
|
const consola = require('consola')
|
||||||
const { Utils } = require('../..')
|
const esm = require('esm')(module, {})
|
||||||
const { requireModule } = require('../../lib/common/module')
|
|
||||||
|
|
||||||
const getRootDir = argv => resolve(argv._[0] || '.')
|
const getRootDir = argv => resolve(argv._[0] || '.')
|
||||||
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file'])
|
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file'])
|
||||||
@ -17,10 +16,9 @@ exports.loadNuxtConfig = argv => {
|
|||||||
let options = {}
|
let options = {}
|
||||||
|
|
||||||
if (existsSync(nuxtConfigFile)) {
|
if (existsSync(nuxtConfigFile)) {
|
||||||
delete require.cache[nuxtConfigFile]
|
options = esm(nuxtConfigFile).default
|
||||||
options = requireModule(nuxtConfigFile)
|
|
||||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||||
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
consola.fatal('Could not load config file: ' + argv['config-file'])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof options.rootDir !== 'string') {
|
if (typeof options.rootDir !== 'string') {
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
const esm = require('esm')
|
|
||||||
|
|
||||||
const _esm = esm(module, {})
|
|
||||||
|
|
||||||
exports.requireModule = function requireModule() {
|
|
||||||
const m = _esm.apply(this, arguments)
|
|
||||||
return (m && m.default) || m
|
|
||||||
}
|
|
@ -112,7 +112,7 @@ export default class ModuleContainer {
|
|||||||
|
|
||||||
// Resolve handler
|
// Resolve handler
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
handler = this.nuxt.requireModule(src)
|
handler = this.nuxt.requireModule(src, { esm: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate handler
|
// Validate handler
|
||||||
|
@ -6,11 +6,11 @@ import _ from 'lodash'
|
|||||||
import fs from 'fs-extra'
|
import fs from 'fs-extra'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
|
import esm from 'esm'
|
||||||
|
|
||||||
import Options from '../common/options'
|
import Options from '../common/options'
|
||||||
import { sequence } from '../common/utils'
|
import { sequence } from '../common/utils'
|
||||||
import packageJSON from '../../package.json'
|
import packageJSON from '../../package.json'
|
||||||
import moduleUtil from '../common/module'
|
|
||||||
|
|
||||||
import ModuleContainer from './module'
|
import ModuleContainer from './module'
|
||||||
import Renderer from './renderer'
|
import Renderer from './renderer'
|
||||||
@ -37,6 +37,9 @@ export default class Nuxt {
|
|||||||
this.renderer
|
this.renderer
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ESM Loader
|
||||||
|
this.esm = esm(module, {})
|
||||||
|
|
||||||
this._ready = this.ready().catch(err => {
|
this._ready = this.ready().catch(err => {
|
||||||
consola.fatal(err)
|
consola.fatal(err)
|
||||||
})
|
})
|
||||||
@ -195,8 +198,10 @@ export default class Nuxt {
|
|||||||
throw new Error(`Cannot resolve "${_path}" from "${__path}"`)
|
throw new Error(`Cannot resolve "${_path}" from "${__path}"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
requireModule(name) {
|
requireModule(_path, opts = {}) {
|
||||||
return moduleUtil.requireModule(this.resolvePath(name))
|
const _resovledPath = this.resolvePath(_path)
|
||||||
|
const m = opts.esm === false ? require(_resovledPath) : this.esm(_resovledPath)
|
||||||
|
return (m && m.default) || m
|
||||||
}
|
}
|
||||||
|
|
||||||
async close(callback) {
|
async close(callback) {
|
||||||
|
@ -395,7 +395,7 @@ export default class Renderer {
|
|||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (!jsdom) {
|
if (!jsdom) {
|
||||||
try {
|
try {
|
||||||
jsdom = this.nuxt.requireModule('jsdom')
|
jsdom = require('jsdom')
|
||||||
} catch (e) /* istanbul ignore next */ {
|
} catch (e) /* istanbul ignore next */ {
|
||||||
consola.error(`
|
consola.error(`
|
||||||
Fail when calling nuxt.renderAndGetWindow(url)
|
Fail when calling nuxt.renderAndGetWindow(url)
|
||||||
|
@ -4,7 +4,7 @@ import fs from 'fs'
|
|||||||
import _getPort from 'get-port'
|
import _getPort from 'get-port'
|
||||||
import { defaultsDeep } from 'lodash'
|
import { defaultsDeep } from 'lodash'
|
||||||
import _rp from 'request-promise-native'
|
import _rp from 'request-promise-native'
|
||||||
import { requireModule } from '../../lib/common/module'
|
import esm from 'esm'
|
||||||
import pkg from '../../package.json'
|
import pkg from '../../package.json'
|
||||||
import Dist from '../../lib/nuxt'
|
import Dist from '../../lib/nuxt'
|
||||||
|
|
||||||
@ -18,11 +18,13 @@ export const Options = Dist.Options
|
|||||||
export const Builder = Dist.Builder
|
export const Builder = Dist.Builder
|
||||||
export const Generator = Dist.Generator
|
export const Generator = Dist.Generator
|
||||||
|
|
||||||
|
const requireModule = esm(module, {})
|
||||||
|
|
||||||
export const loadFixture = function loadFixture(fixture, overrides) {
|
export const loadFixture = function loadFixture(fixture, overrides) {
|
||||||
const rootDir = path.resolve(__dirname, '../fixtures/' + fixture)
|
const rootDir = path.resolve(__dirname, '../fixtures/' + fixture)
|
||||||
const configFile = path.resolve(rootDir, 'nuxt.config.js')
|
const configFile = path.resolve(rootDir, 'nuxt.config.js')
|
||||||
|
|
||||||
const config = fs.existsSync(configFile) ? requireModule(configFile) : {}
|
const config = fs.existsSync(configFile) ? requireModule(configFile).default : {}
|
||||||
|
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
Loading…
Reference in New Issue
Block a user