Use named export instead of default export (#12740)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-05-05 15:03:08 +08:00
1 parent ba3e736700
commit c1f6d021e5
22 files changed
+136 -135

No files matched your search

+6
View File
@@ -146,6 +146,12 @@ module.exports = {
], ],
"unicorn/prefer-array-flat-map": "error", "unicorn/prefer-array-flat-map": "error",
"unicorn/prefer-array-some": "error", "unicorn/prefer-array-some": "error",
"unicorn/prefer-export-from": [
"error",
{
ignoreUsedVariables: true,
},
],
"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-module": "error",
+13
View File
@@ -0,0 +1,13 @@
#### [Breaking] standalone version export style changed (#12740 by @fisker)
The `esm/standalone.mjs` entry has changed export style from default export to named export.
```diff
- import prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs";
+ import * as prettier from "https://unpkg.com/prettier@3.0.0/esm/standalone.mjs";
```
```diff
- import prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs";
+ import { format } from "https://unpkg.com/prettier@3.0.0/esm/standalone.mjs";
```
+3 -3
View File
@@ -48,7 +48,7 @@ Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `pack
```html ```html
<script type="module"> <script type="module">
import prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs"; import * as prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs";
import parserGraphql from "https://unpkg.com/prettier@2.6.2/esm/parser-graphql.mjs"; import parserGraphql from "https://unpkg.com/prettier@2.6.2/esm/parser-graphql.mjs";
prettier.format("type Query { hello: String }", { prettier.format("type Query { hello: String }", {
@@ -102,7 +102,7 @@ If you want to format [embedded code](options.md#embedded-language-formatting),
```html ```html
<script type="module"> <script type="module">
import prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs"; import * as prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs";
import parserBabel from "https://unpkg.com/prettier@2.6.2/esm/parser-babel.mjs"; import parserBabel from "https://unpkg.com/prettier@2.6.2/esm/parser-babel.mjs";
console.log( console.log(
@@ -119,7 +119,7 @@ The HTML code embedded in JavaScript stays unformatted because the `html` parser
```html ```html
<script type="module"> <script type="module">
import prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs"; import * as prettier from "https://unpkg.com/prettier@2.6.2/esm/standalone.mjs";
import parserBabel from "https://unpkg.com/prettier@2.6.2/esm/parser-babel.mjs"; import parserBabel from "https://unpkg.com/prettier@2.6.2/esm/parser-babel.mjs";
import parserHtml from "https://unpkg.com/prettier@2.6.2/esm/parser-html.mjs"; import parserHtml from "https://unpkg.com/prettier@2.6.2/esm/parser-html.mjs";
+1 -1
View File
@@ -1 +1 @@
export { default } from "./src/index.js"; export * from "./src/index.js";
+2 -2
View File
@@ -3,7 +3,7 @@
import path from "node:path"; import path from "node:path";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import fastGlob from "fast-glob"; import fastGlob from "fast-glob";
import prettier from "prettier"; import { format } from "prettier";
import createEsmUtils from "esm-utils"; import createEsmUtils from "esm-utils";
import { execa } from "execa"; import { execa } from "execa";
import { import {
@@ -76,7 +76,7 @@ async function buildPlaygroundFiles() {
await writeFile( await writeFile(
path.join(PLAYGROUND_PRETTIER_DIR, "parsers-location.js"), path.join(PLAYGROUND_PRETTIER_DIR, "parsers-location.js"),
prettier.format( format(
` `
"use strict"; "use strict";
+2
View File
@@ -261,6 +261,7 @@ const parsers = [
const coreBundles = [ const coreBundles = [
{ {
input: "src/index.js", input: "src/index.js",
interopDefault: false,
replaceModule: [ replaceModule: [
{ {
module: require.resolve("@iarna/toml/lib/toml-parser.js"), module: require.resolve("@iarna/toml/lib/toml-parser.js"),
@@ -292,6 +293,7 @@ const coreBundles = [
}, },
{ {
input: "src/standalone.js", input: "src/standalone.js",
interopDefault: false,
name: "prettier", name: "prettier",
target: "universal", target: "universal",
replaceModule: [ replaceModule: [
+4 -5
View File
@@ -1,11 +1,10 @@
#!/usr/bin/env node #!/usr/bin/env node
import prettier from "../index.js"; import { format, getSupportInfo } from "../index.js";
import generateSchema from "./utils/generate-schema.mjs"; import generateSchema from "./utils/generate-schema.mjs";
console.log( console.log(
prettier.format( format(JSON.stringify(generateSchema(getSupportInfo().options)), {
JSON.stringify(generateSchema(prettier.getSupportInfo().options)), parser: "json",
{ parser: "json" } })
)
); );
+3 -3
View File
@@ -1,5 +1,5 @@
import stringify from "fast-json-stable-stringify"; import stringify from "fast-json-stable-stringify";
import prettier from "../index.js"; import { format, getFileInfo } from "../index.js";
import { printToScreen } from "./utils.js"; import { printToScreen } from "./utils.js";
async function logFileInfoOrDie(context) { async function logFileInfoOrDie(context) {
@@ -12,7 +12,7 @@ async function logFileInfoOrDie(context) {
config, config,
} = context.argv; } = context.argv;
const fileInfo = await prettier.getFileInfo(file, { const fileInfo = await getFileInfo(file, {
ignorePath, ignorePath,
withNodeModules, withNodeModules,
plugins, plugins,
@@ -20,7 +20,7 @@ async function logFileInfoOrDie(context) {
resolveConfig: config !== false, resolveConfig: config !== false,
}); });
printToScreen(await prettier.format(stringify(fileInfo), { parser: "json" })); printToScreen(await format(stringify(fileInfo), { parser: "json" }));
} }
export default logFileInfoOrDie; export default logFileInfoOrDie;
+2 -2
View File
@@ -1,10 +1,10 @@
import path from "node:path"; import path from "node:path";
import prettier from "../index.js"; import { resolveConfigFile } from "../index.js";
import { printToScreen } from "./utils.js"; import { printToScreen } from "./utils.js";
async function logResolvedConfigPathOrDie(context) { async function logResolvedConfigPathOrDie(context) {
const file = context.argv.findConfigPath; const file = context.argv.findConfigPath;
const configFile = await prettier.resolveConfigFile(file); const configFile = await resolveConfigFile(file);
if (configFile) { if (configFile) {
printToScreen(path.relative(process.cwd(), configFile)); printToScreen(path.relative(process.cwd(), configFile));
} else { } else {
+1 -1
View File
@@ -1,7 +1,7 @@
import { promises as fs } from "node:fs"; import { promises as fs } from "node:fs";
import path from "node:path"; import path from "node:path";
import chalk from "chalk"; import chalk from "chalk";
import prettier from "../index.js"; import * as prettier from "../index.js";
import thirdParty from "../common/third-party.cjs"; import thirdParty from "../common/third-party.cjs";
import { createIgnorer, errors } from "./prettier-internal.js"; import { createIgnorer, errors } from "./prettier-internal.js";
import { expandPatterns, fixWindowsSlashes } from "./expand-patterns.js"; import { expandPatterns, fixWindowsSlashes } from "./expand-patterns.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import stringify from "fast-json-stable-stringify"; import stringify from "fast-json-stable-stringify";
import prettier from "../index.js"; import * as prettier from "../index.js";
import createLogger from "./logger.js"; import createLogger from "./logger.js";
import Context from "./context.js"; import Context from "./context.js";
import { parseArgvWithoutPlugins } from "./options/parse-cli-arguments.js"; import { parseArgvWithoutPlugins } from "./options/parse-cli-arguments.js";
+2 -2
View File
@@ -1,4 +1,4 @@
import prettier from "../../index.js"; import { getSupportInfo } from "../../index.js";
import { optionsHiddenDefaults } from "../prettier-internal.js"; import { optionsHiddenDefaults } from "../prettier-internal.js";
import * as constant from "../constant.js"; import * as constant from "../constant.js";
import { arrayify } from "../utils.js"; import { arrayify } from "../utils.js";
@@ -8,7 +8,7 @@ import {
} from "./option-map.js"; } from "./option-map.js";
function getContextOptions(plugins, pluginSearchDirs) { function getContextOptions(plugins, pluginSearchDirs) {
const { options: supportOptions, languages } = prettier.getSupportInfo({ const { options: supportOptions, languages } = getSupportInfo({
showDeprecated: true, showDeprecated: true,
showUnreleased: true, showUnreleased: true,
showInternal: true, showInternal: true,
+2 -2
View File
@@ -1,5 +1,5 @@
import dashify from "dashify"; import dashify from "dashify";
import prettier from "../../index.js"; import { resolveConfig } from "../../index.js";
import { normalizeApiOptions } from "../prettier-internal.js"; import { normalizeApiOptions } from "../prettier-internal.js";
import minimist from "./minimist.js"; import minimist from "./minimist.js";
import createMinimistOptions from "./create-minimist-options.js"; import createMinimistOptions from "./create-minimist-options.js";
@@ -68,7 +68,7 @@ async function getOptionsOrDie(context, filePath) {
: `resolve config from '${filePath}'` : `resolve config from '${filePath}'`
); );
const options = await prettier.resolveConfig(filePath, { const options = await resolveConfig(filePath, {
editorconfig: context.argv.editorconfig, editorconfig: context.argv.editorconfig,
config: context.argv.config, config: context.argv.config,
}); });
+3 -12
View File
@@ -1,19 +1,10 @@
import prettier from "../index.js"; import { __internal as sharedWithCli } from "../index.js";
const { export const {
errors, errors,
coreOptions, coreOptions,
createIgnorer, createIgnorer,
optionsHiddenDefaults, optionsHiddenDefaults,
normalizeApiOptions, normalizeApiOptions,
normalizeCliOptions, normalizeCliOptions,
} = prettier.__internal; } = sharedWithCli;
export {
errors,
coreOptions,
createIgnorer,
optionsHiddenDefaults,
normalizeApiOptions,
normalizeCliOptions,
};
+2 -3
View File
@@ -1,7 +1,6 @@
import prettier from "../index.js"; import { __internal as sharedWithCli } from "../index.js";
const { arrayify, getLast, isNonEmptyArray, partition } = const { arrayify, getLast, isNonEmptyArray, partition } = sharedWithCli.utils;
prettier.__internal.utils;
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
const printToScreen = console.log.bind(console); const printToScreen = console.log.bind(console);
+3 -6
View File
@@ -1,8 +1,5 @@
import escapeStringRegexp from "escape-string-regexp"; import escapeStringRegexp from "escape-string-regexp";
import getLast from "../utils/get-last.js";
import { getSupportInfo } from "../main/support.js"; import { getSupportInfo } from "../main/support.js";
import isNonEmptyArray from "../utils/is-non-empty-array.js";
import getStringWidth from "../utils/get-string-width.js";
import { import {
skipWhitespace, skipWhitespace,
skipSpaces, skipSpaces,
@@ -479,11 +476,9 @@ function describeNodeForDebugging(node) {
export { export {
inferParserByLanguage, inferParserByLanguage,
getStringWidth,
getMaxContinuousCount, getMaxContinuousCount,
getMinNotPresentContinuousCount, getMinNotPresentContinuousCount,
getPenultimate, getPenultimate,
getLast,
getNextNonSpaceNonCommentCharacterIndexWithStartIndex, getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
getNextNonSpaceNonCommentCharacterIndex, getNextNonSpaceNonCommentCharacterIndex,
getNextNonSpaceNonCommentCharacter, getNextNonSpaceNonCommentCharacter,
@@ -511,6 +506,8 @@ export {
addDanglingComment, addDanglingComment,
addTrailingComment, addTrailingComment,
isFrontMatterNode, isFrontMatterNode,
isNonEmptyArray,
createGroupIdMapper, createGroupIdMapper,
}; };
export { default as getLast } from "../utils/get-last.js";
export { default as isNonEmptyArray } from "../utils/is-non-empty-array.js";
export { default as getStringWidth } from "../utils/get-string-width.js";
+55 -54
View File
@@ -1,8 +1,7 @@
import { createRequire } from "node:module"; import { createRequire } from "node:module";
import core from "./main/core.js"; import core from "./main/core.js";
import { getSupportInfo } from "./main/support.js"; import { getSupportInfo as getSupportInfoWithoutPlugins } from "./main/support.js";
import getFileInfo from "./common/get-file-info.js"; import getFileInfoWithoutPlugins from "./common/get-file-info.js";
import * as sharedUtil from "./common/util-shared.js";
import { import {
loadPlugins, loadPlugins,
clearCache as clearPluginCache, clearCache as clearPluginCache,
@@ -12,7 +11,6 @@ import {
resolveConfigFile, resolveConfigFile,
clearCache as clearConfigCache, clearCache as clearConfigCache,
} from "./config/resolve-config.js"; } from "./config/resolve-config.js";
import doc from "./document/index.js";
import languages from "./languages.js"; import languages from "./languages.js";
import * as errors from "./common/errors.js"; import * as errors from "./common/errors.js";
@@ -65,61 +63,64 @@ function withPlugins(fn, optsArgIdx) {
const formatWithCursor = withPlugins(core.formatWithCursor); const formatWithCursor = withPlugins(core.formatWithCursor);
const prettier = { async function format(text, opts) {
formatWithCursor, const { formatted } = await formatWithCursor(text, opts);
return formatted;
}
async format(text, opts) { async function check(text, opts) {
const { formatted } = await formatWithCursor(text, opts); const { formatted } = await formatWithCursor(text, opts);
return formatted; return formatted === text;
}, }
async check(text, opts) { function clearCache() {
const { formatted } = await formatWithCursor(text, opts); clearConfigCache();
return formatted === text; clearPluginCache();
}, }
doc, /** @type {typeof getFileInfoWithoutPlugins} */
const getFileInfo = withPlugins(getFileInfoWithoutPlugins);
resolveConfig, /** @type {typeof getSupportInfoWithoutPlugins} */
resolveConfigFile, const getSupportInfo = withPlugins(getSupportInfoWithoutPlugins, 0);
clearConfigCache() {
clearConfigCache();
clearPluginCache();
},
/** @type {typeof getFileInfo} */ // Internal shared with cli
getFileInfo: withPlugins(getFileInfo), const sharedWithCli = {
/** @type {typeof getSupportInfo} */ errors,
getSupportInfo: withPlugins(getSupportInfo, 0), coreOptions,
createIgnorer,
version, optionsHiddenDefaults,
normalizeApiOptions,
util: sharedUtil, normalizeCliOptions,
utils: {
// Internal shared arrayify,
__internal: { getLast,
errors, isNonEmptyArray,
coreOptions, partition,
createIgnorer,
optionsHiddenDefaults,
normalizeApiOptions,
normalizeCliOptions,
utils: {
arrayify,
getLast,
isNonEmptyArray,
partition,
},
},
/* istanbul ignore next */
__debug: {
parse: withPlugins(core.parse),
formatAST: withPlugins(core.formatAST),
formatDoc: withPlugins(core.formatDoc),
printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString),
}, },
}; };
export default prettier; const debugApis = {
parse: withPlugins(core.parse),
formatAST: withPlugins(core.formatAST),
formatDoc: withPlugins(core.formatDoc),
printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString),
};
export {
version,
formatWithCursor,
format,
check,
resolveConfig,
resolveConfigFile,
// TODO: Expose this as `clearCache` in v3
clearCache as clearConfigCache,
getFileInfo,
getSupportInfo,
sharedWithCli as __internal,
debugApis as __debug,
};
export * as util from "./common/util-shared.js";
export { default as doc } from "./document/index.js";
+26 -29
View File
@@ -1,9 +1,7 @@
import { createRequire } from "node:module"; import { createRequire } from "node:module";
import core from "./main/core.js"; import core from "./main/core.js";
import { getSupportInfo } from "./main/support.js"; import { getSupportInfo as getSupportInfoWithoutPlugins } from "./main/support.js";
import * as sharedUtil from "./common/util-shared.js";
import languages from "./languages.js"; import languages from "./languages.js";
import doc from "./document/index.js";
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
const { version } = require("../package.json"); const { version } = require("../package.json");
@@ -30,34 +28,33 @@ function withPlugins(
const formatWithCursor = withPlugins(core.formatWithCursor); const formatWithCursor = withPlugins(core.formatWithCursor);
const prettierStandalone = { async function format(text, opts) {
formatWithCursor, const { formatted } = await formatWithCursor(text, opts);
return formatted;
}
async format(text, opts) { async function check(text, opts) {
const { formatted } = await formatWithCursor(text, opts); const { formatted } = await formatWithCursor(text, opts);
return formatted; return formatted === text;
}, }
async check(text, opts) { const getSupportInfo = withPlugins(getSupportInfoWithoutPlugins, 0);
const { formatted } = await formatWithCursor(text, opts);
return formatted === text;
},
doc, const debugApis = {
parse: withPlugins(core.parse),
getSupportInfo: withPlugins(getSupportInfo, 0), formatAST: withPlugins(core.formatAST),
formatDoc: withPlugins(core.formatDoc),
version, printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString),
util: sharedUtil,
__debug: {
parse: withPlugins(core.parse),
formatAST: withPlugins(core.formatAST),
formatDoc: withPlugins(core.formatDoc),
printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString),
},
}; };
export default prettierStandalone; export {
version,
formatWithCursor,
format,
check,
getSupportInfo,
debugApis as __debug,
};
export * as util from "./common/util-shared.js";
export { default as doc } from "./document/index.js";
+1 -1
View File
@@ -1 +1 @@
export { default } from "./src/standalone.js"; export * from "./src/standalone.js";
+1 -5
View File
@@ -3,10 +3,6 @@ import path from "node:path";
const entry = pathToFileURL(path.join(process.env.PRETTIER_DIR, "index.js")); const entry = pathToFileURL(path.join(process.env.PRETTIER_DIR, "index.js"));
const getPrettier = async () => { const getPrettier = () => import(entry);
const { default: prettier } = await import(entry);
return prettier;
};
export default getPrettier; export default getPrettier;
+1 -1
View File
@@ -1,4 +1,4 @@
import prettier from "../../../../index.js"; import * as prettier from "../../../../index.js";
const parsers = prettier const parsers = prettier
.getSupportInfo() .getSupportInfo()
+2 -2
View File
@@ -41,9 +41,9 @@ describe("standalone", () => {
let esmStandalone; let esmStandalone;
let esmPlugins; let esmPlugins;
beforeAll(async () => { beforeAll(async () => {
({ default: esmStandalone } = await import( esmStandalone = await import(
path.join(distDirectory, "esm/standalone.mjs") path.join(distDirectory, "esm/standalone.mjs")
)); );
esmPlugins = await Promise.all( esmPlugins = await Promise.all(
fastGlob fastGlob
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true }) .sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })