From 09e637cf4bfc16a508a909b48e0673e11c2ee9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 9 Dec 2016 18:54:17 +0100 Subject: [PATCH] generate dist lib --- .npmignore | 2 -- lib/app/client.js | 2 +- lib/build/index.js | 14 ++++----- lib/build/webpack/base.config.js | 2 +- lib/build/webpack/client.config.js | 2 +- lib/build/webpack/server.config.js | 15 +++++---- package.json | 25 ++++++++++++--- webpack.config.js | 50 ++++++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 23 deletions(-) delete mode 100644 .npmignore create mode 100644 webpack.config.js diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 647ea035d9..0000000000 --- a/.npmignore +++ /dev/null @@ -1,2 +0,0 @@ -# Excludes examples -examples diff --git a/lib/app/client.js b/lib/app/client.js index 5430cf00a2..c5f9dafaf4 100644 --- a/lib/app/client.js +++ b/lib/app/client.js @@ -246,7 +246,7 @@ Promise.all(resolveComponents) // Hot reloading if (module.hot) hotReloadAPI(_app) // Call window.onNuxtReady callbacks - nuxtReady(_app) + Vue.nextTick(() => nuxtReady(_app)) } <% if (store) { %> // Replace store state diff --git a/lib/build/index.js b/lib/build/index.js index a28a8e219c..4957d5105e 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -10,6 +10,8 @@ const pify = require('pify') const webpack = require('webpack') const { createBundleRenderer } = require('vue-server-renderer') const { join, resolve, sep, posix } = require('path') +const clientWebpackConfig = require('./webpack/client.config.js') +const serverWebpackConfig = require('./webpack/server.config.js') const basename = posix.basename const remove = pify(fs.remove) const readFile = pify(fs.readFile) @@ -182,8 +184,8 @@ function * generateRoutesAndFiles () { loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading), transition: this.options.transition, components: { - Loading: r(__dirname, '..', 'app', 'components', 'nuxt-loading.vue'), - ErrorPage: r(__dirname, '..', 'app', 'components', 'nuxt-error.vue') + Loading: r(__dirname, 'app', 'components', 'nuxt-loading.vue'), + ErrorPage: r(__dirname, 'app', 'components', 'nuxt-error.vue') } } if (templateVars.loading === 'string' && templateVars.loading.slice(-4) !== '.vue') { @@ -211,7 +213,7 @@ function * generateRoutesAndFiles () { templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue') } let moveTemplates = templatesFiles.map((file) => { - return readFile(r(__dirname, '..', 'app', file), 'utf8') + return readFile(r(__dirname, 'app', file), 'utf8') .then((fileContent) => { const template = _.template(fileContent) const content = template(templateVars) @@ -222,13 +224,11 @@ function * generateRoutesAndFiles () { } function getWebpackClientConfig () { - const clientConfigPath = r(__dirname, 'webpack', 'client.config.js') - return require(clientConfigPath).call(this) + return clientWebpackConfig.call(this) } function getWebpackServerConfig () { - const configServerPath = r(__dirname, 'webpack', 'server.config.js') - return require(configServerPath).call(this) + return serverWebpackConfig.call(this) } function createWebpackMiddlewares () { diff --git a/lib/build/webpack/base.config.js b/lib/build/webpack/base.config.js index 5a1183fd9f..085fd2b4ca 100644 --- a/lib/build/webpack/base.config.js +++ b/lib/build/webpack/base.config.js @@ -14,7 +14,7 @@ const { urlJoin } = require('../../utils') |-------------------------------------------------------------------------- */ module.exports = function () { - const nodeModulesDir = join(__dirname, '..', '..', '..', 'node_modules') + const nodeModulesDir = join(__dirname, '..', 'node_modules') let config = { devtool: 'source-map', entry: { diff --git a/lib/build/webpack/client.config.js b/lib/build/webpack/client.config.js index 7330c6dd64..b471b9e6d1 100644 --- a/lib/build/webpack/client.config.js +++ b/lib/build/webpack/client.config.js @@ -3,7 +3,7 @@ const _ = require('lodash') const webpack = require('webpack') const ExtractTextPlugin = require('extract-text-webpack-plugin') -const base = require('./base.config') +const base = require('./base.config.js') const { resolve } = require('path') /* diff --git a/lib/build/webpack/server.config.js b/lib/build/webpack/server.config.js index 774507fdd8..5371f40962 100644 --- a/lib/build/webpack/server.config.js +++ b/lib/build/webpack/server.config.js @@ -2,9 +2,9 @@ const _ = require('lodash') const webpack = require('webpack') -const base = require('./base.config') +const base = require('./base.config.js') const { uniq } = require('lodash') -const { existsSync } = require('fs') +const { existsSync, readFileSync } = require('fs') const { resolve } = require('path') /* @@ -40,11 +40,14 @@ module.exports = function () { }) // Externals - const nuxtPackageJson = require(resolve(__dirname, '..', '..', '..', 'package.json')) - const projectPackageJson = resolve(this.dir, 'package.json') + const nuxtPackageJson = require('../../../package.json') + const projectPackageJsonPath = resolve(this.dir, 'package.json') config.externals = Object.keys(nuxtPackageJson.dependencies || {}) - if (existsSync(projectPackageJson)) { - config.externals = config.externals.concat(Object.keys(require(projectPackageJson).dependencies || {})) + if (existsSync(projectPackageJsonPath)) { + try { + const projectPackageJson = JSON.parse(readFileSync(projectPackageJsonPath)) + config.externals = config.externals.concat(Object.keys(projectPackageJson.dependencies || {})) + } catch (e) {} } config.externals = uniq(config.externals) diff --git a/package.json b/package.json index dd65b608c6..9473a8a32c 100644 --- a/package.json +++ b/package.json @@ -10,12 +10,16 @@ "name": "Alexandre Chopin" } ], - "main": "index.js", + "main": "./dist/nuxt.js", "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/nuxt/nuxt.js" }, + "files": [ + "bin", + "dist" + ], "keywords": [ "nuxt", "nuxt.js", @@ -31,15 +35,20 @@ "nuxt": "./bin/nuxt" }, "scripts": { - "test": "npm run lint", - "lint": "eslint --ext .js,.vue bin lib pages test index.js --ignore-pattern lib/app", - "precommit": "npm run lint" + "test": "nyc ava test/", + "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", + "lint": "eslint --ext .js,.vue bin lib pages test --ignore-pattern lib/app", + "build": "webpack", + "watch": "webpack --watch", + "precommit": "npm run lint", + "prepublish": "npm run build" }, "dependencies": { "ansi-html": "^0.0.6", "autoprefixer": "^6.5.3", "babel-core": "^6.18.2", "babel-loader": "^6.2.8", + "babel-polyfill": "^6.20.0", "babel-preset-es2015": "^6.18.0", "babel-preset-stage-2": "^6.18.0", "chokidar": "^1.6.1", @@ -73,12 +82,18 @@ "webpack-hot-middleware": "^2.13.2" }, "devDependencies": { + "ava": "^0.17.0", "babel-eslint": "^7.1.1", + "codecov": "^1.0.1", + "copy-webpack-plugin": "^4.0.1", "eslint": "^3.10.2", "eslint-config-standard": "^6.2.1", "eslint-plugin-html": "^1.7.0", "eslint-plugin-promise": "^3.4.0", "eslint-plugin-standard": "^2.0.1", - "nodemon": "^1.11.0" + "jsdom": "^9.8.3", + "json-loader": "^0.5.4", + "nyc": "^10.0.0", + "webpack-node-externals": "^1.5.4" } } diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000000..8db6a610d0 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,50 @@ +var nodeExternals = require('webpack-node-externals') +var CopyWebpackPlugin = require('copy-webpack-plugin') +var resolve = require('path').resolve +var r = function (p) { return resolve(__dirname, p) } + +module.exports = { + target: 'node', + node: { + __dirname: false, + __filename: false + }, + devtool: 'source-map', + entry: ['babel-polyfill', r('./lib/nuxt.js')], + output: { + path: r('./dist'), + filename: 'nuxt.js', + libraryTarget: 'commonjs2' + }, + externals: [ + nodeExternals({ + whitelist: ['babel-polyfill'] + }) + ], + module: { + rules: [ + { + test: /\.json$/, + loader: 'json-loader' + }, + { + test: /\.js$/, + loader: 'babel-loader', + exclude: /node_modules/, + query: { + presets: [ + ['es2015', { modules: false }], + 'stage-2' + ], + cacheDirectory: true + } + } + ] + }, + plugins: [ + new CopyWebpackPlugin([ + { from: 'lib/app', to: 'app' }, + { from: 'lib/views', to: 'views' } + ]) + ] +}