Skip to content

Commit

Permalink
feat(gulp-task-build-js-rollup): add corejs options
Browse files Browse the repository at this point in the history
  • Loading branch information
hidoo committed Mar 20, 2019
1 parent dbc864b commit a4f7438
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/gulp-task-build-js-rollup/src/babelOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function babelOptions(options = {}) {
modules: false,
targets: {browsers: options.browsers ? options.browsers : []},
useBuiltIns: options.useBuiltIns ? options.useBuiltIns : false,
corejs: options.corejs ? options.corejs : 2, // eslint-disable-line no-magic-numbers
debug: options.verbose ? options.verbose : false
}]
]
Expand Down
4 changes: 4 additions & 0 deletions packages/gulp-task-build-js-rollup/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const DEFAULT_OPTIONS = {
suffix: '.min',
browsers: ['> 0.5% in JP', 'ie >= 10', 'android >= 4.4'],
useBuiltIns: 'usage',
corejs: 2,
babelrc: path.resolve(process.cwd(), '.babelrc.js'),
inputOptions: {},
outputOptions: {},
Expand All @@ -46,6 +47,8 @@ const DEFAULT_OPTIONS = {
* see: {@link http://browserl.ist/?q=%3E+0.5%25+in+JP%2C+ie%3E%3D+10%2C+android+%3E%3D+4.4 default target browsers}
* @param {String|Boolean} [options.useBuiltIns='usage'] - use polyfill or not.
* see: {@link https://babeljs.io/docs/en/babel-preset-env#usebuiltins useBuiltIns in @babel/preset-env}
* @param {Number|String|Object} [options.corejs=2] - specify core-js version
* see: {@link https://github.com/zloirock/core-js#babelpreset-env corejs in @babel/preset-env}
* @param {String} [options.babelrc=path.resolve(process.cwd(), '.babelrc.js')] - babelrc path.
* see: {@link ./src/babelOptions.js Merged with this function}
* @param {Object} [options.inputOptions] - input options for rollup.js.
Expand Down Expand Up @@ -76,6 +79,7 @@ const DEFAULT_OPTIONS = {
* suffix: '.hoge',
* browsers: ['> 0.1% in JP'],
* useBuiltIns: false,
* corejs: 3,
* babelrc: '/path/to/.babelrc.js',
* inputOptions: {...},
* outputOptions: {...},
Expand Down
13 changes: 13 additions & 0 deletions packages/gulp-task-build-js-rollup/test/babelOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ describe('babelOptions', () => {
modules: false,
targets: {browsers: []},
useBuiltIns: 'usage',
corejs: 2,
debug: false
}]
]}
],
[
{useBuiltIns: 'usage', corejs: 3},
{babelrc: false, externalHelpers: false, exclude: 'node_modules/**', plugins: [], presets: [
['@babel/preset-env', {
modules: false,
targets: {browsers: []},
useBuiltIns: 'usage',
corejs: 3,
debug: false
}]
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@
get: _flags
});

// 7.2.1 RequireObjectCoercible(argument)
var _defined = function _defined(it) {
if (it == undefined) throw TypeError("Can't call method on " + it);
return it;
var toString = {}.toString;

var _cof = function _cof(it) {
return toString.call(it).slice(8, -1);
};

var _library = false;
Expand Down Expand Up @@ -215,6 +215,34 @@
$exports.store = store;
});

var TAG = _wks('toStringTag'); // ES3 wrong here

var ARG = _cof(function () {
return arguments;
}()) == 'Arguments'; // fallback for IE11 Script Access Denied error

var tryGet = function tryGet(it, key) {
try {
return it[key];
} catch (e) {
/* empty */
}
};

var _classof = function _classof(it) {
var O, T, B;
return it === undefined ? 'Undefined' : it === null ? 'Null' // @@toStringTag case
: typeof (T = tryGet(O = Object(it), TAG)) == 'string' ? T // builtinTag case
: ARG ? _cof(O) // ES3 arguments fallback
: (B = _cof(O)) == 'Object' && typeof O.callee == 'function' ? 'Arguments' : B;
};

// 7.2.1 RequireObjectCoercible(argument)
var _defined = function _defined(it) {
if (it == undefined) throw TypeError("Can't call method on " + it);
return it;
};

var _fixReWks = function _fixReWks(KEY, length, exec) {
var SYMBOL = _wks(KEY);
var fns = exec(_defined, SYMBOL, ''[KEY]);
Expand Down Expand Up @@ -253,12 +281,6 @@
}, $replace];
});

var toString = {}.toString;

var _cof = function _cof(it) {
return toString.call(it).slice(8, -1);
};

var MATCH = _wks('match');

var _isRegexp = function _isRegexp(it) {
Expand Down Expand Up @@ -375,6 +397,15 @@
});
});

var test = {};
test[_wks('toStringTag')] = 'z';

if (test + '' != '[object z]') {
_redefine(Object.prototype, 'toString', function toString() {
return '[object ' + _classof(this) + ']';
}, true);
}

