relative import url

This commit is contained in:
laotan 2017-06-19 14:02:10 +08:00
parent 9e433764d7
commit b14cc1951a
2 changed files with 13 additions and 11 deletions

View File

@ -6,7 +6,7 @@
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import NuxtChild from './nuxt-child' import NuxtChild from './nuxt-child'
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./nuxt-error.vue" %>' import NuxtError from '<%= components.ErrorPage ? "../" + components.ErrorPage : "./nuxt-error.vue" %>'
export default { export default {
name: 'nuxt', name: 'nuxt',

View File

@ -9,7 +9,7 @@ import webpack from 'webpack'
import PostCompilePlugin from 'post-compile-webpack-plugin' import PostCompilePlugin from 'post-compile-webpack-plugin'
import serialize from 'serialize-javascript' import serialize from 'serialize-javascript'
import { createBundleRenderer } from 'vue-server-renderer' import { createBundleRenderer } from 'vue-server-renderer'
import { join, resolve, basename, dirname } from 'path' import { join, resolve, basename, dirname, relative } from 'path'
import { isUrl, r, wp } from './utils' import { isUrl, r, wp } from './utils'
import clientWebpackConfig from './webpack/client.config.js' import clientWebpackConfig from './webpack/client.config.js'
import serverWebpackConfig from './webpack/server.config.js' import serverWebpackConfig from './webpack/server.config.js'
@ -138,7 +138,7 @@ async function buildFiles () {
} }
function addAppTemplate () { function addAppTemplate () {
let templatePath = resolve(this.buildDir, 'dist', 'index.html') let templatePath = relative(this.buildDir, 'dist', 'index.html')
if (fs.existsSync(templatePath)) { if (fs.existsSync(templatePath)) {
this.appTemplate = _.template(fs.readFileSync(templatePath, 'utf8'), { this.appTemplate = _.template(fs.readFileSync(templatePath, 'utf8'), {
interpolate: /{{([\s\S]+?)}}/g interpolate: /{{([\s\S]+?)}}/g
@ -163,6 +163,8 @@ async function generateRoutesAndFiles () {
'components/nuxt-link.js', 'components/nuxt-link.js',
'components/nuxt.vue' 'components/nuxt.vue'
] ]
const srcImportDir = relative(this.buildDir, this.srcDir)
const templateVars = { const templateVars = {
options: this.options, options: this.options,
uniqBy: _.uniqBy, uniqBy: _.uniqBy,
@ -190,7 +192,7 @@ async function generateRoutesAndFiles () {
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, 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, transition: this.options.transition,
components: { components: {
ErrorPage: this.options.ErrorPage ? r(this.options.ErrorPage) : null ErrorPage: this.options.ErrorPage ? join(srcImportDir, this.options.ErrorPage) : null
} }
} }
@ -200,17 +202,17 @@ async function generateRoutesAndFiles () {
layoutsFiles.forEach((file) => { layoutsFiles.forEach((file) => {
let name = file.split('/').slice(-1)[0].replace('.vue', '') let name = file.split('/').slice(-1)[0].replace('.vue', '')
if (name === 'error') return if (name === 'error') return
templateVars.layouts[name] = r(this.srcDir, file) templateVars.layouts[name] = join(srcImportDir, file)
}) })
if (layoutsFiles.includes('layouts/error.vue')) { if (layoutsFiles.includes('layouts/error.vue')) {
templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue') templateVars.components.ErrorPage = join(srcImportDir, 'layouts/error.vue')
} }
} }
// If no default layout, create its folder and add the default folder // If no default layout, create its folder and add the default folder
if (!templateVars.layouts.default) { if (!templateVars.layouts.default) {
await mkdirp(r(this.buildDir, 'layouts')) await mkdirp(r(this.buildDir, 'layouts'))
templatesFiles.push('layouts/default.vue') templatesFiles.push('layouts/default.vue')
templateVars.layouts.default = r(__dirname, 'app', 'layouts', 'default.vue') templateVars.layouts.default = join(relative(this.buildDir, __dirname), 'app', 'layouts', 'default.vue')
} }
// -- Routes -- // -- Routes --
@ -219,9 +221,9 @@ async function generateRoutesAndFiles () {
if (this._nuxtPages) { if (this._nuxtPages) {
// Use nuxt.js createRoutes bases on pages/ // Use nuxt.js createRoutes bases on pages/
const files = await glob('pages/**/*.vue', {cwd: this.srcDir}) const files = await glob('pages/**/*.vue', {cwd: this.srcDir})
templateVars.router.routes = createRoutes(files, this.srcDir) templateVars.router.routes = createRoutes(files, srcImportDir)
} else { } else {
templateVars.router.routes = this.createRoutes(this.srcDir) templateVars.router.routes = this.createRoutes(srcImportDir)
} }
// router.extendRoutes method // router.extendRoutes method
if (typeof this.options.router.extendRoutes === 'function') { if (typeof this.options.router.extendRoutes === 'function') {
@ -295,11 +297,11 @@ async function generateRoutesAndFiles () {
})) }))
} }
function createRoutes (files, srcDir) { function createRoutes (files, importDir) {
let routes = [] let routes = []
files.forEach((file) => { files.forEach((file) => {
let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1) let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
let route = { name: '', path: '', component: r(srcDir, file) } let route = { name: '', path: '', component: join(importDir, file) }
let parent = routes let parent = routes
keys.forEach((key, i) => { keys.forEach((key, i) => {
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '') route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')