From b0f25cbadf03e96f501e2e21c342e658fb1eb92c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 13:03:41 +0330 Subject: [PATCH 01/15] deps: add esm --- package.json | 1 + yarn.lock | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/package.json b/package.json index fbf722a4fe..f352e73ca7 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "css-loader": "^0.28.9", "debug": "^3.1.0", "es6-promise": "^4.2.4", + "esm": "^3.0.6", "etag": "^1.8.1", "extract-text-webpack-plugin": "^4.0.0-beta.0", "file-loader": "^1.1.11", diff --git a/yarn.lock b/yarn.lock index e05331c921..f73ee7e582 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2787,6 +2787,10 @@ eslint@^4.18.2: table "4.0.2" text-table "~0.2.0" +esm@^3.0.6: + version "3.0.6" + resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.6.tgz#0b327fb312e287d9e1a95e728c18b79603fb6d9e" + espower-location-detector@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/espower-location-detector/-/espower-location-detector-1.0.0.tgz#a17b7ecc59d30e179e2bef73fb4137704cb331b5" From 53e98a958c842d33911fa6ef94fa82753f711bfb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 19:42:06 +0330 Subject: [PATCH 02/15] feat: rewrite core to esm --- bin/common/utils.js | 3 +- bin/nuxt-dev | 12 ++-- .../async-component-injection/nuxt.config.js | 2 +- examples/async-data/nuxt.config.js | 2 +- examples/auth-routes/api/index.js | 4 +- examples/auth-routes/nuxt.config.js | 6 +- examples/axios/nuxt.config.js | 2 +- examples/cached-components/nuxt.config.js | 2 +- examples/coffeescript/modules/coffeescript.js | 2 +- examples/coffeescript/nuxt.config.js | 2 +- examples/custom-build/nuxt.config.js | 2 +- examples/custom-loading/nuxt.config.js | 2 +- examples/custom-routes/nuxt.config.js | 2 +- examples/custom-server/nuxt.config.js | 2 +- examples/custom-server/server.js | 6 +- examples/dynamic-components/nuxt.config.js | 2 +- examples/dynamic-layouts/nuxt.config.js | 2 +- examples/global-css/nuxt.config.js | 2 +- examples/i18n/nuxt.config.js | 2 +- examples/layout-transitions/nuxt.config.js | 2 +- examples/markdownit/nuxt.config.js | 2 +- examples/meta-info/nuxt.config.js | 2 +- examples/middleware/nuxt.config.js | 2 +- examples/nested-routes/nuxt.config.js | 2 +- examples/plugins-vendor/nuxt.config.js | 2 +- examples/routes-transitions/nuxt.config.js | 2 +- examples/scroll-behavior/nuxt.config.js | 2 +- examples/spa/nuxt.config.js | 2 +- examples/storybook/nuxt.config.js | 4 +- examples/style-resources/nuxt.config.js | 2 +- examples/tailwindcss/nuxt.config.js | 2 +- examples/tailwindcss/tailwind.js | 2 +- examples/typescript/modules/typescript.js | 2 +- examples/typescript/nuxt.config.js | 2 +- examples/uikit/nuxt.config.js | 2 +- examples/vue-apollo/nuxt.config.js | 2 +- examples/vue-chartjs/nuxt.config.js | 2 +- examples/vue-class-component/nuxt.config.js | 2 +- examples/vuex-persistedstate/nuxt.config.js | 2 +- examples/with-amp/nuxt.config.js | 2 +- examples/with-buefy/nuxt.config.js | 2 +- examples/with-cookies/nuxt.config.js | 2 +- examples/with-element-ui/nuxt.config.js | 2 +- examples/with-feathers/nuxt.config.js | 2 +- examples/with-feathers/src/app.js | 26 ++++--- examples/with-feathers/src/hooks/index.js | 4 +- examples/with-feathers/src/index.js | 3 +- .../with-feathers/src/middleware/index.js | 6 +- examples/with-feathers/src/middleware/nuxt.js | 6 +- .../src/services/authentication/index.js | 6 +- examples/with-feathers/src/services/index.js | 7 +- .../src/services/user/hooks/index.js | 5 +- .../with-feathers/src/services/user/index.js | 12 ++-- examples/with-feathers/test/app.test.js | 8 +-- .../test/services/user/index.test.js | 6 +- examples/with-firebase/nuxt.config.js | 2 +- examples/with-museui/nuxt.config.js | 2 +- examples/with-purgecss/nuxt.config.js | 8 +-- examples/with-purgecss/tailwind.js | 2 +- examples/with-sockets/io/index.js | 9 ++- examples/with-sockets/nuxt.config.js | 2 +- examples/with-sockets/server.js | 13 ++-- examples/with-tape/test/setup.js | 2 +- examples/with-vue-material/nuxt.config.js | 2 +- examples/with-vuetify/nuxt.config.js | 4 +- examples/with-vux/nuxt.config.js | 6 +- lib/builder/builder.js | 44 ++++++------ lib/builder/generator.js | 29 ++------ lib/builder/index.js | 6 +- lib/builder/webpack/base.config.js | 22 +++--- lib/builder/webpack/client.config.js | 24 +++---- lib/builder/webpack/plugins/progress.js | 8 +-- lib/builder/webpack/plugins/vue/client.js | 8 +-- lib/builder/webpack/plugins/vue/server.js | 4 +- lib/builder/webpack/plugins/vue/util.js | 14 ++-- lib/builder/webpack/plugins/warnfix.js | 2 +- lib/builder/webpack/postcss.js | 14 ++-- lib/builder/webpack/server.config.js | 18 ++--- lib/builder/webpack/style-loader.js | 8 +-- lib/builder/webpack/vue-loader.js | 6 +- lib/common/index.js | 7 -- lib/common/module.js | 8 +++ lib/common/options.js | 12 ++-- lib/common/utils.js | 72 +++++++++---------- lib/core/index.js | 13 ++-- lib/core/meta.js | 10 +-- lib/core/middleware/error.js | 8 +-- lib/core/middleware/nuxt.js | 8 +-- lib/core/module.js | 12 ++-- lib/core/nuxt.js | 31 ++++---- lib/core/renderer.js | 42 ++++++----- lib/index.js | 19 +++-- lib/index.mjs | 18 +++++ package.json | 1 + test/basic.generate.test.js | 10 +-- test/custom-dirs.test.js | 7 +- test/debug.test.js | 6 +- test/deprecate.test.js | 7 +- test/error.test.js | 7 +- test/fallback.generate.test.js | 10 +-- test/fixtures/basic/nuxt.config.js | 4 +- test/fixtures/custom-dirs/nuxt.config.js | 2 +- test/fixtures/debug/nuxt.config.js | 2 +- .../modules/deprecated-apis/index.js | 2 +- test/fixtures/deprecate/nuxt.config.js | 2 +- test/fixtures/error/nuxt.config.js | 2 +- test/fixtures/module/modules/basic/index.js | 4 +- test/fixtures/module/modules/empty/index.js | 2 +- test/fixtures/module/modules/hooks/index.js | 2 +- .../module/modules/middleware/index.js | 2 +- .../fixtures/module/modules/middleware/log.js | 2 +- .../module/modules/middleware/midd1.js | 2 +- .../module/modules/middleware/midd2.js | 2 +- .../modules/middleware/use-middleware.js | 2 +- .../fixtures/module/modules/template/index.js | 4 +- test/fixtures/module/nuxt.config.js | 2 +- test/fixtures/spa/nuxt.config.js | 2 +- test/fixtures/ssr/nuxt.config.js | 2 +- test/fixtures/with-config/nuxt.config.js | 4 +- test/helpers/config.js | 11 +++ test/module.test.js | 9 ++- test/spa.test.js | 6 +- test/ssr.test.js | 6 +- test/with-config.test.js | 8 +-- 124 files changed, 427 insertions(+), 426 deletions(-) delete mode 100644 lib/common/index.js create mode 100644 lib/common/module.js create mode 100644 lib/index.mjs create mode 100644 test/helpers/config.js diff --git a/bin/common/utils.js b/bin/common/utils.js index 63aa89dd3d..4f352ff2e6 100644 --- a/bin/common/utils.js +++ b/bin/common/utils.js @@ -1,6 +1,7 @@ const { Utils } = require('../..') const { resolve } = require('path') const { existsSync } = require('fs') +const { requireModule } = require('../../lib/common/module') const getRootDir = argv => resolve(argv._[0] || '.') const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file']) @@ -15,7 +16,7 @@ exports.loadNuxtConfig = argv => { if (existsSync(nuxtConfigFile)) { delete require.cache[nuxtConfigFile] - options = require(nuxtConfigFile) + options = requireModule(nuxtConfigFile) } else if (argv['config-file'] !== 'nuxt.config.js') { Utils.fatalError('Could not load config file: ' + argv['config-file']) } diff --git a/bin/nuxt-dev b/bin/nuxt-dev index e71811cbed..766d734078 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -3,17 +3,15 @@ const defaultsDeep = require('lodash/defaultsDeep') const debug = require('debug')('nuxt:build') -debug.color = 2 // force green color const parseArgs = require('minimist') const chokidar = require('chokidar') -const { version } = require('../package.json') +const { version } = require('../package.json') const { Nuxt, Builder, Utils } = require('..') -const { - loadNuxtConfig, - getLatestHost, - nuxtConfigFile -} = require('./common/utils') + +const { loadNuxtConfig, getLatestHost, nuxtConfigFile } = require('./common/utils') + +debug.color = 2 // force green color const argv = parseArgs(process.argv.slice(2), { alias: { diff --git a/examples/async-component-injection/nuxt.config.js b/examples/async-component-injection/nuxt.config.js index 569cee3b0b..94c5adc998 100644 --- a/examples/async-component-injection/nuxt.config.js +++ b/examples/async-component-injection/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { title: 'Nuxt Blog', meta: [ diff --git a/examples/async-data/nuxt.config.js b/examples/async-data/nuxt.config.js index a54c344d5d..883ef1b53d 100644 --- a/examples/async-data/nuxt.config.js +++ b/examples/async-data/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { loading: { color: '#4FC08D', failedColor: '#bf5050', diff --git a/examples/auth-routes/api/index.js b/examples/auth-routes/api/index.js index 04d6febace..7450bc9e6d 100644 --- a/examples/auth-routes/api/index.js +++ b/examples/auth-routes/api/index.js @@ -1,4 +1,4 @@ -const express = require('express') +import express from 'express' // Create express router const router = express.Router() @@ -30,7 +30,7 @@ router.post('/logout', (req, res) => { }) // Export the server middleware -module.exports = { +export default { path: '/api', handler: router } diff --git a/examples/auth-routes/nuxt.config.js b/examples/auth-routes/nuxt.config.js index 9ae03b9259..e0efd9aebf 100644 --- a/examples/auth-routes/nuxt.config.js +++ b/examples/auth-routes/nuxt.config.js @@ -1,7 +1,7 @@ -const bodyParser = require('body-parser') -const session = require('express-session') +import bodyParser from 'body-parser' +import session from 'express-session' -module.exports = { +export default { head: { title: 'Auth Routes', meta: [ diff --git a/examples/axios/nuxt.config.js b/examples/axios/nuxt.config.js index 19d049afa8..35675b0809 100644 --- a/examples/axios/nuxt.config.js +++ b/examples/axios/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { modules: [ '@nuxtjs/axios', '@nuxtjs/proxy' diff --git a/examples/cached-components/nuxt.config.js b/examples/cached-components/nuxt.config.js index e9f046659f..485ee61e0c 100644 --- a/examples/cached-components/nuxt.config.js +++ b/examples/cached-components/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { render: { bundleRenderer: { cache: require('lru-cache')({ diff --git a/examples/coffeescript/modules/coffeescript.js b/examples/coffeescript/modules/coffeescript.js index a6a0a6c040..daf60cd477 100644 --- a/examples/coffeescript/modules/coffeescript.js +++ b/examples/coffeescript/modules/coffeescript.js @@ -1,4 +1,4 @@ -module.exports = function () { +export default function () { // Add .coffee extension for store, middleware and more this.nuxt.options.extensions.push('coffee') // Extend build diff --git a/examples/coffeescript/nuxt.config.js b/examples/coffeescript/nuxt.config.js index 67afea59ff..3d8378c381 100644 --- a/examples/coffeescript/nuxt.config.js +++ b/examples/coffeescript/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { /* ** Headers of the page */ diff --git a/examples/custom-build/nuxt.config.js b/examples/custom-build/nuxt.config.js index 26ad2681d5..aab9dcac78 100644 --- a/examples/custom-build/nuxt.config.js +++ b/examples/custom-build/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { build: { filenames: { css: 'styles.[chunkhash].css', // default: common.[chunkhash].css diff --git a/examples/custom-loading/nuxt.config.js b/examples/custom-loading/nuxt.config.js index b37861914c..f39ade9d80 100644 --- a/examples/custom-loading/nuxt.config.js +++ b/examples/custom-loading/nuxt.config.js @@ -1,3 +1,3 @@ -module.exports = { +export default { loading: '~/components/loading.vue' } diff --git a/examples/custom-routes/nuxt.config.js b/examples/custom-routes/nuxt.config.js index 631f375658..efba7fa697 100644 --- a/examples/custom-routes/nuxt.config.js +++ b/examples/custom-routes/nuxt.config.js @@ -1,2 +1,2 @@ -module.exports = { +export default { } diff --git a/examples/custom-server/nuxt.config.js b/examples/custom-server/nuxt.config.js index 4ba52ba2c8..b1c6ea436a 100644 --- a/examples/custom-server/nuxt.config.js +++ b/examples/custom-server/nuxt.config.js @@ -1 +1 @@ -module.exports = {} +export default {} diff --git a/examples/custom-server/server.js b/examples/custom-server/server.js index 234b379204..05cd6338cb 100644 --- a/examples/custom-server/server.js +++ b/examples/custom-server/server.js @@ -1,5 +1,7 @@ -const app = require('express')() -const { Nuxt, Builder } = require('nuxt') +import express from 'express' +import { Nuxt, Builder } from 'nuxt' + +const app = express() const host = process.env.HOST || '127.0.0.1' const port = process.env.PORT || 3000 diff --git a/examples/dynamic-components/nuxt.config.js b/examples/dynamic-components/nuxt.config.js index 4cd058a6a5..cc195cee5e 100644 --- a/examples/dynamic-components/nuxt.config.js +++ b/examples/dynamic-components/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { titleTemplate: 'Nuxt.js - Dynamic Components', meta: [ diff --git a/examples/dynamic-layouts/nuxt.config.js b/examples/dynamic-layouts/nuxt.config.js index ab913e20b1..7a6bb49c01 100644 --- a/examples/dynamic-layouts/nuxt.config.js +++ b/examples/dynamic-layouts/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { content: 'width=device-width,initial-scale=1', name: 'viewport' } diff --git a/examples/global-css/nuxt.config.js b/examples/global-css/nuxt.config.js index 3dc2839759..1846b62750 100644 --- a/examples/global-css/nuxt.config.js +++ b/examples/global-css/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { charset: 'utf-8' }, diff --git a/examples/i18n/nuxt.config.js b/examples/i18n/nuxt.config.js index f8964c5bb0..45979caaf2 100644 --- a/examples/i18n/nuxt.config.js +++ b/examples/i18n/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { loading: { color: 'cyan' }, router: { middleware: 'i18n' diff --git a/examples/layout-transitions/nuxt.config.js b/examples/layout-transitions/nuxt.config.js index d002ba62da..a8ac6ebd10 100644 --- a/examples/layout-transitions/nuxt.config.js +++ b/examples/layout-transitions/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { css: ['~/assets/main.css'], layoutTransition: { name: 'layout', diff --git a/examples/markdownit/nuxt.config.js b/examples/markdownit/nuxt.config.js index a1d31f09bd..d0d4440acc 100644 --- a/examples/markdownit/nuxt.config.js +++ b/examples/markdownit/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { modules: [ '@nuxtjs/markdownit' ], diff --git a/examples/meta-info/nuxt.config.js b/examples/meta-info/nuxt.config.js index b7ee0984f2..eb1d1a0265 100644 --- a/examples/meta-info/nuxt.config.js +++ b/examples/meta-info/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { titleTemplate: '%s - Nuxt.js', meta: [ diff --git a/examples/middleware/nuxt.config.js b/examples/middleware/nuxt.config.js index 1e075eff57..ce672e35ab 100644 --- a/examples/middleware/nuxt.config.js +++ b/examples/middleware/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { router: { middleware: ['visits', 'user-agent'] } diff --git a/examples/nested-routes/nuxt.config.js b/examples/nested-routes/nuxt.config.js index 8dd154d104..b72f941c44 100644 --- a/examples/nested-routes/nuxt.config.js +++ b/examples/nested-routes/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { loading: false, head: { meta: [ diff --git a/examples/plugins-vendor/nuxt.config.js b/examples/plugins-vendor/nuxt.config.js index 6487a6dea1..182eba4922 100644 --- a/examples/plugins-vendor/nuxt.config.js +++ b/examples/plugins-vendor/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { plugins: [ // ssr: false to only include it on client-side { src: '~/plugins/vue-notifications.js', ssr: false } diff --git a/examples/routes-transitions/nuxt.config.js b/examples/routes-transitions/nuxt.config.js index eb16f6780d..737f516991 100644 --- a/examples/routes-transitions/nuxt.config.js +++ b/examples/routes-transitions/nuxt.config.js @@ -1,3 +1,3 @@ -module.exports = { +export default { css: ['~/assets/main.css'] } diff --git a/examples/scroll-behavior/nuxt.config.js b/examples/scroll-behavior/nuxt.config.js index eb16f6780d..737f516991 100644 --- a/examples/scroll-behavior/nuxt.config.js +++ b/examples/scroll-behavior/nuxt.config.js @@ -1,3 +1,3 @@ -module.exports = { +export default { css: ['~/assets/main.css'] } diff --git a/examples/spa/nuxt.config.js b/examples/spa/nuxt.config.js index 7108b6c479..84d3d9b1b8 100644 --- a/examples/spa/nuxt.config.js +++ b/examples/spa/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { /* ** Single Page Application mode ** Means no SSR diff --git a/examples/storybook/nuxt.config.js b/examples/storybook/nuxt.config.js index 24a6bfbb03..b54202b6a6 100644 --- a/examples/storybook/nuxt.config.js +++ b/examples/storybook/nuxt.config.js @@ -1,6 +1,6 @@ -const pkg = require('./package') +import pkg from './package' -module.exports = { +export default { mode: 'universal', /* diff --git a/examples/style-resources/nuxt.config.js b/examples/style-resources/nuxt.config.js index 32611aab6f..c3eb94b569 100644 --- a/examples/style-resources/nuxt.config.js +++ b/examples/style-resources/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { build: { // You cannot use ~/ or @/ here since it's a Webpack plugin styleResources: { diff --git a/examples/tailwindcss/nuxt.config.js b/examples/tailwindcss/nuxt.config.js index 7550d05a97..69cf86bd22 100644 --- a/examples/tailwindcss/nuxt.config.js +++ b/examples/tailwindcss/nuxt.config.js @@ -1,3 +1,3 @@ -module.exports = { +export default { css: ['~/assets/css/tailwind.css'] } diff --git a/examples/tailwindcss/tailwind.js b/examples/tailwindcss/tailwind.js index 9a4af76aba..040b3a7fbe 100644 --- a/examples/tailwindcss/tailwind.js +++ b/examples/tailwindcss/tailwind.js @@ -127,7 +127,7 @@ var colors = { 'pink-lightest': '#ffebef' } -module.exports = { +export default { /* |----------------------------------------------------------------------------- diff --git a/examples/typescript/modules/typescript.js b/examples/typescript/modules/typescript.js index 3c430b165b..6e672db8cb 100644 --- a/examples/typescript/modules/typescript.js +++ b/examples/typescript/modules/typescript.js @@ -1,4 +1,4 @@ -module.exports = function () { +export default function () { // Add .ts extension for store, middleware and more this.nuxt.options.extensions.push('ts') // Extend build diff --git a/examples/typescript/nuxt.config.js b/examples/typescript/nuxt.config.js index 6bba75ad6c..682c251744 100644 --- a/examples/typescript/nuxt.config.js +++ b/examples/typescript/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { env: { baseUrl: process.env.BASE_URL || 'http://localhost:3000' }, diff --git a/examples/uikit/nuxt.config.js b/examples/uikit/nuxt.config.js index 6f916f758e..33ff283acc 100644 --- a/examples/uikit/nuxt.config.js +++ b/examples/uikit/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { css: ['uikit/dist/css/uikit.css'], plugins: [ { src: '~/plugins/uikit.js', ssr: false } diff --git a/examples/vue-apollo/nuxt.config.js b/examples/vue-apollo/nuxt.config.js index bdf96f2a93..1530134236 100644 --- a/examples/vue-apollo/nuxt.config.js +++ b/examples/vue-apollo/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { modules: ['@nuxtjs/apollo'], apollo: { networkInterfaces: { diff --git a/examples/vue-chartjs/nuxt.config.js b/examples/vue-chartjs/nuxt.config.js index 6bb371ac07..1b67d5a07b 100644 --- a/examples/vue-chartjs/nuxt.config.js +++ b/examples/vue-chartjs/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { title: 'Nuxt.js + Vue-ChartJS', meta: [ diff --git a/examples/vue-class-component/nuxt.config.js b/examples/vue-class-component/nuxt.config.js index 4d32a9ca0a..1319871f8b 100644 --- a/examples/vue-class-component/nuxt.config.js +++ b/examples/vue-class-component/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { build: { babel: { plugins: ['transform-decorators-legacy', 'transform-class-properties'] diff --git a/examples/vuex-persistedstate/nuxt.config.js b/examples/vuex-persistedstate/nuxt.config.js index 274248d91b..e5e23be598 100644 --- a/examples/vuex-persistedstate/nuxt.config.js +++ b/examples/vuex-persistedstate/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { /* ** We set `spa` mode to have only client-side rendering */ diff --git a/examples/with-amp/nuxt.config.js b/examples/with-amp/nuxt.config.js index 6ae265293c..0a706c0042 100644 --- a/examples/with-amp/nuxt.config.js +++ b/examples/with-amp/nuxt.config.js @@ -8,7 +8,7 @@ const modifyHtml = (html) => { html = html.replace('', ampScript + '') return html } -module.exports = { +export default { head: { meta: [ { charset: 'utf-8' }, diff --git a/examples/with-buefy/nuxt.config.js b/examples/with-buefy/nuxt.config.js index 8b53395980..06cc234cad 100644 --- a/examples/with-buefy/nuxt.config.js +++ b/examples/with-buefy/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { /* ** Customize the progress bar color */ diff --git a/examples/with-cookies/nuxt.config.js b/examples/with-cookies/nuxt.config.js index f5d66e70ce..339699b0aa 100644 --- a/examples/with-cookies/nuxt.config.js +++ b/examples/with-cookies/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { title: 'Nuxt-Cookies', meta: [ diff --git a/examples/with-element-ui/nuxt.config.js b/examples/with-element-ui/nuxt.config.js index e53632dade..29fd3a0b7e 100644 --- a/examples/with-element-ui/nuxt.config.js +++ b/examples/with-element-ui/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { /* ** Global CSS */ diff --git a/examples/with-feathers/nuxt.config.js b/examples/with-feathers/nuxt.config.js index 1fec96a57b..5fadcc875c 100644 --- a/examples/with-feathers/nuxt.config.js +++ b/examples/with-feathers/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { loading: { color: 'purple' } diff --git a/examples/with-feathers/src/app.js b/examples/with-feathers/src/app.js index 6f0e8103dd..c8d255a220 100644 --- a/examples/with-feathers/src/app.js +++ b/examples/with-feathers/src/app.js @@ -1,16 +1,14 @@ -'use strict' - -const path = require('path') -const compress = require('compression') -const cors = require('cors') -const feathers = require('feathers') -const configuration = require('feathers-configuration') -const hooks = require('feathers-hooks') -const rest = require('feathers-rest') -const bodyParser = require('body-parser') -const socketio = require('feathers-socketio') -const middleware = require('./middleware') -const services = require('./services') +import path from 'path' +import compress from 'compression' +import cors from 'cors' +import feathers from 'feathers' +import configuration from 'feathers-configuration' +import hooks from 'feathers-hooks' +import rest from 'feathers-rest' +import bodyParser from 'body-parser' +import socketio from 'feathers-socketio' +import middleware from './middleware' +import services from './services' const app = feathers() @@ -27,4 +25,4 @@ app.use(compress()) .configure(services) .configure(middleware) -module.exports = app +export default app diff --git a/examples/with-feathers/src/hooks/index.js b/examples/with-feathers/src/hooks/index.js index 5489242974..9f64020c75 100644 --- a/examples/with-feathers/src/hooks/index.js +++ b/examples/with-feathers/src/hooks/index.js @@ -1,12 +1,10 @@ -'use strict' - // Add any common hooks you want to share across services in here. // // Below is an example of how a hook is written and exported. Please // see http://docs.feathersjs.com/hooks/readme.html for more details // on hooks. -exports.myHook = function (options) { +export function myHook(options) { return function (hook) { console.log('My custom global hook ran. Feathers is awesome!') // eslint-disable-line no-console } diff --git a/examples/with-feathers/src/index.js b/examples/with-feathers/src/index.js index 011d14d1cb..07d0202e1b 100644 --- a/examples/with-feathers/src/index.js +++ b/examples/with-feathers/src/index.js @@ -1,6 +1,5 @@ -'use strict' +import app from './app' -const app = require('./app') const port = app.get('port') process.on('nuxt:build:done', (err) => { diff --git a/examples/with-feathers/src/middleware/index.js b/examples/with-feathers/src/middleware/index.js index c572b6b5cd..64a383c6e1 100644 --- a/examples/with-feathers/src/middleware/index.js +++ b/examples/with-feathers/src/middleware/index.js @@ -1,8 +1,6 @@ -'use strict' +import nuxt from './nuxt' -const nuxt = require('./nuxt') - -module.exports = function () { +export default function () { // Add your custom middleware here. Remember, that // just like Express the order matters, so error // handling middleware should go last. diff --git a/examples/with-feathers/src/middleware/nuxt.js b/examples/with-feathers/src/middleware/nuxt.js index c389b9f259..91f809aa6e 100644 --- a/examples/with-feathers/src/middleware/nuxt.js +++ b/examples/with-feathers/src/middleware/nuxt.js @@ -1,5 +1,5 @@ -const resolve = require('path').resolve -const { Nuxt, Builder } = require('nuxt') +import { resolve } from 'path' +import { Nuxt, Builder } from 'nuxt' // Setup nuxt.js let config = {} @@ -18,6 +18,6 @@ if (config.dev) { } // Add nuxt.js middleware -module.exports = function (req, res) { +export default function (req, res) { nuxt.render(req, res) } diff --git a/examples/with-feathers/src/services/authentication/index.js b/examples/with-feathers/src/services/authentication/index.js index 84665c7e9b..453db60b8b 100644 --- a/examples/with-feathers/src/services/authentication/index.js +++ b/examples/with-feathers/src/services/authentication/index.js @@ -1,8 +1,6 @@ -'use strict' +import authentication from 'feathers-authentication' -const authentication = require('feathers-authentication') - -module.exports = function () { +export default function () { const app = this let config = app.get('auth') diff --git a/examples/with-feathers/src/services/index.js b/examples/with-feathers/src/services/index.js index b481d8ebf8..a09ac9b374 100644 --- a/examples/with-feathers/src/services/index.js +++ b/examples/with-feathers/src/services/index.js @@ -1,8 +1,7 @@ -'use strict' -const authentication = require('./authentication') -const user = require('./user') +import authentication from './authentication' +import user from './user' -module.exports = function () { +export default function () { const app = this app.configure(authentication) diff --git a/examples/with-feathers/src/services/user/hooks/index.js b/examples/with-feathers/src/services/user/hooks/index.js index 48bc62acd9..3ecc6e074d 100644 --- a/examples/with-feathers/src/services/user/hooks/index.js +++ b/examples/with-feathers/src/services/user/hooks/index.js @@ -1,8 +1,7 @@ -'use strict' +import hooks from 'feathers-hooks' +import { hooks as auth } from 'feathers-authentication' require('../../../hooks') -const hooks = require('feathers-hooks') -const auth = require('feathers-authentication').hooks exports.before = { all: [], diff --git a/examples/with-feathers/src/services/user/index.js b/examples/with-feathers/src/services/user/index.js index 2ecf62873c..f84ec580a8 100644 --- a/examples/with-feathers/src/services/user/index.js +++ b/examples/with-feathers/src/services/user/index.js @@ -1,11 +1,9 @@ -'use strict' +import path from 'path' +import NeDB from 'nedb' +import service from 'feathers-nedb' +import hooks from './hooks' -const path = require('path') -const NeDB = require('nedb') -const service = require('feathers-nedb') -const hooks = require('./hooks') - -module.exports = function () { +export default function () { const app = this const db = new NeDB({ diff --git a/examples/with-feathers/test/app.test.js b/examples/with-feathers/test/app.test.js index 60e1e30d63..f7f621363b 100644 --- a/examples/with-feathers/test/app.test.js +++ b/examples/with-feathers/test/app.test.js @@ -1,8 +1,6 @@ -'use strict' - -const assert = require('assert') -const request = require('request') -const app = require('../src/app') +import assert from 'assert' +import request from 'request' +import app from '../src/app' describe('Feathers application tests', function () { before(function (done) { diff --git a/examples/with-feathers/test/services/user/index.test.js b/examples/with-feathers/test/services/user/index.test.js index 2695b85eb1..d6b4df6264 100644 --- a/examples/with-feathers/test/services/user/index.test.js +++ b/examples/with-feathers/test/services/user/index.test.js @@ -1,7 +1,5 @@ -'use strict' - -const assert = require('assert') -const app = require('../../../src/app') +import assert from 'assert' +import app from '../../../src/app' describe('user service', function () { it('registered the users service', () => { diff --git a/examples/with-firebase/nuxt.config.js b/examples/with-firebase/nuxt.config.js index 3ea4943aba..ddb39cc360 100644 --- a/examples/with-firebase/nuxt.config.js +++ b/examples/with-firebase/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { charset: 'utf-8' }, diff --git a/examples/with-museui/nuxt.config.js b/examples/with-museui/nuxt.config.js index 55a86f08c1..1c13f5c91b 100644 --- a/examples/with-museui/nuxt.config.js +++ b/examples/with-museui/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { diff --git a/examples/with-purgecss/nuxt.config.js b/examples/with-purgecss/nuxt.config.js index 0c5335c106..86da6d8683 100644 --- a/examples/with-purgecss/nuxt.config.js +++ b/examples/with-purgecss/nuxt.config.js @@ -1,6 +1,6 @@ -const path = require('path') -const PurgecssPlugin = require('purgecss-webpack-plugin') -const glob = require('glob-all') +import path from 'path' +import PurgecssPlugin from 'purgecss-webpack-plugin' +import glob from 'glob-all' class TailwindExtractor { static extract(content) { @@ -8,7 +8,7 @@ class TailwindExtractor { } } -module.exports = { +export default { build: { extractCSS: true, postcss: [ diff --git a/examples/with-purgecss/tailwind.js b/examples/with-purgecss/tailwind.js index 9a4af76aba..040b3a7fbe 100644 --- a/examples/with-purgecss/tailwind.js +++ b/examples/with-purgecss/tailwind.js @@ -127,7 +127,7 @@ var colors = { 'pink-lightest': '#ffebef' } -module.exports = { +export default { /* |----------------------------------------------------------------------------- diff --git a/examples/with-sockets/io/index.js b/examples/with-sockets/io/index.js index b9bf2364e0..636167e435 100644 --- a/examples/with-sockets/io/index.js +++ b/examples/with-sockets/io/index.js @@ -1,7 +1,10 @@ -module.exports = function () { - const server = require('http').createServer(this.nuxt.renderer.app) - const io = require('socket.io')(server) +import http from 'http' +import socketIO from 'socket.io' +const server = http.createServer(this.nuxt.renderer.app) +const io = socketIO(server) + +export default function () { // overwrite nuxt.listen() this.nuxt.listen = (port, host) => new Promise((resolve) => server.listen(port || 3000, host || 'localhost', resolve)) // close this server on 'close' event diff --git a/examples/with-sockets/nuxt.config.js b/examples/with-sockets/nuxt.config.js index 34e2ec30a1..538c237f8f 100644 --- a/examples/with-sockets/nuxt.config.js +++ b/examples/with-sockets/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { charset: 'utf-8' }, diff --git a/examples/with-sockets/server.js b/examples/with-sockets/server.js index 1cd396440d..017d62a609 100644 --- a/examples/with-sockets/server.js +++ b/examples/with-sockets/server.js @@ -1,10 +1,15 @@ -const { Nuxt, Builder } = require('nuxt') -const app = require('express')() -const server = require('http').createServer(app) -const io = require('socket.io')(server) +import { Nuxt, Builder } from 'nuxt' +import express from 'express' +import http from 'http' +import SocketIO from 'socket.io' + const port = process.env.PORT || 3000 const isProd = process.env.NODE_ENV === 'production' +const app = express() +const server = http.createServer(app) +const io = SocketIO(server) + // We instantiate Nuxt.js with the options let config = require('./nuxt.config.js') config.dev = !isProd diff --git a/examples/with-tape/test/setup.js b/examples/with-tape/test/setup.js index 69cf3aa353..c193cfd102 100644 --- a/examples/with-tape/test/setup.js +++ b/examples/with-tape/test/setup.js @@ -1,4 +1,4 @@ -const hooks = require('require-extension-hooks') +import hooks from 'require-extension-hooks' // Setup browser environment require('browser-env')() diff --git a/examples/with-vue-material/nuxt.config.js b/examples/with-vue-material/nuxt.config.js index 38d84fe8c0..c62c272b75 100644 --- a/examples/with-vue-material/nuxt.config.js +++ b/examples/with-vue-material/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { head: { meta: [ { diff --git a/examples/with-vuetify/nuxt.config.js b/examples/with-vuetify/nuxt.config.js index 1f03dd3738..6164300a33 100644 --- a/examples/with-vuetify/nuxt.config.js +++ b/examples/with-vuetify/nuxt.config.js @@ -1,6 +1,6 @@ -const nodeExternals = require('webpack-node-externals') +import nodeExternals from 'webpack-node-externals' -module.exports = { +export default { /* ** Head elements ** Add Roboto font and Material Icons diff --git a/examples/with-vux/nuxt.config.js b/examples/with-vux/nuxt.config.js index ff07ba04b9..a2133ab74a 100644 --- a/examples/with-vux/nuxt.config.js +++ b/examples/with-vux/nuxt.config.js @@ -1,7 +1,7 @@ -const vuxLoader = require('vux-loader') -const path = require('path') +import vuxLoader from 'vux-loader' +import path from 'path' -module.exports = { +export default { head: { meta: [ { charset: 'utf-8' }, diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 9bcd83499f..f29a3d5a38 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -1,29 +1,29 @@ -const { promisify } = require('util') -const _ = require('lodash') -const chokidar = require('chokidar') -const { remove, readFile, writeFile, mkdirp, existsSync } = require('fs-extra') -const fs = require('fs') -const hash = require('hash-sum') -const webpack = require('webpack') -const serialize = require('serialize-javascript') -const { join, resolve, basename, extname, dirname } = require('path') -const MFS = require('memory-fs') -const webpackDevMiddleware = require('webpack-dev-middleware') -const webpackHotMiddleware = require('webpack-hot-middleware') -const Debug = require('debug') -const Glob = require('glob') -const { r, wp, wChunk, createRoutes, parallel, relativeTo, waitFor, createSpinner } = require('../common/utils') -const { Options } = require('../common') -const clientWebpackConfig = require('./webpack/client.config.js') -const serverWebpackConfig = require('./webpack/server.config.js') -const upath = require('upath') +import { promisify } from 'util' +import _ from 'lodash' +import chokidar from 'chokidar' +import { remove, readFile, writeFile, mkdirp, existsSync } from 'fs-extra' +import fs from 'fs' +import hash from 'hash-sum' +import webpack from 'webpack' +import serialize from 'serialize-javascript' +import { join, resolve, basename, extname, dirname } from 'path' +import MFS from 'memory-fs' +import webpackDevMiddleware from 'webpack-dev-middleware' +import webpackHotMiddleware from 'webpack-hot-middleware' +import Debug from 'debug' +import Glob from 'glob' +import { r, wp, wChunk, createRoutes, parallel, relativeTo, waitFor, createSpinner } from '../common/utils' +import Options from '../common/options' +import clientWebpackConfig from './webpack/client.config.js' +import serverWebpackConfig from './webpack/server.config.js' +import upath from 'upath' const debug = Debug('nuxt:build') debug.color = 2 // Force green color const glob = promisify(Glob) -module.exports = class Builder { +export default class Builder { constructor(nuxt) { this.nuxt = nuxt this.isStatic = false // Flag to know if the build is for a generated app @@ -156,7 +156,7 @@ module.exports = class Builder { if (!options.babelrc && !options.presets) { options.presets = [ [ - require.resolve('babel-preset-vue-app'), + 'babel-preset-vue-app', { targets: isServer ? { node: '8.0.0' } : { ie: 9, uglify: true } } @@ -648,7 +648,7 @@ module.exports = class Builder { const options = _.omit(this.options, Options.unsafeKeys) await writeFile( config, - `module.exports = ${JSON.stringify(options, null, ' ')}`, + `export default ${JSON.stringify(options, null, ' ')}`, 'utf8' ) } diff --git a/lib/builder/generator.js b/lib/builder/generator.js index f01bd6522b..758d8e6b77 100644 --- a/lib/builder/generator.js +++ b/lib/builder/generator.js @@ -1,26 +1,11 @@ -const { - copy, - remove, - writeFile, - mkdirp, - removeSync, - existsSync -} = require('fs-extra') -const _ = require('lodash') -const { resolve, join, dirname, sep } = require('path') -const { minify } = require('html-minifier') -const Chalk = require('chalk') -const { printWarn, createSpinner } = require('../common/utils') +import _ from 'lodash' +import { resolve, join, dirname, sep } from 'path' +import { minify } from 'html-minifier' +import Chalk from 'chalk' +import { copy, remove, writeFile, mkdirp, removeSync, existsSync } from 'fs-extra' +import { isUrl, promisifyRoute, waitFor, flatRoutes, pe, printWarn, createSpinner } from '../common/utils' -const { - isUrl, - promisifyRoute, - waitFor, - flatRoutes, - pe -} = require('../common/utils') - -module.exports = class Generator { +export default class Generator { constructor(nuxt, builder) { this.nuxt = nuxt this.options = nuxt.options diff --git a/lib/builder/index.js b/lib/builder/index.js index fc3309238b..34547d0c2a 100644 --- a/lib/builder/index.js +++ b/lib/builder/index.js @@ -1,7 +1,7 @@ -const Builder = require('./builder') -const Generator = require('./generator') +import Builder from './builder' +import Generator from './generator' -module.exports = { +export default { Builder, Generator } diff --git a/lib/builder/webpack/base.config.js b/lib/builder/webpack/base.config.js index 40747820b5..b60e72eaaa 100644 --- a/lib/builder/webpack/base.config.js +++ b/lib/builder/webpack/base.config.js @@ -1,16 +1,16 @@ -const ExtractTextPlugin = require('extract-text-webpack-plugin') -const TimeFixPlugin = require('time-fix-plugin') -const WarnFixPlugin = require('./plugins/warnfix') -const ProgressPlugin = require('./plugins/progress') +import ExtractTextPlugin from 'extract-text-webpack-plugin' +import TimeFixPlugin from 'time-fix-plugin' +import WarnFixPlugin from './plugins/warnfix' +import ProgressPlugin from './plugins/progress' -const webpack = require('webpack') -const { cloneDeep } = require('lodash') -const { join, resolve } = require('path') +import webpack from 'webpack' +import { cloneDeep } from 'lodash' +import { join, resolve } from 'path' -const { isUrl, urlJoin } = require('../../common/utils') +import { isUrl, urlJoin } from '../../common/utils' -const vueLoader = require('./vue-loader') -const styleLoader = require('./style-loader') +import vueLoader from './vue-loader' +import styleLoader from './style-loader' /* |-------------------------------------------------------------------------- @@ -20,7 +20,7 @@ const styleLoader = require('./style-loader') | webpack config files |-------------------------------------------------------------------------- */ -module.exports = function webpackBaseConfig({ name, isServer }) { +export default function webpackBaseConfig({ name, isServer }) { // Prioritize nested node_modules in webpack search path (#2558) const webpackModulesDir = ['node_modules'].concat(this.options.modulesDir) diff --git a/lib/builder/webpack/client.config.js b/lib/builder/webpack/client.config.js index 004e9628f4..4163a21e15 100644 --- a/lib/builder/webpack/client.config.js +++ b/lib/builder/webpack/client.config.js @@ -1,19 +1,19 @@ -const { each } = require('lodash') -const webpack = require('webpack') -// const VueSSRClientPlugin = require('vue-server-renderer/client-plugin') -const VueSSRClientPlugin = require('./plugins/vue/client') -const HTMLPlugin = require('html-webpack-plugin') -const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin') -const StylishPlugin = require('webpack-stylish') -const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer') -const { resolve } = require('path') -const Debug = require('debug') -const base = require('./base.config.js') +import { each } from 'lodash' +import webpack from 'webpack' +// import VueSSRClientPlugin from 'vue-server-renderer/client-plugin' +import VueSSRClientPlugin from './plugins/vue/client' +import HTMLPlugin from 'html-webpack-plugin' +import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin' +import StylishPlugin from 'webpack-stylish' +import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' +import { resolve } from 'path' +import Debug from 'debug' +import base from './base.config.js' const debug = Debug('nuxt:build') debug.color = 2 // Force green color -module.exports = function webpackClientConfig() { +export default function webpackClientConfig() { let config = base.call(this, { name: 'client', isServer: false }) // Entry points diff --git a/lib/builder/webpack/plugins/progress.js b/lib/builder/webpack/plugins/progress.js index 48e47645ec..8cb8361706 100644 --- a/lib/builder/webpack/plugins/progress.js +++ b/lib/builder/webpack/plugins/progress.js @@ -1,12 +1,12 @@ -const webpack = require('webpack') -const chalk = require('chalk') -const _ = require('lodash') +import webpack from 'webpack' +import chalk from 'chalk' +import _ from 'lodash' const sharedState = {} const BLOCK_CHAR = '█' -module.exports = class ProgressPlugin extends webpack.ProgressPlugin { +export default class ProgressPlugin extends webpack.ProgressPlugin { constructor(options) { super(options) diff --git a/lib/builder/webpack/plugins/vue/client.js b/lib/builder/webpack/plugins/vue/client.js index 76fcad5ed2..ffc8a8e0d5 100644 --- a/lib/builder/webpack/plugins/vue/client.js +++ b/lib/builder/webpack/plugins/vue/client.js @@ -1,8 +1,8 @@ -const hash = require('hash-sum') -const uniq = require('lodash.uniq') -const { isJS, onEmit } = require('./util') +import hash from 'hash-sum' +import uniq from 'lodash.uniq' +import { isJS, onEmit } from './util' -module.exports = class VueSSRClientPlugin { +export default class VueSSRClientPlugin { constructor(options = {}) { this.options = Object.assign({ filename: 'vue-ssr-client-manifest.json' diff --git a/lib/builder/webpack/plugins/vue/server.js b/lib/builder/webpack/plugins/vue/server.js index e3cc74940a..7c539d5399 100644 --- a/lib/builder/webpack/plugins/vue/server.js +++ b/lib/builder/webpack/plugins/vue/server.js @@ -1,6 +1,6 @@ -const { validate, isJS, onEmit } = require('./util') +import { validate, isJS, onEmit } from './util' -module.exports = class VueSSRServerPlugin { +export default class VueSSRServerPlugin { constructor(options = {}) { this.options = Object.assign({ filename: 'vue-ssr-server-bundle.json' diff --git a/lib/builder/webpack/plugins/vue/util.js b/lib/builder/webpack/plugins/vue/util.js index 0f5377946a..fb58bc0bac 100644 --- a/lib/builder/webpack/plugins/vue/util.js +++ b/lib/builder/webpack/plugins/vue/util.js @@ -1,10 +1,10 @@ -const { red, yellow } = require('chalk') +import chalk from 'chalk' const prefix = `[vue-server-renderer-webpack-plugin]` -const warn = exports.warn = msg => console.error(red(`${prefix} ${msg}\n`)) // eslint-disable-line no-console -const tip = exports.tip = msg => console.log(yellow(`${prefix} ${msg}\n`)) // eslint-disable-line no-console +export const warn = msg => console.error(chalk.red(`${prefix} ${msg}\n`)) // eslint-disable-line no-console +export const tip = msg => console.log(chalk.yellow(`${prefix} ${msg}\n`)) // eslint-disable-line no-console -exports.validate = compiler => { +export const validate = compiler => { if (compiler.options.target !== 'node') { warn('webpack config `target` should be "node".') } @@ -21,7 +21,7 @@ exports.validate = compiler => { } } -exports.onEmit = (compiler, name, hook) => { +export const onEmit = (compiler, name, hook) => { if (compiler.hooks) { // Webpack >= 4.0.0 compiler.hooks.emit.tap(name, @@ -38,6 +38,6 @@ exports.onEmit = (compiler, name, hook) => { } } -exports.isJS = (file) => /\.js(\?[^.]+)?$/.test(file) +export const isJS = (file) => /\.js(\?[^.]+)?$/.test(file) -exports.isCSS = (file) => /\.css(\?[^.]+)?$/.test(file) +export const isCSS = (file) => /\.css(\?[^.]+)?$/.test(file) diff --git a/lib/builder/webpack/plugins/warnfix.js b/lib/builder/webpack/plugins/warnfix.js index c15ea0034f..cb74bd0062 100644 --- a/lib/builder/webpack/plugins/warnfix.js +++ b/lib/builder/webpack/plugins/warnfix.js @@ -1,4 +1,4 @@ -module.exports = class WarnFixPlugin { +export default class WarnFixPlugin { apply(compiler) /* istanbul ignore next */ { compiler.hooks.done.tap('warnfix-plugin', stats => { stats.compilation.warnings = stats.compilation.warnings.filter(warn => { diff --git a/lib/builder/webpack/postcss.js b/lib/builder/webpack/postcss.js index 8acf8b3cf5..a8488d996a 100644 --- a/lib/builder/webpack/postcss.js +++ b/lib/builder/webpack/postcss.js @@ -1,10 +1,10 @@ -const { existsSync } = require('fs') -const { resolve, join } = require('path') -const { cloneDeep } = require('lodash') -const { isPureObject } = require('../../common/utils') -const createResolver = require('postcss-import-resolver') +import { existsSync } from 'fs' +import { resolve, join } from 'path' +import { cloneDeep } from 'lodash' +import { isPureObject } from '../../common/utils' +import createResolver from 'postcss-import-resolver' -module.exports = function postcssConfig() { +export default function postcssConfig() { let config = cloneDeep(this.options.build.postcss) /* istanbul ignore if */ @@ -78,7 +78,7 @@ module.exports = function postcssConfig() { if (isPureObject(config) && isPureObject(config.plugins)) { config.plugins = Object.keys(config.plugins) .map(p => { - const plugin = require(this.nuxt.resolvePath(p)) + const plugin = this.nuxt.requireModule(p) const opts = config.plugins[p] if (opts === false) return // Disabled const instance = plugin(opts) diff --git a/lib/builder/webpack/server.config.js b/lib/builder/webpack/server.config.js index 25f0017cd0..d91b146828 100644 --- a/lib/builder/webpack/server.config.js +++ b/lib/builder/webpack/server.config.js @@ -1,18 +1,18 @@ -const webpack = require('webpack') -// const VueSSRServerPlugin = require('vue-server-renderer/server-plugin') -const VueSSRServerPlugin = require('./plugins/vue/server') -const nodeExternals = require('webpack-node-externals') -const { each } = require('lodash') -const { resolve } = require('path') -const { existsSync } = require('fs') -const base = require('./base.config.js') +import webpack from 'webpack' +// import VueSSRServerPlugin from 'vue-server-renderer/server-plugin' +import VueSSRServerPlugin from './plugins/vue/server' +import nodeExternals from 'webpack-node-externals' +import { each } from 'lodash' +import { resolve } from 'path' +import { existsSync } from 'fs' +import base from './base.config.js' /* |-------------------------------------------------------------------------- | Webpack Server Config |-------------------------------------------------------------------------- */ -module.exports = function webpackServerConfig() { +export default function webpackServerConfig() { let config = base.call(this, { name: 'server', isServer: true }) // Env object defined in nuxt.config.js diff --git a/lib/builder/webpack/style-loader.js b/lib/builder/webpack/style-loader.js index ba3a463a12..9c5e2b500e 100644 --- a/lib/builder/webpack/style-loader.js +++ b/lib/builder/webpack/style-loader.js @@ -1,8 +1,8 @@ -const ExtractTextPlugin = require('extract-text-webpack-plugin') -const { join } = require('path') -const postcssConfig = require('./postcss') +import ExtractTextPlugin from 'extract-text-webpack-plugin' +import { join } from 'path' +import postcssConfig from './postcss' -module.exports = function styleLoader(ext, loaders = [], isVueLoader = false) { +export default function styleLoader(ext, loaders = [], isVueLoader = false) { const sourceMap = Boolean(this.options.build.cssSourceMap) // Normalize loaders diff --git a/lib/builder/webpack/vue-loader.js b/lib/builder/webpack/vue-loader.js index e31fae130d..f426c7df17 100644 --- a/lib/builder/webpack/vue-loader.js +++ b/lib/builder/webpack/vue-loader.js @@ -1,7 +1,7 @@ -const postcssConfig = require('./postcss') -const styleLoader = require('./style-loader') +import postcssConfig from './postcss' +import styleLoader from './style-loader' -module.exports = function vueLoader({ isServer }) { +export default function vueLoader({ isServer }) { // https://vue-loader.vuejs.org/en const config = { postcss: postcssConfig.call(this), diff --git a/lib/common/index.js b/lib/common/index.js deleted file mode 100644 index 3a241c30d1..0000000000 --- a/lib/common/index.js +++ /dev/null @@ -1,7 +0,0 @@ -const Utils = require('./utils') -const Options = require('./options') - -module.exports = { - Utils, - Options -} diff --git a/lib/common/module.js b/lib/common/module.js new file mode 100644 index 0000000000..8376ee110b --- /dev/null +++ b/lib/common/module.js @@ -0,0 +1,8 @@ +const esm = require('esm') + +const _esm = esm(module, {}) + +exports.requireModule = function requireModule() { + const m = _esm.apply(this, arguments) + return (m && m.default) || m +} diff --git a/lib/common/options.js b/lib/common/options.js index c91d333183..d02782fc83 100644 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -1,15 +1,15 @@ -const _ = require('lodash') -const Debug = require('debug') -const { join, resolve } = require('path') -const { existsSync, readdirSync } = require('fs') -const { isUrl, isPureObject } = require('../common/utils') +import _ from 'lodash' +import Debug from 'debug' +import { join, resolve } from 'path' +import { existsSync, readdirSync } from 'fs' +import { isUrl, isPureObject } from '../common/utils' const debug = Debug('nuxt:build') debug.color = 2 // Force green color const Options = {} -module.exports = Options +export default Options Options.from = function (_options) { // Clone options to prevent unwanted side-effects diff --git a/lib/common/utils.js b/lib/common/utils.js index 07a7507b65..e61f8e0087 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -1,35 +1,35 @@ -const { resolve, relative, sep } = require('path') -const _ = require('lodash') -const PrettyError = require('pretty-error') -const Chalk = require('chalk') -const ORA = require('ora') +import { resolve, relative, sep } from 'path' +import _ from 'lodash' +import PrettyError from 'pretty-error' +import Chalk from 'chalk' +import ORA from 'ora' -exports.pe = new PrettyError() +export const pe = new PrettyError() -exports.printWarn = function (msg, from) { +export const printWarn = function (msg, from) { /* eslint-disable no-console */ const fromStr = from ? Chalk.yellow(` ${from}\n\n`) : ' ' console.warn('\n' + Chalk.bgYellow.black(' WARN ') + fromStr + msg + '\n') } -exports.renderError = function (_error, from) { - const errStr = exports.pe.render(_error) +export const renderError = function (_error, from) { + const errStr = pe.render(_error) const fromStr = from ? Chalk.red(` ${from}`) : '' return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + '\n\n' + errStr } -exports.printError = function () { +export const printError = function () { /* eslint-disable no-console */ - console.error(exports.renderError(...arguments)) + console.error(renderError(...arguments)) } -exports.fatalError = function () { +export const fatalError = function () { /* eslint-disable no-console */ - console.error(exports.renderError(...arguments)) + console.error(renderError(...arguments)) process.exit(1) } -exports.createSpinner = function () { +export const createSpinner = function () { return new ORA({ color: 'green', spinner: 'clock', @@ -37,15 +37,15 @@ exports.createSpinner = function () { }) } -exports.encodeHtml = function encodeHtml(str) { +export const encodeHtml = function encodeHtml(str) { return str.replace(//g, '>') } -exports.getContext = function getContext(req, res) { +export const getContext = function getContext(req, res) { return { req, res } } -exports.setAnsiColors = function setAnsiColors(ansiHTML) { +export const setAnsiColors = function setAnsiColors(ansiHTML) { ansiHTML.setColors({ reset: ['efefef', 'a6004c'], darkgrey: '5a012b', @@ -58,7 +58,7 @@ exports.setAnsiColors = function setAnsiColors(ansiHTML) { }) } -exports.waitFor = function waitFor(ms) { +export const waitFor = function waitFor(ms) { return new Promise(resolve => setTimeout(resolve, ms || 0)) } @@ -76,9 +76,7 @@ async function promiseFinally(fn, finalFn) { return result } -exports.promiseFinally = promiseFinally - -exports.timeout = function timeout(fn, ms, msg) { +export const timeout = function timeout(fn, ms, msg) { let timerId const warpPromise = promiseFinally(fn, () => clearTimeout(timerId)) const timerPromise = new Promise((resolve, reject) => { @@ -87,7 +85,7 @@ exports.timeout = function timeout(fn, ms, msg) { return Promise.race([warpPromise, timerPromise]) } -exports.urlJoin = function urlJoin() { +export const urlJoin = function urlJoin() { return [].slice .call(arguments) .join('/') @@ -95,11 +93,11 @@ exports.urlJoin = function urlJoin() { .replace(':/', '://') } -exports.isUrl = function isUrl(url) { +export const isUrl = function isUrl(url) { return url.indexOf('http') === 0 || url.indexOf('//') === 0 } -exports.promisifyRoute = function promisifyRoute(fn, ...args) { +export const promisifyRoute = function promisifyRoute(fn, ...args) { // If routes is an array if (Array.isArray(fn)) { return Promise.resolve(fn) @@ -125,18 +123,18 @@ exports.promisifyRoute = function promisifyRoute(fn, ...args) { return promise } -exports.sequence = function sequence(tasks, fn) { +export const sequence = function sequence(tasks, fn) { return tasks.reduce( (promise, task) => promise.then(() => fn(task)), Promise.resolve() ) } -exports.parallel = function parallel(tasks, fn) { +export const parallel = function parallel(tasks, fn) { return Promise.all(tasks.map(task => fn(task))) } -exports.chainFn = function chainFn(base, fn) { +export const chainFn = function chainFn(base, fn) { /* istanbul ignore if */ if (!(fn instanceof Function)) { return @@ -163,21 +161,21 @@ exports.chainFn = function chainFn(base, fn) { } } -exports.isPureObject = function isPureObject(o) { +export const isPureObject = function isPureObject(o) { return !Array.isArray(o) && typeof o === 'object' } -const isWindows = (exports.isWindows = /^win/.test(process.platform)) +export const isWindows = /^win/.test(process.platform) -const wp = (exports.wp = function wp(p = '') { +export const wp = function wp(p = '') { /* istanbul ignore if */ if (isWindows) { return p.replace(/\\/g, '\\\\') } return p -}) +} -exports.wChunk = function wChunk(p = '') { +export const wChunk = function wChunk(p = '') { /* istanbul ignore if */ if (isWindows) { return p.replace(/\//g, '_') @@ -189,7 +187,7 @@ const reqSep = /\//g const sysSep = _.escapeRegExp(sep) const normalize = string => string.replace(reqSep, sysSep) -const r = (exports.r = function r() { +export const r = function r() { let args = Array.prototype.slice.apply(arguments) let lastArg = _.last(args) @@ -198,9 +196,9 @@ const r = (exports.r = function r() { } return wp(resolve(...args.map(normalize))) -}) +} -exports.relativeTo = function relativeTo() { +export const relativeTo = function relativeTo() { let args = Array.prototype.slice.apply(arguments) let dir = args.shift() @@ -220,7 +218,7 @@ exports.relativeTo = function relativeTo() { return wp(rp) } -exports.flatRoutes = function flatRoutes(router, path = '', routes = []) { +export const flatRoutes = function flatRoutes(router, path = '', routes = []) { router.forEach(r => { if (!r.path.includes(':') && !r.path.includes('*')) { /* istanbul ignore if */ @@ -288,7 +286,7 @@ function cleanChildrenRoutes(routes, isChild = false) { return routes } -exports.createRoutes = function createRoutes(files, srcDir, pagesDir) { +export const createRoutes = function createRoutes(files, srcDir, pagesDir) { let routes = [] files.forEach(file => { let keys = file diff --git a/lib/core/index.js b/lib/core/index.js index b4e0d1399a..282238f0d2 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -1,12 +1,9 @@ -const { Options, Utils } = require('../common') -const Module = require('./module') -const Nuxt = require('./nuxt') -const Renderer = require('./renderer') +import Module from './module' +import Nuxt from './nuxt' +import Renderer from './renderer' -module.exports = { +export default { Nuxt, Module, - Renderer, - Options, - Utils + Renderer } diff --git a/lib/core/meta.js b/lib/core/meta.js index 28af4a63a9..2915fbdb15 100644 --- a/lib/core/meta.js +++ b/lib/core/meta.js @@ -1,9 +1,9 @@ -const Vue = require('vue') -const VueMeta = require('vue-meta') -const VueServerRenderer = require('vue-server-renderer') -const LRU = require('lru-cache') +import Vue from 'vue' +import VueMeta from 'vue-meta' +import VueServerRenderer from 'vue-server-renderer' +import LRU from 'lru-cache' -module.exports = class MetaRenderer { +export default class MetaRenderer { constructor(nuxt, renderer) { this.nuxt = nuxt this.renderer = renderer diff --git a/lib/core/middleware/error.js b/lib/core/middleware/error.js index 53b6b8f180..3b528992d6 100644 --- a/lib/core/middleware/error.js +++ b/lib/core/middleware/error.js @@ -1,8 +1,8 @@ -const Youch = require('@nuxtjs/youch') -const { join, resolve, relative, isAbsolute } = require('path') -const { readFile } = require('fs-extra') +import Youch from '@nuxtjs/youch' +import { join, resolve, relative, isAbsolute } from 'path' +import { readFile } from 'fs-extra' -module.exports = function errorMiddleware(err, req, res, next) { +export default function errorMiddleware(err, req, res, next) { // ensure statusCode, message and name fields err.statusCode = err.statusCode || 500 err.message = err.message || 'Nuxt Server Error' diff --git a/lib/core/middleware/nuxt.js b/lib/core/middleware/nuxt.js index b2eb7c8939..8f075d5926 100644 --- a/lib/core/middleware/nuxt.js +++ b/lib/core/middleware/nuxt.js @@ -1,9 +1,9 @@ -const generateETag = require('etag') -const fresh = require('fresh') +import generateETag from 'etag' +import fresh from 'fresh' -const { getContext } = require('../../common/utils') +import { getContext } from '../../common/utils' -module.exports = async function nuxtMiddleware(req, res, next) { +export default async function nuxtMiddleware(req, res, next) { // Get context const context = getContext(req, res) diff --git a/lib/core/module.js b/lib/core/module.js index 815d60ba77..4a3e9e36fc 100644 --- a/lib/core/module.js +++ b/lib/core/module.js @@ -1,9 +1,9 @@ -const path = require('path') -const fs = require('fs') -const hash = require('hash-sum') -const { chainFn, sequence, printWarn } = require('../common/utils') +import path from 'path' +import fs from 'fs' +import hash from 'hash-sum' +import { chainFn, sequence, printWarn } from '../common/utils' -module.exports = class ModuleContainer { +export default class ModuleContainer { constructor(nuxt) { this.nuxt = nuxt this.options = nuxt.options @@ -112,7 +112,7 @@ module.exports = class ModuleContainer { // Resolve handler if (!handler) { - handler = require(this.nuxt.resolvePath(src)) + handler = this.nuxt.requireModule(src) } // Validate handler diff --git a/lib/core/nuxt.js b/lib/core/nuxt.js index 901f1fe0bd..ac6df46298 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.js @@ -1,20 +1,21 @@ -const Debug = require('debug') -const enableDestroy = require('server-destroy') -const Module = require('module') -const { isPlainObject } = require('lodash') -const chalk = require('chalk') -const { existsSync } = require('fs-extra') -const { Options } = require('../common') -const { sequence, printError } = require('../common/utils') -const { resolve, join } = require('path') -const { version } = require('../../package.json') -const ModuleContainer = require('./module') -const Renderer = require('./renderer') +import Debug from 'debug' +import enableDestroy from 'server-destroy' +import Module from 'module' +import { isPlainObject } from 'lodash' +import chalk from 'chalk' +import { existsSync } from 'fs-extra' +import Options from '../common/options' +import { sequence, printError } from '../common/utils' +import { resolve, join } from 'path' +import { version } from '../../package.json' +import ModuleContainer from './module' +import Renderer from './renderer' +import { requireModule } from '../common/module' const debug = Debug('nuxt:') debug.color = 5 -module.exports = class Nuxt { +export default class Nuxt { constructor(options = {}) { this.options = Options.from(options) @@ -196,6 +197,10 @@ module.exports = class Nuxt { throw new Error(`Cannot resolve "${path}" from "${_path}"`) } + requireModule(name) { + return requireModule(this.resolvePath(name)) + } + async close(callback) { await this.callHook('close', this) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index e634b3da49..885cf03554 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -1,22 +1,22 @@ -const ansiHTML = require('ansi-html') -const serialize = require('serialize-javascript') -const serveStatic = require('serve-static') -const compression = require('compression') -const _ = require('lodash') -const { join, resolve } = require('path') -const fs = require('fs-extra') -const { createBundleRenderer } = require('vue-server-renderer') -const Debug = require('debug') -const connect = require('connect') -const launchMiddleware = require('launch-editor-middleware') -const crypto = require('crypto') +import ansiHTML from 'ansi-html' +import serialize from 'serialize-javascript' +import serveStatic from 'serve-static' +import compression from 'compression' +import _ from 'lodash' +import { join, resolve } from 'path' +import fs from 'fs-extra' +import { createBundleRenderer } from 'vue-server-renderer' +import Debug from 'debug' +import connect from 'connect' +import launchMiddleware from 'launch-editor-middleware' +import crypto from 'crypto' -const { setAnsiColors, isUrl, waitFor, timeout } = require('../common/utils') -const { Options } = require('../common') +import { setAnsiColors, isUrl, waitFor, timeout } from '../common/utils' +import Options from '../common/options' -const MetaRenderer = require('./meta') -const errorMiddleware = require('./middleware/error') -const nuxtMiddleware = require('./middleware/nuxt') +import MetaRenderer from './meta' +import errorMiddleware from './middleware/error' +import nuxtMiddleware from './middleware/nuxt' const debug = Debug('nuxt:render') debug.color = 4 // Force blue color @@ -25,7 +25,7 @@ setAnsiColors(ansiHTML) let jsdom = null -module.exports = class Renderer { +export default class Renderer { constructor(nuxt) { this.nuxt = nuxt this.options = nuxt.options @@ -180,12 +180,10 @@ module.exports = class Renderer { const $m = m let src if (typeof m === 'string') { - src = this.nuxt.resolvePath(m) - m = require(src) + m = this.nuxt.requireModule(m) } if (typeof m.handler === 'string') { - src = this.nuxt.resolvePath(m.handler) - m.handler = require(src) + m.handler = this.nuxt.requireModule(m.handler) } const handler = m.handler || m diff --git a/lib/index.js b/lib/index.js index f0b416e198..6f0d3a8d2b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,20 @@ /*! * Nuxt.js - * (c) 2016-2017 Chopin Brothers - * Core maintainer: Pooya Parsa (@pi0) + * (c) 2016-2018 Chopin Brothers + * Core maintainers: Pooya Parsa (@pi0) - Clark Du (@clarkdo) * Released under the MIT License. */ -const core = require('./core') -const builder = require('./builder') +const requireModule = require('esm')(module, {}) -module.exports = Object.assign({}, core, builder) +const core = requireModule('./core').default +const builder = requireModule('./builder').default +const Utils = requireModule('./common/utils') +const Options = requireModule('./common/options').default + +module.exports = { + Utils, + Options, + ...core, + ...builder +} diff --git a/lib/index.mjs b/lib/index.mjs new file mode 100644 index 0000000000..b607bacbb9 --- /dev/null +++ b/lib/index.mjs @@ -0,0 +1,18 @@ +/*! + * Nuxt.js + * (c) 2016-2018 Chopin Brothers + * Core maintainers: Pooya Parsa (@pi0) - Clark Du (@clarkdo) + * Released under the MIT License. + */ + +import core from './core' +import builder from './builder' +import * as Utils from './common/utils' +import Options from './common/options' + +export default { + Utils, + Options, + ...core, + ...builder +} diff --git a/package.json b/package.json index f352e73ca7..d400b3b149 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ } ], "main": "./lib/index.js", + "module": "./lib/index.mjs", "license": "MIT", "repository": { "type": "git", diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 9309b5eca5..072067bac6 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -8,6 +8,7 @@ import finalhandler from 'finalhandler' import rp from 'request-promise-native' import { interceptLog, release } from './helpers/console' import { Nuxt, Builder, Generator } from '..' +import { loadConfig } from './helpers/config' const port = 4002 const url = route => 'http://localhost:' + port + route @@ -19,10 +20,11 @@ let generator = null // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.buildDir = '.nuxt-generate' - config.dev = false + const config = loadConfig('basic', { + buildDir: '.nuxt-generate', + dev: false + }) + config.build.stats = false const logSpy = await interceptLog(async () => { diff --git a/test/custom-dirs.test.js b/test/custom-dirs.test.js index 7201c0c7aa..d893deac98 100644 --- a/test/custom-dirs.test.js +++ b/test/custom-dirs.test.js @@ -1,8 +1,8 @@ import test from 'ava' -import { resolve } from 'path' import rp from 'request-promise-native' import { Nuxt, Builder } from '..' import { interceptLog } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4007 const url = route => 'http://localhost:' + port + route @@ -12,10 +12,7 @@ let builder = null // Init nuxt.js and create server listening on localhost:4000 test.before('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/custom-dirs') - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false + const config = loadConfig('/custom-dirs', { dev: false }) const logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) diff --git a/test/debug.test.js b/test/debug.test.js index 3a58f4895f..bc0c341038 100644 --- a/test/debug.test.js +++ b/test/debug.test.js @@ -1,8 +1,8 @@ import test from 'ava' -import { resolve } from 'path' import rp from 'request-promise-native' import { Nuxt, Builder } from '..' import { interceptLog, interceptError, release } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4009 const url = route => 'http://localhost:' + port + route @@ -11,9 +11,7 @@ let nuxt = null // Init nuxt.js and create server listening on localhost:4000 test.before('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/debug') - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir + const config = loadConfig('debug') const logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) diff --git a/test/deprecate.test.js b/test/deprecate.test.js index a86ca2e428..2eced35c9f 100644 --- a/test/deprecate.test.js +++ b/test/deprecate.test.js @@ -1,7 +1,7 @@ import test from 'ava' -import { resolve } from 'path' import { Nuxt, Builder } from '..' import { intercept } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4010 @@ -11,10 +11,7 @@ let buildSpies = null // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/deprecate') - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false + const config = loadConfig('deprecate', { dev: false }) buildSpies = await intercept(async () => { nuxt = new Nuxt(config) diff --git a/test/error.test.js b/test/error.test.js index c27f169c94..7c7abdd042 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -1,8 +1,8 @@ import test from 'ava' -import { resolve } from 'path' import rp from 'request-promise-native' import { Nuxt, Builder } from '..' import { interceptLog, interceptError, release } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4005 const url = route => 'http://localhost:' + port + route @@ -12,10 +12,7 @@ let logSpy // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/error') - const config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false + const config = loadConfig('error', { dev: false }) logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) diff --git a/test/fallback.generate.test.js b/test/fallback.generate.test.js index f041616d69..68d68b8fa4 100644 --- a/test/fallback.generate.test.js +++ b/test/fallback.generate.test.js @@ -7,10 +7,10 @@ import finalhandler from 'finalhandler' import rp from 'request-promise-native' import { intercept, interceptLog } from './helpers/console' import { Nuxt, Builder, Generator, Options } from '..' +import { loadConfig } from './helpers/config' const port = 4015 const url = route => 'http://localhost:' + port + route -const rootDir = resolve(__dirname, 'fixtures/basic') let nuxt = null let server = null @@ -18,10 +18,10 @@ let generator = null // Init nuxt.js and create server listening on localhost:4015 test.serial('Init Nuxt.js', async t => { - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.buildDir = '.nuxt-spa-fallback' - config.dev = false + let config = loadConfig('basic', { + buildDir: '.nuxt-spa-fallback', + dev: false + }) config.build.stats = false const logSpy = await interceptLog(async () => { diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index e2c5472a90..201210802b 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -1,6 +1,6 @@ -const path = require('path') +import path from 'path' -module.exports = { +export default { generate: { routes: [ '/users/1', diff --git a/test/fixtures/custom-dirs/nuxt.config.js b/test/fixtures/custom-dirs/nuxt.config.js index f150701a46..45c658ef3a 100644 --- a/test/fixtures/custom-dirs/nuxt.config.js +++ b/test/fixtures/custom-dirs/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { css: [{ src: '~/custom-assets/app.css' }], dir: { assets: 'custom-assets', diff --git a/test/fixtures/debug/nuxt.config.js b/test/fixtures/debug/nuxt.config.js index a9111cd54a..494d20b5a1 100644 --- a/test/fixtures/debug/nuxt.config.js +++ b/test/fixtures/debug/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { router: { base: '/test/' }, diff --git a/test/fixtures/deprecate/modules/deprecated-apis/index.js b/test/fixtures/deprecate/modules/deprecated-apis/index.js index 9f06056b9e..b2ceaf3de5 100644 --- a/test/fixtures/deprecate/modules/deprecated-apis/index.js +++ b/test/fixtures/deprecate/modules/deprecated-apis/index.js @@ -1,3 +1,3 @@ -module.exports = function basicModule(options, resolve) { +export default function basicModule(options, resolve) { this.addVendor('lodash') } diff --git a/test/fixtures/deprecate/nuxt.config.js b/test/fixtures/deprecate/nuxt.config.js index 6415102f2f..977eaf5a40 100644 --- a/test/fixtures/deprecate/nuxt.config.js +++ b/test/fixtures/deprecate/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { modules: ['~/modules/deprecated-apis'], build: { stats: false diff --git a/test/fixtures/error/nuxt.config.js b/test/fixtures/error/nuxt.config.js index 39dc2dca7a..96e260ba0b 100644 --- a/test/fixtures/error/nuxt.config.js +++ b/test/fixtures/error/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { hooks(hook) { hook('build:done', nuxt => { throw new Error('hook error') diff --git a/test/fixtures/module/modules/basic/index.js b/test/fixtures/module/modules/basic/index.js index 19c8cf976c..33233dc860 100644 --- a/test/fixtures/module/modules/basic/index.js +++ b/test/fixtures/module/modules/basic/index.js @@ -1,6 +1,6 @@ -const path = require('path') +import path from 'path' -module.exports = function basicModule(options) { +export default function basicModule(options) { // Add a plugin this.addPlugin(path.resolve(__dirname, 'reverse.js')) diff --git a/test/fixtures/module/modules/empty/index.js b/test/fixtures/module/modules/empty/index.js index 05c0b3f72e..bf3b979298 100644 --- a/test/fixtures/module/modules/empty/index.js +++ b/test/fixtures/module/modules/empty/index.js @@ -1,4 +1,4 @@ -module.exports = function middlewareModule(options) { +export default function middlewareModule(options) { // Empty module } diff --git a/test/fixtures/module/modules/hooks/index.js b/test/fixtures/module/modules/hooks/index.js index 6a94b51671..eac4a10e97 100644 --- a/test/fixtures/module/modules/hooks/index.js +++ b/test/fixtures/module/modules/hooks/index.js @@ -1,4 +1,4 @@ -module.exports = function () { +export default function () { let ctr = 1 // Add hook for module diff --git a/test/fixtures/module/modules/middleware/index.js b/test/fixtures/module/modules/middleware/index.js index 33f1880806..fb46907191 100644 --- a/test/fixtures/module/modules/middleware/index.js +++ b/test/fixtures/module/modules/middleware/index.js @@ -1,4 +1,4 @@ -module.exports = function middlewareModule(options) { +export default function middlewareModule(options) { return new Promise((resolve, reject) => { // Add /api endpoint this.addServerMiddleware({ diff --git a/test/fixtures/module/modules/middleware/log.js b/test/fixtures/module/modules/middleware/log.js index c8d2b3ebcb..400f47cb51 100644 --- a/test/fixtures/module/modules/middleware/log.js +++ b/test/fixtures/module/modules/middleware/log.js @@ -1,4 +1,4 @@ -module.exports = function (req, res, next) { +export default function (req, res, next) { // eslint-disable-next-line no-console console.log(req.url) next() diff --git a/test/fixtures/module/modules/middleware/midd1.js b/test/fixtures/module/modules/middleware/midd1.js index 78ec0d0355..4ccf32924d 100644 --- a/test/fixtures/module/modules/middleware/midd1.js +++ b/test/fixtures/module/modules/middleware/midd1.js @@ -1,4 +1,4 @@ -module.exports = function (req, res, next) { +export default function (req, res, next) { res.setHeader('x-midd-1', 'ok') next() } diff --git a/test/fixtures/module/modules/middleware/midd2.js b/test/fixtures/module/modules/middleware/midd2.js index 64babff282..cd0d65cbaa 100644 --- a/test/fixtures/module/modules/middleware/midd2.js +++ b/test/fixtures/module/modules/middleware/midd2.js @@ -1,4 +1,4 @@ -module.exports = function (req, res, next) { +export default function (req, res, next) { res.setHeader('x-midd-2', 'ok') next() } diff --git a/test/fixtures/module/modules/middleware/use-middleware.js b/test/fixtures/module/modules/middleware/use-middleware.js index f6156520c3..624ff48a83 100644 --- a/test/fixtures/module/modules/middleware/use-middleware.js +++ b/test/fixtures/module/modules/middleware/use-middleware.js @@ -1,3 +1,3 @@ -module.exports = function (req, res, next) { +export default function (req, res, next) { res.end('Use external middleware') } diff --git a/test/fixtures/module/modules/template/index.js b/test/fixtures/module/modules/template/index.js index 6033364d55..3d95885e14 100644 --- a/test/fixtures/module/modules/template/index.js +++ b/test/fixtures/module/modules/template/index.js @@ -1,6 +1,6 @@ -const path = require('path') +import path from 'path' -module.exports = function () { +export default function () { // Disable parsing pages/ this.nuxt.options.build.createRoutes = () => {} diff --git a/test/fixtures/module/nuxt.config.js b/test/fixtures/module/nuxt.config.js index 7f06cbbeae..0eadbe29aa 100644 --- a/test/fixtures/module/nuxt.config.js +++ b/test/fixtures/module/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { loading: true, modules: [ '~~/modules/basic', diff --git a/test/fixtures/spa/nuxt.config.js b/test/fixtures/spa/nuxt.config.js index 9bb39b668f..ba9bea0eac 100644 --- a/test/fixtures/spa/nuxt.config.js +++ b/test/fixtures/spa/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { mode: 'spa', dev: false, transition: false, diff --git a/test/fixtures/ssr/nuxt.config.js b/test/fixtures/ssr/nuxt.config.js index 792e6b0db2..d6f6862005 100644 --- a/test/fixtures/ssr/nuxt.config.js +++ b/test/fixtures/ssr/nuxt.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { dev: false, render: { resourceHints: false, diff --git a/test/fixtures/with-config/nuxt.config.js b/test/fixtures/with-config/nuxt.config.js index 1997fd52ad..80f0920abb 100644 --- a/test/fixtures/with-config/nuxt.config.js +++ b/test/fixtures/with-config/nuxt.config.js @@ -1,6 +1,6 @@ -const path = require('path') +import path from 'path' -module.exports = { +export default { srcDir: __dirname, router: { base: '/test/', diff --git a/test/helpers/config.js b/test/helpers/config.js new file mode 100644 index 0000000000..c0547c4ad7 --- /dev/null +++ b/test/helpers/config.js @@ -0,0 +1,11 @@ +import { resolve } from 'path' + +import { requireModule } from '../../lib/common/module' + +export function loadConfig(fixture, overrides) { + const rootDir = resolve(__dirname, '../fixtures/' + fixture) + + const config = requireModule(resolve(rootDir, 'nuxt.config.js')) + + return Object.assign({ rootDir }, config, overrides) +} diff --git a/test/module.test.js b/test/module.test.js index 7e4b3e3965..85444d0218 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -1,8 +1,9 @@ import test from 'ava' -import { resolve, normalize } from 'path' +import { normalize } from 'path' import rp from 'request-promise-native' import { Nuxt, Builder } from '..' import { intercept } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4006 const url = route => 'http://localhost:' + port + route @@ -13,10 +14,8 @@ let buildSpies = null // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/module') - const config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false + const config = loadConfig('module', { dev: false }) + nuxt = new Nuxt(config) builder = new Builder(nuxt) diff --git a/test/spa.test.js b/test/spa.test.js index 9e44427e0b..311bb60d1f 100644 --- a/test/spa.test.js +++ b/test/spa.test.js @@ -1,7 +1,7 @@ import test from 'ava' -import { resolve } from 'path' import { Nuxt, Builder } from '..' import { interceptLog, release } from './helpers/console' +import { loadConfig } from './helpers/config' let nuxt = null @@ -17,9 +17,7 @@ const renderRoute = async _url => { // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/spa') - const config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir + const config = loadConfig('spa') const logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) diff --git a/test/ssr.test.js b/test/ssr.test.js index a5e3d65da3..76638b2cc6 100644 --- a/test/ssr.test.js +++ b/test/ssr.test.js @@ -1,9 +1,9 @@ import test from 'ava' -import { resolve } from 'path' import { Nuxt, Builder, Utils } from '..' import { uniq } from 'lodash' import rp from 'request-promise-native' import { interceptLog } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4008 let nuxt = null @@ -19,9 +19,7 @@ const isWindows = /^win/.test(process.platform) // Init nuxt.js and create server listening on localhost:4000 test.serial('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/ssr') - const config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir + const config = loadConfig('ssr') const logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) diff --git a/test/with-config.test.js b/test/with-config.test.js index 8900379d10..e1bbcb3ef0 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -4,6 +4,7 @@ import rp from 'request-promise-native' import { Nuxt, Builder } from '..' import styleLoader from '../lib/builder/webpack/style-loader' import { interceptLog, release } from './helpers/console' +import { loadConfig } from './helpers/config' const port = 4007 const url = route => 'http://localhost:' + port + route @@ -13,10 +14,9 @@ let builder = null // Init nuxt.js and create server listening on localhost:4000 test.before('Init Nuxt.js', async t => { - const rootDir = resolve(__dirname, 'fixtures/with-config') - let config = require(resolve(rootDir, 'nuxt.config.js')) - config.rootDir = rootDir - config.dev = false + const config = loadConfig('with-config', { + dev: false + }) const logSpy = await interceptLog(async () => { nuxt = new Nuxt(config) From 189c928a6d11a452745b8a60b2dd8f82eade1d1d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 20:00:45 +0330 Subject: [PATCH 03/15] fix eslint --- lib/builder/webpack/client.config.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/builder/webpack/client.config.js b/lib/builder/webpack/client.config.js index 5c1e70922c..0978e065b2 100644 --- a/lib/builder/webpack/client.config.js +++ b/lib/builder/webpack/client.config.js @@ -10,7 +10,6 @@ import { resolve } from 'path' import Debug from 'debug' import base from './base.config.js' - const debug = Debug('nuxt:build') debug.color = 2 // Force green color From f20ad95b2bf49d463792ab61f5ac21ccd3e33539 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 20:32:56 +0330 Subject: [PATCH 04/15] remove pretty-error https://github.com/standard-things/esm/wiki/improved-errors --- lib/builder/generator.js | 6 +++--- lib/common/utils.js | 5 +---- package.json | 1 - scripts/start | 2 +- start/package.json | 1 - yarn.lock | 2 +- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/builder/generator.js b/lib/builder/generator.js index 758d8e6b77..bf1995b28b 100644 --- a/lib/builder/generator.js +++ b/lib/builder/generator.js @@ -3,7 +3,7 @@ import { resolve, join, dirname, sep } from 'path' import { minify } from 'html-minifier' import Chalk from 'chalk' import { copy, remove, writeFile, mkdirp, removeSync, existsSync } from 'fs-extra' -import { isUrl, promisifyRoute, waitFor, flatRoutes, pe, printWarn, createSpinner } from '../common/utils' +import { isUrl, promisifyRoute, waitFor, flatRoutes, printWarn, createSpinner } from '../common/utils' export default class Generator { constructor(nuxt, builder) { @@ -122,12 +122,12 @@ export default class Generator { const color = isHandled ? 'yellow' : 'red' let line = - Chalk.black[bgColor](' GENERATE ERR ') + Chalk[color](` ${route}\n\n`) + Chalk.black[bgColor](' GEN ERR ') + Chalk[color](` ${route}\n\n`) if (isHandled) { line += Chalk.grey(JSON.stringify(error, undefined, 2) + '\n') } else { - line += Chalk.grey(pe.render(error)) + line += Chalk.grey(error.toString()) } return line diff --git a/lib/common/utils.js b/lib/common/utils.js index e61f8e0087..fb3b55e2c8 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -1,11 +1,8 @@ import { resolve, relative, sep } from 'path' import _ from 'lodash' -import PrettyError from 'pretty-error' import Chalk from 'chalk' import ORA from 'ora' -export const pe = new PrettyError() - export const printWarn = function (msg, from) { /* eslint-disable no-console */ const fromStr = from ? Chalk.yellow(` ${from}\n\n`) : ' ' @@ -13,7 +10,7 @@ export const printWarn = function (msg, from) { } export const renderError = function (_error, from) { - const errStr = pe.render(_error) + const errStr = _error.toString() const fromStr = from ? Chalk.red(` ${from}`) : '' return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + '\n\n' + errStr } diff --git a/package.json b/package.json index e8d47669d2..666498e419 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,6 @@ "postcss-import-resolver": "^1.1.0", "postcss-loader": "^2.1.0", "postcss-url": "^7.3.0", - "pretty-error": "^2.1.1", "semver": "^5.5.0", "serialize-javascript": "^1.4.0", "serve-static": "^1.13.2", diff --git a/scripts/start b/scripts/start index 70ebb69d3d..7b048be4c8 100755 --- a/scripts/start +++ b/scripts/start @@ -19,7 +19,7 @@ const startDir = resolve(rootDir, 'start') const packageJSON = readJSONSync(resolve(rootDir, 'package.json')) // Required and Excluded packages for start -let requires = ['source-map-support', 'pretty-error', 'minimist'] +let requires = ['minimist'] const excludes = ['path', 'fs', 'http', 'module'].concat( Object.keys(packageJSON.devDependencies) diff --git a/start/package.json b/start/package.json index 16dd7d8e75..1abea418c4 100644 --- a/start/package.json +++ b/start/package.json @@ -47,7 +47,6 @@ }, "dependencies": { "source-map-support": "^0.5.0", - "pretty-error": "^2.1.1", "minimist": "^1.2.0", "lodash": "^4.17.4", "debug": "^3.1.0", diff --git a/yarn.lock b/yarn.lock index a5650761c1..9c5a322e69 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6070,7 +6070,7 @@ pretty-bytes@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-4.0.2.tgz#b2bf82e7350d65c6c33aa95aaa5a4f6327f61cd9" -pretty-error@^2.0.2, pretty-error@^2.1.1: +pretty-error@^2.0.2: version "2.1.1" resolved "https://registry.yarnpkg.com/pretty-error/-/pretty-error-2.1.1.tgz#5f4f87c8f91e5ae3f3ba87ab4cf5e03b1a17f1a3" dependencies: From ec616f109b7f90083f90f399d66943660e87bd82 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 20:53:15 +0330 Subject: [PATCH 05/15] refactor options --- lib/common/modes.js | 18 ++++ lib/common/nuxt.config.js | 177 +++++++++++++++++++++++++++++++ lib/common/options.js | 213 ++------------------------------------ lib/core/renderer.js | 4 +- 4 files changed, 205 insertions(+), 207 deletions(-) create mode 100644 lib/common/modes.js create mode 100644 lib/common/nuxt.config.js diff --git a/lib/common/modes.js b/lib/common/modes.js new file mode 100644 index 0000000000..676aea8b6d --- /dev/null +++ b/lib/common/modes.js @@ -0,0 +1,18 @@ +export default { + universal: { + build: { + ssr: true + }, + render: { + ssr: true + } + }, + spa: { + build: { + ssr: false + }, + render: { + ssr: false + } + } +} diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js new file mode 100644 index 0000000000..a1505263c8 --- /dev/null +++ b/lib/common/nuxt.config.js @@ -0,0 +1,177 @@ +import { resolve } from 'path' + +export default { + mode: 'universal', + dev: process.env.NODE_ENV !== 'production', + debug: undefined, // Will be equal to dev if not provided + buildDir: '.nuxt', + cacheDir: '.cache', + nuxtDir: resolve(__dirname, '../..'), + nuxtAppDir: resolve(__dirname, '../app'), + modulesDir: ['node_modules'], // ~> relative to options.rootDir + ignorePrefix: '-', + ignore: [ + '**/*.test.*' + ], + extensions: [], + build: { + analyze: false, + profile: process.argv.includes('--profile'), + splitPages: true, + maxChunkSize: false, + extractCSS: false, + cssSourceMap: undefined, + ssr: undefined, + publicPath: '/_nuxt/', + filenames: { + css: '[name].[contenthash].css', + manifest: 'manifest.[hash].js', + app: '[name].[chunkhash].js', + chunk: '[name].[chunkhash].js' + }, + styleResources: {}, + plugins: [], + babel: { + babelrc: false + }, + postcss: {}, + templates: [], + watch: [], + devMiddleware: {}, + hotMiddleware: {}, + stats: { + chunks: false, + children: false, + modules: false, + colors: true, + excludeAssets: [ + /.map$/, + /index\..+\.html$/, + /vue-ssr-client-manifest.json/ + ] + } + }, + generate: { + dir: 'dist', + routes: [], + concurrency: 500, + interval: 0, + subFolders: true, + fallback: '200.html', + minify: { + collapseBooleanAttributes: true, + collapseWhitespace: false, + decodeEntities: true, + minifyCSS: true, + minifyJS: true, + processConditionalComments: true, + removeAttributeQuotes: false, + removeComments: false, + removeEmptyAttributes: true, + removeOptionalTags: true, + removeRedundantAttributes: true, + removeScriptTypeAttributes: false, + removeStyleLinkTypeAttributes: false, + removeTagWhitespace: false, + sortAttributes: true, + sortClassName: false, + trimCustomFragments: true, + useShortDoctype: true + } + }, + env: {}, + head: { + meta: [], + link: [], + style: [], + script: [] + }, + plugins: [], + css: [], + modules: [], + layouts: {}, + serverMiddleware: [], + ErrorPage: null, + loading: { + color: 'black', + failedColor: 'red', + height: '2px', + duration: 5000, + rtl: false + }, + loadingIndicator: {}, + transition: { + name: 'page', + mode: 'out-in', + appear: false, + appearClass: 'appear', + appearActiveClass: 'appear-active', + appearToClass: 'appear-to' + }, + layoutTransition: { + name: 'layout', + mode: 'out-in' + }, + dir: { + assets: 'assets', + layouts: 'layouts', + middleware: 'middleware', + pages: 'pages', + static: 'static', + store: 'store' + }, + router: { + mode: 'history', + base: '/', + routes: [], + middleware: [], + linkActiveClass: 'nuxt-link-active', + linkExactActiveClass: 'nuxt-link-exact-active', + extendRoutes: null, + scrollBehavior: null, + parseQuery: false, + stringifyQuery: false, + fallback: false + }, + render: { + bundleRenderer: {}, + resourceHints: true, + ssr: undefined, + http2: { + push: false, + shouldPush: null + }, + static: { + prefix: true + }, + gzip: { + threshold: 0 + }, + etag: { + weak: false + }, + csp: { + enabled: false, + hashAlgorithm: 'sha256', + allowedSources: undefined, + policies: undefined + } + }, + watchers: { + webpack: {}, + chokidar: {} + }, + editor: undefined, + hooks: null, + messages: { + error_404: 'This page could not be found', + server_error: 'Server error', + nuxtjs: 'Nuxt.js', + back_to_home: 'Back to the home page', + server_error_details: + 'An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.', + client_error: 'Error', + client_error_details: + 'An error occurred while rendering the page. Check developer tools console for details.' + } +} diff --git a/lib/common/options.js b/lib/common/options.js index d02782fc83..b3ecf1212b 100644 --- a/lib/common/options.js +++ b/lib/common/options.js @@ -2,8 +2,12 @@ import _ from 'lodash' import Debug from 'debug' import { join, resolve } from 'path' import { existsSync, readdirSync } from 'fs' + import { isUrl, isPureObject } from '../common/utils' +import modes from './modes' +import defaults from './nuxt.config' + const debug = Debug('nuxt:build') debug.color = 2 // Force green color @@ -44,14 +48,14 @@ Options.from = function (_options) { // Apply defaults by ${buildDir}/dist/build.config.js // TODO: Unsafe operation. - // const buildDir = options.buildDir || Options.defaults.buildDir + // const buildDir = options.buildDir || defaults.buildDir // const buildConfig = resolve(options.rootDir, buildDir, 'build.config.js') // if (existsSync(buildConfig)) { // _.defaultsDeep(options, require(buildConfig)) // } // Apply defaults - _.defaultsDeep(options, Options.defaults) + _.defaultsDeep(options, defaults) // Resolve dirs options.srcDir = hasValue(options.srcDir) @@ -81,7 +85,7 @@ Options.from = function (_options) { // Ignore publicPath on dev /* istanbul ignore if */ if (options.dev && isUrl(options.build.publicPath)) { - options.build.publicPath = Options.defaults.build.publicPath + options.build.publicPath = defaults.build.publicPath } // If store defined, update store options to true unless explicitly disabled @@ -138,8 +142,7 @@ Options.from = function (_options) { } // Apply mode preset - let modePreset = - Options.modes[options.mode || 'universal'] || Options.modes['universal'] + const modePreset = modes[options.mode || 'universal'] || modes['universal'] _.defaultsDeep(options, modePreset) // If no server-side rendering, add appear true transition @@ -155,203 +158,3 @@ Options.from = function (_options) { return options } - -Options.modes = { - universal: { - build: { - ssr: true - }, - render: { - ssr: true - } - }, - spa: { - build: { - ssr: false - }, - render: { - ssr: false - } - } -} - -// Options.unsafeKeys = [ -// 'rootDir', 'srcDir', 'buildDir', 'modulesDir', 'cacheDir', 'nuxtDir', -// 'nuxtAppDir', 'build', 'generate', 'router.routes', 'appTemplatePath' -// ] - -Options.defaults = { - mode: 'universal', - dev: process.env.NODE_ENV !== 'production', - debug: undefined, // Will be equal to dev if not provided - buildDir: '.nuxt', - cacheDir: '.cache', - nuxtDir: resolve(__dirname, '../..'), - nuxtAppDir: resolve(__dirname, '../app'), - modulesDir: ['node_modules'], // ~> relative to options.rootDir - ignorePrefix: '-', - ignore: [ - '**/*.test.*' - ], - extensions: [], - build: { - analyze: false, - profile: process.argv.includes('--profile'), - splitPages: true, - maxChunkSize: false, - extractCSS: false, - cssSourceMap: undefined, - ssr: undefined, - publicPath: '/_nuxt/', - filenames: { - css: '[name].[contenthash].css', - manifest: 'manifest.[hash].js', - app: '[name].[chunkhash].js', - chunk: '[name].[chunkhash].js' - }, - styleResources: {}, - plugins: [], - babel: { - babelrc: false - }, - postcss: {}, - templates: [], - watch: [], - devMiddleware: {}, - hotMiddleware: {}, - stats: { - chunks: false, - children: false, - modules: false, - colors: true, - excludeAssets: [ - /.map$/, - /index\..+\.html$/, - /vue-ssr-client-manifest.json/ - ] - } - }, - generate: { - dir: 'dist', - routes: [], - concurrency: 500, - interval: 0, - subFolders: true, - fallback: '200.html', - minify: { - collapseBooleanAttributes: true, - collapseWhitespace: false, - decodeEntities: true, - minifyCSS: true, - minifyJS: true, - processConditionalComments: true, - removeAttributeQuotes: false, - removeComments: false, - removeEmptyAttributes: true, - removeOptionalTags: true, - removeRedundantAttributes: true, - removeScriptTypeAttributes: false, - removeStyleLinkTypeAttributes: false, - removeTagWhitespace: false, - sortAttributes: true, - sortClassName: false, - trimCustomFragments: true, - useShortDoctype: true - } - }, - env: {}, - head: { - meta: [], - link: [], - style: [], - script: [] - }, - plugins: [], - css: [], - modules: [], - layouts: {}, - serverMiddleware: [], - ErrorPage: null, - loading: { - color: 'black', - failedColor: 'red', - height: '2px', - duration: 5000, - rtl: false - }, - loadingIndicator: {}, - transition: { - name: 'page', - mode: 'out-in', - appear: false, - appearClass: 'appear', - appearActiveClass: 'appear-active', - appearToClass: 'appear-to' - }, - layoutTransition: { - name: 'layout', - mode: 'out-in' - }, - dir: { - assets: 'assets', - layouts: 'layouts', - middleware: 'middleware', - pages: 'pages', - static: 'static', - store: 'store' - }, - router: { - mode: 'history', - base: '/', - routes: [], - middleware: [], - linkActiveClass: 'nuxt-link-active', - linkExactActiveClass: 'nuxt-link-exact-active', - extendRoutes: null, - scrollBehavior: null, - parseQuery: false, - stringifyQuery: false, - fallback: false - }, - render: { - bundleRenderer: {}, - resourceHints: true, - ssr: undefined, - http2: { - push: false, - shouldPush: null - }, - static: { - prefix: true - }, - gzip: { - threshold: 0 - }, - etag: { - weak: false - }, - csp: { - enabled: false, - hashAlgorithm: 'sha256', - allowedSources: undefined, - policies: undefined - } - }, - watchers: { - webpack: {}, - chokidar: {} - }, - editor: undefined, - hooks: null, - messages: { - error_404: 'This page could not be found', - server_error: 'Server error', - nuxtjs: 'Nuxt.js', - back_to_home: 'Back to the home page', - server_error_details: - 'An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details.', - client_error: 'Error', - client_error_details: - 'An error occurred while rendering the page. Check developer tools console for details.' - } -} diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 885cf03554..396e59f45b 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -12,7 +12,7 @@ import launchMiddleware from 'launch-editor-middleware' import crypto from 'crypto' import { setAnsiColors, isUrl, waitFor, timeout } from '../common/utils' -import Options from '../common/options' +import defaults from '../common/nuxt.config' import MetaRenderer from './meta' import errorMiddleware from './middleware/error' @@ -202,7 +202,7 @@ export default class Renderer { get publicPath() { return isUrl(this.options.build.publicPath) - ? Options.defaults.build.publicPath + ? defaults.build.publicPath : this.options.build.publicPath } From 6234ae84c0b8adb23b0874ef20ef4f73f3944b2d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 22:41:24 +0330 Subject: [PATCH 06/15] refactor(core): use strict mjs --- lib/builder/{builder.js => builder.mjs} | 60 +++++++++---------- lib/builder/{generator.js => generator.mjs} | 52 ++++++++-------- lib/builder/{index.js => index.mjs} | 0 .../{base.config.js => base.config.mjs} | 20 +++---- .../{client.config.js => client.config.mjs} | 14 ++--- .../plugins/{progress.js => progress.mjs} | 0 .../plugins/vue/{client.js => client.mjs} | 0 .../plugins/vue/{server.js => server.mjs} | 0 .../webpack/plugins/vue/{util.js => util.mjs} | 0 .../plugins/{warnfix.js => warnfix.mjs} | 0 .../webpack/{postcss.js => postcss.mjs} | 20 +++---- .../{server.config.js => server.config.mjs} | 14 ++--- lib/builder/webpack/style-loader.js | 6 +- .../webpack/{vue-loader.js => vue-loader.mjs} | 0 lib/common/{modes.js => modes.mjs} | 0 lib/common/nuxt.config.js | 6 +- lib/common/{options.js => options.mjs} | 24 ++++---- lib/common/{utils.js => utils.mjs} | 31 +++++----- lib/core/{index.js => index.mjs} | 0 lib/core/{meta.js => meta.mjs} | 0 lib/core/middleware/{error.js => error.mjs} | 14 ++--- lib/core/middleware/{nuxt.js => nuxt.mjs} | 0 lib/core/{module.js => module.mjs} | 0 lib/core/{nuxt.js => nuxt.mjs} | 42 ++++++------- lib/core/{renderer.js => renderer.mjs} | 24 ++++---- 25 files changed, 164 insertions(+), 163 deletions(-) rename lib/builder/{builder.js => builder.mjs} (90%) rename lib/builder/{generator.js => generator.mjs} (82%) rename lib/builder/{index.js => index.mjs} (100%) rename lib/builder/webpack/{base.config.js => base.config.mjs} (91%) rename lib/builder/webpack/{client.config.js => client.config.mjs} (91%) rename lib/builder/webpack/plugins/{progress.js => progress.mjs} (100%) rename lib/builder/webpack/plugins/vue/{client.js => client.mjs} (100%) rename lib/builder/webpack/plugins/vue/{server.js => server.mjs} (100%) rename lib/builder/webpack/plugins/vue/{util.js => util.mjs} (100%) rename lib/builder/webpack/plugins/{warnfix.js => warnfix.mjs} (100%) rename lib/builder/webpack/{postcss.js => postcss.mjs} (81%) rename lib/builder/webpack/{server.config.js => server.config.mjs} (90%) rename lib/builder/webpack/{vue-loader.js => vue-loader.mjs} (100%) rename lib/common/{modes.js => modes.mjs} (100%) rename lib/common/{options.js => options.mjs} (85%) rename lib/common/{utils.js => utils.mjs} (93%) rename lib/core/{index.js => index.mjs} (100%) rename lib/core/{meta.js => meta.mjs} (100%) rename lib/core/middleware/{error.js => error.mjs} (88%) rename lib/core/middleware/{nuxt.js => nuxt.mjs} (100%) rename lib/core/{module.js => module.mjs} (100%) rename lib/core/{nuxt.js => nuxt.mjs} (82%) rename lib/core/{renderer.js => renderer.mjs} (94%) diff --git a/lib/builder/builder.js b/lib/builder/builder.mjs similarity index 90% rename from lib/builder/builder.js rename to lib/builder/builder.mjs index f29a3d5a38..b9b4a8b21b 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.mjs @@ -1,12 +1,12 @@ -import { promisify } from 'util' +import util from 'util' import _ from 'lodash' import chokidar from 'chokidar' -import { remove, readFile, writeFile, mkdirp, existsSync } from 'fs-extra' +import fsExtra from 'fs-extra' import fs from 'fs' import hash from 'hash-sum' import webpack from 'webpack' import serialize from 'serialize-javascript' -import { join, resolve, basename, extname, dirname } from 'path' +import path from 'path' import MFS from 'memory-fs' import webpackDevMiddleware from 'webpack-dev-middleware' import webpackHotMiddleware from 'webpack-hot-middleware' @@ -14,14 +14,14 @@ import Debug from 'debug' import Glob from 'glob' import { r, wp, wChunk, createRoutes, parallel, relativeTo, waitFor, createSpinner } from '../common/utils' import Options from '../common/options' -import clientWebpackConfig from './webpack/client.config.js' -import serverWebpackConfig from './webpack/server.config.js' +import clientWebpackConfig from './webpack/client.config' +import serverWebpackConfig from './webpack/server.config' import upath from 'upath' const debug = Debug('nuxt:build') debug.color = 2 // Force green color -const glob = promisify(Glob) +const glob = util.promisify(Glob) export default class Builder { constructor(nuxt) { @@ -61,7 +61,7 @@ export default class Builder { return _.uniqBy( this.options.plugins.map((p, i) => { if (typeof p === 'string') p = { src: p } - const pluginBaseName = basename(p.src, extname(p.src)).replace( + const pluginBaseName = path.basename(p.src, path.extname(p.src)).replace( /[^a-zA-Z?\d\s:]/g, '' ) @@ -104,9 +104,9 @@ export default class Builder { // Check if pages dir exists and warn if not this._nuxtPages = typeof this.options.build.createRoutes !== 'function' if (this._nuxtPages) { - if (!existsSync(join(this.options.srcDir, this.options.dir.pages))) { + if (!fsExtra.existsSync(path.join(this.options.srcDir, this.options.dir.pages))) { let dir = this.options.srcDir - if (existsSync(join(this.options.srcDir, '..', this.options.dir.pages))) { + if (fsExtra.existsSync(path.join(this.options.srcDir, '..', this.options.dir.pages))) { throw new Error( `No \`${this.options.dir.pages}\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?` ) @@ -123,10 +123,10 @@ export default class Builder { debug(`App root: ${this.options.srcDir}`) // Create .nuxt/, .nuxt/components and .nuxt/dist folders - await remove(r(this.options.buildDir)) - await mkdirp(r(this.options.buildDir, 'components')) + await fsExtra.remove(r(this.options.buildDir)) + await fsExtra.mkdirp(r(this.options.buildDir, 'components')) if (!this.options.dev) { - await mkdirp(r(this.options.buildDir, 'dist')) + await fsExtra.mkdirp(r(this.options.buildDir, 'dist')) } // Generate routes and interpret the template files @@ -215,7 +215,7 @@ export default class Builder { router: this.options.router, env: this.options.env, head: this.options.head, - middleware: existsSync(join(this.options.srcDir, this.options.dir.middleware)), + middleware: fsExtra.existsSync(path.join(this.options.srcDir, this.options.dir.middleware)), store: this.options.store, css: this.options.css, plugins: this.plugins, @@ -237,7 +237,7 @@ export default class Builder { } // -- Layouts -- - if (existsSync(resolve(this.options.srcDir, this.options.dir.layouts))) { + if (fsExtra.existsSync(path.resolve(this.options.srcDir, this.options.dir.layouts))) { const layoutsFiles = await glob(`${this.options.dir.layouts}/**/*.{vue,js}`, { cwd: this.options.srcDir, ignore: this.options.ignore @@ -269,7 +269,7 @@ export default class Builder { } // If no default layout, create its folder and add the default folder if (!templateVars.layouts.default) { - await mkdirp(r(this.options.buildDir, 'layouts')) + await fsExtra.mkdirp(r(this.options.buildDir, 'layouts')) templatesFiles.push('layouts/default.vue') templateVars.layouts.default = './layouts/default.vue' } @@ -330,7 +330,7 @@ export default class Builder { // Resolve template files const customTemplateFiles = this.options.build.templates.map( - t => t.dst || basename(t.src || t) + t => t.dst || path.basename(t.src || t) ) templatesFiles = templatesFiles @@ -341,7 +341,7 @@ export default class Builder { } // Allow override templates using a file with same name in ${srcDir}/app const customPath = r(this.options.srcDir, 'app', file) - const customFileExists = existsSync(customPath) + const customFileExists = fsExtra.existsSync(customPath) return { src: customFileExists ? customPath : r(this.options.nuxtAppDir, file), @@ -358,7 +358,7 @@ export default class Builder { return Object.assign( { src: r(this.options.srcDir, t.src || t), - dst: t.dst || basename(t.src || t), + dst: t.dst || path.basename(t.src || t), custom: true }, t @@ -368,7 +368,7 @@ export default class Builder { // -- Loading indicator -- if (this.options.loadingIndicator.name) { - const indicatorPath1 = resolve( + const indicatorPath1 = path.resolve( this.options.nuxtAppDir, 'views/loading', this.options.loadingIndicator.name + '.html' @@ -376,9 +376,9 @@ export default class Builder { const indicatorPath2 = this.nuxt.resolveAlias( this.options.loadingIndicator.name ) - const indicatorPath = existsSync(indicatorPath1) + const indicatorPath = fsExtra.existsSync(indicatorPath1) ? indicatorPath1 - : existsSync(indicatorPath2) ? indicatorPath2 : null + : fsExtra.existsSync(indicatorPath2) ? indicatorPath2 : null if (indicatorPath) { templatesFiles.push({ src: indicatorPath, @@ -408,7 +408,7 @@ export default class Builder { // Add template to watchers this.options.build.watch.push(src) // Render template to dst - const fileContent = await readFile(src, 'utf8') + const fileContent = await fsExtra.readFile(src, 'utf8') let content try { const template = _.template(fileContent, { @@ -435,11 +435,11 @@ export default class Builder { /* istanbul ignore next */ throw new Error(`Could not compile template ${src}: ${err.message}`) } - const path = r(this.options.buildDir, dst) + const _path = r(this.options.buildDir, dst) // Ensure parent dir exits - await mkdirp(dirname(path)) + await fsExtra.mkdirp(path.dirname(_path)) // Write file - await writeFile(path, content, 'utf8') + await fsExtra.writeFile(_path, content, 'utf8') }) ) @@ -544,7 +544,7 @@ export default class Builder { debug('Adding webpack middleware...') // Create webpack dev middleware - this.webpackDevMiddleware = promisify( + this.webpackDevMiddleware = util.promisify( webpackDevMiddleware( compiler, Object.assign( @@ -559,9 +559,9 @@ export default class Builder { ) ) - this.webpackDevMiddleware.close = promisify(this.webpackDevMiddleware.close) + this.webpackDevMiddleware.close = util.promisify(this.webpackDevMiddleware.close) - this.webpackHotMiddleware = promisify( + this.webpackHotMiddleware = util.promisify( webpackHotMiddleware( compiler, Object.assign( @@ -644,9 +644,9 @@ export default class Builder { // TODO: remove ignore when generateConfig enabled again async generateConfig() /* istanbul ignore next */ { - const config = resolve(this.options.buildDir, 'build.config.js') + const config = path.resolve(this.options.buildDir, 'build.config.js') const options = _.omit(this.options, Options.unsafeKeys) - await writeFile( + await fsExtra.writeFile( config, `export default ${JSON.stringify(options, null, ' ')}`, 'utf8' diff --git a/lib/builder/generator.js b/lib/builder/generator.mjs similarity index 82% rename from lib/builder/generator.js rename to lib/builder/generator.mjs index bf1995b28b..0da99d3878 100644 --- a/lib/builder/generator.js +++ b/lib/builder/generator.mjs @@ -1,8 +1,8 @@ import _ from 'lodash' -import { resolve, join, dirname, sep } from 'path' -import { minify } from 'html-minifier' +import path from 'path' +import htmlMinifier from 'html-minifier' import Chalk from 'chalk' -import { copy, remove, writeFile, mkdirp, removeSync, existsSync } from 'fs-extra' +import fsExtra from 'fs-extra' import { isUrl, promisifyRoute, waitFor, flatRoutes, printWarn, createSpinner } from '../common/utils' export default class Generator { @@ -12,10 +12,10 @@ export default class Generator { this.builder = builder // Set variables - this.staticRoutes = resolve(this.options.srcDir, this.options.dir.static) - this.srcBuiltPath = resolve(this.options.buildDir, 'dist') - this.distPath = resolve(this.options.rootDir, this.options.generate.dir) - this.distNuxtPath = join( + this.staticRoutes = path.resolve(this.options.srcDir, this.options.dir.static) + this.srcBuiltPath = path.resolve(this.options.buildDir, 'dist') + this.distPath = path.resolve(this.options.rootDir, this.options.generate.dir) + this.distNuxtPath = path.join( this.distPath, isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath ) @@ -127,7 +127,7 @@ export default class Generator { if (isHandled) { line += Chalk.grey(JSON.stringify(error, undefined, 2) + '\n') } else { - line += Chalk.grey(error.toString()) + line += Chalk.grey(error.stack) } return line @@ -141,36 +141,36 @@ export default class Generator { // Disable SPA fallback if value isn't true or a string if (fallback !== true && typeof fallback !== 'string') return - const fallbackPath = join(this.distPath, fallback) + const fallbackPath = path.join(this.distPath, fallback) // Prevent conflicts - if (existsSync(fallbackPath)) { + if (fsExtra.existsSync(fallbackPath)) { printWarn(`SPA fallback was configured, but the configured path (${fallbackPath}) already exists.`) return } // Render and write the SPA template to the fallback path const { html } = await this.nuxt.renderRoute('/', { spa: true }) - await writeFile(fallbackPath, html, 'utf8') + await fsExtra.writeFile(fallbackPath, html, 'utf8') } async initDist() { // Clean destination folder - await remove(this.distPath) + await fsExtra.remove(this.distPath) await this.nuxt.callHook('generate:distRemoved', this) // Copy static and built files /* istanbul ignore if */ - if (existsSync(this.staticRoutes)) { - await copy(this.staticRoutes, this.distPath) + if (fsExtra.existsSync(this.staticRoutes)) { + await fsExtra.copy(this.staticRoutes, this.distPath) } - await copy(this.srcBuiltPath, this.distNuxtPath) + await fsExtra.copy(this.srcBuiltPath, this.distNuxtPath) // Add .nojekyll file to let Github Pages add the _nuxt/ folder // https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/ - const nojekyllPath = resolve(this.distPath, '.nojekyll') - writeFile(nojekyllPath, '') + const nojekyllPath = path.resolve(this.distPath, '.nojekyll') + fsExtra.writeFile(nojekyllPath, '') // Cleanup SSR related files const extraFiles = [ @@ -178,11 +178,11 @@ export default class Generator { 'index.ssr.html', 'server-bundle.json', 'vue-ssr-client-manifest.json' - ].map(file => resolve(this.distNuxtPath, file)) + ].map(file => path.resolve(this.distNuxtPath, file)) extraFiles.forEach(file => { - if (existsSync(file)) { - removeSync(file) + if (fsExtra.existsSync(file)) { + fsExtra.removeSync(file) } }) @@ -238,7 +238,7 @@ export default class Generator { if (this.options.generate.minify) { try { - html = minify(html, this.options.generate.minify) + html = htmlMinifier.minify(html, this.options.generate.minify) } catch (err) /* istanbul ignore next */ { const minifyErr = new Error( `HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}` @@ -250,22 +250,22 @@ export default class Generator { let path if (this.options.generate.subFolders) { - path = join(route, sep, 'index.html') // /about -> /about/index.html + path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html path = path === '/404/index.html' ? '/404.html' : path // /404 -> /404.html } else { path = - route.length > 1 ? join(sep, route + '.html') : join(sep, 'index.html') + route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html') } // Call hook to let user update the path & html const page = { route, path, html } await this.nuxt.callHook('generate:page', page) - page.path = join(this.distPath, page.path) + page.path = path.join(this.distPath, page.path) // Make sure the sub folders are created - await mkdirp(dirname(page.path)) - await writeFile(page.path, page.html, 'utf8') + await fsExtra.mkdirp(path.dirname(page.path)) + await fsExtra.writeFile(page.path, page.html, 'utf8') await this.nuxt.callHook('generate:routeCreated', { route, diff --git a/lib/builder/index.js b/lib/builder/index.mjs similarity index 100% rename from lib/builder/index.js rename to lib/builder/index.mjs diff --git a/lib/builder/webpack/base.config.js b/lib/builder/webpack/base.config.mjs similarity index 91% rename from lib/builder/webpack/base.config.js rename to lib/builder/webpack/base.config.mjs index b60e72eaaa..9cfc79c933 100644 --- a/lib/builder/webpack/base.config.js +++ b/lib/builder/webpack/base.config.mjs @@ -4,8 +4,8 @@ import WarnFixPlugin from './plugins/warnfix' import ProgressPlugin from './plugins/progress' import webpack from 'webpack' -import { cloneDeep } from 'lodash' -import { join, resolve } from 'path' +import _ from 'lodash' +import path from 'path' import { isUrl, urlJoin } from '../../common/utils' @@ -28,11 +28,11 @@ export default function webpackBaseConfig({ name, isServer }) { // Used by vue-loader so we can use in templates // with - configAlias[this.options.dir.assets] = join( + configAlias[this.options.dir.assets] = path.join( this.options.srcDir, this.options.dir.assets ) - configAlias[this.options.dir.static] = join( + configAlias[this.options.dir.static] = path.join( this.options.srcDir, this.options.dir.static ) @@ -45,7 +45,7 @@ export default function webpackBaseConfig({ name, isServer }) { }, optimization: {}, output: { - path: resolve(this.options.buildDir, 'dist'), + path: path.resolve(this.options.buildDir, 'dist'), filename: this.getFileName('app'), chunkFilename: this.getFileName('chunk'), publicPath: isUrl(this.options.build.publicPath) @@ -61,10 +61,10 @@ export default function webpackBaseConfig({ name, isServer }) { extensions: ['.js', '.json', '.vue', '.jsx'], alias: Object.assign( { - '~': join(this.options.srcDir), - '~~': join(this.options.rootDir), - '@': join(this.options.srcDir), - '@@': join(this.options.rootDir) + '~': path.join(this.options.srcDir), + '~~': path.join(this.options.rootDir), + '@': path.join(this.options.srcDir), + '@@': path.join(this.options.rootDir) }, configAlias ), @@ -159,5 +159,5 @@ export default function webpackBaseConfig({ name, isServer }) { } // Clone deep avoid leaking config between Client and Server - return cloneDeep(config) + return _.cloneDeep(config) } diff --git a/lib/builder/webpack/client.config.js b/lib/builder/webpack/client.config.mjs similarity index 91% rename from lib/builder/webpack/client.config.js rename to lib/builder/webpack/client.config.mjs index 0978e065b2..f5ec4a0248 100644 --- a/lib/builder/webpack/client.config.js +++ b/lib/builder/webpack/client.config.mjs @@ -1,14 +1,14 @@ -import { each } from 'lodash' +import _ from 'lodash' import webpack from 'webpack' // import VueSSRClientPlugin from 'vue-server-renderer/client-plugin' import VueSSRClientPlugin from './plugins/vue/client' import HTMLPlugin from 'html-webpack-plugin' import FriendlyErrorsWebpackPlugin from '@nuxtjs/friendly-errors-webpack-plugin' import StylishPlugin from 'webpack-stylish' -import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' -import { resolve } from 'path' +import BundleAnalyzer from 'webpack-bundle-analyzer' +import path from 'path' import Debug from 'debug' -import base from './base.config.js' +import base from './base.config' const debug = Debug('nuxt:build') debug.color = 2 // Force green color @@ -17,11 +17,11 @@ export default function webpackClientConfig() { let config = base.call(this, { name: 'client', isServer: false }) // Entry points - config.entry.app = resolve(this.options.buildDir, 'client.js') + config.entry.app = path.resolve(this.options.buildDir, 'client.js') // Env object defined in nuxt.config.js let env = {} - each(this.options.env, (value, key) => { + _.each(this.options.env, (value, key) => { env['process.env.' + key] = ['boolean', 'number'].indexOf(typeof value) !== -1 ? value @@ -128,7 +128,7 @@ export default function webpackClientConfig() { // Webpack Bundle Analyzer if (this.options.build.analyze) { config.plugins.push( - new BundleAnalyzerPlugin(Object.assign({}, this.options.build.analyze)) + new BundleAnalyzer.BundleAnalyzerPlugin(Object.assign({}, this.options.build.analyze)) ) } } diff --git a/lib/builder/webpack/plugins/progress.js b/lib/builder/webpack/plugins/progress.mjs similarity index 100% rename from lib/builder/webpack/plugins/progress.js rename to lib/builder/webpack/plugins/progress.mjs diff --git a/lib/builder/webpack/plugins/vue/client.js b/lib/builder/webpack/plugins/vue/client.mjs similarity index 100% rename from lib/builder/webpack/plugins/vue/client.js rename to lib/builder/webpack/plugins/vue/client.mjs diff --git a/lib/builder/webpack/plugins/vue/server.js b/lib/builder/webpack/plugins/vue/server.mjs similarity index 100% rename from lib/builder/webpack/plugins/vue/server.js rename to lib/builder/webpack/plugins/vue/server.mjs diff --git a/lib/builder/webpack/plugins/vue/util.js b/lib/builder/webpack/plugins/vue/util.mjs similarity index 100% rename from lib/builder/webpack/plugins/vue/util.js rename to lib/builder/webpack/plugins/vue/util.mjs diff --git a/lib/builder/webpack/plugins/warnfix.js b/lib/builder/webpack/plugins/warnfix.mjs similarity index 100% rename from lib/builder/webpack/plugins/warnfix.js rename to lib/builder/webpack/plugins/warnfix.mjs diff --git a/lib/builder/webpack/postcss.js b/lib/builder/webpack/postcss.mjs similarity index 81% rename from lib/builder/webpack/postcss.js rename to lib/builder/webpack/postcss.mjs index a8488d996a..b789f1fd99 100644 --- a/lib/builder/webpack/postcss.js +++ b/lib/builder/webpack/postcss.mjs @@ -1,11 +1,11 @@ -import { existsSync } from 'fs' -import { resolve, join } from 'path' -import { cloneDeep } from 'lodash' +import fs from 'fs' +import path from 'path' +import ـ from 'lodash' import { isPureObject } from '../../common/utils' import createResolver from 'postcss-import-resolver' export default function postcssConfig() { - let config = cloneDeep(this.options.build.postcss) + let config = ـ.cloneDeep(this.options.build.postcss) /* istanbul ignore if */ if (!config) { @@ -22,8 +22,8 @@ export default function postcssConfig() { '.postcssrc.json', '.postcssrc.yaml' ]) { - if (existsSync(resolve(dir, file))) { - const postcssConfigPath = resolve(dir, file) + if (fs.existsSync(path.resolve(dir, file))) { + const postcssConfigPath = path.resolve(dir, file) return { sourceMap: this.options.build.cssSourceMap, config: { @@ -50,10 +50,10 @@ export default function postcssConfig() { 'postcss-import': { resolve: createResolver({ alias: { - '~': join(this.options.srcDir), - '~~': join(this.options.rootDir), - '@': join(this.options.srcDir), - '@@': join(this.options.rootDir) + '~': path.join(this.options.srcDir), + '~~': path.join(this.options.rootDir), + '@': path.join(this.options.srcDir), + '@@': path.join(this.options.rootDir) }, modules: [ this.options.srcDir, diff --git a/lib/builder/webpack/server.config.js b/lib/builder/webpack/server.config.mjs similarity index 90% rename from lib/builder/webpack/server.config.js rename to lib/builder/webpack/server.config.mjs index d91b146828..a92b6d7a18 100644 --- a/lib/builder/webpack/server.config.js +++ b/lib/builder/webpack/server.config.mjs @@ -2,10 +2,10 @@ import webpack from 'webpack' // import VueSSRServerPlugin from 'vue-server-renderer/server-plugin' import VueSSRServerPlugin from './plugins/vue/server' import nodeExternals from 'webpack-node-externals' -import { each } from 'lodash' -import { resolve } from 'path' -import { existsSync } from 'fs' -import base from './base.config.js' +import _ from 'lodash' +import path from 'path' +import fs from 'fs' +import base from './base.config' /* |-------------------------------------------------------------------------- @@ -17,7 +17,7 @@ export default function webpackServerConfig() { // Env object defined in nuxt.config.js let env = {} - each(this.options.env, (value, key) => { + _.each(this.options.env, (value, key) => { env['process.env.' + key] = ['boolean', 'number'].indexOf(typeof value) !== -1 ? value @@ -30,7 +30,7 @@ export default function webpackServerConfig() { config = Object.assign(config, { target: 'node', node: false, - entry: resolve(this.options.buildDir, 'server.js'), + entry: path.resolve(this.options.buildDir, 'server.js'), output: Object.assign({}, config.output, { filename: 'server-bundle.js', libraryTarget: 'commonjs2' @@ -60,7 +60,7 @@ export default function webpackServerConfig() { // https://webpack.js.org/configuration/externals/#externals // https://github.com/liady/webpack-node-externals this.options.modulesDir.forEach(dir => { - if (existsSync(dir)) { + if (fs.existsSync(dir)) { config.externals.push( nodeExternals({ // load non-javascript files with extensions, presumably via loaders diff --git a/lib/builder/webpack/style-loader.js b/lib/builder/webpack/style-loader.js index 9c5e2b500e..c7f15e3797 100644 --- a/lib/builder/webpack/style-loader.js +++ b/lib/builder/webpack/style-loader.js @@ -1,5 +1,5 @@ import ExtractTextPlugin from 'extract-text-webpack-plugin' -import { join } from 'path' +import path from 'path' import postcssConfig from './postcss' export default function styleLoader(ext, loaders = [], isVueLoader = false) { @@ -57,8 +57,8 @@ export default function styleLoader(ext, loaders = [], isVueLoader = false) { // css-loader // https://github.com/webpack-contrib/css-loader const cssLoaderAlias = {} - cssLoaderAlias[`/${this.options.dir.assets}`] = join(this.options.srcDir, this.options.dir.assets) - cssLoaderAlias[`/${this.options.dir.static}`] = join(this.options.srcDir, this.options.dir.static) + cssLoaderAlias[`/${this.options.dir.assets}`] = path.join(this.options.srcDir, this.options.dir.assets) + cssLoaderAlias[`/${this.options.dir.static}`] = path.join(this.options.srcDir, this.options.dir.static) loaders.unshift({ loader: 'css-loader', diff --git a/lib/builder/webpack/vue-loader.js b/lib/builder/webpack/vue-loader.mjs similarity index 100% rename from lib/builder/webpack/vue-loader.js rename to lib/builder/webpack/vue-loader.mjs diff --git a/lib/common/modes.js b/lib/common/modes.mjs similarity index 100% rename from lib/common/modes.js rename to lib/common/modes.mjs diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index a1505263c8..8a30262ae5 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -1,4 +1,4 @@ -import { resolve } from 'path' +import path from 'path' export default { mode: 'universal', @@ -6,8 +6,8 @@ export default { debug: undefined, // Will be equal to dev if not provided buildDir: '.nuxt', cacheDir: '.cache', - nuxtDir: resolve(__dirname, '../..'), - nuxtAppDir: resolve(__dirname, '../app'), + nuxtDir: path.resolve(__dirname, '../..'), + nuxtAppDir: path.resolve(__dirname, '../app'), modulesDir: ['node_modules'], // ~> relative to options.rootDir ignorePrefix: '-', ignore: [ diff --git a/lib/common/options.js b/lib/common/options.mjs similarity index 85% rename from lib/common/options.js rename to lib/common/options.mjs index b3ecf1212b..e97411b773 100644 --- a/lib/common/options.js +++ b/lib/common/options.mjs @@ -1,7 +1,7 @@ import _ from 'lodash' import Debug from 'debug' -import { join, resolve } from 'path' -import { existsSync, readdirSync } from 'fs' +import path from 'path' +import fs from 'fs' import { isUrl, isPureObject } from '../common/utils' @@ -59,17 +59,17 @@ Options.from = function (_options) { // Resolve dirs options.srcDir = hasValue(options.srcDir) - ? resolve(options.rootDir, options.srcDir) + ? path.resolve(options.rootDir, options.srcDir) : options.rootDir - options.buildDir = resolve(options.rootDir, options.buildDir) - options.cacheDir = resolve(options.rootDir, options.cacheDir) + options.buildDir = path.resolve(options.rootDir, options.buildDir) + options.cacheDir = path.resolve(options.rootDir, options.cacheDir) // Populate modulesDir options.modulesDir = [] .concat(options.modulesDir) - .concat(join(options.nuxtDir, 'node_modules')) + .concat(path.join(options.nuxtDir, 'node_modules')) .filter(dir => hasValue(dir)) - .map(dir => resolve(options.rootDir, dir)) + .map(dir => path.resolve(options.rootDir, dir)) // Sanitize extensions if (options.extensions.indexOf('js') === -1) { @@ -77,9 +77,9 @@ Options.from = function (_options) { } // If app.html is defined, set the template path to the user template - options.appTemplatePath = resolve(options.buildDir, 'views/app.template.html') - if (existsSync(join(options.srcDir, 'app.html'))) { - options.appTemplatePath = join(options.srcDir, 'app.html') + options.appTemplatePath = path.resolve(options.buildDir, 'views/app.template.html') + if (fs.existsSync(path.join(options.srcDir, 'app.html'))) { + options.appTemplatePath = path.join(options.srcDir, 'app.html') } // Ignore publicPath on dev @@ -91,8 +91,8 @@ Options.from = function (_options) { // If store defined, update store options to true unless explicitly disabled if ( options.store !== false && - existsSync(join(options.srcDir, options.dir.store)) && - readdirSync(join(options.srcDir, options.dir.store)) + fs.existsSync(path.join(options.srcDir, options.dir.store)) && + fs.readdirSync(path.join(options.srcDir, options.dir.store)) .find(filename => filename !== 'README.md' && filename[0] !== '.') ) { options.store = true diff --git a/lib/common/utils.js b/lib/common/utils.mjs similarity index 93% rename from lib/common/utils.js rename to lib/common/utils.mjs index fb3b55e2c8..e22d4eed3d 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.mjs @@ -1,4 +1,4 @@ -import { resolve, relative, sep } from 'path' +import path from 'path' import _ from 'lodash' import Chalk from 'chalk' import ORA from 'ora' @@ -10,7 +10,7 @@ export const printWarn = function (msg, from) { } export const renderError = function (_error, from) { - const errStr = _error.toString() + const errStr = _error.stack const fromStr = from ? Chalk.red(` ${from}`) : '' return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + '\n\n' + errStr } @@ -181,7 +181,7 @@ export const wChunk = function wChunk(p = '') { } const reqSep = /\//g -const sysSep = _.escapeRegExp(sep) +const sysSep = _.escapeRegExp(path.sep) const normalize = string => string.replace(reqSep, sysSep) export const r = function r() { @@ -192,7 +192,7 @@ export const r = function r() { return wp(lastArg) } - return wp(resolve(...args.map(normalize))) + return wp(path.resolve(...args.map(normalize))) } export const relativeTo = function relativeTo() { @@ -200,36 +200,37 @@ export const relativeTo = function relativeTo() { let dir = args.shift() // Resolve path - let path = r(...args) + let _path = r(...args) // Check if path is an alias - if (path.indexOf('@') === 0 || path.indexOf('~') === 0) { - return path + if (_path.indexOf('@') === 0 || _path.indexOf('~') === 0) { + return _path } // Make correct relative path - let rp = relative(dir, path) + let rp = path.relative(dir, _path) if (rp[0] !== '.') { rp = './' + rp } + return wp(rp) } -export const flatRoutes = function flatRoutes(router, path = '', routes = []) { +export const flatRoutes = function flatRoutes(router, _path = '', routes = []) { router.forEach(r => { if (!r.path.includes(':') && !r.path.includes('*')) { /* istanbul ignore if */ if (r.children) { - if (path === '' && r.path === '/') { + if (_path === '' && r.path === '/') { routes.push('/') } - flatRoutes(r.children, path + r.path + '/', routes) + flatRoutes(r.children, _path + r.path + '/', routes) } else { - path = path.replace(/^\/+$/, '/') + _path = _path.replace(/^\/+$/, '/') routes.push( - (r.path === '' && path[path.length - 1] === '/' - ? path.slice(0, -1) - : path) + r.path + (r.path === '' && _path[_path.length - 1] === '/' + ? _path.slice(0, -1) + : _path) + r.path ) } } diff --git a/lib/core/index.js b/lib/core/index.mjs similarity index 100% rename from lib/core/index.js rename to lib/core/index.mjs diff --git a/lib/core/meta.js b/lib/core/meta.mjs similarity index 100% rename from lib/core/meta.js rename to lib/core/meta.mjs diff --git a/lib/core/middleware/error.js b/lib/core/middleware/error.mjs similarity index 88% rename from lib/core/middleware/error.js rename to lib/core/middleware/error.mjs index 3b528992d6..39a8d60738 100644 --- a/lib/core/middleware/error.js +++ b/lib/core/middleware/error.mjs @@ -1,6 +1,6 @@ import Youch from '@nuxtjs/youch' -import { join, resolve, relative, isAbsolute } from 'path' -import { readFile } from 'fs-extra' +import path from 'path' +import fs from 'fs-extra' export default function errorMiddleware(err, req, res, next) { // ensure statusCode, message and name fields @@ -83,20 +83,20 @@ async function readSource(frame) { const searchPath = [ this.options.srcDir, this.options.rootDir, - join(this.options.buildDir, 'dist'), + path.join(this.options.buildDir, 'dist'), this.options.buildDir, process.cwd() ] // Scan filesystem for real source for (let pathDir of searchPath) { - let fullPath = resolve(pathDir, frame.fileName) - let source = await readFile(fullPath, 'utf-8').catch(() => null) + let fullPath = path.resolve(pathDir, frame.fileName) + let source = await fs.readFile(fullPath, 'utf-8').catch(() => null) if (source) { frame.contents = source frame.fullPath = fullPath - if (isAbsolute(frame.fileName)) { - frame.fileName = relative(this.options.rootDir, fullPath) + if (path.isAbsolute(frame.fileName)) { + frame.fileName = path.relative(this.options.rootDir, fullPath) } return } diff --git a/lib/core/middleware/nuxt.js b/lib/core/middleware/nuxt.mjs similarity index 100% rename from lib/core/middleware/nuxt.js rename to lib/core/middleware/nuxt.mjs diff --git a/lib/core/module.js b/lib/core/module.mjs similarity index 100% rename from lib/core/module.js rename to lib/core/module.mjs diff --git a/lib/core/nuxt.js b/lib/core/nuxt.mjs similarity index 82% rename from lib/core/nuxt.js rename to lib/core/nuxt.mjs index ac6df46298..97add02345 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.mjs @@ -1,16 +1,16 @@ import Debug from 'debug' import enableDestroy from 'server-destroy' import Module from 'module' -import { isPlainObject } from 'lodash' +import _ from 'lodash' import chalk from 'chalk' -import { existsSync } from 'fs-extra' +import fs from 'fs-extra' +import path from 'path' import Options from '../common/options' import { sequence, printError } from '../common/utils' -import { resolve, join } from 'path' -import { version } from '../../package.json' +import packageJSON from '../../package.json' import ModuleContainer from './module' import Renderer from './renderer' -import { requireModule } from '../common/module' +import moduleUtil from '../common/module' const debug = Debug('nuxt:') debug.color = 5 @@ -42,7 +42,7 @@ export default class Nuxt { } static get version() { - return version + return packageJSON.version } async ready() { @@ -51,7 +51,7 @@ export default class Nuxt { } // Add hooks - if (isPlainObject(this.options.hooks)) { + if (_.isPlainObject(this.options.hooks)) { this.addObjectHooks(this.options.hooks) } else if (typeof this.options.hooks === 'function') { this.options.hooks(this.hook) @@ -157,22 +157,22 @@ export default class Nuxt { }) } - resolveAlias(path) { - if (path.indexOf('@@') === 0 || path.indexOf('~~') === 0) { - return join(this.options.rootDir, path.substr(2)) + resolveAlias(_path) { + if (_path.indexOf('@@') === 0 || _path.indexOf('~~') === 0) { + return path.join(this.options.rootDir, _path.substr(2)) } - if (path.indexOf('@') === 0 || path.indexOf('~') === 0) { - return join(this.options.srcDir, path.substr(1)) + if (_path.indexOf('@') === 0 || _path.indexOf('~') === 0) { + return path.join(this.options.srcDir, _path.substr(1)) } - return resolve(this.options.srcDir, path) + return path.resolve(this.options.srcDir, _path) } - resolvePath(path) { + resolvePath(_path) { // Try to resolve using NPM resolve path first try { - const resolvedPath = Module._resolveFilename(path, { + const resolvedPath = Module._resolveFilename(_path, { paths: this.options.modulesDir }) return resolvedPath @@ -182,23 +182,23 @@ export default class Nuxt { } } - let _path = this.resolveAlias(path) + let __path = this.resolveAlias(_path) - if (existsSync(_path)) { - return _path + if (fs.existsSync(__path)) { + return __path } for (let ext of this.options.extensions) { - if (existsSync(_path + '.' + ext)) { + if (fs.existsSync(_path + '.' + ext)) { return _path + '.' + ext } } - throw new Error(`Cannot resolve "${path}" from "${_path}"`) + throw new Error(`Cannot resolve "${_path}" from "${__path}"`) } requireModule(name) { - return requireModule(this.resolvePath(name)) + return moduleUtil.requireModule(this.resolvePath(name)) } async close(callback) { diff --git a/lib/core/renderer.js b/lib/core/renderer.mjs similarity index 94% rename from lib/core/renderer.js rename to lib/core/renderer.mjs index 396e59f45b..203e6cbfa2 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.mjs @@ -3,9 +3,9 @@ import serialize from 'serialize-javascript' import serveStatic from 'serve-static' import compression from 'compression' import _ from 'lodash' -import { join, resolve } from 'path' +import path from 'path' import fs from 'fs-extra' -import { createBundleRenderer } from 'vue-server-renderer' +import vueServerRenderer from 'vue-server-renderer' import Debug from 'debug' import connect from 'connect' import launchMiddleware from 'launch-editor-middleware' @@ -66,18 +66,18 @@ export default class Renderer { } async loadResources(_fs = fs) { - let distPath = resolve(this.options.buildDir, 'dist') + let distPath = path.resolve(this.options.buildDir, 'dist') let updated = [] resourceMap.forEach(({ key, fileName, transform }) => { let rawKey = '$$' + key - const path = join(distPath, fileName) + const _path = path.join(distPath, fileName) let rawData, data - if (!_fs.existsSync(path)) { + if (!_fs.existsSync(_path)) { return // Resource not exists } - rawData = _fs.readFileSync(path, 'utf8') + rawData = _fs.readFileSync(_path, 'utf8') if (!rawData || rawData === this.resources[rawKey]) { return // No changes } @@ -92,7 +92,7 @@ export default class Renderer { }) // Reload error template - const errorTemplatePath = resolve(this.options.buildDir, 'views/error.html') + const errorTemplatePath = path.resolve(this.options.buildDir, 'views/error.html') if (fs.existsSync(errorTemplatePath)) { this.resources.errorTemplate = parseTemplate( fs.readFileSync(errorTemplatePath, 'utf8') @@ -100,7 +100,7 @@ export default class Renderer { } // Load loading template - const loadingHTMLPath = resolve(this.options.buildDir, 'loading.html') + const loadingHTMLPath = path.resolve(this.options.buildDir, 'loading.html') if (fs.existsSync(loadingHTMLPath)) { this.resources.loadingHTML = fs.readFileSync(loadingHTMLPath, 'utf8') this.resources.loadingHTML = this.resources.loadingHTML.replace( @@ -162,7 +162,7 @@ export default class Renderer { } // Create bundle renderer for SSR - this.bundleRenderer = createBundleRenderer( + this.bundleRenderer = vueServerRenderer.createBundleRenderer( this.resources.serverBundle, Object.assign( { @@ -248,7 +248,7 @@ export default class Renderer { // For serving static/ files to / const staticMiddleware = serveStatic( - resolve(this.options.srcDir, this.options.dir.static), + path.resolve(this.options.srcDir, this.options.dir.static), this.options.render.static ) staticMiddleware.prefix = this.options.render.static.prefix @@ -257,7 +257,7 @@ export default class Renderer { // Serve .nuxt/dist/ files only for production // For dev they will be served with devMiddleware if (!this.options.dev) { - const distDir = resolve(this.options.buildDir, 'dist') + const distDir = path.resolve(this.options.buildDir, 'dist') this.useMiddleware({ path: this.publicPath, handler: serveStatic(distDir, { @@ -401,7 +401,7 @@ export default class Renderer { /* istanbul ignore if */ if (!jsdom) { try { - jsdom = require('jsdom') + jsdom = this.nuxt.requireModule('jsdom') } catch (e) /* istanbul ignore next */ { /* eslint-disable no-console */ console.error('Fail when calling nuxt.renderAndGetWindow(url)') From efd7c1b4d1b0ccd0e0c2e94300a91132e5bca181 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 22:51:55 +0330 Subject: [PATCH 07/15] fix typos in generator --- lib/builder/generator.mjs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/builder/generator.mjs b/lib/builder/generator.mjs index 0da99d3878..675e86529d 100644 --- a/lib/builder/generator.mjs +++ b/lib/builder/generator.mjs @@ -247,18 +247,17 @@ export default class Generator { } } - let path + let _path if (this.options.generate.subFolders) { - path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html - path = path === '/404/index.html' ? '/404.html' : path // /404 -> /404.html + _path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html + _path = _path === '/404/index.html' ? '/404.html' : _path // /404 -> /404.html } else { - path = - route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html') + _path = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html') } // Call hook to let user update the path & html - const page = { route, path, html } + const page = { route, path: _path, html } await this.nuxt.callHook('generate:page', page) page.path = path.join(this.distPath, page.path) From 46216e65484588fc7318c53bb3d0eebeb8a5c080 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:22:03 +0330 Subject: [PATCH 08/15] improve eslint --- .eslintrc.js | 37 +++++++++++++++++++++++++++++++------ package.json | 2 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 451755e074..861dfbbdd1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,20 +8,44 @@ module.exports = { browser: true, node: true }, - extends: ['standard', 'standard-jsx'], + extends: [ + 'standard', + 'standard-jsx', + 'plugin:import/errors', + 'plugin:import/warnings' + ], // required to lint *.vue files plugins: [ 'html' ], - // add your custom rules here + settings: { + 'import/resolver': { + node: { extensions: ['.js', '.mjs'] } + } + }, rules: { - // allow paren-less arrow functions + // Enforce import order + 'import/order': 2, + + // Imports should come first + 'import/first': 2, + + // Other import rules + "import/no-mutable-exports": 2, + + // Allow unresolved imports + 'import/no-unresolved': 0, + + // Allow paren-less arrow functions 'arrow-parens': 0, - // allow async-await + + // Allow async-await 'generator-star-spacing': 0, - // allow debugger during development + + // Allow debugger during development 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, - // do not allow console.logs etc... + + // Do not allow console.logs etc... 'no-console': 2, 'space-before-function-paren': [ 2, @@ -31,5 +55,6 @@ module.exports = { } ], }, + globals: {} } diff --git a/package.json b/package.json index 666498e419..dee2e2f39a 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "test": "npm run lint && nyc ava --verbose test/ -- && nyc report --reporter=html", "test-appveyor": "npm run lint && nyc ava --serial test/ -- && nyc report --reporter=html", "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", - "lint": "eslint --ext .js,.vue bin/* build/ lib/ test/ examples/", + "lint": "eslint --ext .js,.mjs,.vue bin/* build/ lib/ test/ examples/", "precommit": "npm run lint", "postinstall": "opencollective postinstall || exit 0" }, From ed5bfacb4d2cf34abd8f5daa360ab4f649913159 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:22:17 +0330 Subject: [PATCH 09/15] eslint: fix import/order --- bin/common/utils.js | 4 +++- bin/nuxt-start | 2 +- examples/with-ava/test/index.test.js | 3 ++- examples/with-sockets/server.js | 3 ++- examples/with-vux/nuxt.config.js | 3 ++- lib/builder/builder.mjs | 9 ++++++--- lib/builder/generator.mjs | 4 +++- lib/builder/webpack/base.config.mjs | 8 ++++---- lib/builder/webpack/client.config.mjs | 9 ++++++--- lib/builder/webpack/postcss.mjs | 4 +++- lib/builder/webpack/server.config.mjs | 13 ++++++++----- lib/builder/webpack/style-loader.js | 4 +++- lib/common/options.mjs | 5 +++-- lib/core/middleware/error.mjs | 3 ++- lib/core/nuxt.mjs | 9 ++++++--- lib/core/renderer.mjs | 5 +++-- test/basic.config.defaults.test.js | 4 +++- test/basic.csr.test.js | 5 ++++- test/basic.fail.generate.test.js | 5 ++++- test/basic.generate.test.js | 11 +++++++---- test/basic.ssr.csp.test.js | 5 ++++- test/basic.ssr.test.js | 5 ++++- test/children.patch.test.js | 5 ++++- test/children.test.js | 5 ++++- test/dynamic-routes.test.js | 7 +++++-- test/express.test.js | 6 ++++-- test/fallback.generate.test.js | 11 +++++++---- test/module.test.js | 5 ++++- test/with-config.test.js | 5 ++++- 29 files changed, 115 insertions(+), 52 deletions(-) diff --git a/bin/common/utils.js b/bin/common/utils.js index 4f352ff2e6..27c4fb138b 100644 --- a/bin/common/utils.js +++ b/bin/common/utils.js @@ -1,6 +1,8 @@ -const { Utils } = require('../..') + const { resolve } = require('path') const { existsSync } = require('fs') + +const { Utils } = require('../..') const { requireModule } = require('../../lib/common/module') const getRootDir = argv => resolve(argv._[0] || '.') diff --git a/bin/nuxt-start b/bin/nuxt-start index 7033e1711f..ca9c2e63e9 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -2,8 +2,8 @@ /* eslint-disable no-console */ const fs = require('fs') -const parseArgs = require('minimist') const { resolve } = require('path') +const parseArgs = require('minimist') const { Nuxt, Utils } = require('..') const { loadNuxtConfig, getLatestHost } = require('./common/utils') diff --git a/examples/with-ava/test/index.test.js b/examples/with-ava/test/index.test.js index 47ce1fadec..431f98f41f 100755 --- a/examples/with-ava/test/index.test.js +++ b/examples/with-ava/test/index.test.js @@ -1,6 +1,7 @@ +import { resolve } from 'path' + import test from 'ava' import { Nuxt, Builder } from 'nuxt' -import { resolve } from 'path' // We keep the nuxt and server instance // So we can close them at the end of the test diff --git a/examples/with-sockets/server.js b/examples/with-sockets/server.js index 017d62a609..0e668f3ccb 100644 --- a/examples/with-sockets/server.js +++ b/examples/with-sockets/server.js @@ -1,6 +1,7 @@ +import http from 'http' + import { Nuxt, Builder } from 'nuxt' import express from 'express' -import http from 'http' import SocketIO from 'socket.io' const port = process.env.PORT || 3000 diff --git a/examples/with-vux/nuxt.config.js b/examples/with-vux/nuxt.config.js index a2133ab74a..4c9aaa4e4d 100644 --- a/examples/with-vux/nuxt.config.js +++ b/examples/with-vux/nuxt.config.js @@ -1,6 +1,7 @@ -import vuxLoader from 'vux-loader' import path from 'path' +import vuxLoader from 'vux-loader' + export default { head: { meta: [ diff --git a/lib/builder/builder.mjs b/lib/builder/builder.mjs index b9b4a8b21b..02060e9af7 100644 --- a/lib/builder/builder.mjs +++ b/lib/builder/builder.mjs @@ -1,22 +1,25 @@ import util from 'util' +import path from 'path' +import fs from 'fs' + import _ from 'lodash' import chokidar from 'chokidar' import fsExtra from 'fs-extra' -import fs from 'fs' import hash from 'hash-sum' import webpack from 'webpack' import serialize from 'serialize-javascript' -import path from 'path' import MFS from 'memory-fs' import webpackDevMiddleware from 'webpack-dev-middleware' import webpackHotMiddleware from 'webpack-hot-middleware' import Debug from 'debug' import Glob from 'glob' +import upath from 'upath' + import { r, wp, wChunk, createRoutes, parallel, relativeTo, waitFor, createSpinner } from '../common/utils' import Options from '../common/options' + import clientWebpackConfig from './webpack/client.config' import serverWebpackConfig from './webpack/server.config' -import upath from 'upath' const debug = Debug('nuxt:build') debug.color = 2 // Force green color diff --git a/lib/builder/generator.mjs b/lib/builder/generator.mjs index 675e86529d..063370d87f 100644 --- a/lib/builder/generator.mjs +++ b/lib/builder/generator.mjs @@ -1,5 +1,5 @@ -import _ from 'lodash' import path from 'path' +import _ from 'lodash' import htmlMinifier from 'html-minifier' import Chalk from 'chalk' import fsExtra from 'fs-extra' @@ -247,6 +247,8 @@ export default class Generator { } } + path = 123 + let _path if (this.options.generate.subFolders) { diff --git a/lib/builder/webpack/base.config.mjs b/lib/builder/webpack/base.config.mjs index 9cfc79c933..2400199c90 100644 --- a/lib/builder/webpack/base.config.mjs +++ b/lib/builder/webpack/base.config.mjs @@ -1,14 +1,14 @@ +import path from 'path' + import ExtractTextPlugin from 'extract-text-webpack-plugin' import TimeFixPlugin from 'time-fix-plugin' -import WarnFixPlugin from './plugins/warnfix' -import ProgressPlugin from './plugins/progress' - import webpack from 'webpack' import _ from 'lodash' -import path from 'path' import { isUrl, urlJoin } from '../../common/utils' +import WarnFixPlugin from './plugins/warnfix' +import ProgressPlugin from './plugins/progress' import vueLoader from './vue-loader' import styleLoader from './style-loader' diff --git a/lib/builder/webpack/client.config.mjs b/lib/builder/webpack/client.config.mjs index f5ec4a0248..be3ef6dc01 100644 --- a/lib/builder/webpack/client.config.mjs +++ b/lib/builder/webpack/client.config.mjs @@ -1,15 +1,18 @@ +import path from 'path' + import _ from 'lodash' import webpack from 'webpack' -// import VueSSRClientPlugin from 'vue-server-renderer/client-plugin' -import VueSSRClientPlugin from './plugins/vue/client' import HTMLPlugin from 'html-webpack-plugin' import FriendlyErrorsWebpackPlugin from '@nuxtjs/friendly-errors-webpack-plugin' import StylishPlugin from 'webpack-stylish' import BundleAnalyzer from 'webpack-bundle-analyzer' -import path from 'path' + import Debug from 'debug' import base from './base.config' +// import VueSSRClientPlugin from 'vue-server-renderer/client-plugin' +import VueSSRClientPlugin from './plugins/vue/client' + const debug = Debug('nuxt:build') debug.color = 2 // Force green color diff --git a/lib/builder/webpack/postcss.mjs b/lib/builder/webpack/postcss.mjs index b789f1fd99..9c3904aeb1 100644 --- a/lib/builder/webpack/postcss.mjs +++ b/lib/builder/webpack/postcss.mjs @@ -1,9 +1,11 @@ import fs from 'fs' import path from 'path' + import ـ from 'lodash' -import { isPureObject } from '../../common/utils' import createResolver from 'postcss-import-resolver' +import { isPureObject } from '../../common/utils' + export default function postcssConfig() { let config = ـ.cloneDeep(this.options.build.postcss) diff --git a/lib/builder/webpack/server.config.mjs b/lib/builder/webpack/server.config.mjs index a92b6d7a18..4cc202c866 100644 --- a/lib/builder/webpack/server.config.mjs +++ b/lib/builder/webpack/server.config.mjs @@ -1,12 +1,15 @@ -import webpack from 'webpack' -// import VueSSRServerPlugin from 'vue-server-renderer/server-plugin' -import VueSSRServerPlugin from './plugins/vue/server' -import nodeExternals from 'webpack-node-externals' -import _ from 'lodash' import path from 'path' import fs from 'fs' + +import webpack from 'webpack' +import nodeExternals from 'webpack-node-externals' +import _ from 'lodash' + import base from './base.config' +// import VueSSRServerPlugin from 'vue-server-renderer/server-plugin' +import VueSSRServerPlugin from './plugins/vue/server' + /* |-------------------------------------------------------------------------- | Webpack Server Config diff --git a/lib/builder/webpack/style-loader.js b/lib/builder/webpack/style-loader.js index c7f15e3797..2bbfc771ed 100644 --- a/lib/builder/webpack/style-loader.js +++ b/lib/builder/webpack/style-loader.js @@ -1,5 +1,7 @@ -import ExtractTextPlugin from 'extract-text-webpack-plugin' import path from 'path' + +import ExtractTextPlugin from 'extract-text-webpack-plugin' + import postcssConfig from './postcss' export default function styleLoader(ext, loaders = [], isVueLoader = false) { diff --git a/lib/common/options.mjs b/lib/common/options.mjs index e97411b773..ed5b0c8135 100644 --- a/lib/common/options.mjs +++ b/lib/common/options.mjs @@ -1,8 +1,9 @@ -import _ from 'lodash' -import Debug from 'debug' import path from 'path' import fs from 'fs' +import _ from 'lodash' +import Debug from 'debug' + import { isUrl, isPureObject } from '../common/utils' import modes from './modes' diff --git a/lib/core/middleware/error.mjs b/lib/core/middleware/error.mjs index 39a8d60738..b877c8406d 100644 --- a/lib/core/middleware/error.mjs +++ b/lib/core/middleware/error.mjs @@ -1,5 +1,6 @@ -import Youch from '@nuxtjs/youch' import path from 'path' + +import Youch from '@nuxtjs/youch' import fs from 'fs-extra' export default function errorMiddleware(err, req, res, next) { diff --git a/lib/core/nuxt.mjs b/lib/core/nuxt.mjs index 97add02345..321200498b 100644 --- a/lib/core/nuxt.mjs +++ b/lib/core/nuxt.mjs @@ -1,16 +1,19 @@ +import Module from 'module' +import path from 'path' + import Debug from 'debug' import enableDestroy from 'server-destroy' -import Module from 'module' import _ from 'lodash' import chalk from 'chalk' import fs from 'fs-extra' -import path from 'path' + import Options from '../common/options' import { sequence, printError } from '../common/utils' import packageJSON from '../../package.json' +import moduleUtil from '../common/module' + import ModuleContainer from './module' import Renderer from './renderer' -import moduleUtil from '../common/module' const debug = Debug('nuxt:') debug.color = 5 diff --git a/lib/core/renderer.mjs b/lib/core/renderer.mjs index 203e6cbfa2..ea2bb08f1f 100644 --- a/lib/core/renderer.mjs +++ b/lib/core/renderer.mjs @@ -1,15 +1,16 @@ +import path from 'path' +import crypto from 'crypto' + import ansiHTML from 'ansi-html' import serialize from 'serialize-javascript' import serveStatic from 'serve-static' import compression from 'compression' import _ from 'lodash' -import path from 'path' import fs from 'fs-extra' import vueServerRenderer from 'vue-server-renderer' import Debug from 'debug' import connect from 'connect' import launchMiddleware from 'launch-editor-middleware' -import crypto from 'crypto' import { setAnsiColors, isUrl, waitFor, timeout } from '../common/utils' import defaults from '../common/nuxt.config' diff --git a/test/basic.config.defaults.test.js b/test/basic.config.defaults.test.js index 5f7136119c..efdb391a6f 100644 --- a/test/basic.config.defaults.test.js +++ b/test/basic.config.defaults.test.js @@ -1,5 +1,7 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' + import { Nuxt, Options } from '..' import { version } from '../package.json' diff --git a/test/basic.csr.test.js b/test/basic.csr.test.js index eb430a62a0..26aae84b20 100644 --- a/test/basic.csr.test.js +++ b/test/basic.csr.test.js @@ -1,6 +1,9 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' + import { Nuxt, Builder } from '..' + import * as browser from './helpers/browser' import { interceptLog } from './helpers/console' diff --git a/test/basic.fail.generate.test.js b/test/basic.fail.generate.test.js index 799bbfb3d4..606e2dbd9c 100644 --- a/test/basic.fail.generate.test.js +++ b/test/basic.fail.generate.test.js @@ -1,6 +1,9 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' + import { Nuxt, Builder, Generator } from '..' + import { intercept } from './helpers/console' test('Fail with routes() which throw an error', async t => { diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 072067bac6..cd2a4cc494 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -1,13 +1,16 @@ -import test from 'ava' -import { resolve } from 'path' import { existsSync } from 'fs' -import { remove } from 'fs-extra' import http from 'http' +import { resolve } from 'path' + +import test from 'ava' +import { remove } from 'fs-extra' import serveStatic from 'serve-static' import finalhandler from 'finalhandler' import rp from 'request-promise-native' -import { interceptLog, release } from './helpers/console' + import { Nuxt, Builder, Generator } from '..' + +import { interceptLog, release } from './helpers/console' import { loadConfig } from './helpers/config' const port = 4002 diff --git a/test/basic.ssr.csp.test.js b/test/basic.ssr.csp.test.js index 2cad0acee2..471e46a258 100644 --- a/test/basic.ssr.csp.test.js +++ b/test/basic.ssr.csp.test.js @@ -1,7 +1,10 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' import rp from 'request-promise-native' + import { Nuxt, Builder } from '..' + import { interceptLog } from './helpers/console' const port = 4005 diff --git a/test/basic.ssr.test.js b/test/basic.ssr.test.js index de32607100..1d4415e73c 100644 --- a/test/basic.ssr.test.js +++ b/test/basic.ssr.test.js @@ -1,7 +1,10 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' import rp from 'request-promise-native' + import { Nuxt, Builder } from '..' + import { interceptLog, interceptError, release } from './helpers/console' const port = 4004 diff --git a/test/children.patch.test.js b/test/children.patch.test.js index 6258152442..befcb646b8 100644 --- a/test/children.patch.test.js +++ b/test/children.patch.test.js @@ -1,6 +1,9 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' + import { Nuxt, Builder, Utils } from '..' + import * as browser from './helpers/browser' import { interceptLog } from './helpers/console' diff --git a/test/children.test.js b/test/children.test.js index 165ee19b09..8e15e8168d 100644 --- a/test/children.test.js +++ b/test/children.test.js @@ -1,6 +1,9 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' + import { Nuxt, Builder } from '..' + import { interceptLog } from './helpers/console' const port = 4013 diff --git a/test/dynamic-routes.test.js b/test/dynamic-routes.test.js index 675c43141c..166b642e81 100644 --- a/test/dynamic-routes.test.js +++ b/test/dynamic-routes.test.js @@ -1,8 +1,11 @@ -import test from 'ava' import { resolve } from 'path' import fs from 'fs' -import { Nuxt, Builder } from '..' import { promisify } from 'util' + +import test from 'ava' + +import { Nuxt, Builder } from '..' + import { interceptLog } from './helpers/console' const readFile = promisify(fs.readFile) diff --git a/test/express.test.js b/test/express.test.js index 46907a820c..7e0a0af82d 100644 --- a/test/express.test.js +++ b/test/express.test.js @@ -1,8 +1,10 @@ -import test from 'ava' import { resolve } from 'path' -import { Nuxt, Builder } from '..' + +import test from 'ava' import express from 'express' import rp from 'request-promise-native' + +import { Nuxt, Builder } from '..' import { interceptLog } from './helpers/console' const port = 4000 diff --git a/test/fallback.generate.test.js b/test/fallback.generate.test.js index 68d68b8fa4..7477be5451 100644 --- a/test/fallback.generate.test.js +++ b/test/fallback.generate.test.js @@ -1,12 +1,15 @@ -import test from 'ava' -import { resolve } from 'path' -import { existsSync } from 'fs' import http from 'http' +import { existsSync } from 'fs' +import { resolve } from 'path' + +import test from 'ava' import serveStatic from 'serve-static' import finalhandler from 'finalhandler' import rp from 'request-promise-native' -import { intercept, interceptLog } from './helpers/console' + import { Nuxt, Builder, Generator, Options } from '..' + +import { intercept, interceptLog } from './helpers/console' import { loadConfig } from './helpers/config' const port = 4015 diff --git a/test/module.test.js b/test/module.test.js index 85444d0218..475061d37a 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -1,7 +1,10 @@ -import test from 'ava' import { normalize } from 'path' + +import test from 'ava' import rp from 'request-promise-native' + import { Nuxt, Builder } from '..' + import { intercept } from './helpers/console' import { loadConfig } from './helpers/config' diff --git a/test/with-config.test.js b/test/with-config.test.js index 4ace3bd8d2..94eb1ac3d3 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -1,8 +1,11 @@ -import test from 'ava' import { resolve } from 'path' + +import test from 'ava' import rp from 'request-promise-native' + import { Nuxt, Builder } from '..' import styleLoader from '../lib/builder/webpack/style-loader' + import { interceptLog, release } from './helpers/console' import { loadConfig } from './helpers/config' From f186e1bd7408aee73faae280bba80fe9caf56c3f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:24:44 +0330 Subject: [PATCH 10/15] remove debug line --- lib/builder/generator.mjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/builder/generator.mjs b/lib/builder/generator.mjs index 063370d87f..7e129cb750 100644 --- a/lib/builder/generator.mjs +++ b/lib/builder/generator.mjs @@ -247,8 +247,6 @@ export default class Generator { } } - path = 123 - let _path if (this.options.generate.subFolders) { From 6e54a3868d3e0f33e054a21e3c9121416437190e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:33:33 +0330 Subject: [PATCH 11/15] fix resolvePath with relative paths --- lib/core/nuxt.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/nuxt.mjs b/lib/core/nuxt.mjs index 321200498b..15b3a5a1a9 100644 --- a/lib/core/nuxt.mjs +++ b/lib/core/nuxt.mjs @@ -192,8 +192,8 @@ export default class Nuxt { } for (let ext of this.options.extensions) { - if (fs.existsSync(_path + '.' + ext)) { - return _path + '.' + ext + if (fs.existsSync(__path + '.' + ext)) { + return __path + '.' + ext } } From 3f83921c71eb2f176885ef7ca774385d5e0ec1f4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:34:54 +0330 Subject: [PATCH 12/15] add mjs to supported extensions --- lib/common/options.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/common/options.mjs b/lib/common/options.mjs index ed5b0c8135..624f427ad0 100644 --- a/lib/common/options.mjs +++ b/lib/common/options.mjs @@ -77,6 +77,10 @@ Options.from = function (_options) { options.extensions.unshift('js') } + if (options.extensions.indexOf('mjs') === -1) { + options.extensions.unshift('mjs') + } + // If app.html is defined, set the template path to the user template options.appTemplatePath = path.resolve(options.buildDir, 'views/app.template.html') if (fs.existsSync(path.join(options.srcDir, 'app.html'))) { From 5d0deefa6aff704232eb720df17eef4b8fb9e187 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 16 Mar 2018 23:48:19 +0330 Subject: [PATCH 13/15] fix: handle string errors --- lib/common/utils.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/common/utils.mjs b/lib/common/utils.mjs index e22d4eed3d..96730ab8c3 100644 --- a/lib/common/utils.mjs +++ b/lib/common/utils.mjs @@ -10,9 +10,9 @@ export const printWarn = function (msg, from) { } export const renderError = function (_error, from) { - const errStr = _error.stack + const errStr = _error.stack || String(_error) const fromStr = from ? Chalk.red(` ${from}`) : '' - return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + '\n\n' + errStr + return '\n' + Chalk.bgRed.black(' ERROR ') + fromStr + ' ' + errStr } export const printError = function () { From 84487ef108674fe6d140a6d593f28d153dcbdad7 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 17 Mar 2018 14:01:47 +0330 Subject: [PATCH 14/15] update filenames --- lib/common/nuxt.config.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/common/nuxt.config.js b/lib/common/nuxt.config.js index 8a30262ae5..db90aad66c 100644 --- a/lib/common/nuxt.config.js +++ b/lib/common/nuxt.config.js @@ -24,10 +24,9 @@ export default { ssr: undefined, publicPath: '/_nuxt/', filenames: { - css: '[name].[contenthash].css', - manifest: 'manifest.[hash].js', app: '[name].[chunkhash].js', - chunk: '[name].[chunkhash].js' + chunk: '[name].[chunkhash].js', + css: '[name].[contenthash].css' }, styleResources: {}, plugins: [], From cf1fc9cb1c2b3902c7391b171aa3d17cf58c5195 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Mon, 19 Mar 2018 22:41:16 +0800 Subject: [PATCH 15/15] fix: extractCSS:true and dev:false break building --- lib/builder/webpack/client.config.mjs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/builder/webpack/client.config.mjs b/lib/builder/webpack/client.config.mjs index 817c006554..b880224bb1 100644 --- a/lib/builder/webpack/client.config.mjs +++ b/lib/builder/webpack/client.config.mjs @@ -133,7 +133,9 @@ export default function webpackClientConfig() { // CSS extraction const extractCSS = this.options.build.extractCSS - if (extractCSS) { + // TODO: Temporary disabled in dev mode for fixing source maps + // (We need `source-map` devtool for *.css modules) + if (extractCSS && !this.options.dev) { config.plugins.push(new ExtractTextPlugin(Object.assign({ filename: this.getFileName('css')