Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
feat: use serveStatic server middleware for static files
Browse files Browse the repository at this point in the history
feat: add support for app files

fix: only use top level dir as type name
  • Loading branch information
pimlie committed Oct 10, 2019
1 parent d83ea16 commit e8e2e4f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 22 deletions.
47 changes: 36 additions & 11 deletions src/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import consola from 'consola'
import defu from 'defu'
import serveStatic from 'serve-static'
import { Module } from '@nuxt/core'
import {
ucfirst,
Expand Down Expand Up @@ -66,9 +67,17 @@ export default class Blueprint extends Module {
}
}

init (files) {
async init (files) {
this.setup()

// static files need to be added immediately
// because otherwise the serveStatic middleware
// is added after the server has already started listening
if (files && files.static) {
await this.resolveFiles({ static: files.static })
delete files.static
}

this.nuxt.hook('builder:prepared', async () => {
if (this.blueprintOptions.autodiscover) {
const autodiscoveredFiles = await this.autodiscover()
Expand Down Expand Up @@ -105,6 +114,8 @@ export default class Blueprint extends Module {

async autodiscover (rootDir, { validate, filter } = {}) {
rootDir = rootDir || this.blueprintOptions.dir
filter = filter || this.blueprintOptions.filter
validate = validate || this.blueprintOptions.validate

if (!rootDir || !await exists(rootDir)) {
return {}
Expand All @@ -122,7 +133,8 @@ export default class Blueprint extends Module {
const parsedFile = path.parse(file)

// TODO: fix sub folders
const { dir: type, ext } = parsedFile
const { dir, ext } = parsedFile
const [type] = dir.split(path.sep)

// dont add anything without an extension -> not a proper file
if (!type && !ext) {
Expand Down Expand Up @@ -382,13 +394,19 @@ export default class Blueprint extends Module {
this.nuxt.options.plugins[pluginsStrategy](...newPlugins)
}

addStatic (staticFiles) {
this.nuxt.hook('build:done', () => {
/* istanbul ignore next */
return Promise.all(staticFiles.map((file) => {
return this.copyFile(this.createTemplatePaths(file))
}))
})
async addStatic (staticFiles) {
/* istanbul ignore next */
const files = await Promise.all(staticFiles.map((file) => {
return this.addTemplateOrCopy(file)
}))

const staticMiddleware = serveStatic(
path.resolve(this.nuxt.options.buildDir, path.dirname(files[0])),
this.nuxt.options.render.static
)
staticMiddleware.prefix = this.nuxt.options.render.static.prefix

this.addServerMiddleware(staticMiddleware)
}

addStyles (stylesheets) {
Expand All @@ -403,8 +421,15 @@ export default class Blueprint extends Module {
}
}

addApp () {
consola.warn(`${this.constructor.name}: app overrides are not (yet) implemented`)
addApp (appFiles) {
return Promise.all(appFiles.map(({ src, dst }) => {
return this.addTemplate({
src,
// dst has blueprint id and app dir name added, remove those
// eg dst: blueprint/app/router.js -> router.js
fileName: dst.substr(dst.indexOf('app') + 3)
})
}))
}

addStore () {
Expand Down
104 changes: 93 additions & 11 deletions test/unit/blueprint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jest.mock('src/utils')
const resetUtilMocks = utilNames => _resetUtilMocks(utils, utilNames)

jest.mock('fs-extra')
jest.mock('serve-static', () => () => jest.fn())

describe('blueprint', () => {
beforeAll(() => resetUtilMocks())
Expand Down Expand Up @@ -105,7 +106,10 @@ describe('blueprint', () => {
},
layouts: {},
plugins: [],
css: []
css: [],
render: {
static: {}
}
}
}
const options = {
Expand All @@ -115,6 +119,7 @@ describe('blueprint', () => {
const blueprint = new Blueprint(nuxt, options)
blueprint.addModule = jest.fn()
blueprint.addTemplate = jest.fn(({ src, fileName }) => ({ dst: fileName }))
blueprint.addServerMiddleware = jest.fn()

const files = {
assets: [
Expand Down Expand Up @@ -143,6 +148,12 @@ describe('blueprint', () => {
styles: [
'styles/my-test.css'
],
app: [
{
src: 'app/empty.js',
dst: 'app/empty.js'
}
],
custom: [
'custom-file.log',
{
Expand All @@ -167,22 +178,101 @@ describe('blueprint', () => {
'layouts/docs.tmpl.vue': 'blueprint/layouts/docs.vue',
'modules/my-module.js': '../my-blueprint-dir/modules/my-module.js',
'plugins/my-plugin.$tmpl.js': 'blueprint/plugins/my-plugin.blueprint.js',
'static/my-static.txt': '../my-blueprint-dir/static/my-static.txt',
'styles/my-test.css': '../my-blueprint-dir/styles/my-test.css'
})

expect(nuxt.options.layouts).toEqual({ docs: './blueprint/layouts/docs.vue' })
expect(nuxt.options.plugins).toEqual([{ src: '/var/nuxt/.nuxt/blueprint/plugins/my-plugin.blueprint.js' }])
expect(nuxt.options.css).toEqual(['/var/nuxt/my-blueprint-dir/styles/my-test.css'])
expect(nuxt.options.build.plugins).toEqual([{ apply: expect.any(Function) }])
expect(nuxt.hook).toHaveBeenCalledTimes(1)
expect(nuxt.hook).toHaveBeenCalledWith('build:done', expect.any(Function))
expect(blueprint.addServerMiddleware).toHaveBeenCalledTimes(1)
expect(blueprint.addServerMiddleware).toHaveBeenCalledWith(expect.any(Function))
expect(blueprint.addModule).toHaveBeenCalledTimes(1)
expect(blueprint.addModule).toHaveBeenCalledWith('/var/nuxt/my-blueprint-dir/modules/my-module.js')
expect(blueprint.addTemplate).toHaveBeenCalledTimes(5)
expect(blueprint.addTemplate).toHaveBeenCalledWith(expect.objectContaining({ src: 'app/empty.js' }))

expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledWith(expect.stringContaining('Duplicate layout registration'))
})

test('createTemplatePaths returns when filePath is already object', () => {
const blueprint = new Blueprint({}, { id: 'test' })

const filePath = { src: 'test.js' }

expect(blueprint.createTemplatePaths(filePath)).toBe(filePath)
})

test('resolveAppPath', () => {
const nuxt = {
options: {
srcDir: '/test-src',
dir: {
app: 'app'
}
}
}

const blueprint = new Blueprint(nuxt, { id: 'test' })
expect(blueprint.resolveAppPath({ dstRelative: 'rel' })).toEqual('/test-src/app/test/rel')
})

test('resolveAppOverrides exists', async () => {
utils.exists.mockReturnValue(true)

const nuxt = {
options: {
srcDir: '/test-src',
dir: {
app: 'app'
}
}
}
const templates = [
{
src: '/my-src/test.js',
dstRelative: 'test.js'
}
]

const blueprint = new Blueprint(nuxt, { id: 'test' })
await expect(blueprint.resolveAppOverrides(templates)).resolves.toEqual([{
src: '/test-src/app/test/test.js',
dstRelative: 'test.js'
}])
})

test('resolveAppOverrides does not exists', async () => {
let callCount = 0
utils.exists.mockImplementation(() => {
callCount++
return callCount > 2
})

const nuxt = {
options: {
srcDir: '/test-src',
dir: {
app: 'app'
}
}
}
const templates = [
{
src: '/my-src/test.js',
dstRelative: 'test.js'
}
]

const blueprint = new Blueprint(nuxt, { id: 'test' })
await expect(blueprint.resolveAppOverrides(templates)).resolves.toEqual([{
src: '/my-src/test.js',
dstRelative: 'test.js'
}])
})

test('copyFile logs on error', () => {
const blueprint = new Blueprint({
options: { srcDir: '/', buildDir: '/.nuxt' }
Expand Down Expand Up @@ -247,14 +337,6 @@ describe('blueprint', () => {
await expect(blueprint.addPlugins(plugins)).rejects.toThrowError('Unsupported')
})

test('appDir overrides warns not implemented', () => {
const blueprint = new Blueprint({})
blueprint.addApp()

expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledWith(expect.stringContaining('not (yet) implemented'))
})

test('store module warns not implemented', () => {
const blueprint = new Blueprint({})
blueprint.addStore()
Expand Down

0 comments on commit e8e2e4f

Please sign in to comment.