Pass env variable to webpack.config file for making production build.

Build script for js files.
Signed-Off-By: Piyush Vijay <piyushvijay.1997@gmail.com>
This commit is contained in:
Piyush Vijay 2018-08-01 20:47:58 +05:30
parent 0b5c9b3eea
commit 61e97801d4
3 changed files with 122 additions and 72 deletions

View File

@ -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"
},

39
scripts/build-js-files Executable file
View File

@ -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"

View File

@ -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;