feat: improve esm handling

disables esm for nuxt modules
This commit is contained in:
Pooya Parsa 2018-04-05 13:08:54 +04:30
parent 14a6cd1237
commit e4ee624011
6 changed files with 18 additions and 21 deletions

View File

@ -1,9 +1,8 @@
const { resolve } = require('path')
const { existsSync } = require('fs')
const { Utils } = require('../..')
const { requireModule } = require('../../lib/common/module')
const consola = require('consola')
const esm = require('esm')(module, {})
const getRootDir = argv => resolve(argv._[0] || '.')
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file'])
@ -17,10 +16,9 @@ exports.loadNuxtConfig = argv => {
let options = {}
if (existsSync(nuxtConfigFile)) {
delete require.cache[nuxtConfigFile]
options = requireModule(nuxtConfigFile)
options = esm(nuxtConfigFile).default
} 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') {

View File

@ -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
}

View File

@ -112,7 +112,7 @@ export default class ModuleContainer {
// Resolve handler
if (!handler) {
handler = this.nuxt.requireModule(src)
handler = this.nuxt.requireModule(src, { esm: false })
}
// Validate handler

View File

@ -6,11 +6,11 @@ import _ from 'lodash'
import fs from 'fs-extra'
import consola from 'consola'
import chalk from 'chalk'
import esm from 'esm'
import Options from '../common/options'
import { sequence } from '../common/utils'
import packageJSON from '../../package.json'
import moduleUtil from '../common/module'
import ModuleContainer from './module'
import Renderer from './renderer'
@ -37,6 +37,9 @@ export default class Nuxt {
this.renderer
)
// ESM Loader
this.esm = esm(module, {})
this._ready = this.ready().catch(err => {
consola.fatal(err)
})
@ -195,8 +198,10 @@ export default class Nuxt {
throw new Error(`Cannot resolve "${_path}" from "${__path}"`)
}
requireModule(name) {
return moduleUtil.requireModule(this.resolvePath(name))
requireModule(_path, opts = {}) {
const _resovledPath = this.resolvePath(_path)
const m = opts.esm === false ? require(_resovledPath) : this.esm(_resovledPath)
return (m && m.default) || m
}
async close(callback) {

View File

@ -395,7 +395,7 @@ export default class Renderer {
/* istanbul ignore if */
if (!jsdom) {
try {
jsdom = this.nuxt.requireModule('jsdom')
jsdom = require('jsdom')
} catch (e) /* istanbul ignore next */ {
consola.error(`
Fail when calling nuxt.renderAndGetWindow(url)

View File

@ -4,7 +4,7 @@ import fs from 'fs'
import _getPort from 'get-port'
import { defaultsDeep } from 'lodash'
import _rp from 'request-promise-native'
import { requireModule } from '../../lib/common/module'
import esm from 'esm'
import pkg from '../../package.json'
import Dist from '../../lib/nuxt'
@ -18,11 +18,13 @@ export const Options = Dist.Options
export const Builder = Dist.Builder
export const Generator = Dist.Generator
const requireModule = esm(module, {})
export const loadFixture = function loadFixture(fixture, overrides) {
const rootDir = path.resolve(__dirname, '../fixtures/' + fixture)
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.dev = false