Skip to content

Commit

Permalink
Merge pull request #407 from umijs/feat/es5ImcompatibleVersions
Browse files Browse the repository at this point in the history
feat(af-webpack): new option es5ImcompatibleVersions
  • Loading branch information
sorrycc authored Apr 28, 2018
2 parents 9eb9747 + d9b6401 commit 6d49e14
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/af-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"deprecate": "^1.0.0",
"detect-port": "^1.2.1",
"didyoumean": "^1.2.1",
"es5-imcompatible-versions": "^0.1.2",
"eslint": "^4.7.1",
"eslint-config-umi": "^0.1.4",
"eslint-loader": "^2.0.0",
Expand All @@ -40,6 +41,7 @@
"less": "^2.7.2",
"less-loader": "^4.0.5",
"lodash.isequal": "^4.5.0",
"pkg-up": "^2.0.0",
"postcss": "^6.0.11",
"postcss-flexbugs-fixes": "^3.2.0",
"postcss-loader": "^2.0.6",
Expand All @@ -48,6 +50,7 @@
"react-error-overlay": "^3.0.0",
"requireindex": "^1.1.0",
"resolve": "^1.5.0",
"semver": "^5.5.0",
"sockjs-client": "1.1.4",
"strip-ansi": "3.0.1",
"strip-json-comments": "^2.0.1",
Expand Down
31 changes: 31 additions & 0 deletions packages/af-webpack/src/es5ImcompatibleVersions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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.json');

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

export 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;
}
}
12 changes: 11 additions & 1 deletion packages/af-webpack/src/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import normalizeTheme from './normalizeTheme';
import { applyWebpackConfig } from './applyWebpackConfig';
import readRc from './readRc';
import { stripLastSlash } from './utils';
import { getPkgPath, shouldTransform } from './es5ImcompatibleVersions';

const { TsConfigPathsPlugin } = require('awesome-typescript-loader'); // eslint-disable-line
const debug = require('debug')('af-webpack:getConfig');
Expand Down Expand Up @@ -333,6 +334,15 @@ export default function getConfig(opts = {}) {
}
}

const extraBabelIncludes = opts.extraBabelIncludes || [];
if (opts.es5ImcompatibleVersions) {
extraBabelIncludes.push(a => {
if (a.indexOf('node_modules') === -1) return false;
const pkgPath = getPkgPath(a);
return shouldTransform(pkgPath);
});
}

const config = {
bail: !isDev,
devtool: opts.devtool || undefined,
Expand Down Expand Up @@ -449,7 +459,7 @@ export default function getConfig(opts = {}) {
},
],
},
...(opts.extraBabelIncludes || []).map(include => {
...extraBabelIncludes.map(include => {
return {
test: /\.(js|jsx)$/,
include:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from 'assert';

export default function() {
return {
name: 'es5ImcompatibleVersions',
validate(val) {
assert(
typeof val === 'boolean',
`The es5ImcompatibleVersions config must be Boolean, but got ${val}`,
);
},
};
}

0 comments on commit 6d49e14

Please sign in to comment.