Skip to content

Commit

Permalink
Fix plugin compatibility when loaded with require (#159)
Browse files Browse the repository at this point in the history
* Refactor

* Handle when compatible plugins are loaded by `require()`

* Add types

* Update changelog
  • Loading branch information
thecrypticace committed May 12, 2023
1 parent 6ba6faa commit d3f787d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

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

## [0.2.8] - 2023-04-28

Expand Down
67 changes: 49 additions & 18 deletions src/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export function getCompatibleParser(base, parserFormat, options) {
return parser
}

/**
*
* @param {*} base
* @param {string} parserFormat
* @param {import('prettier').Options} options
* @returns {import('prettier').Parser<any>}
*/
function getFreshCompatibleParser(base, parserFormat, options) {
if (!options.plugins) {
return base.parsers[parserFormat]
Expand All @@ -57,31 +64,19 @@ function getFreshCompatibleParser(base, parserFormat, options) {

// Now load parsers from plugins
for (const name of compatiblePlugins) {
let path = null
let plugin = findEnabledPlugin(options, name)

try {
path = require.resolve(name)
} catch (err) {
continue
if (plugin) {
Object.assign(parser, plugin.parsers[parserFormat])
}

let plugin = options.plugins.find(
(plugin) => plugin.name === name || plugin.name === path,
)

// The plugin is not loaded
if (!plugin) {
continue
}

Object.assign(parser, plugin.parsers[parserFormat])
}

return parser
}

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

Expand All @@ -92,6 +87,9 @@ export function getAdditionalParsers() {
return parsers
}

/**
* @returns {Record<string, import('prettier').Printer<any>>}
*/
export function getAdditionalPrinters() {
let printers = {}

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

return printers
}

/**
*
* @param {import('prettier').Options} options
* @param {string} name
* @returns {import('prettier').Plugin<any> | null}
*/
function findEnabledPlugin(options, name) {
let path = null

try {
path = require.resolve(name)
} catch (err) {
return null
}

let plugin = options.plugins.find(
(plugin) => plugin.name === name || plugin.name === path,
)

// The plugin was found by name or path
if (plugin) {
return plugin
}

// The plugin was loaded with require so we use object equality to find it
let mod = loadIfExists(path)
if (mod && mod.parsers && options.plugins.includes(mod)) {
return mod
}

return null
}

0 comments on commit d3f787d

Please sign in to comment.