Skip to content

Commit

Permalink
[babel 8] Use ESM-based chalk@5 (#15792)
Browse files Browse the repository at this point in the history
Co-authored-by: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com>
  • Loading branch information
nicolo-ribaudo and liuxingbaoyu committed Aug 25, 2023
1 parent 2a6d803 commit e43d8e7
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 147 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ jobs:
run: |
yarn dedupe --check
- name: Check for dependency cycles
# todo: investigate why we need to install dependencies here to not get
# the following error:
# Internal Error: chalk@condition:BABEL_8_BREAKING ?[...]: This package
# doesn't seem to be present in your lockfile; run "yarn install" to
# update the lockfile
run: |
yarn
yarn release-tool check-cycles
test-coverage:
Expand Down
17 changes: 12 additions & 5 deletions .yarn/plugins/@yarnpkg/plugin-conditions.cjs

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ module.exports = function (api) {
"@babel/transform-modules-commonjs",
{ importInterop: importInteropTest },
],
"@babel/plugin-transform-dynamic-import",
],
},
{
Expand All @@ -315,6 +316,10 @@ module.exports = function (api) {
exclude: /regenerator-runtime/,
plugins: [["@babel/transform-runtime", transformRuntimeOptions]],
},
env === "standalone" && {
test: /chalk/,
plugins: [pluginReplaceNavigator],
},
].filter(Boolean),
};

Expand Down Expand Up @@ -969,3 +974,20 @@ function pluginGeneratorOptimization({ types: t }) {
},
};
}

