diff --git a/package.json b/package.json index 2dece9119c..00a4a3c193 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "license": "GPL-2.0", "scripts": { "dev:wds": "webpack-dev-server --progress", - "prod:build": "del js/lib js/dist && babel js/src/ -d js/lib --ignore .test.js && cross-env NODE_ENV=production webpack -p --progress", + "prod:build": "del js/lib js/dist && babel js/src/ -d js/lib --ignore .test.js && cross-env NODE_ENV=production webpack -p --env.production --progress", "lint:check": "eslint js/src/*.js", "lint:fix": "eslint js/src/*.js --fix" }, diff --git a/scripts/build-js-files b/scripts/build-js-files new file mode 100755 index 0000000000..2a9a4d8d79 --- /dev/null +++ b/scripts/build-js-files @@ -0,0 +1,39 @@ +#!/bin/sh +# +# vim: expandtab sw=4 ts=4 sts=4: +# + +# Do not run as CGI +if [ -n "$GATEWAY_INTERFACE" ] ; then + echo 'Can not invoke as CGI!' + exit 1 +fi + +# Going back into the direcory +cd .. + +if [ -f package.json ] ; then + echo "Running Yarn Install" + yarn install + + echo "Creating production build of js files" + yarn prod:build +fi + +#Performing cleanup +#Removing yarn files +rm yarn.lock +if [ -f yarn-error.log ] ; then + rm yarn-error.log +fi +#Removing node_modules from the directory +rm -rf node_modules +#Removing the babel transcompiled code +rm -rf js/lib +#Removing JavaScript source code +rm -rf js/src +#Removing babel files as they are not required in production +rm .babelrc +rm webpack.config.babel.js + +echo "Finished building js files" diff --git a/webpack.config.babel.js b/webpack.config.babel.js index 634666a8c1..936a351055 100644 --- a/webpack.config.babel.js +++ b/webpack.config.babel.js @@ -2,84 +2,95 @@ import path from 'path'; import webpack from 'webpack'; import BundleAnalyzerPlugin from 'webpack-bundle-analyzer'; -let BindleAnalyzer = BundleAnalyzerPlugin.BundleAnalyzerPlugin; +function WebpackConfig (env) { + let BindleAnalyzer = BundleAnalyzerPlugin.BundleAnalyzerPlugin; -// environment either development or production -var MODE = 'development'; + // environment either development or production + var MODE; + if (typeof env !== 'undefined' + && env.production === true + ) { + MODE = 'production'; + } else { + MODE = 'development'; + } -var WEBPACK_HOST = 'http://localhost'; + var WEBPACK_HOST = 'http://localhost'; -// port number of dev server -// let dev server port be 3307 -var WEBPACK_PORT = 3307; + // port number of dev server + // let dev server port be 3307 + var WEBPACK_PORT = 3307; -var PUBLIC_PATH; + var PUBLIC_PATH; -if (MODE === 'development') { - PUBLIC_PATH = WEBPACK_HOST + ':' + WEBPACK_PORT + '/js/dist/'; -} else { - PUBLIC_PATH = 'js/dist/'; -} + if (MODE === 'development') { + PUBLIC_PATH = WEBPACK_HOST + ':' + WEBPACK_PORT + '/js/dist/'; + } else { + PUBLIC_PATH = 'js/dist/'; + } -var module = { - rules: [ - { test: /\.(js)$/, use: 'babel-loader', exclude: /node_modules/ } - ] -}; -var devServer = { - port: WEBPACK_PORT, - hot: false, - headers: { - 'Access-Control-Allow-Origin': '*' - }, + var module = { + rules: [ + { test: /\.(js)$/, use: 'babel-loader', exclude: /node_modules/ } + ] + }; + var devServer = { + port: WEBPACK_PORT, + hot: false, + headers: { + 'Access-Control-Allow-Origin': '*' + }, -}; -var plugins = [ - new webpack.optimize.OccurrenceOrderPlugin(), - new webpack.HotModuleReplacementPlugin(), - new webpack.NamedModulesPlugin(), - new webpack.NoEmitOnErrorsPlugin() -]; -if (MODE === 'development') { - plugins.push(new BindleAnalyzer()); -} + }; + var plugins = [ + new webpack.optimize.OccurrenceOrderPlugin(), + new webpack.HotModuleReplacementPlugin(), + new webpack.NamedModulesPlugin(), + new webpack.NoEmitOnErrorsPlugin() + ]; + if (MODE === 'development') { + plugins.push(new BindleAnalyzer()); + } -export default [{ - mode: MODE, - entry: { - index_new: './js/src/index.js' - }, - output: { - filename: '[name].js', - path: path.resolve(__dirname, 'js/dist'), - publicPath: PUBLIC_PATH - }, - optimization: { - splitChunks: { - chunks: 'all', - minSize: 30000, - minChunks: 1, - maxAsyncRequests: 5, - maxInitialRequests: 3, - automaticNameDelimiter: '~', - name: true, - cacheGroups: { - vendors: { - test: /[\\/]node_modules[\\/]/, - priority: -10 - }, - default: { - minChunks: 2, - priority: -20, - reuseExistingChunk: true + return { + mode: MODE, + entry: { + index_new: './js/src/index.js' + }, + output: { + filename: '[name].js', + path: path.resolve(__dirname, 'js/dist'), + publicPath: PUBLIC_PATH + }, + optimization: { + splitChunks: { + chunks: 'all', + minSize: 30000, + minChunks: 1, + maxAsyncRequests: 5, + maxInitialRequests: 3, + automaticNameDelimiter: '~', + name: true, + cacheGroups: { + vendors: { + test: /[\\/]node_modules[\\/]/, + priority: -10 + }, + default: { + minChunks: 2, + priority: -20, + reuseExistingChunk: true + } } } - } - }, - module: module, - resolve: { - extensions: ['.js'] - }, - devServer: devServer, - plugins: plugins -}]; + }, + module: module, + resolve: { + extensions: ['.js'] + }, + devServer: devServer, + plugins: plugins + }; +}; + +export default WebpackConfig;