Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin compatibility when loaded with require #159

Merged
merged 4 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}