Skip to content

Commit eb900b0

Browse files
authored
Migrate to ESM (#2213)
* Migrate to ESM * Fix build script * Remove unicorn * Format * Add `browser` entry * Remove default export * Add `./standalone` and `./package.json` entry * Add `await` to example
1 parent f277152 commit eb900b0

23 files changed

+277
-253
lines changed

.eslintrc.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ extends:
66
- plugin:prettier-doc/recommended
77
parserOptions:
88
ecmaVersion: 2023
9+
sourceType: "module"
910
plugins:
1011
- import
1112
root: true
1213
env:
13-
es6: true
14+
es2024: true
1415
node: true
1516
jest: true
1617
rules:
1718
eqeqeq: error
1819
curly: error
19-
import/no-extraneous-dependencies:
20-
- error
21-
- devDependencies: ["tests*/**", "scripts/**", "build/**"]
2220
no-else-return: error
2321
no-inner-declarations: error
2422
no-useless-return: error
@@ -38,13 +36,17 @@ rules:
3836
- error
3937
- never
4038
- exceptRange: true
39+
40+
import/extensions:
41+
- error
42+
- ignorePackages
43+
import/no-extraneous-dependencies:
44+
- error
45+
- devDependencies: ["tests*/**", "scripts/**", "build/**"]
4146
overrides:
42-
- files: "{tests,tests_config}/**/*.js"
47+
- files: "**/*.cjs"
4348
parserOptions:
44-
sourceType: "module"
45-
rules:
46-
# Turned off temporarily since we add a package.json without dependencies in these dirs
47-
import/no-extraneous-dependencies: off
49+
sourceType: "script"
4850
- files: "tests/**/*.js"
4951
globals:
5052
run_spec: true

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ First, grab both standalone scripts from an npm CDN like [unpkg](https://unpkg.c
139139
Then use Prettier with PHP, just like this:
140140

141141
```js
142-
prettier.format(YOUR_CODE, {
142+
await prettier.format(YOUR_CODE, {
143143
plugins: prettierPlugins,
144144
parser: "php",
145145
});
@@ -153,10 +153,10 @@ Bundlers like webpack, Rollup or browserify automatically recognize how to handl
153153

154154
```js
155155
import prettier from "prettier/standalone";
156-
import phpPlugin from "@prettier/plugin-php/standalone";
156+
import * as prettierPluginPhp from "@prettier/plugin-php/standalone";
157157

158-
prettier.format(YOUR_CODE, {
159-
plugins: [phpPlugin],
158+
await prettier.format(YOUR_CODE, {
159+
plugins: [prettierPluginPhp],
160160
parser: "php",
161161
});
162162
```

build/rollup.config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { resolve } from "path";
1+
import { resolve, dirname } from "path";
2+
import url from "url";
23

3-
import nodeResolve from "rollup-plugin-node-resolve";
4-
import commonjs from "rollup-plugin-commonjs";
4+
import nodeResolve from "@rollup/plugin-node-resolve";
5+
import commonjs from "@rollup/plugin-commonjs";
56
import alias from "rollup-plugin-alias";
67
import inject from "rollup-plugin-inject";
78
import replace from "rollup-plugin-replace";
89
import babel from "rollup-plugin-babel";
910
import json from "rollup-plugin-json";
1011
import { terser } from "rollup-plugin-terser";
1112

13+
const __dirname = dirname(url.fileURLToPath(import.meta.url));
1214
const SRC_DIR = resolve(__dirname, "..", "src");
1315
const BUILD_DIR = resolve(__dirname, "..", "build");
1416

@@ -50,7 +52,7 @@ export default {
5052
compact: false,
5153
presets: [
5254
[
53-
require.resolve("@babel/preset-env"),
55+
"@babel/preset-env",
5456
{
5557
targets: { browsers: [">0.25%", "not ie 11", "not op_mini all"] },
5658
modules: false,

build/shims/assert.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
"use strict";
2-
function assert() {}
3-
assert.strictEqual = function () {};
4-
module.exports = assert;
1+
const assert = new Proxy(() => {}, { get: () => assert });
2+
3+
export default assert;

build/shims/buffer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
"use strict";
2-
const Buffer = {};
3-
Buffer.isBuffer = function () {
4-
return false;
1+
const Buffer = {
2+
isBuffer: () => false,
53
};
6-
module.exports = Buffer;
4+
5+
export default Buffer;

jest.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
"use strict";
2-
31
const ENABLE_COVERAGE = !!process.env.CI;
42
const RUN_STANDALONE_TESTS = Boolean(process.env.RUN_STANDALONE_TESTS);
53

6-
module.exports = {
4+
export default {
75
collectCoverage: ENABLE_COVERAGE,
86
collectCoverageFrom: [
97
"<rootDir>/src/**/*.js",

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
"repository": "prettier/prettier-php",
66
"author": "Lucas Azzola <@azz>",
77
"license": "MIT",
8-
"main": "src",
8+
"type": "module",
9+
"unpkg": "./standalone.js",
10+
"browser": "./standalone.js",
11+
"exports": {
12+
".": {
13+
"browser": "./standalone.js",
14+
"default": "./src/index.js"
15+
},
16+
"./standalone": "./standalone.js",
17+
"./package.json": "./package.json"
18+
},
919
"files": [
1020
"src",
1121
"standalone.js"
@@ -17,6 +27,8 @@
1727
},
1828
"devDependencies": {
1929
"@babel/preset-env": "^7.22.10",
30+
"@rollup/plugin-commonjs": "^25.0.4",
31+
"@rollup/plugin-node-resolve": "^15.1.0",
2032
"codecov": "3.8.3",
2133
"cross-env": "^7.0.2",
2234
"eslint": "8.47.0",
@@ -34,10 +46,8 @@
3446
"rollup": "^2.75.7",
3547
"rollup-plugin-alias": "^2.0.0",
3648
"rollup-plugin-babel": "^4.3.2",
37-
"rollup-plugin-commonjs": "^10.0.2",
3849
"rollup-plugin-inject": "^3.0.1",
3950
"rollup-plugin-json": "^4.0.0",
40-
"rollup-plugin-node-resolve": "^5.2.0",
4151
"rollup-plugin-replace": "^2.1.0",
4252
"rollup-plugin-terser": "^7.0.2",
4353
"strip-ansi": "^6.0.0"

src/clean.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
const util = require("./util");
1+
import { printNumber, normalizeMagicMethodName } from "./util.js";
42

53
const ignoredProperties = new Set([
64
"loc",
@@ -28,7 +26,7 @@ const ignoredProperties = new Set([
2826
function clean(node, newObj) {
2927
if (node.kind === "string") {
3028
// TODO if options are available in this method, replace with
31-
// newObj.isDoubleQuote = !util.useSingleQuote(node, options);
29+
// newObj.isDoubleQuote = !useSingleQuote(node, options);
3230
delete newObj.isDoubleQuote;
3331
}
3432

@@ -66,7 +64,7 @@ function clean(node, newObj) {
6664

6765
// Normalize numbers
6866
if (node.kind === "number") {
69-
newObj.value = util.printNumber(node.value);
67+
newObj.value = printNumber(node.value);
7068
}
7169

7270
const statements = ["foreach", "for", "if", "while", "do"];
@@ -100,7 +98,7 @@ function clean(node, newObj) {
10098
}
10199

102100
if (node.kind === "method" && node.name.kind === "identifier") {
103-
newObj.name.name = util.normalizeMagicMethodName(newObj.name.name);
101+
newObj.name.name = normalizeMagicMethodName(newObj.name.name);
104102
}
105103

106104
if (node.kind === "noop") {
@@ -110,4 +108,4 @@ function clean(node, newObj) {
110108

111109
clean.ignoredProperties = ignoredProperties;
112110

113-
module.exports = clean;
111+
export default clean;

src/comments.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
"use strict";
1+
import { util as prettierUtil, doc } from "prettier";
2+
import {
3+
getNextNonSpaceNonCommentCharacterIndex,
4+
isNextLineEmpty,
5+
isPreviousLineEmpty,
6+
isLookupNode,
7+
} from "./util.js";
28

39
const {
410
addLeadingComment,
@@ -7,15 +13,9 @@ const {
713
skipNewline,
814
hasNewline,
915
hasNewlineInRange,
10-
} = require("prettier").util;
16+
} = prettierUtil;
1117
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
12-
require("prettier").doc.builders;
13-
const {
14-
getNextNonSpaceNonCommentCharacterIndex,
15-
isNextLineEmpty,
16-
isPreviousLineEmpty,
17-
isLookupNode,
18-
} = require("./util");
18+
doc.builders;
1919

2020
/*
2121
Comment functions are meant to inspect various edge cases using given comment nodes,
@@ -1029,7 +1029,7 @@ function printAllComments(path, print, options, needsSemi) {
10291029
);
10301030
}
10311031

1032-
module.exports = {
1032+
export {
10331033
handleOwnLineComment,
10341034
handleEndOfLineComment,
10351035
handleRemainingComment,

src/index.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
"use strict";
2-
3-
const parse = require("./parser");
4-
const print = require("./printer");
5-
const clean = require("./clean");
6-
const options = require("./options");
7-
const comments = require("./comments");
8-
const { join, hardline } = require("prettier").doc.builders;
9-
const { hasPragma, insertPragma } = require("./pragma");
1+
import { doc } from "prettier";
2+
import {
3+
LINGUIST_LANGUAGES_PHP,
4+
LINGUIST_LANGUAGES_HTML_PHP,
5+
} from "./linguist-languages.cjs";
6+
import parse from "./parser.js";
7+
import print from "./printer.js";
8+
import clean from "./clean.js";
9+
import options from "./options.js";
10+
import {
11+
handleOwnLineComment,
12+
handleEndOfLineComment,
13+
handleRemainingComment,
14+
getCommentChildNodes,
15+
canAttachComment,
16+
isBlockComment,
17+
} from "./comments.js";
18+
import { hasPragma, insertPragma } from "./pragma.js";
19+
20+
const { join, hardline } = doc.builders;
1021

1122
function createLanguage(linguistData, { extend, override }) {
1223
const language = {};
@@ -30,13 +41,13 @@ function createLanguage(linguistData, { extend, override }) {
3041
}
3142

3243
const languages = [
33-
createLanguage(require("linguist-languages/data/PHP"), {
44+
createLanguage(LINGUIST_LANGUAGES_PHP, {
3445
override: {
3546
parsers: ["php"],
3647
vscodeLanguageIds: ["php"],
3748
},
3849
}),
39-
createLanguage(require("linguist-languages/data/HTML+PHP"), {
50+
createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
4051
override: {
4152
parsers: ["php"],
4253
vscodeLanguageIds: ["php"],
@@ -81,13 +92,13 @@ const printers = {
8192
getVisitorKeys,
8293
insertPragma,
8394
massageAstNode: clean,
84-
getCommentChildNodes: comments.getCommentChildNodes,
85-
canAttachComment: comments.canAttachComment,
86-
isBlockComment: comments.isBlockComment,
95+
getCommentChildNodes,
96+
canAttachComment,
97+
isBlockComment,
8798
handleComments: {
88-
ownLine: comments.handleOwnLineComment,
89-
endOfLine: comments.handleEndOfLineComment,
90-
remaining: comments.handleRemainingComment,
99+
ownLine: handleOwnLineComment,
100+
endOfLine: handleEndOfLineComment,
101+
remaining: handleRemainingComment,
91102
},
92103
willPrintOwnComments(path) {
93104
const node = path.getValue();
@@ -161,12 +172,8 @@ const printers = {
161172
},
162173
};
163174

164-
module.exports = {
165-
languages,
166-
printers,
167-
parsers,
168-
options,
169-
defaultOptions: {
170-
tabWidth: 4,
171-
},
175+
const defaultOptions = {
176+
tabWidth: 4,
172177
};
178+
179+
export { languages, printers, parsers, options, defaultOptions };

0 commit comments

Comments
 (0)