feat: components (#7374)

[release]
This commit is contained in:
Pooya Parsa 2020-05-18 13:58:48 +02:00 committed by GitHub
parent 7fa5fb0a58
commit 6a6794e020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 21 deletions

View File

@ -60,6 +60,7 @@
"dependencies": {
"@nuxt/builder": "2.12.1",
"@nuxt/cli": "2.12.1",
"@nuxt/components": "^0.2.2",
"@nuxt/core": "2.12.1",
"@nuxt/generator": "2.12.1",
"@nuxt/loading-screen": "^1.2.0",

View File

@ -448,6 +448,11 @@ 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) {

View File

@ -112,10 +112,10 @@ export default class ModuleContainer {
}
requireModule (moduleOpts) {
return this.addModule(moduleOpts, true /* require once */)
return this.addModule(moduleOpts)
}
async addModule (moduleOpts, requireOnce) {
async addModule (moduleOpts) {
let src
let options
let handler
@ -176,15 +176,10 @@ export default class ModuleContainer {
throw new TypeError('Module should export a function: ' + src)
}
// Resolve module meta
let key = (handler.meta && handler.meta.name) || handler.name
if (!key || key === 'default') {
key = src
}
// Update requiredModules
// Ensure module is required once
const key = (handler.meta && handler.meta.name) || src
if (typeof key === 'string') {
if (requireOnce && this.requiredModules[key]) {
if (this.requiredModules[key]) {
return
}
this.requiredModules[key] = { src, options, handler }

View File

@ -317,7 +317,7 @@ describe('core: module', () => {
module.requireModule(moduleOpts)
expect(module.addModule).toBeCalledTimes(1)
expect(module.addModule).toBeCalledWith(moduleOpts, true)
expect(module.addModule).toBeCalledWith(moduleOpts)
})
test('should add string module', async () => {
@ -402,21 +402,17 @@ describe('core: module', () => {
})
const result = await module.addModule({
src: 'moduleTest',
src: 'pathToModule',
options: { test: true },
handler: function objectModule (options) {
return Promise.resolve(options)
}
handler: opts => opts
})
expect(requireModule).not.toBeCalled()
expect(module.requiredModules).toEqual({
objectModule: {
handler: expect.any(Function),
options: {
test: true
},
src: 'moduleTest'
pathToModule: {
src: 'pathToModule',
options: { test: true },
handler: expect.any(Function)
}
})
expect(result).toEqual({ test: true })

View File

@ -370,6 +370,11 @@ describe('basic ssr', () => {
expect(html).toContain('<h1>Nested symlink page</h1>')
})
test('/components', async () => {
const { html } = await nuxt.server.renderRoute('/components')
expect(html).toContain('Auto discovered component!')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()

View File

@ -0,0 +1,3 @@
<template>
<div>Auto discovered component!</div>
</template>

View File

@ -63,6 +63,7 @@ export default {
'': true
},
pageTransition: false,
components: true,
plugins: [
'~/plugins/vuex-module',
'~/plugins/dir-plugin',

View File

@ -0,0 +1,5 @@
<template>
<div>
<MyComponent />
</div>
</template>

View File

@ -1819,6 +1819,11 @@
"@nodelib/fs.scandir" "2.1.3"
fastq "^1.6.0"
"@nuxt/components@^0.2.2":
version "0.2.2"
resolved "https://registry.npmjs.org/@nuxt/components/-/components-0.2.2.tgz#194f65574dc40aacf8d0f132a134168ce1b58c83"
integrity sha512-mTDZLmfx65fuI8haXZLeh425vSrCAHRGyK3G6Ckr/QSgmle9HDuaQX1Jl/yEEvBxiUzcPp1fhMexNMn2mvkofg==
"@nuxt/devalue@^1.2.4":
version "1.2.4"
resolved "https://registry.npmjs.org/@nuxt/devalue/-/devalue-1.2.4.tgz#69eca032b7481fd3c019a78ade65d642da3f2f35"