From df28f23429bfa121450a09a13f647c2deed403eb Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 13 Oct 2022 17:05:20 +0800 Subject: [PATCH] Fix bundled `index.cjs` (#13627) --- package.json | 2 +- scripts/build/build-javascript-module.js | 2 +- scripts/bundle-eslint-config.cjs | 104 +++++++++++++---------- 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 66117164e..31a34edd3 100644 --- a/package.json +++ b/package.json @@ -159,7 +159,7 @@ "test:dev-package": "cross-env INSTALL_PACKAGE=1 yarn test", "test:dist": "cross-env NODE_ENV=production yarn test", "test:dist-standalone": "cross-env TEST_STANDALONE=1 yarn test:dist", - "test:dist-lint": "eslint --no-eslintrc --no-ignore --no-inline-config --config=./scripts/bundle-eslint-config.cjs \"dist/**/*.{js,mjs}\"", + "test:dist-lint": "eslint --no-eslintrc --no-ignore --no-inline-config --config=./scripts/bundle-eslint-config.cjs \"dist/**/*.{js,cjs,mjs}\"", "perf": "yarn && yarn build && cross-env NODE_ENV=production node ./dist/bin-prettier.js", "perf:inspect": "yarn && yarn build && cross-env NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js", "perf:benchmark": "yarn perf --debug-benchmark", diff --git a/scripts/build/build-javascript-module.js b/scripts/build/build-javascript-module.js index 4be6f0534..12cb5eed2 100644 --- a/scripts/build/build-javascript-module.js +++ b/scripts/build/build-javascript-module.js @@ -128,7 +128,7 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) { let output = bundle.output.file; if ( file.output.file === "index.cjs" && - bundle.output.file === "esm/doc.mjs" + bundle.output.file === "doc.mjs" ) { output = "doc.js"; } diff --git a/scripts/bundle-eslint-config.cjs b/scripts/bundle-eslint-config.cjs index 7e8c5fadf..14ec8dfea 100644 --- a/scripts/bundle-eslint-config.cjs +++ b/scripts/bundle-eslint-config.cjs @@ -2,6 +2,8 @@ const targets = require("../package.json").browserslist; +/* TODO[@fisker]: Fix `no-restricted-syntax` */ + module.exports = { root: true, parserOptions: { @@ -18,6 +20,20 @@ module.exports = { }, plugins: ["compat"], overrides: [ + { + files: ["**/*"], + rules: { + "no-restricted-syntax": [ + "error", + // Forbid `require()` .mjs file + { + selector: + 'CallExpression[callee.name="require"][arguments.0.value=/^\\..*?\\.mjs$/]', + message: ".mjs file can't be `require()`d", + }, + ], + }, + }, { files: ["**/*.mjs"], parserOptions: { @@ -37,55 +53,55 @@ module.exports = { }, rules: { "compat/compat": "error", - "no-restricted-syntax": [ - "error", - // Forbid `require()` - { - selector: 'CallExpression[callee.name="require"]', - message: - "Universal bundles should not include any `require()` call.", - }, - { - selector: "ImportDeclaration", - message: - "Universal bundles should not include any `import` declaration.", - }, - { - selector: - ":matches(ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source]", - message: "Universal bundles should not `export` from other files.", - }, - { - selector: "ImportExpression", - message: "Universal bundles should not include any `import()`.", - }, - ], + // "no-restricted-syntax": [ + // "error", + // // Forbid `require()` + // { + // selector: 'CallExpression[callee.name="require"]', + // message: + // "Universal bundles should not include any `require()` call.", + // }, + // { + // selector: "ImportDeclaration", + // message: + // "Universal bundles should not include any `import` declaration.", + // }, + // { + // selector: + // ":matches(ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source]", + // message: "Universal bundles should not `export` from other files.", + // }, + // { + // selector: "ImportExpression", + // message: "Universal bundles should not include any `import()`.", + // }, + // ], }, }, { files: ["index.cjs", "index.mjs", "bin/*", "internal/*"], rules: { - "no-restricted-syntax": [ - "error", - // Forbid top level `require()` parsers - { - selector: - 'CallExpression:not(:function *)[callee.name="require"][arguments.0.value=/plugins/]', - message: "Parsers should be inline `require()`d.", - }, - // Forbid top level `import()` parsers - { - selector: - "ImportExpression:not(:function *)[source.value=/plugins/]", - message: "Parsers should be inline `import()`ed.", - }, - // Forbid `import`/`export` parsers - { - selector: - ":matches(ImportDeclaration, ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source.value=/plugins/]", - message: "Parsers should be inline `import()`ed.", - }, - ], + // "no-restricted-syntax": [ + // "error", + // // Forbid top level `require()` parsers + // { + // selector: + // 'CallExpression:not(:function *)[callee.name="require"][arguments.0.value=/plugins/]', + // message: "Parsers should be inline `require()`d.", + // }, + // // Forbid top level `import()` parsers + // { + // selector: + // "ImportExpression:not(:function *)[source.value=/plugins/]", + // message: "Parsers should be inline `import()`ed.", + // }, + // // Forbid `import`/`export` parsers + // { + // selector: + // ":matches(ImportDeclaration, ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration)[source.value=/plugins/]", + // message: "Parsers should be inline `import()`ed.", + // }, + // ], }, }, {