Use ES6 syntax with Webpack RC4

This commit is contained in:
Sébastien Chopin 2017-01-11 20:14:59 +01:00
parent ea31b09ee2
commit da63846f55
11 changed files with 83 additions and 75 deletions

View File

@ -1,18 +1,17 @@
'use strict' 'use strict'
const debug = require('debug')('nuxt:build') const debug = require('debug')('nuxt:build')
debug.color = 2 // force green color import _ from 'lodash'
const _ = require('lodash') import co from 'co'
const co = require('co') import chokidar from 'chokidar'
const chokidar = require('chokidar') import fs from 'fs-extra'
const fs = require('fs-extra') import hash from 'hash-sum'
const hash = require('hash-sum') import pify from 'pify'
const pify = require('pify') import webpack from 'webpack'
const webpack = require('webpack') import { createBundleRenderer } from 'vue-server-renderer'
const { createBundleRenderer } = require('vue-server-renderer') import { join, resolve, sep } from 'path'
const { join, resolve, sep } = require('path') import clientWebpackConfig from './webpack/client.config.js'
const clientWebpackConfig = require('./webpack/client.config.js') import serverWebpackConfig from './webpack/server.config.js'
const serverWebpackConfig = require('./webpack/server.config.js')
const remove = pify(fs.remove) const remove = pify(fs.remove)
const readFile = pify(fs.readFile) const readFile = pify(fs.readFile)
const writeFile = pify(fs.writeFile) const writeFile = pify(fs.writeFile)
@ -36,6 +35,8 @@ const r = function () {
args = args.map(normalize) args = args.map(normalize)
return wp(resolve.apply(null, args)) return wp(resolve.apply(null, args))
} }
// force green color
debug.color = 2
const defaults = { const defaults = {
filenames: { filenames: {
@ -73,7 +74,7 @@ const defaultsPostcss = [
}) })
] ]
exports.options = function () { export function options () {
// Defaults build options // Defaults build options
let extraDefaults = {} let extraDefaults = {}
if (this.options.build && !Array.isArray(this.options.build.loaders)) extraDefaults.loaders = defaultsLoaders if (this.options.build && !Array.isArray(this.options.build.loaders)) extraDefaults.loaders = defaultsLoaders
@ -90,7 +91,7 @@ exports.options = function () {
} }
} }
exports.build = function * () { export function * build () {
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (!fs.existsSync(join(this.srcDir, 'pages'))) { if (!fs.existsSync(join(this.srcDir, 'pages'))) {
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) { if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {

View File

@ -1,9 +1,9 @@
'use strict' 'use strict'
const vueLoaderConfig = require('./vue-loader.config') import vueLoaderConfig from './vue-loader.config'
const { defaults } = require('lodash') import { defaults } from 'lodash'
const { join } = require('path') import { join } from 'path'
const { urlJoin } = require('../../utils') import { urlJoin } from '../../utils'
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -13,7 +13,7 @@ const { urlJoin } = require('../../utils')
| webpack config files | webpack config files
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
module.exports = function ({ isClient, isServer }) { export default function ({ isClient, isServer }) {
const nodeModulesDir = join(__dirname, '..', 'node_modules') const nodeModulesDir = join(__dirname, '..', 'node_modules')
let config = { let config = {
devtool: 'source-map', devtool: 'source-map',

View File

@ -1,10 +1,10 @@
'use strict' 'use strict'
const _ = require('lodash') import { each } from 'lodash'
const webpack = require('webpack') import webpack from 'webpack'
const ExtractTextPlugin = require('extract-text-webpack-plugin') import ExtractTextPlugin from 'extract-text-webpack-plugin'
const base = require('./base.config.js') import base from './base.config.js'
const { resolve } = require('path') import { resolve } from 'path'
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -16,7 +16,7 @@ const { resolve } = require('path')
| In production, will generate public/dist/style.css | In production, will generate public/dist/style.css
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
module.exports = function () { export default function () {
let config = base.call(this, { isClient: true }) let config = base.call(this, { isClient: true })
// Entry // Entry
@ -34,7 +34,7 @@ module.exports = function () {
// env object defined in nuxt.config.js // env object defined in nuxt.config.js
let env = {} let env = {}
_.each(this.options.env, (value, key) => { each(this.options.env, (value, key) => {
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value) env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value)
}) })
// Webpack plugins // Webpack plugins

View File

@ -1,23 +1,22 @@
'use strict' 'use strict'
const _ = require('lodash') import webpack from 'webpack'
const webpack = require('webpack') import base from './base.config.js'
const base = require('./base.config.js') import { each, uniq } from 'lodash'
const { uniq } = require('lodash') import { existsSync, readFileSync } from 'fs'
const { existsSync, readFileSync } = require('fs') import { resolve } from 'path'
const { resolve } = require('path')
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Webpack Server Config | Webpack Server Config
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
module.exports = function () { export default function () {
let config = base.call(this, { isServer: true }) let config = base.call(this, { isServer: true })
// env object defined in nuxt.config.js // env object defined in nuxt.config.js
let env = {} let env = {}
_.each(this.options.env, (value, key) => { each(this.options.env, (value, key) => {
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value) env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value)
}) })

View File

@ -1,8 +1,8 @@
'use strict' 'use strict'
const { defaults } = require('lodash') import { defaults } from 'lodash'
module.exports = function ({ isClient }) { export default function ({ isClient }) {
let babelOptions = JSON.stringify(defaults(this.options.build.babel, { let babelOptions = JSON.stringify(defaults(this.options.build.babel, {
plugins: [ plugins: [
'transform-async-to-generator', 'transform-async-to-generator',

View File

@ -1,14 +1,14 @@
'use strict' 'use strict'
const debug = require('debug')('nuxt:generate') const debug = require('debug')('nuxt:generate')
const fs = require('fs-extra') import fs from 'fs-extra'
const co = require('co') import co from 'co'
const pify = require('pify') import pify from 'pify'
const pathToRegexp = require('path-to-regexp') import pathToRegexp from 'path-to-regexp'
const _ = require('lodash') import _ from 'lodash'
const { resolve, join, dirname, sep } = require('path') import { resolve, join, dirname, sep } from 'path'
const { promisifyRouteParams } = require('./utils') import { promisifyRouteParams } from './utils'
const { minify } = require('html-minifier') import { minify } from 'html-minifier'
const copy = pify(fs.copy) const copy = pify(fs.copy)
const remove = pify(fs.remove) const remove = pify(fs.remove)
const writeFile = pify(fs.writeFile) const writeFile = pify(fs.writeFile)
@ -19,7 +19,7 @@ const defaults = {
routeParams: {} routeParams: {}
} }
module.exports = function () { export default function () {
const s = Date.now() const s = Date.now()
/* /*
** Update loaders config to add router.base path ** Update loaders config to add router.base path

View File

@ -1,16 +1,18 @@
const _ = require('lodash') 'use strict'
const co = require('co')
const fs = require('fs-extra') import _ from 'lodash'
const pify = require('pify') import co from 'co'
const ansiHTML = require('ansi-html') import fs from 'fs-extra'
const serialize = require('serialize-javascript') import pify from 'pify'
const Server = require('./server') import ansiHTML from 'ansi-html'
const build = require('./build') import serialize from 'serialize-javascript'
const render = require('./render') import Server from './server'
const generate = require('./generate') import * as build from './build'
const serveStatic = require('serve-static') import * as render from './render'
const { resolve, join } = require('path') import generate from './generate'
const { encodeHtml, setAnsiColors } = require('./utils') import serveStatic from 'serve-static'
import { resolve, join } from 'path'
import { encodeHtml, setAnsiColors } from './utils'
setAnsiColors(ansiHTML) setAnsiColors(ansiHTML)
class Nuxt { class Nuxt {
@ -112,4 +114,4 @@ class Nuxt {
} }
module.exports = Nuxt export default Nuxt

View File

@ -1,10 +1,12 @@
const debug = require('debug')('nuxt:render') 'use strict'
debug.color = 4 // force blue color
const co = require('co')
const { urlJoin } = require('./utils')
const { getContext } = require('./utils')
exports.render = function (req, res) { const debug = require('debug')('nuxt:render')
import co from 'co'
import { urlJoin, getContext } from './utils'
// force blue color
debug.color = 4
export function render (req, res) {
if (!this.renderer && !this.dev) { if (!this.renderer && !this.dev) {
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console
process.exit(1) process.exit(1)
@ -62,7 +64,7 @@ exports.render = function (req, res) {
}) })
} }
exports.renderRoute = function (url, context = {}) { export function renderRoute (url, context = {}) {
debug(`Rendering url ${url}`) debug(`Rendering url ${url}`)
// Add url and isSever to the context // Add url and isSever to the context
context.url = url context.url = url
@ -95,7 +97,7 @@ exports.renderRoute = function (url, context = {}) {
// Function used to do dom checking via jsdom // Function used to do dom checking via jsdom
let jsdom = null let jsdom = null
exports.renderAndGetWindow = function renderAndGetWindow (url) { export function renderAndGetWindow (url) {
/* istanbul ignore if */ /* istanbul ignore if */
if (!jsdom) { if (!jsdom) {
try { try {

View File

@ -30,4 +30,4 @@ class Server {
} }
module.exports = Server export default Server

View File

@ -1,10 +1,14 @@
'use strict' 'use strict'
exports.encodeHtml = (str) => str.replace(/</g, '&lt;').replace(/>/g, '&gt;') export function encodeHtml (str) {
str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
}
exports.getContext = (req, res) => ({ req, res }) export function getContext (req, res) {
return { req, res }
}
exports.setAnsiColors = function (ansiHTML) { export function setAnsiColors (ansiHTML) {
ansiHTML.setColors({ ansiHTML.setColors({
reset: ['efefef', 'a6004c'], reset: ['efefef', 'a6004c'],
darkgrey: '5a012b', darkgrey: '5a012b',
@ -17,17 +21,17 @@ exports.setAnsiColors = function (ansiHTML) {
}) })
} }
exports.waitFor = function * (ms) { export function * waitFor (ms) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
setTimeout(resolve, (ms || 0)) setTimeout(resolve, (ms || 0))
}) })
} }
exports.urlJoin = function () { export function urlJoin () {
return [].slice.call(arguments).join('/').replace(/\/+/g, '/') return [].slice.call(arguments).join('/').replace(/\/+/g, '/')
} }
exports.promisifyRouteParams = function (fn) { export function promisifyRouteParams (fn) {
// If routeParams[route] is an array // If routeParams[route] is an array
if (Array.isArray(fn)) { if (Array.isArray(fn)) {
return Promise.resolve(fn) return Promise.resolve(fn)

View File

@ -81,7 +81,7 @@
"vue-server-renderer": "^2.1.8", "vue-server-renderer": "^2.1.8",
"vue-template-compiler": "^2.1.8", "vue-template-compiler": "^2.1.8",
"vuex": "^2.1.1", "vuex": "^2.1.1",
"webpack": "2.2.0-rc.3", "webpack": "2.2.0-rc.4",
"webpack-dev-middleware": "^1.9.0", "webpack-dev-middleware": "^1.9.0",
"webpack-hot-middleware": "^2.14.0" "webpack-hot-middleware": "^2.14.0"
}, },