Skip to content

Commit

Permalink
Improve require speed (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
stroncium authored and sindresorhus committed Sep 27, 2019
1 parent 4e65299 commit 61aca7c
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions source/index.js
@@ -1,7 +1,6 @@
'use strict';
const ansiStyles = require('ansi-styles');
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
const template = require('./templates');
const {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex
Expand All @@ -15,9 +14,6 @@ const levelMapping = [
'ansi16m'
];

// `color-convert` models to exclude from the Chalk API due to conflicts and such
const skipModels = new Set(['gray']);

const styles = Object.create(null);

const applyOptions = (object, options = {}) => {
Expand Down Expand Up @@ -76,11 +72,9 @@ styles.visible = {
}
};

for (const model of Object.keys(ansiStyles.color.ansi)) {
if (skipModels.has(model)) {
continue;
}
const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', 'ansi256'];

for (const model of usedModels) {
styles[model] = {
get() {
const {level} = this;
Expand All @@ -92,11 +86,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
};
}

for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
if (skipModels.has(model)) {
continue;
}

for (const model of usedModels) {
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
styles[bgModel] = {
get() {
Expand Down Expand Up @@ -194,6 +184,7 @@ const applyStyle = (self, string) => {
return openAll + string + closeAll;
};

let template;
const chalkTag = (chalk, ...strings) => {
const [firstString] = strings;

Expand All @@ -213,6 +204,10 @@ const chalkTag = (chalk, ...strings) => {
);
}

if (template === undefined) {
template = require('./templates');
}

return template(chalk, parts.join(''));
};

Expand Down

0 comments on commit 61aca7c

Please sign in to comment.