2019-02-04 10:34:04 +00:00
|
|
|
import Glob from 'glob'
|
|
|
|
import consola from 'consola'
|
|
|
|
import { isIndexFileAndFolder } from '@nuxt/utils'
|
2019-08-21 19:04:04 +00:00
|
|
|
import { BundleBuilder } from '@nuxt/webpack'
|
2019-02-04 10:34:04 +00:00
|
|
|
|
|
|
|
import Builder from '../src/builder'
|
|
|
|
import { createNuxt } from './__utils__'
|
|
|
|
|
|
|
|
jest.mock('glob')
|
|
|
|
jest.mock('pify', () => fn => fn)
|
|
|
|
jest.mock('hash-sum', () => src => `hash(${src})`)
|
|
|
|
jest.mock('@nuxt/utils')
|
|
|
|
jest.mock('../src/ignore')
|
2019-08-21 19:04:04 +00:00
|
|
|
jest.mock('@nuxt/webpack')
|
2019-02-04 10:34:04 +00:00
|
|
|
|
|
|
|
describe('builder: builder plugins', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
test('should normalize plugins', async () => {
|
2019-02-04 10:34:04 +00:00
|
|
|
const nuxt = createNuxt()
|
|
|
|
nuxt.options.plugins = [
|
|
|
|
'/var/nuxt/plugins/test.js',
|
2019-05-11 09:23:37 +00:00
|
|
|
'/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js',
|
2019-02-04 10:34:04 +00:00
|
|
|
{ src: '/var/nuxt/plugins/test.server', mode: 'server' },
|
|
|
|
{ src: '/var/nuxt/plugins/test.client', ssr: false }
|
|
|
|
]
|
2019-09-04 12:19:50 +00:00
|
|
|
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-09-04 12:19:50 +00:00
|
|
|
const plugins = await builder.normalizePlugins()
|
2019-02-04 10:34:04 +00:00
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
expect(nuxt.callHook).toBeCalledTimes(1)
|
|
|
|
expect(nuxt.callHook).toBeCalledWith('builder:extendPlugins', nuxt.options.plugins)
|
2019-02-04 10:34:04 +00:00
|
|
|
|
|
|
|
expect(plugins).toEqual([
|
|
|
|
{
|
|
|
|
mode: 'all',
|
|
|
|
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.js)'
|
|
|
|
},
|
2019-05-11 09:23:37 +00:00
|
|
|
{
|
|
|
|
mode: 'client',
|
|
|
|
name: 'nuxt_plugin_foobarpluginclient530b6c6a_hash(/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js)'
|
|
|
|
},
|
2019-02-04 10:34:04 +00:00
|
|
|
{
|
|
|
|
mode: 'server',
|
|
|
|
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.server)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mode: 'client',
|
|
|
|
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.client)'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
test('should overwrite plugins from options', async () => {
|
|
|
|
const nuxt = createNuxt()
|
|
|
|
|
|
|
|
nuxt.options.plugins = [ '/var/nuxt/plugins/foo-bar.js' ]
|
|
|
|
nuxt.options.extendPlugins = jest.fn().mockReturnValue([
|
|
|
|
'/var/nuxt/plugins/fizz-fuzz.js'
|
|
|
|
])
|
|
|
|
|
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
|
|
|
const plugins = await builder.normalizePlugins()
|
|
|
|
|
|
|
|
expect(nuxt.options.extendPlugins).toHaveBeenCalledTimes(1)
|
|
|
|
expect(nuxt.options.extendPlugins).toHaveBeenCalledWith([
|
|
|
|
'/var/nuxt/plugins/foo-bar.js'
|
|
|
|
])
|
|
|
|
|
|
|
|
expect(plugins).toEqual([
|
|
|
|
{
|
|
|
|
mode: 'all',
|
|
|
|
name: 'nuxt_plugin_fizzfuzz_hash(/var/nuxt/plugins/fizz-fuzz.js)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/fizz-fuzz.js)'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should warning and fallback invalid mode when normalize plugins', async () => {
|
2019-02-04 10:34:04 +00:00
|
|
|
const nuxt = createNuxt()
|
|
|
|
nuxt.options.plugins = [
|
|
|
|
{ src: '/var/nuxt/plugins/test', mode: 'abc' }
|
|
|
|
]
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-02-04 10:34:04 +00:00
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
const plugins = await builder.normalizePlugins()
|
2019-02-04 10:34:04 +00:00
|
|
|
|
|
|
|
expect(plugins).toEqual([
|
|
|
|
{
|
|
|
|
mode: 'all',
|
|
|
|
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test)',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test)'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
expect(consola.warn).toBeCalledTimes(1)
|
|
|
|
expect(consola.warn).toBeCalledWith("Invalid plugin mode (server/client/all): 'abc'. Falling back to 'all'")
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should resolve plugins', async () => {
|
|
|
|
const nuxt = createNuxt()
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-02-04 10:34:04 +00:00
|
|
|
builder.plugins = [
|
|
|
|
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
|
|
|
|
{ src: '/var/nuxt/plugins/test.client', mode: 'client' },
|
|
|
|
{ src: '/var/nuxt/plugins/test.server', mode: 'server' }
|
|
|
|
]
|
|
|
|
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
|
|
|
|
for (let step = 0; step < builder.plugins.length; step++) {
|
|
|
|
Glob.mockImplementationOnce(src => [`${src.replace(/\{.*\}/, '')}.js`])
|
|
|
|
}
|
|
|
|
|
|
|
|
await builder.resolvePlugins()
|
|
|
|
|
|
|
|
expect(Glob).toBeCalledTimes(3)
|
|
|
|
expect(Glob).nthCalledWith(1, '/var/nuxt/plugins/test.js{?(.+([^.])),/index.+([^.])}')
|
|
|
|
expect(Glob).nthCalledWith(2, '/var/nuxt/plugins/test.client{?(.+([^.])),/index.+([^.])}')
|
|
|
|
expect(Glob).nthCalledWith(3, '/var/nuxt/plugins/test.server{?(.+([^.])),/index.+([^.])}')
|
|
|
|
expect(builder.plugins).toEqual([
|
|
|
|
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test.js)' },
|
|
|
|
{ mode: 'client', src: 'relative(/var/nuxt/plugins/test.client)' },
|
|
|
|
{ mode: 'server', src: 'relative(/var/nuxt/plugins/test.server)' }
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2019-02-04 11:31:24 +00:00
|
|
|
test('should throw error if plugin no existed', async () => {
|
2019-02-04 10:34:04 +00:00
|
|
|
const nuxt = createNuxt()
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-02-04 10:34:04 +00:00
|
|
|
builder.plugins = [
|
|
|
|
{ src: '/var/nuxt/plugins/test.js', mode: 'all' }
|
|
|
|
]
|
|
|
|
Glob.mockImplementationOnce(() => [])
|
|
|
|
|
2019-02-04 11:31:24 +00:00
|
|
|
await expect(builder.resolvePlugins()).rejects.toThrow('Plugin not found: /var/nuxt/plugins/test.js')
|
2019-02-04 10:34:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should warn if there are multiple files and not index', async () => {
|
|
|
|
const nuxt = createNuxt()
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-02-04 10:34:04 +00:00
|
|
|
builder.plugins = [
|
|
|
|
{ src: '/var/nuxt/plugins/test', mode: 'all' }
|
|
|
|
]
|
|
|
|
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
|
|
|
|
|
2019-07-24 07:26:44 +00:00
|
|
|
Glob.mockImplementationOnce(src => [`${src}.js`])
|
2019-02-04 10:34:04 +00:00
|
|
|
isIndexFileAndFolder.mockReturnValueOnce(false)
|
|
|
|
|
|
|
|
await builder.resolvePlugins()
|
|
|
|
|
|
|
|
expect(builder.plugins).toEqual([
|
|
|
|
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test)' }
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
test('should detect plugin mode for client/server plugins', async () => {
|
2019-02-04 10:34:04 +00:00
|
|
|
const nuxt = createNuxt()
|
2019-08-21 19:04:04 +00:00
|
|
|
const builder = new Builder(nuxt, BundleBuilder)
|
2019-03-07 19:33:20 +00:00
|
|
|
builder.options.plugins = [
|
2019-02-04 10:34:04 +00:00
|
|
|
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
|
2019-03-07 19:33:20 +00:00
|
|
|
{ src: '/var/nuxt/plugins/test.client' },
|
|
|
|
{ src: '/var/nuxt/plugins/test.server' }
|
2019-02-04 10:34:04 +00:00
|
|
|
]
|
|
|
|
|
2019-09-04 12:19:50 +00:00
|
|
|
const plugins = await builder.normalizePlugins()
|
2019-02-04 10:34:04 +00:00
|
|
|
|
2019-03-07 19:33:20 +00:00
|
|
|
expect(plugins).toEqual([
|
|
|
|
{ mode: 'all',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.js)',
|
|
|
|
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)' },
|
|
|
|
{ mode: 'client',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.client)',
|
|
|
|
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)' },
|
|
|
|
{ mode: 'server',
|
|
|
|
src: 'resolveAlias(/var/nuxt/plugins/test.server)',
|
|
|
|
'name': 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)' }
|
2019-02-04 10:34:04 +00:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|