function pluginReplaceNavigator({ template }) {
return {
visitor: {
MemberExpression(path) {
const object = path.get("object");
if (object.isIdentifier({ name: "navigator" })) {
object.replaceWith(
template.expression.ast`
typeof navigator == "object" ? navigator : {}
`
);
}
},
},
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@typescript-eslint/parser": "^6.3.0",
"babel-plugin-transform-charcodes": "^0.2.0",
"c8": "^8.0.1",
"chalk": "^5.0.0",
"chalk": "^5.3.0",
"charcodes": "^0.2.0",
"core-js": "^3.31.1",
"eslint": "^8.44.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-code-frame/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/highlight": "workspace:^",
"chalk": "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2"
"chalk": "condition:BABEL_8_BREAKING ? ^5.3.0 : ^2.4.2 (esm:default,Chalk)"
},
"devDependencies": {
"import-meta-resolve": "^3.0.0",
"strip-ansi": "^4.0.0"
},
"engines": {
Expand Down
9 changes: 2 additions & 7 deletions packages/babel-code-frame/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import highlight, { shouldHighlight } from "@babel/highlight";

import _chalk from "chalk";
const chalk = _chalk as unknown as typeof import("chalk-BABEL_8_BREAKING-true");
type Chalk =
typeof import("chalk-BABEL_8_BREAKING-true").Instance extends new () => infer R
? R
: never;
import chalk, { Chalk as ChalkClass, type ChalkInstance as Chalk } from "chalk";

let chalkWithForcedColor: Chalk = undefined;
function getChalk(forceColor: boolean) {
if (forceColor) {
chalkWithForcedColor ??= process.env.BABEL_8_BREAKING
? new chalk.Instance({ level: 1 })
? new ChalkClass({ level: 1 })
: // @ts-expect-error .Instance was .constructor in chalk 2
new chalk.constructor({ enabled: true, level: 1 });
return chalkWithForcedColor;
Expand Down
28 changes: 16 additions & 12 deletions packages/babel-code-frame/test/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import { USE_ESM } from "$repo-utils";

import stripAnsi from "strip-ansi";
import chalk from "chalk";
import _codeFrame, { codeFrameColumns } from "../lib/index.js";
const codeFrame = _codeFrame.default || _codeFrame;

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const babelHighlightChalk = require(require.resolve("chalk", {
paths: [require.resolve("@babel/highlight")],
}));

describe("@babel/code-frame", function () {
let babelHighlightChalk;
beforeAll(async function () {
if (USE_ESM && process.env.BABEL_8_BREAKING) {
const { resolve } = await import("import-meta-resolve");
({ default: babelHighlightChalk } = await import(
resolve("chalk", resolve("@babel/highlight", import.meta.url))
));
} else {
babelHighlightChalk = require(require.resolve("chalk", {
paths: [require.resolve("@babel/highlight")],
}));
}
});

function stubColorSupport(supported) {
let originalChalkEnabled;
let originalChalkLevel;
let originalChalkSupportsColor;
let originalHighlightChalkEnabled;
let originalHighlightChalkLevel;
let originalHighlightChalkSupportsColor;

beforeEach(function () {
originalChalkSupportsColor = chalk.supportsColor;
originalChalkLevel = chalk.level;
originalChalkEnabled = chalk.enabled;
originalHighlightChalkSupportsColor = babelHighlightChalk.supportsColor;
originalHighlightChalkLevel = babelHighlightChalk.level;
originalHighlightChalkEnabled = babelHighlightChalk.enabled;

babelHighlightChalk.supportsColor = chalk.supportsColor = supported
? { level: 1 }
: false;
babelHighlightChalk.level = chalk.level = supported ? 1 : 0;
babelHighlightChalk.enabled = chalk.enabled = supported;
});

afterEach(function () {
chalk.supportsColor = originalChalkSupportsColor;
chalk.level = originalChalkLevel;
chalk.enabled = originalChalkEnabled;
babelHighlightChalk.supportsColor = originalHighlightChalkSupportsColor;
babelHighlightChalk.level = originalHighlightChalkLevel;
babelHighlightChalk.enabled = originalHighlightChalkEnabled;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-highlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-validator-identifier": "workspace:^",
"chalk": "condition:BABEL_8_BREAKING ? ^4.1.2 : ^2.4.2",
"chalk": "condition:BABEL_8_BREAKING ? ^5.3.0 : ^2.4.2 (esm:default,Chalk)",
"js-tokens": "condition:BABEL_8_BREAKING ? ^8.0.0 : ^4.0.0"
},
"devDependencies": {
Expand Down
11 changes: 3 additions & 8 deletions packages/babel-highlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
isKeyword,
} from "@babel/helper-validator-identifier";

import _chalk from "chalk";
const chalk = _chalk as unknown as typeof import("chalk-BABEL_8_BREAKING-true");
type Chalk =
typeof import("chalk-BABEL_8_BREAKING-true").Instance extends new () => infer R
? R
: never;
import chalk, { Chalk as ChalkClass, type ChalkInstance as Chalk } from "chalk";

/**
* Names that are always allowed as identifiers, but also appear as keywords
Expand Down Expand Up @@ -253,14 +248,14 @@ type Options = {
* Whether the code should be highlighted given the passed options.
*/
export function shouldHighlight(options: Options): boolean {
return !!chalk.supportsColor || options.forceColor;
return chalk.level > 0 || options.forceColor;
}

let chalkWithForcedColor: Chalk = undefined;
function getChalk(forceColor: boolean) {
if (forceColor) {
chalkWithForcedColor ??= process.env.BABEL_8_BREAKING
? new chalk.Instance({ level: 1 })
? new ChalkClass({ level: 1 })
: // @ts-expect-error .Instance was .constructor in chalk 2
new chalk.constructor({ enabled: true, level: 1 });
return chalkWithForcedColor;
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-highlight/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ const describeBabel7NoESM =
describe("@babel/highlight", function () {
function stubColorSupport(supported) {
let originalChalkLevel;
let originalChalkSupportsColor;
let originalChalkEnabled;

beforeEach(function () {
originalChalkSupportsColor = chalk.supportsColor;
originalChalkLevel = chalk.level;
originalChalkEnabled = chalk.enabled;
chalk.supportsColor = supported ? { level: 1 } : false;
chalk.level = supported ? 1 : 0;
chalk.enabled = supported;
});

afterEach(function () {
chalk.supportsColor = originalChalkSupportsColor;
chalk.level = originalChalkLevel;
chalk.enabled = originalChalkEnabled;
});
Expand Down
2 changes: 1 addition & 1 deletion test/esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"devDependencies": {
"@babel/runtime": "workspace:^",
"@babel/runtime-corejs3": "workspace:^",
"chalk": "^4.1.0"
"chalk": "^5.3.0"
}
}

0 comments on commit e43d8e7

Please sign in to comment.