feat: custom static directory

This commit is contained in:
Ricardo Gobbo de Souza 2018-02-03 09:54:16 -02:00
parent ba5e608302
commit 483cd9ea29
8 changed files with 28 additions and 15 deletions

View File

@ -27,7 +27,7 @@ module.exports = class Generator {
this.builder = builder
// Set variables
this.staticRoutes = resolve(this.options.srcDir, 'static')
this.staticRoutes = resolve(this.options.srcDir, this.options.staticDir)
this.srcBuiltPath = resolve(this.options.buildDir, 'dist')
this.distPath = resolve(this.options.rootDir, this.options.generate.dir)
this.distNuxtPath = join(

View File

@ -19,11 +19,12 @@ module.exports = function webpackBaseConfig({ name, isServer }) {
// Prioritize nested node_modules in webpack search path (#2558)
const webpackModulesDir = ['node_modules'].concat(this.options.modulesDir)
const assetsAlias = {}
const configAlias = {}
// Used by vue-loader so we can use in templates
// with <img src="~/assets/nuxt.png"/>
assetsAlias[this.options.assetsDir] = join(this.options.srcDir, this.options.assetsDir)
configAlias[this.options.assetsDir] = join(this.options.srcDir, this.options.assetsDir)
configAlias[this.options.staticDir] = join(this.options.srcDir, this.options.staticDir)
const config = {
name,
@ -49,9 +50,8 @@ module.exports = function webpackBaseConfig({ name, isServer }) {
'~': join(this.options.srcDir),
'~~': join(this.options.rootDir),
'@': join(this.options.srcDir),
'@@': join(this.options.rootDir),
static: join(this.options.srcDir, 'static')
}, assetsAlias),
'@@': join(this.options.rootDir)
}, configAlias),
modules: webpackModulesDir
},
resolveLoader: {

View File

@ -56,8 +56,9 @@ module.exports = function styleLoader(ext, loaders = [], isVueLoader = false) {
// css-loader
// https://github.com/webpack-contrib/css-loader
const assetsAlias = {}
assetsAlias[`/${this.options.assetsDir}`] = join(this.options.srcDir, this.options.assetsDir)
const cssLoaderAlias = {}
cssLoaderAlias[`/${this.options.assetsDir}`] = join(this.options.srcDir, this.options.assetsDir)
cssLoaderAlias[`/${this.options.staticDir}`] = join(this.options.srcDir, this.options.staticDir)
loaders.unshift({
loader: 'css-loader',
@ -65,9 +66,7 @@ module.exports = function styleLoader(ext, loaders = [], isVueLoader = false) {
sourceMap,
minimize: !this.options.dev,
importLoaders: loaders.length, // Important!
alias: Object.assign({
'/static': join(this.options.srcDir, 'static')
}, assetsAlias)
alias: cssLoaderAlias
}
})

View File

@ -286,6 +286,7 @@ Options.defaults = {
},
assetsDir: 'assets',
pagesDir: 'pages',
staticDir: 'static',
router: {
mode: 'history',
base: '/',

View File

@ -251,7 +251,7 @@ module.exports = class Renderer {
// For serving static/ files to /
this.useMiddleware(
serveStatic(
resolve(this.options.srcDir, 'static'),
resolve(this.options.srcDir, this.options.staticDir),
this.options.render.static
)
)

View File

@ -1,8 +1,12 @@
import test from 'ava'
import { resolve } from 'path'
import rp from 'request-promise-native'
import { Nuxt, Builder } from '..'
import { interceptLog } from './helpers/console'
const port = 4007
const url = route => 'http://localhost:' + port + route
let nuxt = null
let builder = null
@ -24,14 +28,21 @@ test.before('Init Nuxt.js', async t => {
t.true(logSpy.calledWithMatch('OPEN'))
})
test('/ (custom assets directory)', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('.global-css-selector'))
})
test('/ (custom pages directory)', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom pages directory</h1>'))
})
test('/ (custom assets directory)', async t => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('.global-css-selector'))
test('Check /test.txt with custom static directory', async t => {
const { headers } = await rp(url('/test.txt'), {
resolveWithFullResponse: true
})
t.is(headers['cache-control'], 'public, max-age=0')
})
// Close server and ask nuxt to stop listening to file changes

View File

@ -0,0 +1 @@
A test here :)

View File

@ -1,5 +1,6 @@
module.exports = {
assetsDir: 'custom-assets',
pagesDir: 'custom-pages',
staticDir: 'custom-static',
css: [{ src: '~/custom-assets/app.css' }]
}