From df363e977093691f096121d3e2f4cb80d47e19bb Mon Sep 17 00:00:00 2001 From: cj Date: Thu, 8 Dec 2016 00:45:40 -0600 Subject: [PATCH 1/3] Added an nuxt.config.js option srcDir --- lib/build/index.js | 31 ++++++++++++++++--------------- lib/build/webpack/base.config.js | 18 +++++++++--------- lib/generate.js | 2 +- lib/nuxt.js | 5 +++-- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/build/index.js b/lib/build/index.js index 4fa69d3c7b..6f1c715f28 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -88,18 +88,19 @@ exports.options = function () { } exports.build = function * () { + console.log(this.srcDir) /* ** Check if pages dir exists and warn if not */ - if (!fs.existsSync(join(this.dir, 'pages'))) { - if (fs.existsSync(join(this.dir, '..', 'pages'))) { + 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?') } else { console.error('> Couldn\'t find a `pages` directory. Please create one under the project root') } process.exit(1) } - debug(`App root: ${this.dir}`) + debug(`App root: ${this.srcDir}`) debug('Generating .nuxt/ files...') /* ** Create .nuxt/, .nuxt/components and .nuxt/dist folders @@ -114,7 +115,7 @@ exports.build = function * () { if (route.component.slice(-4) !== '.vue') { route.component = route.component + '.vue' } - route.component = r(this.dir, route.component) + route.component = r(this.srcDir, route.component) }) // Generate routes and interpret the template files yield generateRoutesAndFiles.call(this) @@ -141,13 +142,13 @@ function * generateRoutesAndFiles () { /* ** Generate routes based on files */ - const files = yield glob('pages/**/*.vue', { cwd: this.dir }) + const files = yield glob('pages/**/*.vue', { cwd: this.srcDir }) let routes = [] files.forEach((file) => { let path = file.replace(/^pages/, '').replace(/index\.vue$/, '/').replace(/\.vue$/, '').replace(/\/{2,}/g, '/') let name = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1).join('-') if (basename(path)[0] === '_') return - routes.push({ path: path, component: r(this.dir, file), name: name }) + routes.push({ path: path, component: r(this.srcDir, file), name: name }) }) // Concat pages routes and custom routes in this.routes this.routes = routes.concat(this.options.router.routes) @@ -177,9 +178,9 @@ function * generateRoutesAndFiles () { head: this.options.head, store: this.options.store, css: this.options.css, - plugins: this.options.plugins.map((p) => r(this.dir, p)), + plugins: this.options.plugins.map((p) => r(this.srcDir, p)), appPath: './App.vue', - loading: (typeof this.options.loading === 'string' ? r(this.dir, this.options.loading) : this.options.loading), + loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading), transition: this.options.transition, components: { Loading: r(__dirname, '..', 'app', 'components', 'nuxt-loading.vue'), @@ -199,16 +200,16 @@ function * generateRoutesAndFiles () { return r }) if (files.includes('pages/_app.vue')) { - templateVars.appPath = r(this.dir, 'pages/_app.vue') + templateVars.appPath = r(this.srcDir, 'pages/_app.vue') } - if (fs.existsSync(join(this.dir, 'layouts', 'app.vue'))) { - templateVars.appPath = r(this.dir, 'layouts/app.vue') + if (fs.existsSync(join(this.srcDir, 'layouts', 'app.vue'))) { + templateVars.appPath = r(this.srcDir, 'layouts/app.vue') } if (files.includes('pages/_error.vue')) { - templateVars.components.ErrorPage = r(this.dir, 'pages/_error.vue') + templateVars.components.ErrorPage = r(this.srcDir, 'pages/_error.vue') } - if (fs.existsSync(join(this.dir, 'layouts', 'error.vue'))) { - templateVars.appPath = r(this.dir, 'layouts/error.vue') + if (fs.existsSync(join(this.srcDir, 'layouts', 'error.vue'))) { + templateVars.appPath = r(this.srcDir, 'layouts/error.vue') } let moveTemplates = templatesFiles.map((file) => { return readFile(r(__dirname, '..', 'app', file), 'utf8') @@ -316,7 +317,7 @@ function createRenderer (bundle) { } function watchPages () { - const patterns = [ r(this.dir, 'pages/*.vue'), r(this.dir, 'pages/**/*.vue') ] + const patterns = [ r(this.srcDir, 'pages/*.vue'), r(this.srcDir, 'pages/**/*.vue') ] const options = { // ignored: '**/_*.vue', ignoreInitial: true diff --git a/lib/build/webpack/base.config.js b/lib/build/webpack/base.config.js index d872068ae8..5a1183fd9f 100644 --- a/lib/build/webpack/base.config.js +++ b/lib/build/webpack/base.config.js @@ -26,16 +26,16 @@ module.exports = function () { resolve: { // Disable for now alias: { - '~': join(this.dir), - 'static': join(this.dir, 'static'), // use in template with - '~static': join(this.dir, 'static'), - 'assets': join(this.dir, 'assets'), // use in template with - '~assets': join(this.dir, 'assets'), - '~plugins': join(this.dir, 'plugins'), - '~store': join(this.dir, 'store'), + '~': join(this.srcDir), + 'static': join(this.srcDir, 'static'), // use in template with + '~static': join(this.srcDir, 'static'), + 'assets': join(this.srcDir, 'assets'), // use in template with + '~assets': join(this.srcDir, 'assets'), + '~plugins': join(this.srcDir, 'plugins'), + '~store': join(this.srcDir, 'store'), '~router': join(this.dir, '.nuxt/router'), - '~pages': join(this.dir, 'pages'), - '~components': join(this.dir, 'components') + '~pages': join(this.srcDir, 'pages'), + '~components': join(this.srcDir, 'components') }, modules: [ nodeModulesDir, diff --git a/lib/generate.js b/lib/generate.js index 7d00286663..60bb632e1c 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -33,7 +33,7 @@ module.exports = function () { */ this.options.generate = _.defaultsDeep(this.options.generate, defaults) var self = this - var srcStaticPath = resolve(this.dir, 'static') + var srcStaticPath = resolve(this.srcDir, 'static') var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist') var distPath = resolve(this.dir, this.options.generate.dir) var distNuxtPath = resolve(distPath, '_nuxt') diff --git a/lib/nuxt.js b/lib/nuxt.js index 7e7bc268dc..8dfbc0473b 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -51,8 +51,9 @@ class Nuxt { // Env variables this.dev = this.options.dev this.dir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd()) + this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? options.srcDir : process.cwd()) // If store defined, update store options to true - if (fs.existsSync(join(this.dir, 'store', 'index.js'))) { + if (fs.existsSync(join(this.srcDir, 'store', 'index.js'))) { this.options.store = true } // Template @@ -65,7 +66,7 @@ class Nuxt { // renderer used by Vue.js (via createBundleRenderer) this.renderer = null // For serving static/ files to / - this.serveStatic = pify(serveStatic(resolve(this.dir, 'static'))) + this.serveStatic = pify(serveStatic(resolve(this.srcDir, 'static'))) // For serving .nuxt/dist/ files this._nuxtRegexp = /^\/_nuxt\// this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist'))) From 187fec3f325625af1d3470e38b05d59a4818987b Mon Sep 17 00:00:00 2001 From: cj Date: Thu, 8 Dec 2016 00:55:44 -0600 Subject: [PATCH 2/3] if srcDir not set, default to this.dir (rootDir) --- lib/nuxt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nuxt.js b/lib/nuxt.js index 8dfbc0473b..5b841cdc92 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -51,7 +51,7 @@ class Nuxt { // Env variables this.dev = this.options.dev this.dir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd()) - this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? options.srcDir : process.cwd()) + this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? options.srcDir : this.dir) // If store defined, update store options to true if (fs.existsSync(join(this.srcDir, 'store', 'index.js'))) { this.options.store = true From 6713f68443298617f9a05f8a0a6389fb6f696761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Thu, 8 Dec 2016 19:19:39 +0100 Subject: [PATCH 3/3] Join with this.dir if relative path --- lib/nuxt.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nuxt.js b/lib/nuxt.js index 5b841cdc92..a2c812ea17 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -51,7 +51,7 @@ class Nuxt { // Env variables this.dev = this.options.dev this.dir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd()) - this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? options.srcDir : this.dir) + this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? resolve(this.dir, options.srcDir) : this.dir) // If store defined, update store options to true if (fs.existsSync(join(this.srcDir, 'store', 'index.js'))) { this.options.store = true