Skip to content

Commit

Permalink
chore: use UMD bundling in customizer
Browse files Browse the repository at this point in the history
Note: this also ensure that no ES6 code is used in JavaScript gulp task
  • Loading branch information
ncoden committed Mar 20, 2018
1 parent e3b8d5e commit 3ed2ed2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
35 changes: 22 additions & 13 deletions gulp/tasks/customizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var webpackStream = require('webpack-stream');
var webpack2 = require('webpack');
var named = require('vinyl-named');

var utils = require('../utils.js');

var ARGS = yargs.argv;
var FOUNDATION_VERSION = require('../../package.json').version;
Expand All @@ -38,18 +39,26 @@ var CUSTOMIZER_CONFIG;
var MODULE_LIST;
var VARIABLE_LIST;

var WEBPACK_MODULE_CONFIG = {
rules: [
{
test: /.js$/,
use: [
{
loader: 'babel-loader'
}
]
}
]
}
var WEBPACK_CONFIG = {
externals: utils.umdExternals({
'jquery': 'jQuery'
}),
module: {
rules: [
{
test: /.js$/,
use: [
{
loader: 'babel-loader'
}
]
}
]
},
output: {
libraryTarget: 'umd',
}
};

// Load the configuration file for the customizer. It's a list of modules to load and Sass variables to override
gulp.task('customizer:loadConfig', function(done) {
Expand Down Expand Up @@ -113,7 +122,7 @@ gulp.task('customizer:javascript-entry', ['customizer:loadConfig'], function() {

gulp.task('customizer:javascript', ['customizer:javascript-entry'], function() {
return gulp.src(path.join(OUTPUT_DIR, 'js/vendor/foundation.js'))
.pipe(webpackStream({externals: {jquery: 'jQuery'}, module: WEBPACK_MODULE_CONFIG}, webpack2))
.pipe(webpackStream(WEBPACK_CONFIG, webpack2))
.pipe(rename('foundation.js'))
.pipe(gulp.dest(path.join(OUTPUT_DIR, 'js/vendor')))
.pipe(uglify())
Expand Down
28 changes: 6 additions & 22 deletions gulp/tasks/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var webpackStream = require('webpack-stream');
var webpack2 = require('webpack');
var named = require('vinyl-named');

var utils = require('../utils.js');
var CONFIG = require('../config.js');

// ----- WEBPACK CONFIGURATION -----
Expand All @@ -17,29 +18,12 @@ var CONFIG = require('../config.js');
// (just throw in foundation.js or foundation.min.js) or you should be using a build
// system.

// Convert an external config object for UMD modules
// See: https://webpack.js.org/configuration/externals/#object
function umdExternals(externals, options) {
options = Object.assign({ namespace: '' }, options);
const umdExternalPath = (...args) => [...args].filter(v => v && !!v.length);

return Object.keys(externals).reduce(function(obj, k) {
obj[k] = {
root: umdExternalPath(options.namespace, externals[k]),
amd: k,
commonjs: k,
commonjs2: k,
};
return obj;
}, {});
};

// Generate plugin Externals config for UMD modules
const webpackExternalPlugins = Object.assign(
umdExternals({
var webpackExternalPlugins = Object.assign(
utils.umdExternals({
'jquery': 'jQuery',
}),
umdExternals({
utils.umdExternals({
// Import path | Exported file
'./foundation.core': 'foundation.core',
'./foundation.core.utils': 'foundation.core',
Expand All @@ -61,13 +45,13 @@ const webpackExternalPlugins = Object.assign(
}, { namespace: CONFIG.JS_BUNDLE_NAMESPACE })
);

const webpackOutputAsExternal = {
var webpackOutputAsExternal = {
library: [CONFIG.JS_BUNDLE_NAMESPACE, '[name]'],
libraryTarget: 'umd',
};

var webpackConfig = {
externals: umdExternals({
externals: utils.umdExternals({
'jquery': 'jQuery'
}),
module: {
Expand Down
20 changes: 20 additions & 0 deletions gulp/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function arrayClear(array) {
return array.filter(function (v) { return !!v });
}

// Convert an external config object for UMD modules
// See: https://webpack.js.org/configuration/externals/#object
module.exports.umdExternals = function(externals, options) {
options = Object.assign({ namespace: '' }, options);

return Object.keys(externals).reduce(function(obj, k) {
obj[k] = {
root: arrayClear([options.namespace, externals[k]]),
amd: k,
commonjs: k,
commonjs2: k,
};
return obj;
}, {});
};

0 comments on commit 3ed2ed2

Please sign in to comment.