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

feat(af-webpack): new option es5ImcompatibleVersions #407

Merged
merged 2 commits into from
Apr 28, 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
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}`,
);
},
};
}