var DateProto = Date.prototype;
var INVALID_DATE = 'Invalid Date';
var TO_STRING = 'toString';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* @license
*
* @hidoo/gulp-task-build-js-rollup:
* author: hidoo
* version: 0.8.2
*/

(function () {
'use strict';

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}

function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}

function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}

function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}

subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}

function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}

function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};

return _setPrototypeOf(o, p);
}

function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}

return self;
}

function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}

return _assertThisInitialized(self);
}

/**
* ESModules 形式のモジュール
* @return {void}
*/
function esModule() {
// eslint-disable-next-line no-console
console.log('This is ES Module.');
}

// eslint-disable-next-line strict
/**
* Common JS 形式のモジュール
* @return {void}
*/

var cjs = function commonJsModule() {
// eslint-disable-next-line no-console
console.log('This is Common JS Module.');
};

esModule();
cjs();

var Es2015Class =
/*#__PURE__*/
function () {
function Es2015Class() {
_classCallCheck(this, Es2015Class);

this.name = 'es2015-class';
}

_createClass(Es2015Class, [{
key: "print",
value: function print() {
console.log(this.name); // eslint-disable-line no-console
}
}]);

return Es2015Class;
}();

var ChildEs2015Class =
/*#__PURE__*/
function (_Es2015Class) {
_inherits(ChildEs2015Class, _Es2015Class);

function ChildEs2015Class() {
var _this;

_classCallCheck(this, ChildEs2015Class);

_this = _possibleConstructorReturn(this, _getPrototypeOf(ChildEs2015Class).call(this));
_this.name = 'child-es2015-class';
return _this;
}

return ChildEs2015Class;
}(Es2015Class);

var child = new ChildEs2015Class();
child.print();

}());
51 changes: 41 additions & 10 deletions packages/gulp-task-build-js-rollup/test/fixtures/expected/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@
get: _flags
});

// 7.2.1 RequireObjectCoercible(argument)
var _defined = function _defined(it) {
if (it == undefined) throw TypeError("Can't call method on " + it);
return it;
var toString = {}.toString;

var _cof = function _cof(it) {
return toString.call(it).slice(8, -1);
};

var _library = false;
Expand Down Expand Up @@ -211,6 +211,34 @@
$exports.store = store;
});

var TAG = _wks('toStringTag'); // ES3 wrong here

var ARG = _cof(function () {
return arguments;
}()) == 'Arguments'; // fallback for IE11 Script Access Denied error

var tryGet = function tryGet(it, key) {
try {
return it[key];
} catch (e) {
/* empty */
}
};

var _classof = function _classof(it) {
var O, T, B;
return it === undefined ? 'Undefined' : it === null ? 'Null' // @@toStringTag case
: typeof (T = tryGet(O = Object(it), TAG)) == 'string' ? T // builtinTag case
: ARG ? _cof(O) // ES3 arguments fallback
: (B = _cof(O)) == 'Object' && typeof O.callee == 'function' ? 'Arguments' : B;
};

// 7.2.1 RequireObjectCoercible(argument)
var _defined = function _defined(it) {
if (it == undefined) throw TypeError("Can't call method on " + it);
return it;
};

var _fixReWks = function _fixReWks(KEY, length, exec) {
var SYMBOL = _wks(KEY);
var fns = exec(_defined, SYMBOL, ''[KEY]);
Expand Down Expand Up @@ -249,12 +277,6 @@
}, $replace];
});

var toString = {}.toString;

var _cof = function _cof(it) {
return toString.call(it).slice(8, -1);
};

var MATCH = _wks('match');

var _isRegexp = function _isRegexp(it) {
Expand Down Expand Up @@ -371,6 +393,15 @@
});
});

var test = {};
test[_wks('toStringTag')] = 'z';

if (test + '' != '[object z]') {
_redefine(Object.prototype, 'toString', function toString() {
return '[object ' + _classof(this) + ']';
}, true);
}

var TO_STRING = 'toString';
var $toString = /./[TO_STRING];

Expand Down

Large diffs are not rendered by default.

Binary file not shown.
20 changes: 20 additions & 0 deletions packages/gulp-task-build-js-rollup/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe('gulp-task-build-js-rollup', () => {
.catch((error) => done(error));
});

it('should out to "main.js" that polyfilled by specified version of core-js if argument "options.corejs" is set.', (done) => {
const task = buildJs({
src: `${path.src}/main.js`,
dest: path.dest,
corejs: 3,
commonjsOptions
});

task()
.then((stream) => stream.on('finish', () => {
const actual = fs.readFileSync(`${path.dest}/main.js`).toString().trim(),
expected = replaceVersion(fs.readFileSync(`${path.expected}/main.corejs.js`).toString().trim());

assert(actual);
assert.deepStrictEqual(actual, expected);
done();
}))
.catch((error) => done(error));
});

it('should out to "main.min.js" and "main.min.js.gz" if argument "options.compress" is set.', (done) => {
const task = buildJs({
src: `${path.src}/main.js`,
Expand Down

0 comments on commit a4f7438

Please sign in to comment.