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

[CJS] Added picocolors.cjs file generator #5

Closed
wants to merge 1 commit into from

Conversation

denysovkos
Copy link
Collaborator

Run npm run build:cjs -- it will create build folder with picocolors.cjs file inside

@alexeyraspopov alexeyraspopov mentioned this pull request Sep 27, 2021
@ai
Copy link
Contributor

ai commented Sep 27, 2021

Compiled CJS file often too big. It was a reason why I avoided it in nanocolors and even in nanoid I do the very simple exports = { x }exports { x } compiling.

@alexeyraspopov
Copy link
Owner

Sorry for misclick

I agree that the build output can be quite bloated. Considering the difference between CJS require and ESM import, an option can be to maintain two files:

// picocolors.js
export let red = ...
export let green = ...
...
// picocolors.cjs
module.exports = {
  red: ...,
  green: ...,
  ...
}

Thus, let { red, green } = require('picocolors') and import { red, green } from 'picocolors' should be working just fine.

@alexeyraspopov
Copy link
Owner

Having picocolors.cjs like this should give us an easy support for Node v6+

let tty = require("tty");

let isColorSupported =
  !("NO_COLOR" in process.env || process.argv.includes("--no-color")) &&
  ("FORCE_COLOR" in process.env ||
    process.argv.includes("--color") ||
    process.platform === "win32" ||
    (tty.isatty(1) && process.env.TERM !== "dumb") ||
    "CI" in process.env);

function formatter(open, close, replace = open) {
  return isColorSupported
    ? (string) => {
        let index = string.indexOf(close, open.length);
        return !~index
          ? open + string + close
          : open + replaceClose(string, close, replace, index) + close;
      }
    : (string) => string;
}

function replaceClose(string, close, replace, index) {
  if (!~index) return string;
  let start = string.substring(0, index) + replace;
  let end = string.substring(index + close.length);
  return start + replaceClose(end, close, replace, end.indexOf(close));
}

module.exports = {
  isColorSupported,
  reset: (s) => `\x1b[0m${s}\x1b[0m`,
  bold: formatter("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
  dim: formatter("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
  italic: formatter("\x1b[3m", "\x1b[23m"),
  underline: formatter("\x1b[4m", "\x1b[24m"),
  inverse: formatter("\x1b[7m", "\x1b[27m"),
  hidden: formatter("\x1b[8m", "\x1b[28m"),
  strikethrough: formatter("\x1b[9m", "\x1b[29m"),
  black: formatter("\x1b[30m", "\x1b[39m"),
  red: formatter("\x1b[31m", "\x1b[39m"),
  green: formatter("\x1b[32m", "\x1b[39m"),
  yellow: formatter("\x1b[33m", "\x1b[39m"),
  blue: formatter("\x1b[34m", "\x1b[39m"),
  magenta: formatter("\x1b[35m", "\x1b[39m"),
  cyan: formatter("\x1b[36m", "\x1b[39m"),
  white: formatter("\x1b[37m", "\x1b[39m"),
  gray: formatter("\x1b[90m", "\x1b[39m"),
  bgBlack: formatter("\x1b[40m", "\x1b[49m"),
  bgRed: formatter("\x1b[41m", "\x1b[49m"),
  bgGreen: formatter("\x1b[42m", "\x1b[49m"),
  bgYellow: formatter("\x1b[43m", "\x1b[49m"),
  bgBlue: formatter("\x1b[44m", "\x1b[49m"),
  bgMagenta: formatter("\x1b[45m", "\x1b[49m"),
  bgCyan: formatter("\x1b[46m", "\x1b[49m"),
  bgWhite: formatter("\x1b[47m", "\x1b[49m"),
};

@alexeyraspopov
Copy link
Owner

Alternatively, we can do Object.assign(module.exports, { ... }) to keep the parity between ESM and CJS in terms of using "named exports"

@alexeyraspopov
Copy link
Owner

Implemented in #7

@alexeyraspopov alexeyraspopov deleted the cjs-file-generator-command branch September 27, 2021 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants