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 this.builder = builder
// Set variables // 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.srcBuiltPath = resolve(this.options.buildDir, 'dist')
this.distPath = resolve(this.options.rootDir, this.options.generate.dir) this.distPath = resolve(this.options.rootDir, this.options.generate.dir)
this.distNuxtPath = join( this.distNuxtPath = join(

View File

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

View File

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

View File

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

View File

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

View File

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