-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bf71a3c
Showing
8 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.tgz | ||
node_modules | ||
coverage | ||
.nyc_output | ||
package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
os: | ||
- windows | ||
- linux | ||
- osx | ||
|
||
language: node_js | ||
|
||
node_js: | ||
- 10 | ||
- 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 CFWare, LLC | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# babel-plugin-remove-ungap | ||
|
||
[![Travis CI][travis-image]][travis-url] | ||
[![Greenkeeper badge][gk-image]](https://greenkeeper.io/) | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![MIT][license-image]](LICENSE) | ||
|
||
Remove @ungap ponyfills | ||
|
||
### Install babel-plugin-remove-ungap | ||
|
||
```sh | ||
npm i -D babel-plugin-remove-ungap | ||
``` | ||
|
||
## Usage | ||
|
||
Add `remove-ungap` to your babelrc plugins if the ponyfills are not needed by your | ||
browser target. This could be due to only targeting modern browsers or because | ||
your project already polyfills the browser. | ||
|
||
This plugin currently only works on ES modules before bundling. It can be run by | ||
`rollup-plugin-babel` before import statements are altered. | ||
|
||
### `exclude` option | ||
|
||
You can add the `exclude` option to block removal/processing of specific @ungap modules. | ||
|
||
```js | ||
{ | ||
plugins: [ | ||
['remove-ungap', { | ||
exclude: [ | ||
'@ungap/essential-map' | ||
] | ||
}] | ||
} | ||
``` | ||
|
||
This config will cause any import of `@ungap/essential-map` to be preserved. | ||
|
||
### Modules that are removed | ||
|
||
* [@ungap/assign](https://github.com/ungap/assign) | ||
* [@ungap/array-iterator](https://github.com/ungap/array-iterator) | ||
* [@ungap/custom-event](https://github.com/ungap/custom-event) | ||
* [@ungap/essential-map](https://github.com/ungap/essential-map) | ||
* [@ungap/essential-set](https://github.com/ungap/essential-set) | ||
* [@ungap/essential-symbol](https://github.com/ungap/essential-symbol) | ||
* [@ungap/essential-weakset](https://github.com/ungap/essential-weakset) | ||
* [@ungap/event](https://github.com/ungap/event) | ||
* [@ungap/event-target](https://github.com/ungap/event-target) | ||
* [@ungap/import-node](https://github.com/ungap/import-node) | ||
* [@ungap/is-array](https://github.com/ungap/is-array) | ||
* [@ungap/map](https://github.com/ungap/map) | ||
* [@ungap/set](https://github.com/ungap/set) | ||
* [@ungap/template-literal](https://github.com/ungap/template-literal) | ||
* [@ungap/trim](https://github.com/ungap/trim) | ||
* [@ungap/weakmap](https://github.com/ungap/weakmap) | ||
* [@ungap/weakset](https://github.com/ungap/weakset) | ||
|
||
### @ungap/create-content is altered | ||
|
||
[@ungap/create-content](https://github.com/ungap/create-content) is altered so that | ||
`HAS_CONTENT` is constant true. This allows minifiers to strip code that is not useed | ||
by modern browsers. | ||
|
||
## Running tests | ||
|
||
Tests are provided by xo and ava. | ||
|
||
```sh | ||
npm install | ||
npm test | ||
``` | ||
|
||
[npm-image]: https://img.shields.io/npm/v/babel-plugin-remove-ungap.svg | ||
[npm-url]: https://npmjs.org/package/babel-plugin-remove-ungap | ||
[travis-image]: https://travis-ci.org/cfware/babel-plugin-remove-ungap.svg?branch=master | ||
[travis-url]: https://travis-ci.org/cfware/babel-plugin-remove-ungap | ||
[gk-image]: https://badges.greenkeeper.io/cfware/babel-plugin-remove-ungap.svg | ||
[downloads-image]: https://img.shields.io/npm/dm/babel-plugin-remove-ungap.svg | ||
[downloads-url]: https://npmjs.org/package/babel-plugin-remove-ungap | ||
[license-image]: https://img.shields.io/npm/l/babel-plugin-remove-ungap.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
|
||
const createContentSource = path.join('node_modules', '@ungap', 'create-content', 'esm', 'index.js'); | ||
|
||
const replacements = { | ||
'@ungap/assign': 'Object.assign', | ||
'@ungap/array-iterator': 'Array.prototype[Symbol.iterator]', | ||
'@ungap/custom-event': 'CustomEvent', | ||
'@ungap/essential-map': 'Map', | ||
'@ungap/essential-set': 'Set', | ||
'@ungap/essential-symbol': 'Symbol', | ||
'@ungap/essential-weakset': 'WeakSet', | ||
'@ungap/event': 'Event', | ||
'@ungap/event-target': 'EventTarget', | ||
'@ungap/import-node': 'document.importNode', | ||
'@ungap/is-array': 'Array.isArray', | ||
'@ungap/map': 'Map', | ||
'@ungap/set': 'Set', | ||
'@ungap/template-literal': 'val => val', | ||
'@ungap/trim': 'String.prototype.trim', | ||
'@ungap/weakmap': 'WeakMap', | ||
'@ungap/weakset': 'WeakSet' | ||
}; | ||
|
||
module.exports = ({types: t, template}) => ({ | ||
visitor: { | ||
Program: { | ||
enter() { | ||
this.bindings = []; | ||
this.specialHandlers = { | ||
'@ungap/create-content': true | ||
}; | ||
this.ungapReplacements = {...replacements}; | ||
|
||
const exclude = this.opts.exclude || []; | ||
exclude.forEach(module => { | ||
if (module === '@ungap/create-content') { | ||
this.specialHandlers['@ungap/create-content'] = false; | ||
|
||
return; | ||
} | ||
|
||
delete this.ungapReplacements[module]; | ||
}); | ||
}, | ||
exit() { | ||
this.bindings.forEach(({path}) => { | ||
path.remove(); | ||
}); | ||
} | ||
}, | ||
ImportDeclaration(path) { | ||
const source = path.get('source'); | ||
const specifiers = path.get('specifiers'); | ||
if (specifiers.length !== 1 || !specifiers[0].isImportDefaultSpecifier()) { | ||
return; | ||
} | ||
|
||
const localName = specifiers[0].node.local.name; | ||
const globalName = this.ungapReplacements[source.node.value]; | ||
if (!globalName) { | ||
return; | ||
} | ||
|
||
if (localName === globalName) { | ||
path.remove(); | ||
} else if (/^[a-zA-Z]*$/.test(globalName)) { | ||
this.bindings.push({ | ||
binding: path.scope.getBinding(localName), | ||
globalName, | ||
path | ||
}); | ||
// Defer removal until Program.exit, it needs to exist for the | ||
// binding lookup to work. | ||
} else { | ||
// BUGBUG: figure out how to directly replace usage of localName | ||
path.replaceWith(t.variableDeclaration('var', [ | ||
t.variableDeclarator(t.identifier(localName), template(globalName)().expression) | ||
])); | ||
} | ||
}, | ||
Identifier(path) { | ||
if (path.parent.type === 'ImportDefaultSpecifier') { | ||
return; | ||
} | ||
|
||
const binding = path.scope.getBinding(path.node.name); | ||
this.bindings.filter(item => item.binding === binding).forEach(({globalName}) => { | ||
path.replaceWith(template(globalName)()); | ||
}); | ||
}, | ||
VariableDeclarator(path) { | ||
const {sourceFileName} = path.hub.file.opts.parserOpts; | ||
if (!sourceFileName) { | ||
return; | ||
} | ||
|
||
const {name} = path.node.id; | ||
if (this.specialHandlers['@ungap/create-content'] && sourceFileName.endsWith(createContentSource) && name === 'HAS_CONTENT') { | ||
path.get('init').replaceWith(t.booleanLiteral(true)); | ||
} | ||
} | ||
} | ||
}); | ||
module.exports.replacements = replacements; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "babel-plugin-remove-ungap", | ||
"version": "0.1.0", | ||
"description": "Remove @ungap ponyfills", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "xo && nyc ava -v" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"author": "Corey Farrell", | ||
"license": "MIT", | ||
"files": [ | ||
"*.js" | ||
], | ||
"keywords": [], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cfware/babel-plugin-remove-ungap.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/cfware/babel-plugin-remove-ungap/issues" | ||
}, | ||
"homepage": "https://github.com/cfware/babel-plugin-remove-ungap#readme", | ||
"devDependencies": { | ||
"@babel/core": "^7.2.2", | ||
"ava": "^1.1.0", | ||
"nyc": "^13.1.0", | ||
"xo": "^0.24.0" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"per-file": true, | ||
"lines": 100, | ||
"statements": 100, | ||
"functions": 100, | ||
"branches": 100, | ||
"include": [ | ||
"*.js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import path from 'path'; | ||
|
||
import test from 'ava'; | ||
import {transform} from '@babel/core'; | ||
|
||
import plugin, {replacements} from '..'; | ||
|
||
function babelTest(t, {source, result, opts, filename}) { | ||
const babelrc = { | ||
babelrc: false, | ||
configFile: false, | ||
compact: true | ||
}; | ||
|
||
const {code} = transform(source, {...babelrc, filename, plugins: [[plugin, opts]]}); | ||
const {code: actual} = transform(result, babelrc); | ||
|
||
t.is(code, actual); | ||
} | ||
|
||
function genSource(module, importDest) { | ||
return ` | ||
import ${importDest} from "${module}"; | ||
console.log(typeof ${importDest}); | ||
${importDest}(); | ||
`; | ||
} | ||
|
||
function genResult(statement) { | ||
return ` | ||
console.log(typeof ${statement}); | ||
${statement}(); | ||
`; | ||
} | ||
|
||
Object.entries(replacements).forEach(([module, statement]) => { | ||
const isBasic = /^[a-zA-Z]*$/.test(statement); | ||
|
||
if (isBasic) { | ||
test(`removes default ${module}`, babelTest, { | ||
source: genSource(module, statement), | ||
result: genResult(statement) | ||
}); | ||
|
||
test(`replaces non-default ${module}`, babelTest, { | ||
source: genSource(module, 'AlternativeName'), | ||
result: genResult(statement) | ||
}); | ||
} else { | ||
test(`replaces ${module}`, babelTest, { | ||
source: genSource(module, 'testImportName'), | ||
result: ` | ||
var testImportName = ${statement}; | ||
console.log(typeof testImportName); | ||
testImportName(); | ||
` | ||
}); | ||
} | ||
|
||
test(`exclude ${module}`, babelTest, { | ||
source: genSource(module, 'testImportName'), | ||
result: genSource(module, 'testImportName'), | ||
opts: { | ||
exclude: [module] | ||
} | ||
}); | ||
}); | ||
|
||
test('ignores imports without local name', babelTest, { | ||
source: 'import "@ungap/weakmap";', | ||
result: 'import "@ungap/weakmap";' | ||
}); | ||
|
||
test('ignores imports with multiple local names', babelTest, { | ||
source: 'import WeakMap, {default as alternate} from "@ungap/weakmap";', | ||
result: 'import WeakMap, {default as alternate} from "@ungap/weakmap";' | ||
}); | ||
|
||
test('ignores named imports', babelTest, { | ||
source: 'import {WeakMap} from "@ungap/weakmap";', | ||
result: 'import {WeakMap} from "@ungap/weakmap";' | ||
}); | ||
|
||
test('rewrites HAS_CONTENT in @ungap/create-content', babelTest, { | ||
source: 'var HAS_CONTENT = \'content\' in document;', | ||
result: 'var HAS_CONTENT = true;', | ||
filename: path.join('node_modules', '@ungap', 'create-content', 'esm', 'index.js') | ||
}); | ||
|
||
test('exclude @ungap/create-content', babelTest, { | ||
source: 'var HAS_CONTENT = \'content\' in document;', | ||
result: 'var HAS_CONTENT = \'content\' in document;', | ||
filename: path.join('node_modules', '@ungap', 'create-content', 'esm', 'index.js'), | ||
opts: { | ||
exclude: ['@ungap/create-content'] | ||
} | ||
}); | ||
|
||
test('ignores HAS_CONTENT outside of @ungap/create-content', babelTest, { | ||
source: 'var HAS_CONTENT = \'content\' in document;', | ||
result: 'var HAS_CONTENT = \'content\' in document;', | ||
filename: path.join('node_modules', '@ungap', 'something-else', 'esm', 'index.js') | ||
}); | ||
|
||
test('tolerates HAS_CONTENT in source without filename', babelTest, { | ||
source: 'var HAS_CONTENT = \'content\' in document;', | ||
result: 'var HAS_CONTENT = \'content\' in document;' | ||
}); |