mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
Refactor
This commit is contained in:
parent
c5b5913402
commit
f050bb6330
44
lib/build.js
44
lib/build.js
@ -97,8 +97,9 @@ export function options () {
|
||||
}
|
||||
|
||||
export async function build () {
|
||||
this._nuxtPages = typeof this.createRoutes !== 'function'
|
||||
// Check if pages dir exists and warn if not
|
||||
if (typeof this.createRoutes !== 'function') {
|
||||
if (this._nuxtPages) {
|
||||
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
|
||||
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
|
||||
console.error('> No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?') // eslint-disable-line no-console
|
||||
@ -128,7 +129,7 @@ async function buildFiles () {
|
||||
debug('Adding webpack middleware...')
|
||||
createWebpackMiddleware.call(this)
|
||||
webpackWatchAndUpdate.call(this)
|
||||
watchPages.call(this)
|
||||
watchFiles.call(this)
|
||||
} else {
|
||||
debug('Building files...')
|
||||
await webpackRunClient.call(this)
|
||||
@ -172,6 +173,7 @@ async function generateRoutesAndFiles () {
|
||||
base: this.options.router.base,
|
||||
middleware: this.options.router.middleware,
|
||||
linkActiveClass: this.options.router.linkActiveClass,
|
||||
linkExactActiveClass: this.options.router.linkExactActiveClass,
|
||||
scrollBehavior: this.options.router.scrollBehavior
|
||||
},
|
||||
env: this.options.env,
|
||||
@ -215,20 +217,22 @@ async function generateRoutesAndFiles () {
|
||||
|
||||
// -- Routes --
|
||||
debug('Generating routes...')
|
||||
// Format routes for the lib/app/router.js template
|
||||
if (typeof this.createRoutes !== 'function') {
|
||||
// Use internal createRoutes
|
||||
// If user defined a custom method to create routes
|
||||
if (this._nuxtPages) {
|
||||
// Use nuxt.js createRoutes bases on pages/
|
||||
const files = await glob('pages/**/*.vue', {cwd: this.srcDir})
|
||||
templateVars.router.routes = createRoutes(files, this.srcDir)
|
||||
} else {
|
||||
templateVars.router.routes = this.createRoutes.call(this, this.srcDir)
|
||||
this.createRoutes = this.createRoutes.bind(this)
|
||||
templateVars.router.routes = this.createRoutes(this.srcDir)
|
||||
}
|
||||
// router.extendRoutes method
|
||||
if (typeof this.options.router.extendRoutes === 'function') {
|
||||
// let the user extend the routes
|
||||
this.options.router.extendRoutes.call(this, templateVars.router.routes, r)
|
||||
this.options.router.extendRoutes.call(this, templateVars.router.routes || [], r)
|
||||
}
|
||||
// Routes for generate command
|
||||
this.routes = flatRoutes(templateVars.router.routes)
|
||||
this.routes = flatRoutes(templateVars.router.routes || [])
|
||||
|
||||
// -- Store --
|
||||
// Add store if needed
|
||||
@ -239,13 +243,13 @@ async function generateRoutesAndFiles () {
|
||||
// Resolve template files
|
||||
const customTemplateFiles = this.options.build.templates.map(t => t.dst || basename(t.src || t))
|
||||
templatesFiles = templatesFiles.map(file => {
|
||||
// Skip if custom file was already provided in build.templates[]
|
||||
if (customTemplateFiles.indexOf(file) !== -1) {
|
||||
return
|
||||
}
|
||||
// Allow override templates using a file with same name in ${srcDir}/app
|
||||
const customPath = r(this.srcDir, 'app', file)
|
||||
const customFileExists = fs.existsSync(customPath)
|
||||
// Skip if custom file was already provided in build.templates[]
|
||||
if (customTemplateFiles.indexOf(file) !== -1 && !customFileExists) {
|
||||
return
|
||||
}
|
||||
return {
|
||||
src: customFileExists ? customPath : r(__dirname, 'app', file),
|
||||
dst: file,
|
||||
@ -264,7 +268,7 @@ async function generateRoutesAndFiles () {
|
||||
}))
|
||||
|
||||
// Interpret and move template files to .nuxt/
|
||||
return Promise.all(templatesFiles.map(async ({src, dst, options, custom}) => {
|
||||
return Promise.all(templatesFiles.map(async ({ src, dst, options, custom }) => {
|
||||
// Add template to watchers
|
||||
this.options.build.watch.push(src)
|
||||
// Render template to dst
|
||||
@ -513,17 +517,19 @@ function createRenderer (bundle, manifest) {
|
||||
this.renderToStream = this.renderer.renderToStream
|
||||
}
|
||||
|
||||
function watchPages () {
|
||||
function watchFiles () {
|
||||
const patterns = [
|
||||
r(this.srcDir, 'pages'),
|
||||
r(this.srcDir, 'layouts'),
|
||||
r(this.srcDir, 'store'),
|
||||
r(this.srcDir, 'middleware'),
|
||||
r(this.srcDir, 'pages/*.vue'),
|
||||
r(this.srcDir, 'pages/**/*.vue'),
|
||||
r(this.srcDir, 'layouts/*.vue'),
|
||||
r(this.srcDir, 'layouts/**/*.vue')
|
||||
]
|
||||
if (this._nuxtPages) {
|
||||
patterns.push(r(this.srcDir, 'pages'))
|
||||
patterns.push(r(this.srcDir, 'pages/*.vue'))
|
||||
patterns.push(r(this.srcDir, 'pages/**/*.vue'))
|
||||
}
|
||||
const options = Object.assign({}, this.options.watchers.chokidar, {
|
||||
ignoreInitial: true
|
||||
})
|
||||
@ -532,10 +538,10 @@ function watchPages () {
|
||||
await generateRoutesAndFiles.call(this)
|
||||
}, 200)
|
||||
// Watch for internals
|
||||
this.pagesFilesWatcher = chokidar.watch(patterns, options)
|
||||
this.filesWatcher = chokidar.watch(patterns, options)
|
||||
.on('add', refreshFiles)
|
||||
.on('unlink', refreshFiles)
|
||||
// Watch for custom provided files
|
||||
this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options)
|
||||
.on('change', refreshFiles)
|
||||
.on('change', refreshFiles)
|
||||
}
|
||||
|
16
lib/nuxt.js
16
lib/nuxt.js
@ -120,12 +120,12 @@ class Nuxt {
|
||||
this.module = new Module(this)
|
||||
// Install all modules in sequence and then return `this` instance
|
||||
return utils.sequence(options.modules, this.module.addModule.bind(this.module))
|
||||
.then(() => this)
|
||||
.catch(/* istanbul ignore next */ (err) => {
|
||||
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console
|
||||
console.error(err) // eslint-disable-line no-console
|
||||
process.exit(1)
|
||||
})
|
||||
.then(() => this)
|
||||
.catch(/* istanbul ignore next */ (err) => {
|
||||
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console
|
||||
console.error(err) // eslint-disable-line no-console
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
close (callback) {
|
||||
@ -145,8 +145,8 @@ class Nuxt {
|
||||
promises.push(p)
|
||||
}
|
||||
/* istanbul ignore if */
|
||||
if (this.pagesFilesWatcher) {
|
||||
this.pagesFilesWatcher.close()
|
||||
if (this.filesWatcher) {
|
||||
this.filesWatcher.close()
|
||||
}
|
||||
/* istanbul ignore if */
|
||||
if (this.customFilesWatcher) {
|
||||
|
@ -23,9 +23,9 @@ class Server {
|
||||
// Require if needed
|
||||
if (typeof m === 'string') {
|
||||
let src = m
|
||||
// Using ~ shorthand to resolve from project srcDir
|
||||
if (src.indexOf('~') === 0) {
|
||||
src = path.resolve(this.nuxt.options.srcDir, src.substr(1))
|
||||
// Using ~ or ./ shorthand to resolve from project srcDir
|
||||
if (src.indexOf('~') === 0 || src.indexOf('./') === 0) {
|
||||
src = path.join(this.nuxt.options.srcDir, src.substr(1))
|
||||
}
|
||||
// eslint-disable-next-line no-eval
|
||||
m = eval('require')(src)
|
||||
|
Loading…
Reference in New Issue
Block a user