Skip to content

Commit d3f787d

Browse files
Fix plugin compatibility when loaded with require (#159)
* Refactor * Handle when compatible plugins are loaded by `require()` * Add types * Update changelog
1 parent 6ba6faa commit d3f787d

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Fixed
1616

1717
- Speed up formatting ([#153](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/153))
18+
- Fix plugin compatibility when loaded with require ([#159](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/159))
1819

1920
## [0.2.8] - 2023-04-28
2021

src/compat.js

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ export function getCompatibleParser(base, parserFormat, options) {
4646
return parser
4747
}
4848

49+
/**
50+
*
51+
* @param {*} base
52+
* @param {string} parserFormat
53+
* @param {import('prettier').Options} options
54+
* @returns {import('prettier').Parser<any>}
55+
*/
4956
function getFreshCompatibleParser(base, parserFormat, options) {
5057
if (!options.plugins) {
5158
return base.parsers[parserFormat]
@@ -57,31 +64,19 @@ function getFreshCompatibleParser(base, parserFormat, options) {
5764

5865
// Now load parsers from plugins
5966
for (const name of compatiblePlugins) {
60-
let path = null
67+
let plugin = findEnabledPlugin(options, name)
6168

62-
try {
63-
path = require.resolve(name)
64-
} catch (err) {
65-
continue
69+
if (plugin) {
70+
Object.assign(parser, plugin.parsers[parserFormat])
6671
}
67-
68-
let plugin = options.plugins.find(
69-
(plugin) => plugin.name === name || plugin.name === path,
70-
)
71-
72-
// The plugin is not loaded
73-
if (!plugin) {
74-
continue
75-
}
76-
77-
Object.assign(parser, plugin.parsers[parserFormat])
7872
}
7973

8074
return parser
8175
}
8276

83-
// We need to load this plugin dynamically because it's not available by default
84-
// And we are not bundling it with the main Prettier plugin
77+
/**
78+
* @returns {Record<string, import('prettier').Parser<any>>}
79+
*/
8580
export function getAdditionalParsers() {
8681
let parsers = {}
8782

@@ -92,6 +87,9 @@ export function getAdditionalParsers() {
9287
return parsers
9388
}
9489

90+
/**
91+
* @returns {Record<string, import('prettier').Printer<any>>}
92+
*/
9593
export function getAdditionalPrinters() {
9694
let printers = {}
9795

@@ -106,3 +104,36 @@ export function getAdditionalPrinters() {
106104

107105
return printers
108106
}
107+
108+
/**
109+
*
110+
* @param {import('prettier').Options} options
111+
* @param {string} name
112+
* @returns {import('prettier').Plugin<any> | null}
113+
*/
114+
function findEnabledPlugin(options, name) {
115+
let path = null
116+
117+
try {
118+
path = require.resolve(name)
119+
} catch (err) {
120+
return null
121+
}
122+
123+
let plugin = options.plugins.find(
124+
(plugin) => plugin.name === name || plugin.name === path,
125+
)
126+
127+
// The plugin was found by name or path
128+
if (plugin) {
129+
return plugin
130+
}
131+
132+
// The plugin was loaded with require so we use object equality to find it
133+
let mod = loadIfExists(path)
134+
if (mod && mod.parsers && options.plugins.includes(mod)) {
135+
return mod
136+
}
137+
138+
return null
139+
}

0 commit comments

Comments
 (0)