Skip to content

Instantly share code, notes, and snippets.

@brigand

brigand/a.js Secret

Last active March 30, 2017 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brigand/f5327551eefcb81d8f77756cb5069ae0 to your computer and use it in GitHub Desktop.
Save brigand/f5327551eefcb81d8f77756cb5069ae0 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const {dirname} = require('path');
const resolve = require('require-relative');
require.extensions['.js'] = (module, filename) => {
const str = fs.readFileSync(module.filename, 'utf-8');
const str2 = str.replace(/require\(([^)]+)\)/, (m, path) => {
const resolved = resolve.resolve(path.replace(/['"]/g, '').trim(), dirname(filename));
return `timeRequire('${resolved}', () => ${m})`;
});
module._compile(str2, filename);
};
const times = [];
global.timeRequire = (path, f) => {
const now = Date.now();
const res = f();
times.push({time: Date.now() - now, path});
return res;
};
require('stylelint');
const output = times
.filter(x => x.time > 50)
.sort((a, b) => b.time - a.time);
console.log(output);
{
"dependencies": {
"require-relative": "^0.8.7",
"stylelint": "^7.9.0"
}
}
[ { time: 309,
path: '/private/tmp/temp-9870/node_modules/stylelint/lib/utils/isAutoprefixable.js' },
{ time: 100,
path: '/private/tmp/temp-9870/node_modules/stylelint/lib/rules/at-rule-blacklist/index.js' },
{ time: 67,
path: '/private/tmp/temp-9870/node_modules/postcss/lib/declaration.js' },
{ time: 65,
path: '/private/tmp/temp-9870/node_modules/doiuse/lib/doiuse.js' },
{ time: 56,
path: '/private/tmp/temp-9870/node_modules/postcss/lib/css-syntax-error.js' },
{ time: 53,
path: '/private/tmp/temp-9870/node_modules/micromatch/lib/expand.js' },
{ time: 51,
path: '/private/tmp/temp-9870/node_modules/stylelint/lib/utils/report.js' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment