refactor: ad-hoc modules (#7452)

This commit is contained in:
pooya parsa 2020-06-03 21:34:10 +02:00 committed by GitHub
parent 15bc5b06e6
commit 1e4ce5055e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 33 deletions

View File

@ -26,9 +26,6 @@ export default () => ({
css: [],
modules: [],
buildModules: [],
layouts: {},
ErrorPage: null,

View File

@ -20,6 +20,11 @@ export default () => ({
mode: MODES.universal,
modern: undefined,
// Modules
modules: [],
buildModules: [],
_modules: [],
globalName: undefined,
globals: {
id: globalName => `__${globalName}`,

View File

@ -424,21 +424,6 @@ export function getNuxtConfig (_options) {
bundleRenderer.runInNewContext = options.dev
}
// Loading screen
// disable for production and programmatic users
if (!options.dev || !options._cli) {
options.build.loadingScreen = false
}
// Add loading-screen module
if (options.build.loadingScreen) {
options.buildModules.push(['@nuxt/loading-screen', options.build.loadingScreen])
}
// When loadingScreen is disabled we should also disable build indicator
if (!options.build.loadingScreen) {
options.build.indicator = false
}
// TODO: Remove this if statement in Nuxt 3
if (options.build.crossorigin) {
consola.warn('Using `build.crossorigin` is deprecated and will be removed in Nuxt 3. Please use `render.crossorigin` instead.')
@ -456,11 +441,6 @@ export function getNuxtConfig (_options) {
.map(([path, handler]) => ({ path, handler }))
}
// Components module
if (options.components) {
options.buildModules.push('@nuxt/components')
}
// Generate staticAssets
const { staticAssets } = options.generate
if (!staticAssets.version) {
@ -474,6 +454,25 @@ export function getNuxtConfig (_options) {
staticAssets.versionBase = urlJoin(staticAssets.base, staticAssets.version)
}
// ----- Builtin modules -----
// Loading screen
// Force disable for production and programmatic users
if (!options.dev || !options._cli || !getPKG('@nuxt/loading-screen')) {
options.build.loadingScreen = false
}
if (options.build.loadingScreen) {
options._modules.push(['@nuxt/loading-screen', options.build.loadingScreen])
} else {
// When loadingScreen is disabled we should also disable build indicator
options.build.indicator = false
}
// Components Module
if (!options._start && getPKG('@nuxt/components')) {
options._modules.push('@nuxt/components')
}
// Nuxt Telemetry
if (
options.telemetry !== false &&
@ -481,11 +480,7 @@ export function getNuxtConfig (_options) {
!destr(process.env.NUXT_TELEMETRY_DISABLED) &&
getPKG('@nuxt/telemetry')
) {
// TODO: Remove before 2.13 in favor of checking .nuxrc for first run message
if (typeof options.telemetry === 'undefined') {
consola.warn('Experimental `@nuxt/telemetry` enabled. You can disable this message by explicitly setting `telemetry: false` or `telemetry: true` in `nuxt.config`')
}
options.modules.push('@nuxt/telemetry')
options._modules.push('@nuxt/telemetry')
}
return options

View File

@ -4,6 +4,9 @@ exports[`config: options should return default nuxt config 1`] = `
Object {
"ErrorPage": null,
"__normalized__": true,
"_modules": Array [
"@nuxt/components",
],
"_nuxtConfigFile": "/var/nuxt/test/nuxt.config.js",
"_nuxtConfigFiles": Array [
"/var/nuxt/test/nuxt.config.js",

View File

@ -3,6 +3,7 @@
exports[`config should return default nuxt configurations 1`] = `
Object {
"ErrorPage": null,
"_modules": Array [],
"_nuxtConfigFile": undefined,
"alias": Object {},
"build": Object {
@ -371,6 +372,7 @@ Object {
exports[`config should return nuxt configurations with custom env 1`] = `
Object {
"ErrorPage": null,
"_modules": Array [],
"_nuxtConfigFile": undefined,
"alias": Object {},
"build": Object {

View File

@ -16,7 +16,8 @@ jest.mock('std-env', () => ({
jest.mock('@nuxt/utils', () => ({
...jest.requireActual('@nuxt/utils'),
getMainModule: () => ({ paths: ['/var/nuxt/node_modules'] })
getMainModule: () => ({ paths: ['/var/nuxt/node_modules'] }),
getPKG: () => ({ name: 'fake' })
}))
describe('config: options', () => {

View File

@ -10,6 +10,13 @@ export default class ModuleContainer {
this.nuxt = nuxt
this.options = nuxt.options
this.requiredModules = {}
// Self bind to allow destructre from container
for (const method of Object.getOwnPropertyNames(ModuleContainer.prototype)) {
if (typeof this[method] === 'function') {
this[method] = this[method].bind(this)
}
}
}
async ready () {
@ -18,11 +25,14 @@ export default class ModuleContainer {
if (this.options.buildModules && !this.options._start) {
// Load every devModule in sequence
await sequence(this.options.buildModules, this.addModule.bind(this))
await sequence(this.options.buildModules, this.addModule)
}
// Load every module in sequence
await sequence(this.options.modules, this.addModule.bind(this))
await sequence(this.options.modules, this.addModule)
// Load ah-hoc modules last
await sequence(this.options._modules, this.addModule)
// Call done hook
await this.nuxt.callHook('modules:done', this)

View File

@ -19,7 +19,8 @@ jest.mock('@nuxt/utils', () => ({
const defaultOptions = {
modules: [],
buildModules: []
buildModules: [],
_modules: []
}
describe('core: module', () => {

View File

@ -1,3 +1,5 @@
import { join } from 'path'
export function isExternalDependency (id) {
return /[/\\]node_modules[/\\]/.test(id)
}
@ -61,5 +63,5 @@ export function tryRequire (id) {
}
export function getPKG (id) {
return tryRequire(id + '/package.json')
return tryRequire(join(id, 'package.json'))
}