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

Improve require speed #358

Merged
merged 4 commits into from Sep 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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']);
stroncium marked this conversation as resolved.
Show resolved Hide resolved

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