Skip to content

Commit 61aca7c

Browse files
stronciumsindresorhus
authored andcommitted
Improve require speed (#358)
1 parent 4e65299 commit 61aca7c

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

source/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const ansiStyles = require('ansi-styles');
33
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
4-
const template = require('./templates');
54
const {
65
stringReplaceAll,
76
stringEncaseCRLFWithFirstIndex
@@ -15,9 +14,6 @@ const levelMapping = [
1514
'ansi16m'
1615
];
1716

18-
// `color-convert` models to exclude from the Chalk API due to conflicts and such
19-
const skipModels = new Set(['gray']);
20-
2117
const styles = Object.create(null);
2218

2319
const applyOptions = (object, options = {}) => {
@@ -76,11 +72,9 @@ styles.visible = {
7672
}
7773
};
7874

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

77+
for (const model of usedModels) {
8478
styles[model] = {
8579
get() {
8680
const {level} = this;
@@ -92,11 +86,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
9286
};
9387
}
9488

95-
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
96-
if (skipModels.has(model)) {
97-
continue;
98-
}
99-
89+
for (const model of usedModels) {
10090
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
10191
styles[bgModel] = {
10292
get() {
@@ -194,6 +184,7 @@ const applyStyle = (self, string) => {
194184
return openAll + string + closeAll;
195185
};
196186

187+
let template;
197188
const chalkTag = (chalk, ...strings) => {
198189
const [firstString] = strings;
199190

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

207+
if (template === undefined) {
208+
template = require('./templates');
209+
}
210+
216211
return template(chalk, parts.join(''));
217212
};
218213

0 commit comments

Comments
 (0)