From: Nicolas Coden Date: Tue, 20 Mar 2018 21:54:42 +0000 (+0100) Subject: chore: use UMD bundling in customizer X-Git-Tag: v6.6.0~3^2~259^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ed2ed2023454d3c2296e7625df267a46eaa2001;p=thirdparty%2Ffoundation%2Ffoundation-sites.git chore: use UMD bundling in customizer Note: this also ensure that no ES6 code is used in JavaScript gulp task --- diff --git a/gulp/tasks/customizer.js b/gulp/tasks/customizer.js index 4d41800f1..5ef6aec0f 100644 --- a/gulp/tasks/customizer.js +++ b/gulp/tasks/customizer.js @@ -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; @@ -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) { @@ -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()) diff --git a/gulp/tasks/javascript.js b/gulp/tasks/javascript.js index 6d152d7e0..51bc590f0 100644 --- a/gulp/tasks/javascript.js +++ b/gulp/tasks/javascript.js @@ -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 ----- @@ -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', @@ -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: { diff --git a/gulp/utils.js b/gulp/utils.js new file mode 100644 index 000000000..8c4ccea8b --- /dev/null +++ b/gulp/utils.js @@ -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; + }, {}); +}; +