Skip to content

Commit 985302f

Browse files
Fix interop with bundled CJS dependencies (#199)
* Refactor * Add workaround for __filename and __dirname not being defined We bundle several CJS deps but the top-level format is ESM. Unfortunately esbuild does not handle converting `__filename` and `__dirname` for us so we have to patch them after build. * Update lockfile * Add global require * Simplify Now that we define a top-level require constant we no longer need to patch the `__require` esbuild function to indirectly use it for dynamic requires * Fix CS * Rename plugin * Update changelog
1 parent 4b768f7 commit 985302f

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Fix intertop with bundled CJS dependencies ([#199](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/199))
1113

1214
## [0.5.1] - 2023-08-10
1315

build.mjs

+16-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import esbuild from 'esbuild'
2-
import path from 'path'
31
import fs from 'fs'
2+
import path from 'path'
43
import { fileURLToPath } from 'url'
4+
import esbuild from 'esbuild'
55

66
/**
77
* @returns {import('esbuild').Plugin}
@@ -33,30 +33,27 @@ function patchRecast() {
3333
/**
3434
* @returns {import('esbuild').Plugin}
3535
*/
36-
function patchDynamicRequires() {
37-
return {
38-
name: 'patch-dynamic-requires',
36+
function patchCjsInterop() {
37+
return {
38+
name: 'patch-cjs-interop',
3939
setup(build) {
4040
build.onEnd(async () => {
4141
let outfile = './dist/index.mjs'
4242

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

4545
// Prepend `createRequire`
46-
content = `import {createRequire} from 'module';\n${content}`
46+
let code = [
47+
`import {createRequire} from 'module'`,
48+
`import {dirname as __global__dirname__} from 'path'`,
4749

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

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

6158
fs.promises.writeFile(outfile, content)
6259
})
@@ -81,7 +78,6 @@ function copyTypes() {
8178
}
8279
}
8380

84-
8581
const __dirname = path.dirname(fileURLToPath(import.meta.url))
8682

8783
let context = await esbuild.context({
@@ -92,12 +88,8 @@ let context = await esbuild.context({
9288
minify: process.argv.includes('--minify'),
9389
entryPoints: [path.resolve(__dirname, './src/index.js')],
9490
outfile: path.resolve(__dirname, './dist/index.mjs'),
95-
format: "esm",
96-
plugins: [
97-
patchRecast(),
98-
patchDynamicRequires(),
99-
copyTypes(),
100-
],
91+
format: 'esm',
92+
plugins: [patchRecast(), patchCjsInterop(), copyTypes()],
10193
})
10294

10395
await context.rebuild()

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)