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