mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
build.templatesFiles
This feature adds advanced plugin and template support to modules
This commit is contained in:
parent
a8d681af3f
commit
4800a9e8fd
22
lib/build.js
22
lib/build.js
@ -57,7 +57,8 @@ const defaults = {
|
||||
loaders: [],
|
||||
plugins: [],
|
||||
babel: {},
|
||||
postcss: []
|
||||
postcss: [],
|
||||
templatesFiles: []
|
||||
}
|
||||
const defaultsLoaders = [
|
||||
{
|
||||
@ -109,6 +110,7 @@ export function options () {
|
||||
const manifest = fs.readFileSync(manifestPath, 'utf8')
|
||||
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
|
||||
addAppTemplate.call(this)
|
||||
this.module.renderPlugins.call(this.module)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,6 +151,7 @@ function * buildFiles () {
|
||||
yield webpackRunClient.call(this)
|
||||
yield webpackRunServer.call(this)
|
||||
addAppTemplate.call(this)
|
||||
this.module.renderPlugins.call(this.module)
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,17 +242,26 @@ function * generateRoutesAndFiles () {
|
||||
if (this.options.store) {
|
||||
templatesFiles.push('store.js')
|
||||
}
|
||||
let moveTemplates = templatesFiles.map((file) => {
|
||||
return readFile(r(__dirname, 'app', file), 'utf8')
|
||||
// Resolve all internal template files relative to app directory
|
||||
templatesFiles = templatesFiles.map(file => { return {src: r(__dirname, 'app', file), dst: file} })
|
||||
// Add external template files (used in modules)
|
||||
if (Array.isArray(this.options.build.templatesFiles)) {
|
||||
templatesFiles = templatesFiles.concat(this.options.build.templatesFiles)
|
||||
}
|
||||
|
||||
let moveTemplates = templatesFiles.map(({src, dst, options}) => {
|
||||
return readFile(src, 'utf8')
|
||||
.then((fileContent) => {
|
||||
const template = _.template(fileContent, {
|
||||
imports: {
|
||||
serialize,
|
||||
hash
|
||||
}
|
||||
},
|
||||
options: options || {},
|
||||
nuxt: this.options
|
||||
})
|
||||
const content = template(templateVars)
|
||||
const path = r(this.dir, '.nuxt', file)
|
||||
const path = r(this.dir, '.nuxt', dst)
|
||||
return writeFile(path, content, 'utf8')
|
||||
.then(() => {
|
||||
// Fix webpack loop (https://github.com/webpack/watchpack/issues/25#issuecomment-287789288)
|
||||
|
@ -3,6 +3,7 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import {uniq} from 'lodash'
|
||||
import hash from 'hash-sum'
|
||||
|
||||
class Module {
|
||||
constructor (nuxt) {
|
||||
@ -17,27 +18,39 @@ class Module {
|
||||
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
||||
}
|
||||
|
||||
addPlugin (plugin) {
|
||||
if (!plugin) {
|
||||
addTemplate (template) {
|
||||
if (!template) {
|
||||
return
|
||||
}
|
||||
const ssr = Boolean(plugin.ssr)
|
||||
const copyOnly = Boolean(plugin.copyOnly)
|
||||
const src = plugin.src || plugin
|
||||
// Validate & parse source
|
||||
const src = template.src || template
|
||||
const srcPath = path.parse(src)
|
||||
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('[Nuxt] invalid plugin', plugin)
|
||||
console.warn('[Nuxt] invalid template', template)
|
||||
return
|
||||
}
|
||||
// Copy plugin to project
|
||||
const fileName = path.basename(src)
|
||||
// TODO: Build removes this?
|
||||
const dst = path.resolve(this.nuxt.rootDir, '.nuxt', fileName)
|
||||
fs.copySync(src, dst)
|
||||
// Add to nuxt plugins
|
||||
if (!copyOnly) {
|
||||
this.options.plugins.push({src: dst, ssr})
|
||||
// Generate unique and human readable dst filename
|
||||
const dst = template.dst ||
|
||||
((template.dstName || (path.basename(srcPath.dir) + '.' + srcPath.name)) + '.' + hash(src) + (template.dstExt || srcPath.ext))
|
||||
|
||||
// Add to templates list
|
||||
const templateObj = {
|
||||
src,
|
||||
dst,
|
||||
options: template.options
|
||||
}
|
||||
this.options.build.templatesFiles.push(templateObj)
|
||||
return templateObj
|
||||
}
|
||||
|
||||
addPlugin (template) {
|
||||
const {dst} = this.addTemplate(template)
|
||||
// Add to nuxt plugins
|
||||
this.options.plugins.push({
|
||||
src: '~/.nuxt/' + dst,
|
||||
ssr: Boolean(template.ssr)
|
||||
})
|
||||
}
|
||||
|
||||
extendBuild (extendFn) {
|
||||
|
@ -56,11 +56,7 @@ class Nuxt {
|
||||
webpack: {},
|
||||
chokidar: {}
|
||||
},
|
||||
build: {
|
||||
postcss: [],
|
||||
vendor: [],
|
||||
plugins: []
|
||||
}
|
||||
build: {}
|
||||
}
|
||||
// Sanitization
|
||||
if (options.loading === true) delete options.loading
|
||||
|
Loading…
Reference in New Issue
Block a user