Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new plugin: umi-plugin-es5-imcompatible-versions #399

Merged
merged 2 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/umi-plugin-es5-imcompatible-versions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# umi-plugin-es5-imcompatible-versions

## Usage

Install,

```bash
$ npm i umi-plugin-es5-imcompatible-versions --save-dev
```

Edit `.umirc.js`,

```js
export default {
plugins: [
'umi-plugin-es5-imcompatible-versions',
],
}
```
25 changes: 25 additions & 0 deletions packages/umi-plugin-es5-imcompatible-versions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "umi-plugin-es5-imcompatible-versions",
"version": "0.0.1",
"main": "./lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-es5-imcompatible-versions"
},
"homepage": "https://github.com/umijs/umi/tree/master/packages/umi-plugin-es5-imcompatible-versions",
"authors": [
"chencheng <[email protected]> (https://github.com/sorrycc)"
],
"bugs": {
"url": "https://github.com/umijs/umi/issues"
},
"files": [
"lib",
"src"
],
"dependencies": {
"es5-imcompatible-versions": "^0.1.1",
"pkg-up": "^2.0.0",
"semver": "^5.5.0"
}
}
45 changes: 45 additions & 0 deletions packages/umi-plugin-es5-imcompatible-versions/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { dirname } from 'path';
import pkgUp from 'pkg-up';
import { satisfies } from 'semver';

const pkgPathCache = {};
const pkgCache = {};
const {
config: { 'es5-imcompatible-versions': config },
} = require('es5-imcompatible-versions/package');

function getPkgPath(filePath) {
const dir = dirname(filePath);
if (dir in pkgPathCache) return pkgPathCache[dir];
pkgPathCache[dir] = pkgUp.sync(filePath);
return pkgPathCache[dir];
}

function shouldTransform(pkgPath) {
if (pkgPath in pkgCache) return pkgCache[pkgPath];
const { name, version } = require(pkgPath); // eslint-disable-line
pkgCache[pkgPath] = isMatch(name, version);
return pkgCache[pkgPath];
}

function isMatch(name, version) {
if (config[name]) {
return Object.keys(config[name]).some(sv => satisfies(version, sv));
} else {
return false;
}
}

export default api => {
api.register('modifyAFWebpackOpts', ({ memo }) => {
memo.extraBabelIncludes = [
...(memo.extraBabelIncludes || []),
a => {
if (a.indexOf('node_modules') === -1) return false;
const pkgPath = getPkgPath(a);
return shouldTransform(pkgPath);
},
];
return memo;
});
};