Add type:"module" to package.json (#12272)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-02-12 15:39:16 +08:00
1 parent dc80e12fd8
commit be0739ec22
32 files changed
+143 -64

No files matched your search

+40 -20
View File
@@ -7,6 +7,10 @@ module.exports = {
es2021: true, es2021: true,
node: true, node: true,
}, },
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
reportUnusedDisableDirectives: true, reportUnusedDisableDirectives: true,
extends: ["eslint:recommended", "prettier"], extends: ["eslint:recommended", "prettier"],
plugins: [ plugins: [
@@ -87,7 +91,6 @@ module.exports = {
}, },
], ],
"require-await": "error", "require-await": "error",
strict: "error",
"symbol-description": "error", "symbol-description": "error",
yoda: [ yoda: [
"error", "error",
@@ -98,12 +101,13 @@ module.exports = {
], ],
"import/extensions": ["error", "ignorePackages"], "import/extensions": ["error", "ignorePackages"],
"import/no-extraneous-dependencies": [ // TODO[@fisker]: Fix this
"error", // "import/no-extraneous-dependencies": [
{ // "error",
devDependencies: ["jest.config.mjs", "tests/**", "scripts/**"], // {
}, // devDependencies: ["jest.config.mjs", "tests/**", "scripts/**"],
], // },
// ],
"import/order": "error", "import/order": "error",
"import/no-anonymous-default-export": "error", "import/no-anonymous-default-export": "error",
@@ -142,6 +146,8 @@ module.exports = {
"unicorn/prefer-array-some": "error", "unicorn/prefer-array-some": "error",
"unicorn/prefer-includes": "error", "unicorn/prefer-includes": "error",
"unicorn/prefer-json-parse-buffer": "error", "unicorn/prefer-json-parse-buffer": "error",
"unicorn/prefer-module": "error",
"unicorn/prefer-node-protocol": "error",
"unicorn/prefer-number-properties": "error", "unicorn/prefer-number-properties": "error",
"unicorn/prefer-optional-catch-binding": "error", "unicorn/prefer-optional-catch-binding": "error",
"unicorn/prefer-regexp-test": "error", "unicorn/prefer-regexp-test": "error",
@@ -155,6 +161,7 @@ module.exports = {
{ {
files: [ files: [
"scripts/**/*.js", "scripts/**/*.js",
"scripts/**/*.cjs",
"scripts/**/*.mjs", "scripts/**/*.mjs",
"tests/config/install-prettier.js", "tests/config/install-prettier.js",
], ],
@@ -162,25 +169,33 @@ module.exports = {
"no-console": "off", "no-console": "off",
}, },
}, },
// CommonJS modules
{ {
files: [ files: [
"**/*.mjs", "**/*.cjs",
"scripts/release/**/*.js", "src/*.js",
"tests/**/*.js", "src/common/**/*.js",
"bin/**/*.js", "src/config/**/*.js",
"src/cli/**/*.js", "src/document/**/*.js",
], "src/language-css/**/*.js",
excludedFiles: [ "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", "tests/integration/plugins/automatic/prettier-plugin-bar.js",
], ],
parserOptions: { parserOptions: {
sourceType: "module", sourceType: "script",
}, },
rules: { rules: {
"unicorn/prefer-module": "error", strict: "error",
"unicorn/prefer-node-protocol": "error", "unicorn/prefer-module": "off",
// TODO[@fisker]: Fix this
"import/no-extraneous-dependencies": "off",
}, },
}, },
// `esbuild` don't support // `esbuild` don't support
@@ -226,7 +241,6 @@ module.exports = {
{ {
files: ["tests/**/*.js"], files: ["tests/**/*.js"],
rules: { rules: {
strict: "off",
"import/no-extraneous-dependencies": "off", "import/no-extraneous-dependencies": "off",
"unicorn/prefer-array-flat": "off", "unicorn/prefer-array-flat": "off",
"unicorn/prefer-array-flat-map": "off", "unicorn/prefer-array-flat-map": "off",
@@ -329,5 +343,11 @@ module.exports = {
sourceType: "module", sourceType: "module",
}, },
}, },
{
files: ["scripts/tools/eslint-plugin-prettier-internal-rules/**/*.js"],
rules: {
"import/no-extraneous-dependencies": "error",
},
},
], ],
}; };
+1 -1
View File
@@ -53,7 +53,7 @@ Parameters:
The implementation of `run_spec` can be found in [`tests/config/format-test.js`](tests/config/format-test.js). 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 ## Debugging
-3
View File
@@ -1,3 +0,0 @@
{
"type": "module"
}
+1 -3
View File
@@ -1,3 +1 @@
"use strict"; export { default } from "./src/index.js";
module.exports = require("./src/index.js");
+1 -1
View File
@@ -4,7 +4,7 @@ const config = {
...baseConfig, ...baseConfig,
runner: "./tests/config/jest-light-runner", runner: "./tests/config/jest-light-runner",
testRegex: "tests/format/.*/jsfmt\\.spec\\.js$", testRegex: "tests/format/.*/jsfmt\\.spec\\.js$",
setupFiles: ["<rootDir>/tests/config/setup.js"], setupFiles: ["<rootDir>/tests/config/format-test-setup.js"],
projects: [], projects: [],
}; };
+4
View File
@@ -4,6 +4,10 @@ const config = {
...baseConfig, ...baseConfig,
testRegex: "tests/integration/__tests__/.*\\.js$", testRegex: "tests/integration/__tests__/.*\\.js$",
projects: [], projects: [],
moduleNameMapper: {
...baseConfig.moduleNameMapper,
"prettier-local": "<rootDir>/tests/config/prettier-entry.common.cjs",
},
}; };
export default config; export default config;
+1 -1
View File
@@ -46,7 +46,7 @@ const config = {
], ],
coverageReporters: ["text", "lcov"], coverageReporters: ["text", "lcov"],
moduleNameMapper: { moduleNameMapper: {
"prettier-local": "<rootDir>/tests/config/require-prettier.cjs", "prettier-local": "<rootDir>/tests/config/prettier-entry.js",
"prettier-standalone": "<rootDir>/tests/config/require-standalone.cjs", "prettier-standalone": "<rootDir>/tests/config/require-standalone.cjs",
}, },
modulePathIgnorePatterns: [ modulePathIgnorePatterns: [
+1
View File
@@ -7,6 +7,7 @@
"funding": "https://github.com/prettier/prettier?sponsor=1", "funding": "https://github.com/prettier/prettier?sponsor=1",
"homepage": "https://prettier.io", "homepage": "https://prettier.io",
"author": "James Long", "author": "James Long",
"type": "module",
"license": "MIT", "license": "MIT",
"main": "./index.js", "main": "./index.js",
"browser": "./standalone.js", "browser": "./standalone.js",
+3 -2
View File
@@ -55,13 +55,14 @@ async function buildPlaygroundFiles() {
const parsers = {}; const parsers = {};
for (const fileName of files) { for (const fileName of files) {
const file = path.join(PRETTIER_DIR, fileName); 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") { if (fileName === "standalone.js") {
continue; continue;
} }
const plugin = require(file); const plugin = require(dist);
// We add plugins to the global `prettierPlugins` object // We add plugins to the global `prettierPlugins` object
// the name after `parser-` is used as property // the name after `parser-` is used as property
// For example to get parsers in `parser-babel.js` via `prettierPlugins.babel` // For example to get parsers in `parser-babel.js` via `prettierPlugins.babel`
+1
View File
@@ -164,6 +164,7 @@ async function preparePackage() {
delete packageJson.dependencies; delete packageJson.dependencies;
delete packageJson.devDependencies; delete packageJson.devDependencies;
delete packageJson.browserslist; delete packageJson.browserslist;
delete packageJson.type;
packageJson.scripts = { packageJson.scripts = {
prepublishOnly: prepublishOnly:
"node -e \"assert.equal(require('.').version, require('..').version)\"", "node -e \"assert.equal(require('.').version, require('..').version)\"",
+8 -3
View File
@@ -49,10 +49,15 @@ function* getEsbuildOptions(bundle, buildOptions) {
const replaceModule = {}; const replaceModule = {};
// Replace other bundled files // Replace other bundled files
if (bundle.target === "node") { 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")] = { replaceModule[path.join(PROJECT_ROOT, "package.json")] = {
path: "./package.json", contents: JSON.stringify({ version: packageJson.version }),
external: true, loader: "json",
}; };
// Dynamic require bundled files // Dynamic require bundled files
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
import prettier from "../index.js"; import prettier from "../index.js";
import generateSchema from "./utils/generate-schema.js"; import generateSchema from "./utils/generate-schema.mjs";
console.log( console.log(
prettier.format( prettier.format(
@@ -79,7 +79,7 @@ function run(argv) {
console.error( console.error(
[ [
"You must provide the path to a flow tests directory to sync from!", "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") ].join("\n")
); );
return 1; return 1;
@@ -3,6 +3,7 @@
"version": "1.0.3", "version": "1.0.3",
"description": "Prettier internal eslint rules", "description": "Prettier internal eslint rules",
"private": true, "private": true,
"type": "commonjs",
"author": "fisker", "author": "fisker",
"main": "./index.js", "main": "./index.js",
"license": "MIT", "license": "MIT",
@@ -1,7 +1,3 @@
"use strict";
module.exports = generateSchema;
function generateSchema(options) { function generateSchema(options) {
return { return {
$schema: "http://json-schema.org/draft-04/schema#", $schema: "http://json-schema.org/draft-04/schema#",
@@ -116,3 +112,5 @@ function optionTypeToSchemaType(optionType) {
function choiceToSchema(choice) { function choiceToSchema(choice) {
return { enum: [choice.value], description: choice.description }; return { enum: [choice.value], description: choice.description };
} }
export default generateSchema;
+3 -1
View File
@@ -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."
} }
+5
View File
@@ -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."
}
+1 -3
View File
@@ -1,3 +1 @@
"use strict"; export { default } from "./src/standalone.js";
module.exports = require("./src/standalone.js");
+19
View File
@@ -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 };
+11 -5
View File
@@ -8,12 +8,9 @@ import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js"; import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.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; let prettier;
const prettier = !TEST_STANDALONE
? require("./require-prettier.cjs")
: require("./require-standalone.cjs");
const { FULL_TEST } = process.env; const { FULL_TEST } = process.env;
const BOM = "\uFEFF"; 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; export default runSpec;
+12
View File
@@ -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;
@@ -28,7 +28,11 @@ export default async function ({ test, updateSnapshot, port }) {
const {setupFiles, snapshotSerializers} = test.context.config; const {setupFiles, snapshotSerializers} = test.context.config;
for (const path of setupFiles) { 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) { for (const path of snapshotSerializers) {
const {default: serializer} = await import(pathToFileURL(path)); const {default: serializer} = await import(pathToFileURL(path));
+7
View File
@@ -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"));
+3
View File
@@ -0,0 +1,3 @@
import getPrettier from "./get-prettier.js";
export default await getPrettier();
-5
View File
@@ -1,5 +0,0 @@
"use strict";
const prettier = require(process.env.PRETTIER_DIR);
module.exports = prettier;
-3
View File
@@ -1,3 +0,0 @@
import run_spec from "./format-test.js";
globalThis.run_spec = run_spec;
+3 -1
View File
@@ -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"]); run_spec(import.meta, ["babel", "typescript", "flow"]);
+1 -1
View File
@@ -1,4 +1,4 @@
import prettier from "../../../config/require-prettier.cjs"; import prettier from "../../../../index.js";
const parsers = prettier const parsers = prettier
.getSupportInfo() .getSupportInfo()
+1 -1
View File
@@ -1,5 +1,5 @@
import prettier from "prettier-local"; import prettier from "prettier-local";
import generateSchema from "../../../scripts/utils/generate-schema.js"; import generateSchema from "../../../scripts/utils/generate-schema.mjs";
test("schema", () => { test("schema", () => {
expect(generateSchema(prettier.getSupportInfo().options)).toMatchSnapshot(); expect(generateSchema(prettier.getSupportInfo().options)).toMatchSnapshot();
-3
View File
@@ -1,3 +0,0 @@
{
"type": "module"
}
+5
View File
@@ -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."
}
+1
View File
@@ -1,4 +1,5 @@
{ {
"private": true,
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "webpack --mode=production && docusaurus-build", "build": "webpack --mode=production && docusaurus-build",