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>
import Vue from 'vue'
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 {
name: 'nuxt',

View File

@ -9,7 +9,7 @@ import webpack from 'webpack'
import PostCompilePlugin from 'post-compile-webpack-plugin'
import serialize from 'serialize-javascript'
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 clientWebpackConfig from './webpack/client.config.js'
import serverWebpackConfig from './webpack/server.config.js'
@ -138,7 +138,7 @@ async function buildFiles () {
}
function addAppTemplate () {
let templatePath = resolve(this.buildDir, 'dist', 'index.html')
let templatePath = relative(this.buildDir, 'dist', 'index.html')
if (fs.existsSync(templatePath)) {
this.appTemplate = _.template(fs.readFileSync(templatePath, 'utf8'), {
interpolate: /{{([\s\S]+?)}}/g
@ -163,6 +163,8 @@ async function generateRoutesAndFiles () {
'components/nuxt-link.js',
'components/nuxt.vue'
]
const srcImportDir = relative(this.buildDir, this.srcDir)
const templateVars = {
options: this.options,
uniqBy: _.uniqBy,
@ -190,7 +192,7 @@ async function generateRoutesAndFiles () {
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),
transition: this.options.transition,
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) => {
let name = file.split('/').slice(-1)[0].replace('.vue', '')
if (name === 'error') return
templateVars.layouts[name] = r(this.srcDir, file)
templateVars.layouts[name] = join(srcImportDir, file)
})
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 (!templateVars.layouts.default) {
await mkdirp(r(this.buildDir, 'layouts'))
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 --
@ -219,9 +221,9 @@ async function generateRoutesAndFiles () {
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)
templateVars.router.routes = createRoutes(files, srcImportDir)
} else {
templateVars.router.routes = this.createRoutes(this.srcDir)
templateVars.router.routes = this.createRoutes(srcImportDir)
}
// router.extendRoutes method
if (typeof this.options.router.extendRoutes === 'function') {
@ -295,11 +297,11 @@ async function generateRoutesAndFiles () {
}))
}
function createRoutes (files, srcDir) {
function createRoutes (files, importDir) {
let routes = []
files.forEach((file) => {
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
keys.forEach((key, i) => {
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')