Skip to content

Instantly share code, notes, and snippets.

@davidosomething
Created January 26, 2016 20:56
Show Gist options
  • Save davidosomething/3ca03a3d3294056b82e9 to your computer and use it in GitHub Desktop.
Save davidosomething/3ca03a3d3294056b82e9 to your computer and use it in GitHub Desktop.
/**
* gulp js
*/
/*eslint-env node, es6*/
'use strict';
const fs = require('fs');
const path = require('path');
const util = require('util');
const log = require('debug')('js');
const edPaths = require('../paths.js');
const webpack = require('webpack');
const JS_SRC_DIR = path.join(process.cwd(), edPaths.themeDir, 'src/js/nda/');
const JS_DIST_DIR = path.join(process.cwd(), edPaths.themeDir, 'dist/js/nda/');
const JS_ENTRIES = (function () {
log('Reading files from ', JS_SRC_DIR);
let files = fs.readdirSync(JS_SRC_DIR);
let entriesObject = files.reduce((entries, filename) => {
// filter & reduce
if (path.extname(filename) === '.js'
&& fs.statSync(path.join(JS_SRC_DIR, filename)).isFile()) {
entries[path.basename(filename, '.js')] = `./${filename}`;
}
return entries;
}, {});
log(entriesObject);
return entriesObject;
})();
const JS_PLUGINS = [];
JS_PLUGINS.push(new webpack.BannerPlugin(
`Built on ${new Date().toString()}`
//{ raw: true }
));
const sp = require('webpack-split-by-path');
JS_PLUGINS.push(new sp([
{
name: 'node_modules',
path: `${JS_SRC_DIR}node_modules/`
},
{
name: 'root-node_modules',
path: `${process.cwd()}/config/node_modules/`
}
]));
JS_PLUGINS.push(new webpack.optimize.CommonsChunkPlugin({
name: 'main',
filename: 'main-bundle.min.js',
chunks: [
'main', 'posttype-post', 'home-cat-tag',
],
minChunks: 2,
}));
JS_PLUGINS.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
preserveComments: function (currentNode, currentComment) {
return currentComment.value.indexOf('/*!') > -1;
}, // not using 'some', since it preserves any comments with jsdoc
}));
const WEBPACK_CONFIG = {
context: JS_SRC_DIR,
entry: JS_ENTRIES,
output: {
path: JS_DIST_DIR,
filename: '[name]-bundle.min.js',
sourceMapFilename: '[name]-bundle.map',
},
module: {
loaders: [
{ test: /\.hbs$/, loader: 'mustache?minify' },
],
},
resolve: {
modulesDirectories: [
'config/node_modules/',
// only use local node_modules/, not ed-com/config/node_modules
`${JS_SRC_DIR}node_modules/`,
// allow require bower packages
`${edPaths.themeDir}src/bower/`,
],
},
// devtool: 'source-map',
plugins: JS_PLUGINS,
};
const compiler = webpack(WEBPACK_CONFIG);
const buildEntries = (gulpDone) => {
const webpackDone = (err, stats) => {
console.log(stats.toString());
gulpDone(err);
};
compiler.run(webpackDone);
};
module.exports = buildEntries;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment