From be0739ec229843ba8b93864e4b070ffe4210dafc Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 12 Feb 2022 15:39:16 +0800 Subject: [PATCH] Add `type:"module"` to `package.json` (#12272) --- .eslintrc.js => .eslintrc.cjs | 60 ++++++++++++------- CONTRIBUTING.md | 2 +- bin/package.json | 3 - index.js | 4 +- jest-format-test.config.mjs | 2 +- jest-integration-test.config.mjs | 4 ++ jest.config.mjs | 2 +- package.json | 1 + scripts/build-website.mjs | 5 +- scripts/build/build.mjs | 1 + scripts/build/bundler.mjs | 11 +++- scripts/generate-schema.mjs | 2 +- ...sync-flow-tests.js => sync-flow-tests.cjs} | 2 +- .../package.json | 1 + ...generate-schema.js => generate-schema.mjs} | 6 +- src/cli/package.json | 4 +- src/package.json | 5 ++ standalone.js | 4 +- tests/config/format-test-setup.js | 19 ++++++ tests/config/format-test.js | 16 +++-- tests/config/get-prettier.js | 12 ++++ .../jest-light-runner/src/worker-runner.js | 6 +- tests/config/prettier-entry.common.cjs | 7 +++ tests/config/prettier-entry.js | 3 + tests/config/require-prettier.cjs | 5 -- tests/config/setup.js | 3 - tests/format/js/cursor/jsfmt.spec.js | 4 +- tests/format/misc/empty/jsfmt.spec.js | 2 +- tests/integration/__tests__/schema.js | 2 +- tests/package.json | 3 - vendors/package.json | 5 ++ website/package.json | 1 + 32 files changed, 143 insertions(+), 64 deletions(-) rename .eslintrc.js => .eslintrc.cjs (86%) delete mode 100644 bin/package.json rename scripts/{sync-flow-tests.js => sync-flow-tests.cjs} (97%) rename scripts/utils/{generate-schema.js => generate-schema.mjs} (98%) create mode 100644 src/package.json create mode 100644 tests/config/format-test-setup.js create mode 100644 tests/config/get-prettier.js create mode 100644 tests/config/prettier-entry.common.cjs create mode 100644 tests/config/prettier-entry.js delete mode 100644 tests/config/require-prettier.cjs delete mode 100644 tests/config/setup.js delete mode 100644 tests/package.json create mode 100644 vendors/package.json diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 86% rename from .eslintrc.js rename to .eslintrc.cjs index 5c1ed27ac..83fa63bc4 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -7,6 +7,10 @@ module.exports = { es2021: true, node: true, }, + parserOptions: { + ecmaVersion: "latest", + sourceType: "module", + }, reportUnusedDisableDirectives: true, extends: ["eslint:recommended", "prettier"], plugins: [ @@ -87,7 +91,6 @@ module.exports = { }, ], "require-await": "error", - strict: "error", "symbol-description": "error", yoda: [ "error", @@ -98,12 +101,13 @@ module.exports = { ], "import/extensions": ["error", "ignorePackages"], - "import/no-extraneous-dependencies": [ - "error", - { - devDependencies: ["jest.config.mjs", "tests/**", "scripts/**"], - }, - ], + // TODO[@fisker]: Fix this + // "import/no-extraneous-dependencies": [ + // "error", + // { + // devDependencies: ["jest.config.mjs", "tests/**", "scripts/**"], + // }, + // ], "import/order": "error", "import/no-anonymous-default-export": "error", @@ -142,6 +146,8 @@ module.exports = { "unicorn/prefer-array-some": "error", "unicorn/prefer-includes": "error", "unicorn/prefer-json-parse-buffer": "error", + "unicorn/prefer-module": "error", + "unicorn/prefer-node-protocol": "error", "unicorn/prefer-number-properties": "error", "unicorn/prefer-optional-catch-binding": "error", "unicorn/prefer-regexp-test": "error", @@ -155,6 +161,7 @@ module.exports = { { files: [ "scripts/**/*.js", + "scripts/**/*.cjs", "scripts/**/*.mjs", "tests/config/install-prettier.js", ], @@ -162,25 +169,33 @@ module.exports = { "no-console": "off", }, }, + // CommonJS modules { files: [ - "**/*.mjs", - "scripts/release/**/*.js", - "tests/**/*.js", - "bin/**/*.js", - "src/cli/**/*.js", - ], - excludedFiles: [ + "**/*.cjs", + "src/*.js", + "src/common/**/*.js", + "src/config/**/*.js", + "src/document/**/*.js", + "src/language-css/**/*.js", + "src/language-graphql/**/*.js", + "src/language-handlebars/**/*.js", + "src/language-html/**/*.js", + "src/language-js/**/*.js", + "src/language-markdown/**/*.js", + "src/language-yaml/**/*.js", + "src/main/**/*.js", + "src/utils/**/*.js", + "scripts/tools/eslint-plugin-prettier-internal-rules/**/*.js", + "website/**/*.js", "tests/integration/plugins/automatic/prettier-plugin-bar.js", ], parserOptions: { - sourceType: "module", + sourceType: "script", }, rules: { - "unicorn/prefer-module": "error", - "unicorn/prefer-node-protocol": "error", - // TODO[@fisker]: Fix this - "import/no-extraneous-dependencies": "off", + strict: "error", + "unicorn/prefer-module": "off", }, }, // `esbuild` don't support @@ -226,7 +241,6 @@ module.exports = { { files: ["tests/**/*.js"], rules: { - strict: "off", "import/no-extraneous-dependencies": "off", "unicorn/prefer-array-flat": "off", "unicorn/prefer-array-flat-map": "off", @@ -329,5 +343,11 @@ module.exports = { sourceType: "module", }, }, + { + files: ["scripts/tools/eslint-plugin-prettier-internal-rules/**/*.js"], + rules: { + "import/no-extraneous-dependencies": "error", + }, + }, ], }; diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d5472531d..5a4ac34b8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -53,7 +53,7 @@ Parameters: The implementation of `run_spec` can be found in [`tests/config/format-test.js`](tests/config/format-test.js). -`tests/format/flow-repo/` contains the Flow test suite and is not supposed to be edited by hand. To update it, clone the Flow repo next to the Prettier repo and run: `node scripts/sync-flow-tests.js ../flow/tests/`. +`tests/format/flow-repo/` contains the Flow test suite and is not supposed to be edited by hand. To update it, clone the Flow repo next to the Prettier repo and run: `node scripts/sync-flow-tests.cjs ../flow/tests/`. ## Debugging diff --git a/bin/package.json b/bin/package.json deleted file mode 100644 index 3dbc1ca59..000000000 --- a/bin/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/index.js b/index.js index dc0ab434b..0fdc642dc 100644 --- a/index.js +++ b/index.js @@ -1,3 +1 @@ -"use strict"; - -module.exports = require("./src/index.js"); +export { default } from "./src/index.js"; diff --git a/jest-format-test.config.mjs b/jest-format-test.config.mjs index f7b95a30c..73d208f03 100644 --- a/jest-format-test.config.mjs +++ b/jest-format-test.config.mjs @@ -4,7 +4,7 @@ const config = { ...baseConfig, runner: "./tests/config/jest-light-runner", testRegex: "tests/format/.*/jsfmt\\.spec\\.js$", - setupFiles: ["/tests/config/setup.js"], + setupFiles: ["/tests/config/format-test-setup.js"], projects: [], }; diff --git a/jest-integration-test.config.mjs b/jest-integration-test.config.mjs index 85d61c55e..20d03a514 100644 --- a/jest-integration-test.config.mjs +++ b/jest-integration-test.config.mjs @@ -4,6 +4,10 @@ const config = { ...baseConfig, testRegex: "tests/integration/__tests__/.*\\.js$", projects: [], + moduleNameMapper: { + ...baseConfig.moduleNameMapper, + "prettier-local": "/tests/config/prettier-entry.common.cjs", + }, }; export default config; diff --git a/jest.config.mjs b/jest.config.mjs index 99fab5f8e..1c39b4df1 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -46,7 +46,7 @@ const config = { ], coverageReporters: ["text", "lcov"], moduleNameMapper: { - "prettier-local": "/tests/config/require-prettier.cjs", + "prettier-local": "/tests/config/prettier-entry.js", "prettier-standalone": "/tests/config/require-standalone.cjs", }, modulePathIgnorePatterns: [ diff --git a/package.json b/package.json index c60ca6953..40b9d1468 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "funding": "https://github.com/prettier/prettier?sponsor=1", "homepage": "https://prettier.io", "author": "James Long", + "type": "module", "license": "MIT", "main": "./index.js", "browser": "./standalone.js", diff --git a/scripts/build-website.mjs b/scripts/build-website.mjs index 5d80368ed..86571bd42 100644 --- a/scripts/build-website.mjs +++ b/scripts/build-website.mjs @@ -55,13 +55,14 @@ async function buildPlaygroundFiles() { const parsers = {}; for (const fileName of files) { const file = path.join(PRETTIER_DIR, fileName); - await copyFile(file, path.join(PLAYGROUND_PRETTIER_DIR, fileName)); + const dist = path.join(PLAYGROUND_PRETTIER_DIR, fileName); + await copyFile(file, dist); if (fileName === "standalone.js") { continue; } - const plugin = require(file); + const plugin = require(dist); // We add plugins to the global `prettierPlugins` object // the name after `parser-` is used as property // For example to get parsers in `parser-babel.js` via `prettierPlugins.babel` diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index 710fb1a55..9f3442c08 100644 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -164,6 +164,7 @@ async function preparePackage() { delete packageJson.dependencies; delete packageJson.devDependencies; delete packageJson.browserslist; + delete packageJson.type; packageJson.scripts = { prepublishOnly: "node -e \"assert.equal(require('.').version, require('..').version)\"", diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index d793de0c6..f0433b8ca 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -49,10 +49,15 @@ function* getEsbuildOptions(bundle, buildOptions) { const replaceModule = {}; // Replace other bundled files if (bundle.target === "node") { - // Replace package.json with dynamic `require("./package.json")` + // TODO[@fisker]: Fix this later, currently esbuild resolve it as ESM + // // Replace package.json with dynamic `require("./package.json")` + // replaceModule[path.join(PROJECT_ROOT, "package.json")] = { + // path: "./package.json", + // external: true, + // }; replaceModule[path.join(PROJECT_ROOT, "package.json")] = { - path: "./package.json", - external: true, + contents: JSON.stringify({ version: packageJson.version }), + loader: "json", }; // Dynamic require bundled files diff --git a/scripts/generate-schema.mjs b/scripts/generate-schema.mjs index 71a61e85c..4b8340fea 100644 --- a/scripts/generate-schema.mjs +++ b/scripts/generate-schema.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import prettier from "../index.js"; -import generateSchema from "./utils/generate-schema.js"; +import generateSchema from "./utils/generate-schema.mjs"; console.log( prettier.format( diff --git a/scripts/sync-flow-tests.js b/scripts/sync-flow-tests.cjs similarity index 97% rename from scripts/sync-flow-tests.js rename to scripts/sync-flow-tests.cjs index 57fc2b9f0..5c32995dd 100644 --- a/scripts/sync-flow-tests.js +++ b/scripts/sync-flow-tests.cjs @@ -79,7 +79,7 @@ function run(argv) { console.error( [ "You must provide the path to a flow tests directory to sync from!", - "Example: node scripts/sync-flow-tests.js ../flow/tests/", + "Example: node scripts/sync-flow-tests.cjs ../flow/tests/", ].join("\n") ); return 1; diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/package.json b/scripts/tools/eslint-plugin-prettier-internal-rules/package.json index aaee2c7dc..bd7d562e9 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/package.json +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/package.json @@ -3,6 +3,7 @@ "version": "1.0.3", "description": "Prettier internal eslint rules", "private": true, + "type": "commonjs", "author": "fisker", "main": "./index.js", "license": "MIT", diff --git a/scripts/utils/generate-schema.js b/scripts/utils/generate-schema.mjs similarity index 98% rename from scripts/utils/generate-schema.js rename to scripts/utils/generate-schema.mjs index 642c69ee4..9859825df 100644 --- a/scripts/utils/generate-schema.js +++ b/scripts/utils/generate-schema.mjs @@ -1,7 +1,3 @@ -"use strict"; - -module.exports = generateSchema; - function generateSchema(options) { return { $schema: "http://json-schema.org/draft-04/schema#", @@ -116,3 +112,5 @@ function optionTypeToSchemaType(optionType) { function choiceToSchema(choice) { return { enum: [choice.value], description: choice.description }; } + +export default generateSchema; diff --git a/src/cli/package.json b/src/cli/package.json index 3dbc1ca59..b4cec2846 100644 --- a/src/cli/package.json +++ b/src/cli/package.json @@ -1,3 +1,5 @@ { - "type": "module" + "//TODO": "Remove this file when we close #10157", + "type": "module", + "description": "A package.json file to use `.js` files in this directory as ES module." } diff --git a/src/package.json b/src/package.json new file mode 100644 index 000000000..54d221724 --- /dev/null +++ b/src/package.json @@ -0,0 +1,5 @@ +{ + "//TODO": "Remove this file when we close #10157", + "type": "commonjs", + "description": "A package.json file to use `.js` files in this directory as CommonJS module." +} diff --git a/standalone.js b/standalone.js index c202611da..bb535cd57 100644 --- a/standalone.js +++ b/standalone.js @@ -1,3 +1 @@ -"use strict"; - -module.exports = require("./src/standalone.js"); +export { default } from "./src/standalone.js"; diff --git a/tests/config/format-test-setup.js b/tests/config/format-test-setup.js new file mode 100644 index 000000000..c185de5bc --- /dev/null +++ b/tests/config/format-test-setup.js @@ -0,0 +1,19 @@ +import createEsmUtils from "esm-utils"; +import getPrettier from "./get-prettier.js"; +import runSpec from "./format-test.js"; + +async function setup() { + const { TEST_STANDALONE } = process.env; + + const prettier = TEST_STANDALONE + ? createEsmUtils(import.meta).require("./require-standalone.cjs") + : await getPrettier(); + + runSpec.prettier = prettier; + + globalThis.run_spec = runSpec; +} + +// `await`ed in `./jest-light-runner/worker-runner.js` +// Use top-level await in `./prettier-entry.js` when we drop support for Node.js 12 +export { setup }; diff --git a/tests/config/format-test.js b/tests/config/format-test.js index e8c17fe2d..8e776fc9a 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -8,12 +8,9 @@ import visualizeEndOfLine from "./utils/visualize-end-of-line.js"; import consistentEndOfLine from "./utils/consistent-end-of-line.js"; import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js"; -const { require, __dirname } = createEsmUtils(import.meta); +const { __dirname } = createEsmUtils(import.meta); -const { TEST_STANDALONE } = process.env; -const prettier = !TEST_STANDALONE - ? require("./require-prettier.cjs") - : require("./require-standalone.cjs"); +let prettier; const { FULL_TEST } = process.env; const BOM = "\uFEFF"; @@ -454,4 +451,13 @@ function format(originalText, originalOptions) { }; } +Object.defineProperty(runSpec, "prettier", { + get() { + return prettier; + }, + set(prettierModule) { + prettier = prettierModule; + }, + configurable: true, +}); export default runSpec; diff --git a/tests/config/get-prettier.js b/tests/config/get-prettier.js new file mode 100644 index 000000000..5776db7d3 --- /dev/null +++ b/tests/config/get-prettier.js @@ -0,0 +1,12 @@ +import { pathToFileURL } from "node:url"; +import path from "node:path"; + +const entry = pathToFileURL(path.join(process.env.PRETTIER_DIR, "index.js")); + +const getPrettier = async () => { + const { default: prettier } = await import(entry); + + return prettier; +}; + +export default getPrettier; diff --git a/tests/config/jest-light-runner/src/worker-runner.js b/tests/config/jest-light-runner/src/worker-runner.js index 4d403aff0..cdaa05e23 100644 --- a/tests/config/jest-light-runner/src/worker-runner.js +++ b/tests/config/jest-light-runner/src/worker-runner.js @@ -28,7 +28,11 @@ export default async function ({ test, updateSnapshot, port }) { const {setupFiles, snapshotSerializers} = test.context.config; for (const path of setupFiles) { - await import(pathToFileURL(path)); + const { setup } = await import(pathToFileURL(path)); + + if (typeof setup === "function") { + await setup(); + } } for (const path of snapshotSerializers) { const {default: serializer} = await import(pathToFileURL(path)); diff --git a/tests/config/prettier-entry.common.cjs b/tests/config/prettier-entry.common.cjs new file mode 100644 index 000000000..a5e403cf1 --- /dev/null +++ b/tests/config/prettier-entry.common.cjs @@ -0,0 +1,7 @@ +"use strict"; + +// This file exists because plugins for test use `require('prettier-local')` + +const path = require("path"); + +module.exports = require(path.join(process.env.PRETTIER_DIR, "index.js")); diff --git a/tests/config/prettier-entry.js b/tests/config/prettier-entry.js new file mode 100644 index 000000000..7525780ff --- /dev/null +++ b/tests/config/prettier-entry.js @@ -0,0 +1,3 @@ +import getPrettier from "./get-prettier.js"; + +export default await getPrettier(); diff --git a/tests/config/require-prettier.cjs b/tests/config/require-prettier.cjs deleted file mode 100644 index e18cf8c6e..000000000 --- a/tests/config/require-prettier.cjs +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -const prettier = require(process.env.PRETTIER_DIR); - -module.exports = prettier; diff --git a/tests/config/setup.js b/tests/config/setup.js deleted file mode 100644 index dee90c002..000000000 --- a/tests/config/setup.js +++ /dev/null @@ -1,3 +0,0 @@ -import run_spec from "./format-test.js"; - -globalThis.run_spec = run_spec; diff --git a/tests/format/js/cursor/jsfmt.spec.js b/tests/format/js/cursor/jsfmt.spec.js index cad58c82c..825c72611 100644 --- a/tests/format/js/cursor/jsfmt.spec.js +++ b/tests/format/js/cursor/jsfmt.spec.js @@ -1,4 +1,6 @@ -import prettier from "../../../config/require-prettier.cjs"; +// TODO: Use this when we drop support for Node.js 12 +// import prettier from "../../../config/prettier-entry.js"; +const { prettier } = run_spec; run_spec(import.meta, ["babel", "typescript", "flow"]); diff --git a/tests/format/misc/empty/jsfmt.spec.js b/tests/format/misc/empty/jsfmt.spec.js index 9d357f07f..9baa077f9 100644 --- a/tests/format/misc/empty/jsfmt.spec.js +++ b/tests/format/misc/empty/jsfmt.spec.js @@ -1,4 +1,4 @@ -import prettier from "../../../config/require-prettier.cjs"; +import prettier from "../../../../index.js"; const parsers = prettier .getSupportInfo() diff --git a/tests/integration/__tests__/schema.js b/tests/integration/__tests__/schema.js index d5572cf5b..ab6336f17 100644 --- a/tests/integration/__tests__/schema.js +++ b/tests/integration/__tests__/schema.js @@ -1,5 +1,5 @@ import prettier from "prettier-local"; -import generateSchema from "../../../scripts/utils/generate-schema.js"; +import generateSchema from "../../../scripts/utils/generate-schema.mjs"; test("schema", () => { expect(generateSchema(prettier.getSupportInfo().options)).toMatchSnapshot(); diff --git a/tests/package.json b/tests/package.json deleted file mode 100644 index 3dbc1ca59..000000000 --- a/tests/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/vendors/package.json b/vendors/package.json new file mode 100644 index 000000000..54d221724 --- /dev/null +++ b/vendors/package.json @@ -0,0 +1,5 @@ +{ + "//TODO": "Remove this file when we close #10157", + "type": "commonjs", + "description": "A package.json file to use `.js` files in this directory as CommonJS module." +} diff --git a/website/package.json b/website/package.json index 9d362592f..8627674e1 100644 --- a/website/package.json +++ b/website/package.json @@ -1,4 +1,5 @@ { + "private": true, "license": "MIT", "scripts": { "build": "webpack --mode=production && docusaurus-build",