More webpack cleanup - add comments liberally and rename lib.css to style.css

This commit is contained in:
Surinder Kumar 2017-07-20 12:52:15 +01:00 committed by Dave Page
parent 4d4da67247
commit 2eb151c2d9
3 changed files with 54 additions and 9 deletions

View File

@ -1,16 +1,21 @@
/* eslint-env node */ /* eslint-env node */
// Import file, libraries and plugins
const path = require('path'); const path = require('path');
const webpack = require('webpack'); const webpack = require('webpack');
const sourceDir = __dirname + '/pgadmin/static';
// webpack.shim.js contains path references for resolve > alias configuration
// and other util function used in CommonsChunksPlugin.
const webpackShimConfig = require('./webpack.shim'); const webpackShimConfig = require('./webpack.shim');
const PRODUCTION = process.env.NODE_ENV === 'production'; const PRODUCTION = process.env.NODE_ENV === 'production';
const envType = PRODUCTION ? 'prod': 'dev'; const envType = PRODUCTION ? 'prod': 'dev';
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const sourceDir = __dirname + '/pgadmin/static';
const ExtractTextPlugin = require('extract-text-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractLib = new ExtractTextPlugin('style.css'); const extractStyle = new ExtractTextPlugin('style.css');
const extractSass = new ExtractTextPlugin('pgadmin.css'); const extractSass = new ExtractTextPlugin('pgadmin.css');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
// Extract vendor related libraries(node_modules/lib/lib.js) from bundles
// specified in `chunks` into vendor.js bundle
const vendorChunks = new webpack.optimize.CommonsChunkPlugin({ const vendorChunks = new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', name: 'vendor',
chunks: ['app.bundle', 'sqleditor', 'codemirror', 'debugger_direct'], chunks: ['app.bundle', 'sqleditor', 'codemirror', 'debugger_direct'],
@ -20,6 +25,9 @@ const vendorChunks = new webpack.optimize.CommonsChunkPlugin({
}, },
}); });
// Extract pgAdmin common libraries(pgadmin/web/module/filename.js) from bundles
// specified in `chunks` into pgadmin_commons.js bundle.
// pgLibs holds files that will be moved into this bundle.
const pgAdminCommonChunks = new webpack.optimize.CommonsChunkPlugin({ const pgAdminCommonChunks = new webpack.optimize.CommonsChunkPlugin({
name: 'pgadmin_commons', name: 'pgadmin_commons',
chunks: ['app.bundle', 'sqleditor', 'codemirror', 'debugger_direct'], chunks: ['app.bundle', 'sqleditor', 'codemirror', 'debugger_direct'],
@ -29,6 +37,8 @@ const pgAdminCommonChunks = new webpack.optimize.CommonsChunkPlugin({
}, },
}); });
// Expose libraries in app context so they need not to
// require('libname') when used in a module
const providePlugin = new webpack.ProvidePlugin({ const providePlugin = new webpack.ProvidePlugin({
$: 'jquery', $: 'jquery',
jQuery: 'jquery', jQuery: 'jquery',
@ -40,16 +50,18 @@ const providePlugin = new webpack.ProvidePlugin({
pgAdmin: 'pgadmin', pgAdmin: 'pgadmin',
}); });
// Minify and omptimize JS/CSS to reduce bundle size. It is used in production
const uglifyPlugin = new webpack.optimize.UglifyJsPlugin({ const uglifyPlugin = new webpack.optimize.UglifyJsPlugin({
output: {comments: false}, output: {comments: false},
compress: { compress: {
warnings: false, warnings: true,
unused: true, unused: true,
dead_code: true, dead_code: true,
drop_console: true, drop_console: true,
}, },
}); });
// Optimize CSS Assets by removing comments while bundling
const optimizeAssetsPlugin = new OptimizeCssAssetsPlugin({ const optimizeAssetsPlugin = new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g, assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'), cssProcessor: require('cssnano'),
@ -57,12 +69,17 @@ const optimizeAssetsPlugin = new OptimizeCssAssetsPlugin({
canPrint: true, canPrint: true,
}); });
// Helps in minimising the `React' production bundle. Bundle only code
// requires in production mode. React keeps the code conditional
// based on 'NODE_ENV' variable. [used only in production]
const definePlugin = new webpack.DefinePlugin({ const definePlugin = new webpack.DefinePlugin({
'process.env': { 'process.env': {
'NODE_ENV': JSON.stringify('production'), 'NODE_ENV': JSON.stringify('production'),
}, },
}); });
// Manages the cache and stores it into 'sources/generated/.cache/<env><hash>/' path
// where env = dev || prod
const hardSourceWebpackPlugin = new HardSourceWebpackPlugin({ const hardSourceWebpackPlugin = new HardSourceWebpackPlugin({
cacheDirectory: './.cache/hard-source/' + envType +'/[confighash]', cacheDirectory: './.cache/hard-source/' + envType +'/[confighash]',
recordsPath: './.cache/hard-source/' + envType +'/[confighash]/records.json', recordsPath: './.cache/hard-source/' + envType +'/[confighash]/records.json',
@ -76,7 +93,10 @@ const hardSourceWebpackPlugin = new HardSourceWebpackPlugin({
module.exports = { module.exports = {
stats: { children: false }, stats: { children: false },
// The base directory, an absolute path, for resolving entry points and loaders
// from configuration.
context: __dirname, context: __dirname,
// Specify entry points of application
entry: { entry: {
'app.bundle': sourceDir + '/bundle/app.js', 'app.bundle': sourceDir + '/bundle/app.js',
codemirror: sourceDir + '/bundle/codemirror.js', codemirror: sourceDir + '/bundle/codemirror.js',
@ -84,15 +104,19 @@ module.exports = {
debugger_direct: './pgadmin/tools/debugger/templates/debugger/js/direct.js', debugger_direct: './pgadmin/tools/debugger/templates/debugger/js/direct.js',
file_utils: './pgadmin/misc/file_manager/templates/file_manager/js/utility.js', file_utils: './pgadmin/misc/file_manager/templates/file_manager/js/utility.js',
pgadmin_css: './pgadmin/static/scss/pgadmin.scss', pgadmin_css: './pgadmin/static/scss/pgadmin.scss',
lib_css: './pgadmin/static/css/lib.css', style_css: './pgadmin/static/css/style.css',
}, },
// path: The output directory for generated bundles(defined in entry)
// Ref: https://webpack.js.org/configuration/output/#output-library
output: { output: {
libraryTarget: 'amd', libraryTarget: 'amd',
path: __dirname + '/pgadmin/static/js/generated', path: __dirname + '/pgadmin/static/js/generated',
filename: '[name].js', filename: '[name].js',
libraryExport: 'default', libraryExport: 'default',
}, },
/*Templates files to be loaded dynamically*/ // Templates files which contains python code needs to load dynamically
// Such files specified in externals are loaded at first and defined in
// the start of generated bundle within define(['libname'],fn) etc.
externals: { externals: {
'pgadmin.browser.messages': 'pgadmin.browser.messages', 'pgadmin.browser.messages': 'pgadmin.browser.messages',
'pgadmin.browser.utils': 'pgadmin.browser.utils', 'pgadmin.browser.utils': 'pgadmin.browser.utils',
@ -102,8 +126,16 @@ module.exports = {
'pgadmin.node.unique_key': 'pgadmin.node.unique_key', 'pgadmin.node.unique_key': 'pgadmin.node.unique_key',
'translations': 'translations', 'translations': 'translations',
}, },
module: { module: {
// References:
// Module and Rules: https://webpack.js.org/configuration/module/
// Loaders: https://webpack.js.org/loaders/
//
// imports-loader: it adds dependent modules(use:imports-loader?module1)
// at the beginning of module it is dependency of like:
// var jQuery = require('jquery'); var browser = require('pgadmin.browser')
// It solves number of problems
// Ref: http:/github.com/webpack-contrib/imports-loader/
rules: [{ rules: [{
test: /\.jsx?$/, test: /\.jsx?$/,
exclude: [/node_modules/, /vendor/], exclude: [/node_modules/, /vendor/],
@ -114,6 +146,10 @@ module.exports = {
}, },
}, },
}, { }, {
// Transforms the code in a way that it works in the webpack environment.
// It uses imports-loader internally to load dependency. Its
// configuration is specified in webpack.shim.js
// Ref: https://www.npmjs.com/package/shim-loader
test: /\.js/, test: /\.js/,
loader: 'shim-loader', loader: 'shim-loader',
query: webpackShimConfig, query: webpackShimConfig,
@ -242,12 +278,13 @@ module.exports = {
}), }),
}, { }, {
test: /\.css$/, test: /\.css$/,
use: extractLib.extract({ use: extractStyle.extract({
use: [{ use: [{
loader: 'css-loader', loader: 'css-loader',
}], }],
}), }),
}], }],
// Prevent module from parsing through webpack, helps in reducing build time
noParse: [/moment.js/], noParse: [/moment.js/],
}, },
resolve: { resolve: {
@ -256,9 +293,11 @@ module.exports = {
extensions: ['.js', '.jsx'], extensions: ['.js', '.jsx'],
unsafeCache: true, unsafeCache: true,
}, },
// Define list of Plugins used in Production or development mode
// Ref:https://webpack.js.org/concepts/plugins/#components/sidebar/sidebar.jsx
plugins: PRODUCTION ? [ plugins: PRODUCTION ? [
extractSass, extractSass,
extractLib, extractStyle,
vendorChunks, vendorChunks,
pgAdminCommonChunks, pgAdminCommonChunks,
providePlugin, providePlugin,
@ -268,7 +307,7 @@ module.exports = {
hardSourceWebpackPlugin, hardSourceWebpackPlugin,
]: [ ]: [
extractSass, extractSass,
extractLib, extractStyle,
vendorChunks, vendorChunks,
pgAdminCommonChunks, pgAdminCommonChunks,
providePlugin, providePlugin,

View File

@ -113,6 +113,8 @@ var webpackShimConfig = {
}, },
}, },
// Map module id to file path used in 'define(['baseurl', 'misc']). It is
// used by webpack while creating bundle
resolveAlias: { resolveAlias: {
'baseurl': path.join(__dirname, './pgadmin'), 'baseurl': path.join(__dirname, './pgadmin'),
'misc': path.join(__dirname, './pgadmin/static/bundle/misc'), 'misc': path.join(__dirname, './pgadmin/static/bundle/misc'),
@ -260,6 +262,8 @@ var webpackShimConfig = {
'pgadmin.node.pga_schedule': path.join(__dirname, './pgadmin/browser/server_groups/servers/pgagent/schedules/static/js/pga_schedule'), 'pgadmin.node.pga_schedule': path.join(__dirname, './pgadmin/browser/server_groups/servers/pgagent/schedules/static/js/pga_schedule'),
'pgadmin.node.pga_jobstep': path.join(__dirname, './pgadmin/browser/server_groups/servers/pgagent/steps/static/js/pga_jobstep'), 'pgadmin.node.pga_jobstep': path.join(__dirname, './pgadmin/browser/server_groups/servers/pgagent/steps/static/js/pga_jobstep'),
}, },
// Define list of pgAdmin common libraries to bundle them separately
// into commons JS from app.bundle.js
pgLibs: [ pgLibs: [
'pgadmin.browser.wizard', 'pgadmin.browser.error', 'pgadmin.browser.server.privilege', 'pgadmin.browser.wizard', 'pgadmin.browser.error', 'pgadmin.browser.server.privilege',
'pgadmin.browser.server.variable', 'pgadmin.browser.collection', 'pgadmin.browser.node.ui', 'pgadmin.browser.server.variable', 'pgadmin.browser.collection', 'pgadmin.browser.node.ui',
@ -269,11 +273,13 @@ var webpackShimConfig = {
'misc.file_utility', 'sources/alerts/alertify_wrapper', 'pgadmin.browser.node', 'misc.file_utility', 'sources/alerts/alertify_wrapper', 'pgadmin.browser.node',
'pgadmin.alertifyjs', 'pgadmin.settings', 'pgadmin.preferences', 'pgadmin.sqlfoldcode', 'pgadmin.alertifyjs', 'pgadmin.settings', 'pgadmin.preferences', 'pgadmin.sqlfoldcode',
], ],
// Checks whether JS module is npm module or not
isExternal: function(module) { isExternal: function(module) {
var context = module.context; var context = module.context;
if (typeof context !== 'string') { return false; } if (typeof context !== 'string') { return false; }
return context.indexOf('node_modules') !== -1; return context.indexOf('node_modules') !== -1;
}, },
// Checks whether module is in pgLibs or not. Returns true if exists
isPgAdminLib: function (module) { isPgAdminLib: function (module) {
if (module.rawRequest === undefined) { return false; } if (module.rawRequest === undefined) { return false; }
return this.pgLibs.indexOf(module.rawRequest) !== -1; return this.pgLibs.indexOf(module.rawRequest) !== -1;