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