From b1e9985837cd81cd1df252ce379939f0103980e7 Mon Sep 17 00:00:00 2001 From: Akira Sudoh Date: Thu, 15 Nov 2018 17:19:51 +0900 Subject: [PATCH] fix(build): ensure es directory has only ESM Fixes #1442. --- gulpfile.js | 30 +++++++++++++++++++++++++++++- package.json | 1 + yarn.lock | 21 +++++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 9536bbfecc98..b948231efd51 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -27,9 +27,11 @@ const sourcemaps = require('gulp-sourcemaps'); const gutil = require('gulp-util'); const header = require('gulp-header'); const jsdoc = require('gulp-jsdoc3'); +const through = require('through2'); // Rollup const rollup = require('rollup'); +const commonjs = require('rollup-plugin-commonjs'); const rollupConfigDev = require('./tools/rollup.config.dev'); const rollupConfigProd = require('./tools/rollup.config'); @@ -208,10 +210,36 @@ gulp.task('scripts:es', () => { ], plugins: ['transform-class-properties'], }; - + const pathsToConvertToESM = new Set([ + path.resolve(__dirname, 'src/globals/js/feature-flags.js'), + path.resolve(__dirname, 'src/globals/js/settings.js'), + ]); + const cjsPlugin = commonjs(); + cjsPlugin.options({ entry: '' }); return gulp .src(srcFiles) .pipe(babel(babelOpts)) + .pipe( + through.obj((file, enc, callback) => { + if (!pathsToConvertToESM.has(file.path)) { + callback(null, file); + } else { + Promise.resolve(cjsPlugin.transform(file.contents.toString(), file.path)).then( + result => { + if (!result) { + callback(null, file); + } else { + file.contents = Buffer.from(result.code); + callback(null, file); + } + }, + err => { + callback(err); + } + ); + } + }) + ) .pipe(gulp.dest('es/')); }); diff --git a/package.json b/package.json index 4cdf1e6ccb06..c27a54a0b332 100644 --- a/package.json +++ b/package.json @@ -146,6 +146,7 @@ "semantic-release": "^15.5.0", "serve-static": "^1.13.0", "style-loader": "^0.20.0", + "through2": "^3.0.0", "vinyl-named": "^1.1.0", "webpack": "^3.10.0", "webpack-dev-middleware": "^2.0.0", diff --git a/yarn.lock b/yarn.lock index 7975806a732b..391f2494db29 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11654,6 +11654,15 @@ read@1, read@~1.0.1, read@~1.0.7: string_decoder "~1.1.1" util-deprecate "~1.0.1" +"readable-stream@2 || 3": + version "3.0.6" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.0.6.tgz#351302e4c68b5abd6a2ed55376a7f9a25be3057a" + integrity sha512-9E1oLoOWfhSXHGv6QlwXJim7uNzd9EVlWK+21tCU9Ju/kR0/p2AZYPz4qSchgO8PlLIH4FpZYfzwS+rEksZjIg== + dependencies: + inherits "^2.0.3" + string_decoder "^1.1.1" + util-deprecate "^1.0.1" + "readable-stream@>=1.0.33-1 <1.1.0-0": version "1.0.34" resolved "http://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" @@ -13203,7 +13212,7 @@ string-width@^1.0.1, string-width@^1.0.2: is-fullwidth-code-point "^2.0.0" strip-ansi "^4.0.0" -string_decoder@^1.0.0, string_decoder@~1.1.1: +string_decoder@^1.0.0, string_decoder@^1.1.1, string_decoder@~1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8" integrity sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg== @@ -13508,6 +13517,14 @@ through2@^2.0.0, through2@^2.0.2, through2@~2.0.0: readable-stream "^2.1.5" xtend "~4.0.1" +through2@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/through2/-/through2-3.0.0.tgz#468b461df9cd9fcc170f22ebf6852e467e578ff2" + integrity sha512-8B+sevlqP4OiCjonI1Zw03Sf8PuV1eRsYQgLad5eonILOdyeRsY27A/2Ze8IlvlMvq31OH+3fz/styI7Ya62yQ== + dependencies: + readable-stream "2 || 3" + xtend "~4.0.1" + through@2, "through@>=2.2.7 <3", through@^2.3.6, through@^2.3.8, through@~2.3, through@~2.3.4, through@~2.3.6: version "2.3.8" resolved "http://registry.npmjs.org/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" @@ -14062,7 +14079,7 @@ useragent@2.2.1: lru-cache "2.2.x" tmp "0.0.x" -util-deprecate@~1.0.1: +util-deprecate@^1.0.1, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=