From 36b8b42b2bc25331bfee4697990ad5e6074aad31 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Fri, 2 Dec 2016 00:03:13 -0500 Subject: [PATCH] add useBuiltIns option --- data/builtInFeatures.js | 72 +++++ data/builtIns.json | 385 ++++++++++++++++++++++++++ scripts/build-data.js | 49 +++- src/index.js | 5 +- src/transformPolyfillRequirePlugin.js | 12 + 5 files changed, 508 insertions(+), 15 deletions(-) create mode 100644 data/builtInFeatures.js create mode 100644 data/builtIns.json create mode 100644 src/transformPolyfillRequirePlugin.js diff --git a/data/builtInFeatures.js b/data/builtInFeatures.js new file mode 100644 index 00000000..81758614 --- /dev/null +++ b/data/builtInFeatures.js @@ -0,0 +1,72 @@ +// https://github.com/zloirock/core-js + +/* eslint-disable quotes */ +module.exports = { + // es2015 + // core-js/fn/map + + // "typed": "typed arrays", ? + // "typed/array-buffer": "typed arrays / ", ? + "typed/data-view": "typed arrays / DataView", + "typed/int8-array": "typed arrays / Int8Array", + "typed/uint8-array": "typed arrays / Uint8Array", + "typed/uint8-clamped-array": "typed arrays / Uint8ClampedArray", + "typed/int16-array": "typed arrays / Int16Array", + "typed/uint16-array": "typed arrays / Uint16Array", + "typed/int32-array": "typed arrays / Int32Array", + "typed/uint32-array": "typed arrays / Uint32Array", + "typed/float32-array": "typed arrays / Float32Array", + "typed/float64-array": "typed arrays / Float64Array", + + "map": "Map", + "set": "Set", + "weak-map": "WeakMap", + "weak-set": "WeakSet", + + // No polyfill for proxy + + "reflect": "Reflect", + "reflect/apply": "Reflect / ", + "reflect/construct": "Reflect / Reflect.construct", + "reflect/define-property": "Reflect / Reflect.defineProperty", + "reflect/delete-property": "Reflect / Reflect.deleteProperty", + "reflect/get": "Reflect / Reflect.get", + "reflect/get-own-property-descriptor": "Reflect / Reflect.getOwnPropertyDescriptor", + "reflect/get-prototype-of": "Reflect / Reflect.getPrototypeOf", + "reflect/has": "Reflect / Reflect.has", + "reflect/is-extensible": "Reflect / Reflect.isExtensible", + "reflect/own-keys": "Reflect / Reflect.ownKeys", + "reflect/prevent-extensions": "Reflect / Reflect.preventExtensions", + "reflect/set": "Reflect / Reflect.set", + "reflect/set-prototype-of": "Reflect / Reflect.setPrototypeOf", + + "promise": "Promise", + + "symbol": "Symbol", + "symbol/for": "Symbol / global symbol registry", + "symbol/key-for": "Symbol / global symbol registry", + + "symbol/has-instance": "well-known symbols / Symbol.hasInstance", + "symbol/is-concat-spreadable": "well-known symbols / Symbol.isConcatSpreadable", + "symbol/iterator": "well-known symbols / Symbol.iterator", + "symbol/match": "well-known symbols / Symbol.match", + "symbol/replace": "well-known symbols / Symbol.replace", + "symbol/search": "well-known symbols / Symbol.search", + "symbol/species": "well-known symbols / Symbol.species", + "symbol/split": "well-known symbols / Symbol.split", + "symbol/to-primitive": "well-known symbols / Symbol.toPrimitive", + "symbol/to-string-tag": "well-known symbols / Symbol.toStringTag", + "symbol/unscopables": "well-known symbols / Symbol.unscopables", + + "object/assign": "Object static methods / Object.assign", + "object/is": "Object static methods / Object.is", + "object/get-own-property-symbols": "Object static methods / Object.getOwnPropertySymbols", + "object/set-prototype-of": "Object static methods / Object.setPrototypeOf", + + "function/name": 'function "name" property', + + "string/raw": "String static methods / String.raw", + "string/string/from-code-point": "String static methods / String.fromCodePoint", + + // String.prototype methods +}; diff --git a/data/builtIns.json b/data/builtIns.json new file mode 100644 index 00000000..306d83ee --- /dev/null +++ b/data/builtIns.json @@ -0,0 +1,385 @@ +{ + "typed/data-view": {}, + "typed/int8-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/uint8-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/uint8-clamped-array": { + "chrome": 5, + "opera": 12, + "edge": 12, + "firefox": 4, + "safari": 6, + "node": 0.12, + "ios": 10 + }, + "typed/int16-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/uint16-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/int32-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/uint32-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/float32-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4, + "ios": 6 + }, + "typed/float64-array": { + "chrome": 5, + "opera": 12, + "firefox": 4, + "safari": 5, + "node": 0.12, + "ie": 10, + "android": 4.1, + "ios": 6 + }, + "map": { + "chrome": 51, + "opera": 38, + "safari": 10, + "node": 6.5, + "ios": 10 + }, + "set": { + "chrome": 51, + "opera": 38, + "safari": 10, + "node": 6.5, + "ios": 10 + }, + "weak-map": { + "chrome": 51, + "opera": 38, + "safari": 9, + "ios": 10 + }, + "weak-set": { + "chrome": 51, + "opera": 38, + "safari": 9, + "ios": 9 + }, + "reflect": { + "chrome": 49, + "opera": 36, + "edge": 13, + "firefox": 45, + "safari": 10, + "ios": 10 + }, + "reflect/apply": {}, + "reflect/construct": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/define-property": { + "chrome": 49, + "opera": 36, + "edge": 13, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/delete-property": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/get": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/get-own-property-descriptor": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/get-prototype-of": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/has": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/is-extensible": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/own-keys": {}, + "reflect/prevent-extensions": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/set": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "reflect/set-prototype-of": { + "chrome": 49, + "opera": 36, + "edge": 12, + "firefox": 42, + "safari": 10, + "ios": 10 + }, + "promise": { + "chrome": 51, + "opera": 38, + "edge": 13, + "firefox": 45, + "safari": 10, + "node": 6.5, + "ios": 10 + }, + "symbol": { + "chrome": 49, + "opera": 36, + "edge": 13, + "firefox": 36, + "safari": 10, + "ios": 10 + }, + "symbol/for": { + "chrome": 38, + "opera": 25, + "edge": 12, + "firefox": 36, + "safari": 9, + "node": 0.12, + "ios": 9 + }, + "symbol/key-for": { + "chrome": 38, + "opera": 25, + "edge": 12, + "firefox": 36, + "safari": 9, + "node": 0.12, + "ios": 9 + }, + "symbol/has-instance": { + "chrome": 51, + "opera": 38, + "firefox": 50, + "safari": 10, + "node": 6.5, + "ios": 10 + }, + "symbol/is-concat-spreadable": { + "chrome": 48, + "opera": 35, + "firefox": 48, + "safari": 10, + "ios": 10 + }, + "symbol/iterator": {}, + "symbol/match": { + "chrome": 50, + "opera": 37, + "firefox": 49, + "safari": 10, + "node": 6, + "ios": 10 + }, + "symbol/replace": { + "chrome": 50, + "opera": 37, + "firefox": 49, + "safari": 10, + "node": 6, + "ios": 10 + }, + "symbol/search": { + "chrome": 50, + "opera": 37, + "firefox": 49, + "safari": 10, + "node": 6, + "ios": 10 + }, + "symbol/species": {}, + "symbol/split": { + "chrome": 50, + "opera": 37, + "firefox": 49, + "safari": 10, + "node": 6, + "ios": 10 + }, + "symbol/to-primitive": { + "chrome": 47, + "opera": 34, + "firefox": 44, + "safari": 10, + "ios": 10 + }, + "symbol/to-string-tag": { + "chrome": 49, + "opera": 36, + "firefox": 51, + "safari": 10, + "ios": 10 + }, + "symbol/unscopables": { + "chrome": 38, + "opera": 25, + "edge": 12, + "firefox": 48, + "safari": 9, + "node": 0.12, + "ios": 9 + }, + "object/assign": { + "chrome": 45, + "opera": 32, + "edge": 12, + "firefox": 34, + "safari": 9, + "node": 4, + "ios": 9 + }, + "object/is": { + "chrome": 19, + "opera": 6, + "edge": 12, + "firefox": 22, + "safari": 9, + "node": 0.12, + "android": 4.1, + "ios": 9 + }, + "object/get-own-property-symbols": { + "chrome": 38, + "opera": 25, + "edge": 12, + "firefox": 36, + "safari": 9, + "node": 0.12, + "ios": 9 + }, + "object/set-prototype-of": { + "chrome": 34, + "opera": 21, + "firefox": 31, + "safari": 9, + "node": 0.12, + "ie": 11, + "ios": 9 + }, + "function/name": { + "chrome": 51, + "opera": 38, + "safari": 10, + "node": 6.5, + "ios": 10 + }, + "string/raw": { + "chrome": 41, + "opera": 28, + "edge": 12, + "firefox": 34, + "safari": 9, + "node": 4, + "ios": 9 + }, + "string/string/from-code-point": { + "chrome": 41, + "opera": 28, + "edge": 12, + "firefox": 29, + "safari": 9, + "node": 4, + "ios": 9 + } +} diff --git a/scripts/build-data.js b/scripts/build-data.js index 1c70a488..42ec5699 100644 --- a/scripts/build-data.js +++ b/scripts/build-data.js @@ -4,6 +4,7 @@ const path = require("path"); const flatten = require("lodash/flatten"); const flattenDeep = require("lodash/flattenDeep"); const pluginFeatures = require("../data/pluginFeatures"); +const builtInFeatures = require("../data/builtInFeatures"); const renameTests = (tests, getName) => tests.map((test) => Object.assign({}, test, { name: getName(test.name) })); @@ -98,7 +99,7 @@ const getLowestImplementedVersion = ({ features }, env) => { }); let envFiltered = envTests.filter((t) => t); - if (envTests.length > envFiltered.length) { + if (envTests.length > envFiltered.length || envTests.length === 0) { // envTests.forEach((test, i) => { // if (!test) { // // print unsupported features @@ -115,27 +116,47 @@ const getLowestImplementedVersion = ({ features }, env) => { .reduce((a, b) => { return (a < b) ? b : a; }); }; -const data = {}; -for (const pluginName in pluginFeatures) { - const options = pluginFeatures[pluginName]; - const plugin = {}; - environments.forEach((env) => { - if (Array.isArray(options.features)) { +function generateData(features) { + let ret = {}; + + Object.keys(features).forEach((pluginName) => { + let options = features[pluginName]; + + if (!options.features) { + options = { + features: [options] + }; + } + + const plugin = {}; + environments.forEach((env) => { const version = getLowestImplementedVersion(options, env); if (version !== null) { plugin[env] = version; } - } - // add opera - if (plugin.chrome) { - plugin.opera = plugin.chrome - 13; - } + // add opera + if (plugin.chrome) { + plugin.opera = plugin.chrome - 13; + + if (plugin.chrome === 5) { + plugin.opera = 12; + } + } + }); + + ret[pluginName] = plugin; }); - data[pluginName] = plugin; + + return ret; } fs.writeFileSync( path.join(__dirname, "../data/plugins.json"), - JSON.stringify(data, null, 2) + "\n" + JSON.stringify(generateData(pluginFeatures), null, 2) + "\n" +); + +fs.writeFileSync( + path.join(__dirname, "../data/builtIns.json"), + JSON.stringify(generateData(builtInFeatures), null, 2) + "\n" ); diff --git a/src/index.js b/src/index.js index bbeefdc7..74f74860 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import pluginList from "../data/plugins.json"; import browserslist from "browserslist"; +import transformPolyfillRequirePlugin from "./transformPolyfillRequirePlugin"; export const MODULE_TRANSFORMATIONS = { "amd": "transform-es2015-modules-amd", @@ -140,6 +141,7 @@ export default function buildPreset(context, opts = {}) { const whitelist = validateWhitelistOption(opts.whitelist); const targets = getTargets(opts.targets); const debug = opts.debug; + const useBuiltIns = opts.useBuiltIns; let transformations = Object.keys(pluginList) .filter((pluginName) => isPluginRequired(targets, pluginList[pluginName])); @@ -179,7 +181,8 @@ export default function buildPreset(context, opts = {}) { return { plugins: [ ...modules, - ...transformations + ...transformations, + useBuiltIns && transformPolyfillRequirePlugin ] }; } diff --git a/src/transformPolyfillRequirePlugin.js b/src/transformPolyfillRequirePlugin.js new file mode 100644 index 00000000..09e99ea1 --- /dev/null +++ b/src/transformPolyfillRequirePlugin.js @@ -0,0 +1,12 @@ +export default function (babel) { + const { types: t } = babel; + + return { + name: "ast-transform", // not required + visitor: { + Identifier(path) { + + } + } + }; +}