Restructure package files, add exports (#13530)
Co-authored-by: Sosuke Suzuki <aosukeke@gmail.com>
This commit is contained in:
1 parent
e132a9ba43
commit
31a796d32c
17 files changed
+232
-111
No files matched your search
@@ -133,4 +133,4 @@ jobs:
|
||||
run: node -v | grep "v0.10.48" || exit 1
|
||||
|
||||
- name: Run CLI on Node.js v0.10.48
|
||||
run: node dist/bin-prettier.cjs --version 2>&1 >/dev/null | grep "prettier requires at least version 14 of Node, please upgrade" || exit 1
|
||||
run: node dist/bin/prettier.cjs --version 2>&1 >/dev/null | grep "prettier requires at least version 14 of Node, please upgrade" || exit 1
|
||||
@@ -1,13 +1,57 @@
|
||||
#### [BREAKING] standalone version export style changed (#12740 by @fisker)
|
||||
#### [BREAKING] npm package file structures changed (#12740 by @fisker, #13530 by @fisker)
|
||||
|
||||
The `esm/standalone.mjs` entry has changed export style from default export to named export.
|
||||
Export style changes:
|
||||
|
||||
The ESM version standalone 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";
|
||||
- import prettier from "https://unpkg.com/prettier@2.7.1/esm/standalone.mjs";
|
||||
+ import * as prettier from "https://unpkg.com/prettier@3.0.0/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";
|
||||
- import prettier from "https://unpkg.com/prettier@2.7.1/esm/standalone.mjs";
|
||||
+ import { format } from "https://unpkg.com/prettier@3.0.0/standalone.mjs";
|
||||
```
|
||||
|
||||
File structures changes:
|
||||
|
||||
- `bin-prettier.js` -> `bin/prettier.cjs`
|
||||
- `esm/standalone.mjs` -> `standalone.mjs`
|
||||
- `esm/parser-angular.mjs` -> `plugins/angular.mjs`
|
||||
- `parser-angular.js` -> `plugins/angular.js`
|
||||
- `esm/parser-babel.mjs` -> `plugins/babel.mjs`
|
||||
- `parser-babel.js` -> `plugins/babel.js`
|
||||
- `esm/parser-espree.mjs` -> `plugins/acorn-and-espree.mjs`
|
||||
- `parser-espree.js` -> `plugins/acorn-and-espree.js`\
|
||||
global object renamed `prettierPlugins.espree` -> `prettierPlugins.acornAndEspree`
|
||||
- `esm/parser-flow.mjs` -> `plugins/flow.mjs`
|
||||
- `parser-flow.js` -> `plugins/flow.js`
|
||||
- `esm/parser-glimmer.mjs` -> `plugins/glimmer.mjs`
|
||||
- `parser-glimmer.js` -> `plugins/glimmer.js`
|
||||
- `esm/parser-graphql.mjs` -> `plugins/graphql.mjs`
|
||||
- `parser-graphql.js` -> `plugins/graphql.js`
|
||||
- `esm/parser-html.mjs` -> `plugins/html.mjs`
|
||||
- `parser-html.js` -> `plugins/html.js`
|
||||
- `esm/parser-markdown.mjs` -> `plugins/markdown.mjs`
|
||||
- `parser-markdown.js` -> `plugins/markdown.js`
|
||||
- `esm/parser-meriyah.mjs` -> `plugins/meriyah.mjs`
|
||||
- `parser-meriyah.js` -> `plugins/meriyah.js`
|
||||
- `esm/parser-postcss.mjs` -> `plugins/postcss.mjs`
|
||||
- `parser-postcss.js` -> `plugins/postcss.js`
|
||||
- `esm/parser-typescript.mjs` -> `plugins/typescript.mjs`
|
||||
- `parser-typescript.js` -> `plugins/typescript.js`
|
||||
- `esm/parser-yaml.mjs` -> `plugins/yaml.mjs`
|
||||
- `parser-yaml.js` -> `plugins/yaml.js`
|
||||
|
||||
Check full list on <https://unpkg.com/browse/prettier@3.0.0/>.
|
||||
|
||||
```diff
|
||||
- import pluginBabel from "prettier/parser-babel";
|
||||
+ import pluginBabel from "prettier/plugins/babel";
|
||||
```
|
||||
|
||||
```diff
|
||||
- node ./node_modules/prettier/bin-prettier.js . --write
|
||||
+ node ./node_modules/prettier/bin/prettier.cjs . --write
|
||||
```
|
||||
@@ -14,16 +14,16 @@ function parse(text: string, options: object): Promise<AST> | AST;
|
||||
|
||||
The second argument `parsers` has been removed, if you still need other parser during parse process, you can:
|
||||
|
||||
1. Import the parser yourself
|
||||
1. Import the plugin yourself
|
||||
|
||||
```js
|
||||
import parserBabel from "prettier/parser-babel.js";
|
||||
import pluginBabel from "prettier/plugins/babel";
|
||||
|
||||
const myCustomPlugin = {
|
||||
parsers: {
|
||||
"my-custom-parser": {
|
||||
async parse(text) {
|
||||
const ast = await parserBabel.parsers.babel.parse(text);
|
||||
const ast = await pluginBabel.parsers.babel.parse(text);
|
||||
ast.program.body[0].expression.callee.name = "_";
|
||||
return ast;
|
||||
},
|
||||
@@ -37,8 +37,8 @@ The second argument `parsers` has been removed, if you still need other parser d
|
||||
|
||||
```js
|
||||
function getParserFromOptions(options, parserName) {
|
||||
const parserOrParserInitFunction = options.plugins.find(({ parsers }) =>
|
||||
Object.prototype.hasOwnProperty.call(parsers, parserName)
|
||||
const parserOrParserInitFunction = options.plugins.find((plugin) =>
|
||||
Object.hasOwn(plugin.parsers, parserName)
|
||||
)?.parsers[parserName];
|
||||
return typeof parserOrParserInitFunction === "function"
|
||||
? parserOrParserInitFunction()
|
||||
|
||||
+2
-2
@@ -151,13 +151,13 @@ format("lodash ( )", {
|
||||
|
||||
```js
|
||||
import { format } from "prettier";
|
||||
import parserBabel from "prettier/parser-babel.js";
|
||||
import pluginBabel from "prettier/plugins/babel";
|
||||
|
||||
const myCustomPlugin = {
|
||||
parsers: {
|
||||
"my-custom-parser": {
|
||||
async parse(text) {
|
||||
const ast = await parserBabel.parsers.babel.parse(text);
|
||||
const ast = await pluginBabel.parsers.babel.parse(text);
|
||||
ast.program.body[0].expression.callee.name = "_";
|
||||
return ast;
|
||||
},
|
||||
|
||||
+16
-19
@@ -7,7 +7,7 @@ Run Prettier in the browser using its **standalone** version. This version doesn
|
||||
|
||||
The standalone version comes as:
|
||||
|
||||
- ES modules: `esm/standalone.mjs`, starting in version 2.2
|
||||
- ES modules: `standalone.mjs`, starting in version 3.0 (In version 2, `esm/standalone.mjs`.)
|
||||
- UMD: `standalone.js`, starting in version 1.13
|
||||
|
||||
The [`browser` field](https://github.com/defunctzombie/package-browser-field-spec) in Prettier’s `package.json` points to `standalone.js`. That’s why you can just `import` or `require` the `prettier` module to access Prettier’s API, and your code can stay compatible with both Node and the browser as long as webpack or another bundler that supports the `browser` field is used. This is especially convenient for [plugins](plugins.md).
|
||||
@@ -18,10 +18,7 @@ Required options:
|
||||
|
||||
- **[`parser`](options.md#parser) (or [`filepath`](options.md#file-path))**: One of these options has to be specified for Prettier to know which parser to use.
|
||||
|
||||
- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource--options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files named
|
||||
|
||||
- `parser-*.js` in <https://unpkg.com/browse/prettier@2.7.1/> and
|
||||
- `parser-*.mjs` in <https://unpkg.com/browse/prettier@2.7.1/esm/>
|
||||
- **`plugins`**: Unlike the `format` function from the [Node.js-based API](api.md#prettierformatsource--options), this function doesn’t load plugins automatically. The `plugins` option is required because all the parsers included in the Prettier package come as plugins (for reasons of file size). These plugins are files in <https://unpkg.com/browse/prettier@2.7.1/plugins/>
|
||||
|
||||
You need to load the ones that you’re going to use and pass them to `prettier.format` using the `plugins` option.
|
||||
|
||||
@@ -33,7 +30,7 @@ See below for examples.
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/prettier@2.7.1/standalone.js"></script>
|
||||
<script src="https://unpkg.com/prettier@2.7.1/parser-graphql.js"></script>
|
||||
<script src="https://unpkg.com/prettier@2.7.1/plugins/graphql.js"></script>
|
||||
<script>
|
||||
(async () => {
|
||||
const formatted = await prettier.format("type Query { hello: String }", {
|
||||
@@ -50,12 +47,12 @@ Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `pack
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/esm/standalone.mjs";
|
||||
import parserGraphql from "https://unpkg.com/prettier@2.7.1/esm/parser-graphql.mjs";
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/standalone.mjs";
|
||||
import pluginGraphql from "https://unpkg.com/prettier@2.7.1/plugins/graphql.mjs";
|
||||
|
||||
const formatted = await prettier.format("type Query { hello: String }", {
|
||||
parser: "graphql",
|
||||
plugins: [parserGraphql],
|
||||
plugins: [pluginGraphql],
|
||||
});
|
||||
</script>
|
||||
```
|
||||
@@ -65,7 +62,7 @@ Note that the [`unpkg` field](https://unpkg.com/#examples) in Prettier’s `pack
|
||||
```js
|
||||
define([
|
||||
"https://unpkg.com/prettier@2.7.1/standalone.js",
|
||||
"https://unpkg.com/prettier@2.7.1/parser-graphql.js",
|
||||
"https://unpkg.com/prettier@2.7.1/plugins/graphql.js",
|
||||
], async (prettier, ...plugins) => {
|
||||
const formatted = await prettier.format("type Query { hello: String }", {
|
||||
parser: "graphql",
|
||||
@@ -78,7 +75,7 @@ define([
|
||||
|
||||
```js
|
||||
const prettier = require("prettier/standalone");
|
||||
const plugins = [require("prettier/parser-graphql")];
|
||||
const plugins = [require("prettier/plugins/graphql")];
|
||||
(async () => {
|
||||
const formatted = await prettier.format("type Query { hello: String }", {
|
||||
parser: "graphql",
|
||||
@@ -93,7 +90,7 @@ This syntax doesn’t necessarily work in the browser, but it can be used when b
|
||||
|
||||
```js
|
||||
importScripts("https://unpkg.com/prettier@2.7.1/standalone.js");
|
||||
importScripts("https://unpkg.com/prettier@2.7.1/parser-graphql.js");
|
||||
importScripts("https://unpkg.com/prettier@2.7.1/plugins/graphql.js");
|
||||
(async () => {
|
||||
const formatted = await prettier.format("type Query { hello: String }", {
|
||||
parser: "graphql",
|
||||
@@ -108,13 +105,13 @@ If you want to format [embedded code](options.md#embedded-language-formatting),
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/esm/standalone.mjs";
|
||||
import parserBabel from "https://unpkg.com/prettier@2.7.1/esm/parser-babel.mjs";
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/standalone.mjs";
|
||||
import pluginBabel from "https://unpkg.com/prettier@2.7.1/plugins/babel.mjs";
|
||||
|
||||
console.log(
|
||||
await prettier.format("const html=/* HTML */ `<DIV> </DIV>`", {
|
||||
parser: "babel",
|
||||
plugins: [parserBabel],
|
||||
plugins: [pluginBabel],
|
||||
})
|
||||
);
|
||||
// Output: const html = /* HTML */ `<DIV> </DIV>`;
|
||||
@@ -125,14 +122,14 @@ The HTML code embedded in JavaScript stays unformatted because the `html` parser
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/esm/standalone.mjs";
|
||||
import parserBabel from "https://unpkg.com/prettier@2.7.1/esm/parser-babel.mjs";
|
||||
import parserHtml from "https://unpkg.com/prettier@2.7.1/esm/parser-html.mjs";
|
||||
import * as prettier from "https://unpkg.com/prettier@2.7.1/standalone.mjs";
|
||||
import pluginBabel from "https://unpkg.com/prettier@2.7.1/plugins/babel.mjs";
|
||||
import pluginHtml from "https://unpkg.com/prettier@2.7.1/plugins/html.mjs";
|
||||
|
||||
console.log(
|
||||
await prettier.format("const html=/* HTML */ `<DIV> </DIV>`", {
|
||||
parser: "babel",
|
||||
plugins: [parserBabel, parserHtml],
|
||||
plugins: [pluginBabel, pluginHtml],
|
||||
})
|
||||
);
|
||||
// Output: const html = /* HTML */ `<div></div>`;
|
||||
|
||||
+8
-1
@@ -9,9 +9,16 @@
|
||||
"author": "James Long",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"main": "./index.js",
|
||||
"main": "./index.cjs",
|
||||
"browser": "./standalone.js",
|
||||
"unpkg": "./standalone.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./src/index.cjs",
|
||||
"default": "./src/index.js"
|
||||
},
|
||||
"./*": "./"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
|
||||
+17
-16
@@ -2,10 +2,11 @@
|
||||
|
||||
import path from "node:path";
|
||||
import fs from "node:fs/promises";
|
||||
import url from "node:url";
|
||||
import fastGlob from "fast-glob";
|
||||
import { format } from "prettier";
|
||||
import createEsmUtils from "esm-utils";
|
||||
import { execa } from "execa";
|
||||
import { format } from "../src/index.js";
|
||||
import {
|
||||
PROJECT_ROOT,
|
||||
DIST_DIR,
|
||||
@@ -25,7 +26,7 @@ const runYarn = (command, args, options) =>
|
||||
const IS_PULL_REQUEST = process.env.PULL_REQUEST === "true";
|
||||
const PRETTIER_DIR = IS_PULL_REQUEST
|
||||
? DIST_DIR
|
||||
: path.dirname(require.resolve("prettier"));
|
||||
: url.fileURLToPath(new URL("../node_modules/prettier", import.meta.url));
|
||||
const PLAYGROUND_PRETTIER_DIR = path.join(WEBSITE_DIR, "static/lib");
|
||||
|
||||
async function buildPrettier() {
|
||||
@@ -49,10 +50,16 @@ async function buildPrettier() {
|
||||
}
|
||||
|
||||
async function buildPlaygroundFiles() {
|
||||
const files = await fastGlob(["standalone.js", "parser-*.js"], {
|
||||
const patterns = IS_PULL_REQUEST
|
||||
? ["standalone.js", "plugins/*.js"]
|
||||
: // TODO: Remove this after we release v3
|
||||
["standalone.js", "parser-*.js"];
|
||||
|
||||
const files = await fastGlob(patterns, {
|
||||
cwd: PRETTIER_DIR,
|
||||
});
|
||||
const parsers = {};
|
||||
|
||||
const parsersLocation = {};
|
||||
for (const fileName of files) {
|
||||
const file = path.join(PRETTIER_DIR, fileName);
|
||||
const dist = path.join(PLAYGROUND_PRETTIER_DIR, fileName);
|
||||
@@ -63,26 +70,20 @@ async function buildPlaygroundFiles() {
|
||||
}
|
||||
|
||||
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`
|
||||
// See `scripts/build/config.mjs`
|
||||
const property = fileName.replace(/\.js$/, "").split("-")[1];
|
||||
parsers[fileName] = {
|
||||
property,
|
||||
parsers: Object.keys(plugin.parsers),
|
||||
};
|
||||
for (const parser of Object.keys(plugin.parsers)) {
|
||||
parsersLocation[parser] = fileName;
|
||||
}
|
||||
}
|
||||
|
||||
await writeFile(
|
||||
path.join(PLAYGROUND_PRETTIER_DIR, "parsers-location.js"),
|
||||
format(
|
||||
await format(
|
||||
`
|
||||
"use strict";
|
||||
|
||||
const parsersLocation = ${JSON.stringify(parsers)};
|
||||
const parsersLocation = ${JSON.stringify(parsersLocation)};
|
||||
`,
|
||||
{ parser: "babel" }
|
||||
{ parser: "meriyah" }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,14 @@ const { dirname, readJsonSync, require } = createEsmUtils(import.meta);
|
||||
const packageJson = readJsonSync("../../package.json");
|
||||
|
||||
const universalTarget = browserslistToEsbuild(packageJson.browserslist);
|
||||
const getRelativePath = (from, to) => {
|
||||
const relativePath = path.posix.relative(path.dirname(`/${from}`), `/${to}`);
|
||||
if (!relativePath.startsWith(".")) {
|
||||
return `./${relativePath}`;
|
||||
}
|
||||
|
||||
return relativePath;
|
||||
};
|
||||
|
||||
function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
// Save dependencies to file
|
||||
@@ -127,7 +135,7 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
|
||||
return {
|
||||
module: path.join(PROJECT_ROOT, bundle.input),
|
||||
external: `./${output}`,
|
||||
external: getRelativePath(file.output.file, output),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
@@ -17,8 +17,48 @@ async function buildPackageJson({ files }) {
|
||||
|
||||
packageJson.bin = `./${bin}`;
|
||||
packageJson.main = "./index.cjs";
|
||||
// TODO: Add `exports`
|
||||
|
||||
packageJson.exports = {
|
||||
".": {
|
||||
require: "./index.cjs",
|
||||
default: "./index.mjs",
|
||||
},
|
||||
"./*": "./",
|
||||
...Object.fromEntries(
|
||||
files
|
||||
.filter((file) => file.output.format === "umd")
|
||||
.map((file) => {
|
||||
const basename = path.basename(file.output.file, ".js");
|
||||
return [
|
||||
file.isPlugin ? `./plugins/${basename}` : `./${basename}`,
|
||||
{
|
||||
require: `./${file.output.file}`,
|
||||
default: `./${file.output.file.replace(/\.js$/, ".mjs")}`,
|
||||
},
|
||||
];
|
||||
})
|
||||
),
|
||||
// Legacy entries
|
||||
// TODO: Remove bellow in v4
|
||||
"./esm/standalone.mjs": "./standalone.mjs",
|
||||
...Object.fromEntries(
|
||||
files
|
||||
.filter((file) => file.isPlugin && file.output.format === "umd")
|
||||
.flatMap((file) => {
|
||||
let basename = path.basename(file.output.file, ".js");
|
||||
if (basename === "acorn-and-espree") {
|
||||
basename = "espree";
|
||||
}
|
||||
return [
|
||||
[`./parser-${basename}`, `./${file.output.file}`],
|
||||
[`./parser-${basename}.js`, `./${file.output.file}`],
|
||||
[
|
||||
`./esm/parser-${basename}.mjs`,
|
||||
`./${file.output.file.replace(/\.js$/, ".mjs")}`,
|
||||
],
|
||||
];
|
||||
})
|
||||
),
|
||||
};
|
||||
// https://github.com/prettier/prettier/pull/13118#discussion_r922708068
|
||||
packageJson.engines.node = ">=14";
|
||||
delete packageJson.dependencies;
|
||||
|
||||
@@ -57,7 +57,8 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
|
||||
if (
|
||||
(cliOptions.files && !cliOptions.files.has(file.output.file)) ||
|
||||
(cliOptions.playground && file.output.format !== "umd")
|
||||
(cliOptions.playground &&
|
||||
(file.output.format !== "umd" || file.output.file === "doc.js"))
|
||||
) {
|
||||
console.log(status.SKIPPED);
|
||||
return;
|
||||
@@ -65,20 +66,22 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = (await file.build({
|
||||
result = await file.build({
|
||||
file,
|
||||
files,
|
||||
shouldCollectLicenses,
|
||||
cliOptions,
|
||||
})) ?? {
|
||||
file: cliOptions.saveAs ?? file.output.file,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(status.FAIL + "\n");
|
||||
console.error(error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
result ??= {
|
||||
file: cliOptions.saveAs ?? file.output.file,
|
||||
};
|
||||
|
||||
if (result.skipped) {
|
||||
console.log(status.SKIPPED);
|
||||
return;
|
||||
|
||||
+10
-12
@@ -114,9 +114,7 @@ const pluginFiles = [
|
||||
},
|
||||
{
|
||||
input: "src/language-js/parse/acorn-and-espree.js",
|
||||
// TODO: Rename this file to `parser-acorn-and-espree.js` or find a better way
|
||||
outputBaseName: "espree",
|
||||
umdPropertyName: "espree",
|
||||
umdPropertyName: "acornAndEspree",
|
||||
replaceModule: [
|
||||
{
|
||||
module: require.resolve("espree"),
|
||||
@@ -198,13 +196,14 @@ const pluginFiles = [
|
||||
outputBaseName ??= input.match(
|
||||
/(?:parser-|parse\/)(?<outputBaseName>.*?)\.js$/
|
||||
).groups.outputBaseName;
|
||||
|
||||
const umdVariableName = `prettierPlugins.${
|
||||
umdPropertyName ?? outputBaseName
|
||||
}`;
|
||||
|
||||
return {
|
||||
input,
|
||||
outputBaseName,
|
||||
outputBaseName: `plugins/${outputBaseName}`,
|
||||
umdVariableName,
|
||||
buildOptions,
|
||||
isPlugin: true,
|
||||
@@ -257,14 +256,11 @@ const universalFiles = [...nonPluginUniversalFiles, ...pluginFiles].flatMap(
|
||||
file;
|
||||
|
||||
outputBaseName ??= path.basename(input);
|
||||
if (file.isPlugin) {
|
||||
outputBaseName = `parser-${outputBaseName}`;
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
format: "esm",
|
||||
file: `esm/${outputBaseName}${extensions.esm}`,
|
||||
file: `${outputBaseName}${extensions.esm}`,
|
||||
},
|
||||
{
|
||||
format: "umd",
|
||||
@@ -315,23 +311,25 @@ const nodejsFiles = [
|
||||
},
|
||||
{
|
||||
input: "bin/prettier.cjs",
|
||||
outputBaseName: "bin-prettier",
|
||||
outputBaseName: "bin/prettier",
|
||||
target: ["node0.10"],
|
||||
replaceModule: [
|
||||
{
|
||||
module: path.join(PROJECT_ROOT, "bin/prettier.cjs"),
|
||||
process: (text) => text.replace('"../src/cli/index.js"', '"./cli.mjs"'),
|
||||
process: (text) =>
|
||||
text.replace('"../src/cli/index.js"', '"../internal/cli.mjs"'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
input: "src/cli/index.js",
|
||||
outputBaseName: "cli",
|
||||
outputBaseName: "internal/cli",
|
||||
external: ["benchmark"],
|
||||
replaceModule: [replaceDiffPackageEntry("lib/patch/create.js")],
|
||||
},
|
||||
{
|
||||
input: "src/common/third-party.js",
|
||||
outputBaseName: "internal/third-party",
|
||||
replaceModule: [
|
||||
// cosmiconfig@6 -> import-fresh can't find parentModule, since module is bundled
|
||||
{
|
||||
@@ -344,7 +342,7 @@ const nodejsFiles = [
|
||||
let { input, output, outputBaseName, ...buildOptions } = file;
|
||||
|
||||
const format = input.endsWith(".cjs") ? "cjs" : "esm";
|
||||
outputBaseName ??= path.basename(input).split(".").slice(0, -1).join(".");
|
||||
outputBaseName ??= path.basename(input, path.extname(input));
|
||||
|
||||
return {
|
||||
input,
|
||||
|
||||
@@ -27,52 +27,74 @@ module.exports = {
|
||||
{
|
||||
files: [
|
||||
"doc.js",
|
||||
"parser-*.js",
|
||||
"doc.mjs",
|
||||
"standalone.js",
|
||||
"parser-*.mjs",
|
||||
"standalone.mjs",
|
||||
"plugins/*",
|
||||
],
|
||||
env: {
|
||||
browser: true,
|
||||
},
|
||||
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()`.",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["index.js", "bin-prettier.js", "cli.js", "third-party.js"],
|
||||
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=/parser-/]',
|
||||
'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.",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
// {
|
||||
// files: ["bin-prettier.js"],
|
||||
// parserOptions: {
|
||||
// ecmaVersion: 5,
|
||||
// },
|
||||
// rules: {
|
||||
// "compat/compat": "error",
|
||||
// },
|
||||
// },
|
||||
{
|
||||
files: ["doc.js", "parser-*.js", "standalone.js"],
|
||||
files: ["bin/prettier.cjs"],
|
||||
parserOptions: {
|
||||
ecmaVersion: 5,
|
||||
},
|
||||
rules: {
|
||||
"no-restricted-syntax": [
|
||||
"error",
|
||||
// Forbid `require()`
|
||||
{
|
||||
selector: 'CallExpression[callee.name="require"]',
|
||||
message: "UMD bundles should not include any `require()` call.",
|
||||
},
|
||||
],
|
||||
"compat/compat": "error",
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
@@ -4,7 +4,7 @@ const fastGlob = require("fast-glob");
|
||||
const createSandBox = require("./utils/create-sandbox.cjs");
|
||||
|
||||
const sandbox = createSandBox({
|
||||
files: fastGlob.sync(["standalone.js", "parser-*.js"], {
|
||||
files: fastGlob.sync(["standalone.js", "plugins/*.js"], {
|
||||
cwd: process.env.PRETTIER_DIR,
|
||||
absolute: true,
|
||||
}),
|
||||
|
||||
@@ -35,18 +35,18 @@ test("code", async () => {
|
||||
describe("standalone", () => {
|
||||
const standalone = require(path.join(distDirectory, "standalone.js"));
|
||||
const plugins = fastGlob
|
||||
.sync(["parser-*.js"], { cwd: distDirectory, absolute: true })
|
||||
.sync(["plugins/*.js"], { cwd: distDirectory, absolute: true })
|
||||
.map((file) => require(file));
|
||||
|
||||
let esmStandalone;
|
||||
let esmPlugins;
|
||||
beforeAll(async () => {
|
||||
esmStandalone = await importModule(
|
||||
path.join(distDirectory, "esm/standalone.mjs")
|
||||
path.join(distDirectory, "standalone.mjs")
|
||||
);
|
||||
esmPlugins = await Promise.all(
|
||||
fastGlob
|
||||
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })
|
||||
.sync(["plugins/*.mjs"], { cwd: distDirectory, absolute: true })
|
||||
.map(async (file) => (await importModule(file)).default)
|
||||
);
|
||||
});
|
||||
@@ -74,7 +74,7 @@ describe("standalone", () => {
|
||||
});
|
||||
|
||||
test("global objects", async () => {
|
||||
const files = await fastGlob(["standalone.js", "parser-*.js"], {
|
||||
const files = await fastGlob(["standalone.js", "plugins/*.js"], {
|
||||
cwd: distDirectory,
|
||||
absolute: true,
|
||||
});
|
||||
|
||||
@@ -12,7 +12,7 @@ const prettierCli = path.join(
|
||||
);
|
||||
|
||||
const thirdParty = isProduction
|
||||
? path.join(PRETTIER_DIR, "./third-party.mjs")
|
||||
? path.join(PRETTIER_DIR, "./internal/third-party.mjs")
|
||||
: path.join(PRETTIER_DIR, "./src/common/third-party.js");
|
||||
|
||||
const projectRoot = path.join(__dirname, "../..");
|
||||
|
||||
@@ -5,13 +5,15 @@
|
||||
importScripts("lib/parsers-location.js");
|
||||
importScripts("https://unpkg.com/sw-toolbox@3.6.0/sw-toolbox.js");
|
||||
|
||||
const parsers = Object.keys(parsersLocation).map((file) => `lib/${file}`);
|
||||
const plugins = [
|
||||
...new Set(Object.values(parsersLocation).map((file) => `lib/${file}`)),
|
||||
];
|
||||
|
||||
toolbox.precache([
|
||||
// Scripts
|
||||
"lib/standalone.js",
|
||||
"lib/parsers-location.js",
|
||||
...parsers,
|
||||
...plugins,
|
||||
"playground.js",
|
||||
"https://unpkg.com/sw-toolbox@3.6.0/sw-toolbox.js",
|
||||
|
||||
|
||||
+10
-11
@@ -15,17 +15,16 @@ importScripts("lib/standalone.js");
|
||||
|
||||
// this is required to only load parsers when we need them
|
||||
const parsers = Object.create(null);
|
||||
for (const file in parsersLocation) {
|
||||
const { parsers: moduleParsers, property } = parsersLocation[file];
|
||||
const url = `lib/${file}`;
|
||||
for (const parserName of moduleParsers) {
|
||||
Object.defineProperty(parsers, parserName, {
|
||||
get() {
|
||||
importScriptOnce(url);
|
||||
return prettierPlugins[property].parsers[parserName];
|
||||
},
|
||||
});
|
||||
}
|
||||
for (const [parser, file] of Object.entries(parsersLocation)) {
|
||||
Object.defineProperty(parsers, parser, {
|
||||
get() {
|
||||
const url = `lib/${file}`;
|
||||
importScriptOnce(url);
|
||||
return Object.values(prettierPlugins).find((plugin) =>
|
||||
Object.hasOwn(plugin.parsers, parser)
|
||||
).parsers[parser];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const docExplorerPlugin = {
|
||||
|
||||
Reference in new issue
Block a user