refactor: plugin sanity check (#3743)

This commit is contained in:
Clark Du 2018-08-16 16:34:32 +01:00 committed by GitHub
parent 5b98c1ccee
commit 50be809b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 5 deletions

View File

@ -446,9 +446,17 @@ export default class Builder {
// Check plugins exist then set alias to their real path // Check plugins exist then set alias to their real path
await Promise.all(this.plugins.map(async (p) => { await Promise.all(this.plugins.map(async (p) => {
const pluginFile = `${p.src}${path.extname(p.src) ? '' : '.js'}` const ext = path.extname(p.src) ? '' : '.+([^.])'
if (!await fsExtra.pathExists(pluginFile)) { const pluginFiles = await glob(`${p.src}${ext}`)
throw new Error(`Plugin not found: ${pluginFile}`)
if (!pluginFiles || pluginFiles.length === 0) {
throw new Error(`Plugin not found: ${p.src}`)
} else if (pluginFiles.length > 1) {
consola.warn({
message: `Found ${pluginFiles.length} plugins that match the configuration, suggest to specify extension:`,
additional: ` ${pluginFiles.join('\n ')}`,
badge: true
})
} }
const src = this.relativeToBuild(p.src) const src = this.relativeToBuild(p.src)

View File

View File

@ -1,3 +1,16 @@
const consola = require('consola')
const { buildFixture } = require('../../utils/build') const { buildFixture } = require('../../utils/build')
buildFixture('with-config') describe('with-config', () => {
beforeAll(() => {
consola.warn = jest.fn()
})
buildFixture('with-config', () => {
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn.mock.calls[0]).toMatchObject([{
message: 'Found 2 plugins that match the configuration, suggest to specify extension:',
additional: expect.stringContaining('plugins/test.json'),
badge: true
}])
})
})

View File

@ -1,6 +1,6 @@
import { loadFixture, Nuxt, Builder } from './index' import { loadFixture, Nuxt, Builder } from './index'
export const buildFixture = function buildFixture(fixture) { export const buildFixture = function (fixture, callback) {
test(`Build ${fixture}`, async () => { test(`Build ${fixture}`, async () => {
const config = loadFixture(fixture) const config = loadFixture(fixture)
const nuxt = new Nuxt(config) const nuxt = new Nuxt(config)
@ -10,5 +10,8 @@ export const buildFixture = function buildFixture(fixture) {
// 2: BUILD_DONE // 2: BUILD_DONE
expect(builder._buildStatus).toBe(2) expect(builder._buildStatus).toBe(2)
expect(buildDone).toHaveBeenCalledTimes(1) expect(buildDone).toHaveBeenCalledTimes(1)
if (typeof callback === 'function') {
callback(builder)
}
}, 120000) }, 120000)
} }