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 interop with bundled CJS dependencies #199

Merged
merged 8 commits into from Aug 10, 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Fix intertop with bundled CJS dependencies ([#199](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/199))

## [0.5.1] - 2023-08-10

Expand Down
40 changes: 16 additions & 24 deletions build.mjs
@@ -1,7 +1,7 @@
import esbuild from 'esbuild'
import path from 'path'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import esbuild from 'esbuild'

/**
* @returns {import('esbuild').Plugin}
Expand Down Expand Up @@ -33,30 +33,27 @@ function patchRecast() {
/**
* @returns {import('esbuild').Plugin}
*/
function patchDynamicRequires() {
return {
name: 'patch-dynamic-requires',
function patchCjsInterop() {
return {
name: 'patch-cjs-interop',
setup(build) {
build.onEnd(async () => {
let outfile = './dist/index.mjs'

let content = await fs.promises.readFile(outfile)

// Prepend `createRequire`
content = `import {createRequire} from 'module';\n${content}`
let code = [
`import {createRequire} from 'module'`,
`import {dirname as __global__dirname__} from 'path'`,

// Replace dynamic require error with createRequire
// unminified version
content = content.replace(
`throw Error('Dynamic require of "' + x + '" is not supported');`,
`return createRequire(import.meta.url).apply(this, arguments);`,
)
// CJS interop fixes
`const require=createRequire(import.meta.url)`,
`const __filename=new URL(import.meta.url).pathname`,
`const __dirname=__global__dirname__(__filename)`,
]

// minified version
content = content.replace(
`throw Error('Dynamic require of "'+e+'" is not supported')`,
`return createRequire(import.meta.url).apply(this,arguments)`,
)
content = `${code.join('\n')}\n${content}`

fs.promises.writeFile(outfile, content)
})
Expand All @@ -81,7 +78,6 @@ function copyTypes() {
}
}


const __dirname = path.dirname(fileURLToPath(import.meta.url))

let context = await esbuild.context({
Expand All @@ -92,12 +88,8 @@ let context = await esbuild.context({
minify: process.argv.includes('--minify'),
entryPoints: [path.resolve(__dirname, './src/index.js')],
outfile: path.resolve(__dirname, './dist/index.mjs'),
format: "esm",
plugins: [
patchRecast(),
patchDynamicRequires(),
copyTypes(),
],
format: 'esm',
plugins: [patchRecast(), patchCjsInterop(), copyTypes()],
})

await context.rebuild()
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.