From f8a7483b08eb647ccdcf4a2e1aec26bf2cbdbf1a Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sat, 15 Jul 2017 21:58:05 +0200 Subject: [PATCH] refactor: move webpack to root folder and rewrite to es6 node style (legacy compatibility) --- src/webui/.babelrc => .babelrc | 0 package.json | 7 +-- {src/webui/scripts => tools}/dev.server.js | 2 +- .../webui/scripts => tools}/webpack.config.js | 16 +++--- .../webpack.dev.config.babel.js | 2 +- .../webpack.prod.config.babel.js | 52 ++++++++++--------- 6 files changed, 42 insertions(+), 37 deletions(-) rename src/webui/.babelrc => .babelrc (100%) rename {src/webui/scripts => tools}/dev.server.js (95%) rename {src/webui/scripts => tools}/webpack.config.js (86%) rename {src/webui/scripts => tools}/webpack.dev.config.babel.js (96%) rename {src/webui/scripts => tools}/webpack.prod.config.babel.js (53%) diff --git a/src/webui/.babelrc b/.babelrc similarity index 100% rename from src/webui/.babelrc rename to .babelrc diff --git a/package.json b/package.json index af98f2bad..9efa214bc 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "pkginfo": "^0.4.0", "request": "^2.72.0", "semver": "^5.1.0", - "unix-crypt-td-js": "^1.0.0" + "unix-crypt-td-js": "^1.0.0", + "webpack-merge": "^4.1.0" }, "devDependencies": { "axios": "0.16.2", @@ -125,8 +126,8 @@ "lint": "eslint .", "lint:css": "stylelint 'src/**/*.scss' --syntax scss", "build-docker": "docker build -t verdaccio .", - "build:webui": "rimraf static/* && webpack --config src/webui/scripts/webpack.prod.config.babel.js", - "dev:webui": "babel-node src/webui/scripts/dev.server.js", + "build:webui": "rimraf static/* && webpack --config tools/webpack.prod.config.babel.js --debug", + "dev:webui": "babel-node tools/dev.server.js", "prepublish": "in-publish && npm run build:webpack || not-in-publish", "build-docker:rpi": "docker build -f Dockerfile.rpi -t verdaccio:rpi ." }, diff --git a/src/webui/scripts/dev.server.js b/tools/dev.server.js similarity index 95% rename from src/webui/scripts/dev.server.js rename to tools/dev.server.js index 7dd2bf7bb..ae9e347ef 100644 --- a/src/webui/scripts/dev.server.js +++ b/tools/dev.server.js @@ -2,7 +2,7 @@ import webpack from 'webpack'; import WebpackDevServer from 'webpack-dev-server'; import config from './webpack.dev.config.babel'; import ora from 'ora'; -import env from '../../config/env'; +import env from '../src/config/env'; const compiler = webpack(config); const spinner = ora('Compiler is running...').start(); diff --git a/src/webui/scripts/webpack.config.js b/tools/webpack.config.js similarity index 86% rename from src/webui/scripts/webpack.config.js rename to tools/webpack.config.js index 823213b7d..d2a34a754 100644 --- a/src/webui/scripts/webpack.config.js +++ b/tools/webpack.config.js @@ -1,17 +1,17 @@ -import env from '../../config/env'; +const env = require('../src/config/env'); const isDev = process.env.NODE_ENV === 'development'; -export default { +module.exports = { entry: `${env.SRC_ROOT}/webui/src/index.js`, output: { path: `${env.APP_ROOT}/static/`, - filename: '[name].[hash].js' + filename: '[name].[hash].js', }, resolve: { - extensions: ['.js', '.jsx'] + extensions: ['.js', '.jsx'], }, module: { @@ -19,14 +19,14 @@ export default { /* Pre loader */ { enforce: 'pre', - test: /\.jsx?$/, + test: /\.js?$/, exclude: /node_modules/, use: 'eslint-loader' }, /* Normal loader */ { - test: /\.jsx?$/, + test: /\.js?$/, exclude: /node_modules/, use: 'babel-loader' }, @@ -78,5 +78,7 @@ export default { devtool: isDev ? 'source-map' : 'eval', - stats: {children: false} + stats: { + children: false + } }; diff --git a/src/webui/scripts/webpack.dev.config.babel.js b/tools/webpack.dev.config.babel.js similarity index 96% rename from src/webui/scripts/webpack.dev.config.babel.js rename to tools/webpack.dev.config.babel.js index 4f1e83bcf..138f0b484 100644 --- a/src/webui/scripts/webpack.dev.config.babel.js +++ b/tools/webpack.dev.config.babel.js @@ -2,7 +2,7 @@ import webpack from 'webpack'; import HTMLWebpackPlugin from 'html-webpack-plugin'; import FriendlyErrorsPlugin from 'friendly-errors-webpack-plugin'; import baseConfig from './webpack.config'; -import env from '../../config/env'; +import env from '../src/config/env'; export default { ...baseConfig, diff --git a/src/webui/scripts/webpack.prod.config.babel.js b/tools/webpack.prod.config.babel.js similarity index 53% rename from src/webui/scripts/webpack.prod.config.babel.js rename to tools/webpack.prod.config.babel.js index 625ce0f69..f65f50043 100644 --- a/src/webui/scripts/webpack.prod.config.babel.js +++ b/tools/webpack.prod.config.babel.js @@ -1,40 +1,30 @@ -import webpack from 'webpack'; -import HTMLWebpackPlugin from 'html-webpack-plugin'; -import ExtractTextPlugin from 'extract-text-webpack-plugin'; -import baseConfig from './webpack.config'; -import env from '../../config/env'; -import _ from 'lodash'; +const webpack = require('webpack'); +const HTMLWebpackPlugin = require('html-webpack-plugin'); +const ExtractTextPlugin = require('extract-text-webpack-plugin'); +const baseConfig = require('./webpack.config'); +const env = require('../src/config/env'); +const _ = require('lodash'); +const merge = require('webpack-merge'); -baseConfig.module.rules - .filter((loader) => - Array.isArray(loader.use) && loader.use.find((v) => /css/.test(v.loader.split('-')[0])) - ).forEach((loader) => { - loader.use = ExtractTextPlugin.extract({ - fallback: 'style-loader', - use: _.tail(loader.use) - }); -}); - -export default { - ...baseConfig, +const prodConf = { entry: { - main: `${env.SRC_ROOT}/webui/src/index.js` + main: `${env.SRC_ROOT}/webui/src/index.js`, }, - output: { - ...baseConfig.output + module: { + rules: [], }, plugins: [ new webpack.DefinePlugin({ '__DEBUG__': false, - 'process.env.NODE_ENV': '"production"' + 'process.env.NODE_ENV': '"production"', }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false - } + }, }), new ExtractTextPlugin('style.[contenthash].css'), new HTMLWebpackPlugin({ @@ -45,6 +35,18 @@ export default { debug: false, inject: true, }), - new webpack.NoEmitOnErrorsPlugin() - ] + new webpack.NoEmitOnErrorsPlugin(), + ], }; + +prodConf.module.rules = baseConfig.module.rules + .filter((loader) => + Array.isArray(loader.use) && loader.use.find((v) => /css/.test(v.loader.split('-')[0])) + ).forEach((loader) => { + loader.use = ExtractTextPlugin.extract({ + fallback: 'style-loader', + use: _.tail(loader.use), + }); +}); + +module.exports = merge(baseConfig, prodConf);