From 97a36f9438b88562b6d502ff5ab7b481ec81829c Mon Sep 17 00:00:00 2001 From: Pan Date: Tue, 14 Aug 2018 18:13:25 +0800 Subject: [PATCH] format code --- build/build.js | 51 ++++++++++++++++----------- build/check-versions.js | 24 +++++++++---- build/utils.js | 13 +++---- build/webpack.base.conf.js | 21 ++++++----- build/webpack.dev.conf.js | 33 +++++++++++------- build/webpack.prod.conf.js | 71 ++++++++++++++++++++------------------ 6 files changed, 126 insertions(+), 87 deletions(-) diff --git a/build/build.js b/build/build.js index fe24ac8f..34c71a55 100644 --- a/build/build.js +++ b/build/build.js @@ -8,10 +8,12 @@ const chalk = require('chalk') const webpack = require('webpack') const config = require('../config') const webpackConfig = require('./webpack.prod.conf') -var connect = require('connect'); +var connect = require('connect') var serveStatic = require('serve-static') -const spinner = ora('building for ' + process.env.env_config + ' environment...') +const spinner = ora( + 'building for ' + process.env.env_config + ' environment...' +) spinner.start() rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => { @@ -19,13 +21,15 @@ rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => { webpack(webpackConfig, (err, stats) => { spinner.stop() if (err) throw err - process.stdout.write(stats.toString({ - colors: true, - modules: false, - children: false, - chunks: false, - chunkModules: false - }) + '\n\n') + process.stdout.write( + stats.toString({ + colors: true, + modules: false, + children: false, + chunks: false, + chunkModules: false + }) + '\n\n' + ) if (stats.hasErrors()) { console.log(chalk.red(' Build failed with errors.\n')) @@ -33,24 +37,31 @@ rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => { } console.log(chalk.cyan(' Build complete.\n')) - console.log(chalk.yellow( - ' Tip: built files are meant to be served over an HTTP server.\n' + - ' Opening index.html over file:// won\'t work.\n' - )) + console.log( + chalk.yellow( + ' Tip: built files are meant to be served over an HTTP server.\n' + + " Opening index.html over file:// won't work.\n" + ) + ) if (process.env.npm_config_preview) { const port = 9526 - const host = "http://localhost:" + port + const host = 'http://localhost:' + port const basePath = config.build.assetsPublicPath const app = connect() - app.use(basePath, serveStatic('./dist', { - 'index': ['index.html', '/'] - })) + app.use( + basePath, + serveStatic('./dist', { + index: ['index.html', '/'] + }) + ) - app.listen(port, function () { - console.log(chalk.green(`> Listening at http://localhost:${port}${basePath}`)) - }); + app.listen(port, function() { + console.log( + chalk.green(`> Listening at http://localhost:${port}${basePath}`) + ) + }) } }) }) diff --git a/build/check-versions.js b/build/check-versions.js index 3ef972a0..c5c29e90 100644 --- a/build/check-versions.js +++ b/build/check-versions.js @@ -4,8 +4,11 @@ const semver = require('semver') const packageConfig = require('../package.json') const shell = require('shelljs') -function exec (cmd) { - return require('child_process').execSync(cmd).toString().trim() +function exec(cmd) { + return require('child_process') + .execSync(cmd) + .toString() + .trim() } const versionRequirements = [ @@ -24,23 +27,30 @@ if (shell.which('npm')) { }) } -module.exports = function () { +module.exports = function() { const warnings = [] for (let i = 0; i < versionRequirements.length; i++) { const mod = versionRequirements[i] if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) { - warnings.push(mod.name + ': ' + - chalk.red(mod.currentVersion) + ' should be ' + - chalk.green(mod.versionRequirement) + warnings.push( + mod.name + + ': ' + + chalk.red(mod.currentVersion) + + ' should be ' + + chalk.green(mod.versionRequirement) ) } } if (warnings.length) { console.log('') - console.log(chalk.yellow('To use this template, you must update following to modules:')) + console.log( + chalk.yellow( + 'To use this template, you must update following to modules:' + ) + ) console.log() for (let i = 0; i < warnings.length; i++) { diff --git a/build/utils.js b/build/utils.js index 8e6c9ccd..c96d0936 100644 --- a/build/utils.js +++ b/build/utils.js @@ -4,15 +4,16 @@ const config = require('../config') const MiniCssExtractPlugin = require('mini-css-extract-plugin') const packageConfig = require('../package.json') -exports.assetsPath = function (_path) { - const assetsSubDirectory = process.env.NODE_ENV === 'production' ? - config.build.assetsSubDirectory : - config.dev.assetsSubDirectory +exports.assetsPath = function(_path) { + const assetsSubDirectory = + process.env.NODE_ENV === 'production' + ? config.build.assetsSubDirectory + : config.dev.assetsSubDirectory return path.posix.join(assetsSubDirectory, _path) } -exports.cssLoaders = function (options) { +exports.cssLoaders = function(options) { options = options || {} const cssLoader = { @@ -73,7 +74,7 @@ exports.cssLoaders = function (options) { } // Generate loaders for standalone style files (outside of .vue) -exports.styleLoaders = function (options) { +exports.styleLoaders = function(options) { const output = [] const loaders = exports.cssLoaders(options) diff --git a/build/webpack.base.conf.js b/build/webpack.base.conf.js index 06910a68..3b946b4b 100644 --- a/build/webpack.base.conf.js +++ b/build/webpack.base.conf.js @@ -5,7 +5,7 @@ const config = require('../config') const { VueLoaderPlugin } = require('vue-loader') const vueLoaderConfig = require('./vue-loader.conf') -function resolve (dir) { +function resolve(dir) { return path.join(__dirname, '..', dir) } @@ -28,14 +28,15 @@ module.exports = { output: { path: config.build.assetsRoot, filename: '[name].js', - publicPath: process.env.NODE_ENV === 'production' - ? config.build.assetsPublicPath - : config.dev.assetsPublicPath + publicPath: + process.env.NODE_ENV === 'production' + ? config.build.assetsPublicPath + : config.dev.assetsPublicPath }, resolve: { extensions: ['.js', '.vue', '.json'], alias: { - '@': resolve('src'), + '@': resolve('src') } }, module: { @@ -49,7 +50,11 @@ module.exports = { { test: /\.js$/, loader: 'babel-loader?cacheDirectory', - include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')] + include: [ + resolve('src'), + resolve('test'), + resolve('node_modules/webpack-dev-server/client') + ] }, { test: /\.svg$/, @@ -86,9 +91,7 @@ module.exports = { } ] }, - plugins: [ - new VueLoaderPlugin(), - ], + plugins: [new VueLoaderPlugin()], node: { // prevent webpack from injecting useless setImmediate polyfill because Vue // source contains it (although only uses it if it's native). diff --git a/build/webpack.dev.conf.js b/build/webpack.dev.conf.js index 33fcf251..5eba7eb3 100644 --- a/build/webpack.dev.conf.js +++ b/build/webpack.dev.conf.js @@ -9,7 +9,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin') const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin') const portfinder = require('portfinder') -function resolve (dir) { +function resolve(dir) { return path.join(__dirname, '..', dir) } @@ -19,7 +19,10 @@ const PORT = process.env.PORT && Number(process.env.PORT) const devWebpackConfig = merge(baseWebpackConfig, { mode: 'development', module: { - rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true }) + rules: utils.styleLoaders({ + sourceMap: config.dev.cssSourceMap, + usePostCSS: true + }) }, // cheap-module-eval-source-map is faster for development devtool: config.dev.devtool, @@ -40,7 +43,7 @@ const devWebpackConfig = merge(baseWebpackConfig, { proxy: config.dev.proxyTable, quiet: true, // necessary for FriendlyErrorsPlugin watchOptions: { - poll: config.dev.poll, + poll: config.dev.poll } }, plugins: [ @@ -56,7 +59,7 @@ const devWebpackConfig = merge(baseWebpackConfig, { favicon: resolve('favicon.ico'), title: 'vue-element-admin', path: config.dev.assetsPublicPath + config.dev.assetsSubDirectory - }), + }) ] }) @@ -72,14 +75,20 @@ module.exports = new Promise((resolve, reject) => { devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin - devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({ - compilationSuccessInfo: { - messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`], - }, - onErrors: config.dev.notifyOnErrors - ? utils.createNotifierCallback() - : undefined - })) + devWebpackConfig.plugins.push( + new FriendlyErrorsPlugin({ + compilationSuccessInfo: { + messages: [ + `Your application is running here: http://${ + devWebpackConfig.devServer.host + }:${port}` + ] + }, + onErrors: config.dev.notifyOnErrors + ? utils.createNotifierCallback() + : undefined + }) + ) resolve(devWebpackConfig) } diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index ec60a4e9..6220cc6b 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -19,8 +19,8 @@ function resolve(dir) { const env = require('../config/' + process.env.env_config + '.env') // For NamedChunksPlugin -const seen = new Set(); -const nameLength = 4; +const seen = new Set() +const nameLength = 4 const webpackConfig = merge(baseWebpackConfig, { mode: 'production', @@ -63,7 +63,7 @@ const webpackConfig = merge(baseWebpackConfig, { removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference - }, + } // default sort mode uses toposort which cannot handle cyclic deps // in certain cases, and in webpack 4, chunk order in HTML doesn't // matter anyway @@ -75,28 +75,30 @@ const webpackConfig = merge(baseWebpackConfig, { // keep chunk.id stable when chunk has no name new webpack.NamedChunksPlugin(chunk => { if (chunk.name) { - return chunk.name; + return chunk.name } - const modules = Array.from(chunk.modulesIterable); + const modules = Array.from(chunk.modulesIterable) if (modules.length > 1) { - const hash = require("hash-sum"); - const joinedHash = hash(modules.map(m => m.id).join("_")); - let len = nameLength; - while (seen.has(joinedHash.substr(0, len))) len++; - seen.add(joinedHash.substr(0, len)); - return `chunk-${joinedHash.substr(0, len)}`; + const hash = require('hash-sum') + const joinedHash = hash(modules.map(m => m.id).join('_')) + let len = nameLength + while (seen.has(joinedHash.substr(0, len))) len++ + seen.add(joinedHash.substr(0, len)) + return `chunk-${joinedHash.substr(0, len)}` } else { - return modules[0].id; + return modules[0].id } }), // keep module.id stable when vender modules does not change new webpack.HashedModuleIdsPlugin(), // copy custom static assets - new CopyWebpackPlugin([{ - from: path.resolve(__dirname, '../static'), - to: config.build.assetsSubDirectory, - ignore: ['.*'] - }]) + new CopyWebpackPlugin([ + { + from: path.resolve(__dirname, '../static'), + to: config.build.assetsSubDirectory, + ignore: ['.*'] + } + ]) ], optimization: { splitChunks: { @@ -115,11 +117,11 @@ const webpackConfig = merge(baseWebpackConfig, { }, commons: { name: 'chunk-comomns', - test: resolve("src/components"), // 可自定义拓展你的规则 + test: resolve('src/components'), // 可自定义拓展你的规则 minChunks: 3, // 最小公用次数 priority: 5, reuseExistingChunk: true - }, + } } }, runtimeChunk: 'single', @@ -137,7 +139,7 @@ const webpackConfig = merge(baseWebpackConfig, { // Compress extracted CSS. We are using this plugin so that possible // duplicated CSS from different components can be deduped. new OptimizeCSSAssetsPlugin() - ], + ] } }) @@ -149,9 +151,7 @@ if (config.build.productionGzip) { asset: '[path].gz[query]', algorithm: 'gzip', test: new RegExp( - '\\.(' + - config.build.productionGzipExtensions.join('|') + - ')$' + '\\.(' + config.build.productionGzipExtensions.join('|') + ')$' ), threshold: 10240, minRatio: 0.8 @@ -160,21 +160,26 @@ if (config.build.productionGzip) { } if (config.build.generateAnalyzerReport || config.build.bundleAnalyzerReport) { - const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin + const BundleAnalyzerPlugin = require('webpack-bundle-analyzer') + .BundleAnalyzerPlugin if (config.build.bundleAnalyzerReport) { - webpackConfig.plugins.push(new BundleAnalyzerPlugin({ - analyzerPort: 8080, - generateStatsFile: false - })) + webpackConfig.plugins.push( + new BundleAnalyzerPlugin({ + analyzerPort: 8080, + generateStatsFile: false + }) + ) } if (config.build.generateAnalyzerReport) { - webpackConfig.plugins.push(new BundleAnalyzerPlugin({ - analyzerMode: 'static', - reportFilename: 'bundle-report.html', - openAnalyzer: false - })) + webpackConfig.plugins.push( + new BundleAnalyzerPlugin({ + analyzerMode: 'static', + reportFilename: 'bundle-report.html', + openAnalyzer: false + }) + ) } }