From 076704a8d184788d4afdad871aaac6acaa168255 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 14:39:08 -0400 Subject: [PATCH 1/8] Refactor --- build.mjs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/build.mjs b/build.mjs index 5eae3b4b..4b184438 100644 --- a/build.mjs +++ b/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} @@ -34,7 +34,7 @@ function patchRecast() { * @returns {import('esbuild').Plugin} */ function patchDynamicRequires() { - return { + return { name: 'patch-dynamic-requires', setup(build) { build.onEnd(async () => { @@ -43,7 +43,11 @@ function patchDynamicRequires() { let content = await fs.promises.readFile(outfile) // Prepend `createRequire` - content = `import {createRequire} from 'module';\n${content}` + let code = [ + `import {createRequire} from 'module'`, + ] + + content = `${code.join('\n')}\n${content}` // Replace dynamic require error with createRequire // unminified version @@ -81,7 +85,6 @@ function copyTypes() { } } - const __dirname = path.dirname(fileURLToPath(import.meta.url)) let context = await esbuild.context({ @@ -92,7 +95,7 @@ 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", + format: 'esm', plugins: [ patchRecast(), patchDynamicRequires(), From be9797c34f1decc3d5616cd1fa63eb960ca1a3b0 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 14:42:16 -0400 Subject: [PATCH 2/8] 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. --- build.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.mjs b/build.mjs index 4b184438..0f6e199d 100644 --- a/build.mjs +++ b/build.mjs @@ -45,6 +45,9 @@ function patchDynamicRequires() { // Prepend `createRequire` let code = [ `import {createRequire} from 'module'`, + `import {dirname as __workaround__dirname__} from 'path'`, + `const __filename=new URL(import.meta.url).pathname`, + `const __dirname=__workaround__dirname__(__filename)`, ] content = `${code.join('\n')}\n${content}` From 7ebe1f09ced27462dbce21892dd235b77812b725 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 14:42:40 -0400 Subject: [PATCH 3/8] Update lockfile --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 04a8d4f7..bcf802c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ "@shopify/prettier-plugin-liquid": "*", "@shufo/prettier-plugin-blade": "*", "@trivago/prettier-plugin-sort-imports": "*", - "prettier": "^2.2 || ^3.0", + "prettier": "^3.0", "prettier-plugin-astro": "*", "prettier-plugin-css-order": "*", "prettier-plugin-import-sort": "*", From a00d349973d278cbb5ffe4dbcfa78f7ced4f0e1b Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 14:56:45 -0400 Subject: [PATCH 4/8] Add global require --- build.mjs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/build.mjs b/build.mjs index 0f6e199d..a36a4b68 100644 --- a/build.mjs +++ b/build.mjs @@ -45,9 +45,12 @@ function patchDynamicRequires() { // Prepend `createRequire` let code = [ `import {createRequire} from 'module'`, - `import {dirname as __workaround__dirname__} from 'path'`, + `import {dirname as __global__dirname__} from 'path'`, + + // CJS interop fixes + `const require=createRequire(import.meta.url)`, `const __filename=new URL(import.meta.url).pathname`, - `const __dirname=__workaround__dirname__(__filename)`, + `const __dirname=__global__dirname__(__filename)`, ] content = `${code.join('\n')}\n${content}` @@ -56,13 +59,13 @@ function patchDynamicRequires() { // unminified version content = content.replace( `throw Error('Dynamic require of "' + x + '" is not supported');`, - `return createRequire(import.meta.url).apply(this, arguments);`, + `return require.apply(this, arguments);`, ) // minified version content = content.replace( `throw Error('Dynamic require of "'+e+'" is not supported')`, - `return createRequire(import.meta.url).apply(this,arguments)`, + `return require.apply(this,arguments)`, ) fs.promises.writeFile(outfile, content) From e3bdb0d5317d8c4d528dfdc523793cb162f04000 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 15:00:06 -0400 Subject: [PATCH 5/8] 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 --- build.mjs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/build.mjs b/build.mjs index a36a4b68..4edff74e 100644 --- a/build.mjs +++ b/build.mjs @@ -55,19 +55,6 @@ function patchDynamicRequires() { content = `${code.join('\n')}\n${content}` - // Replace dynamic require error with createRequire - // unminified version - content = content.replace( - `throw Error('Dynamic require of "' + x + '" is not supported');`, - `return require.apply(this, arguments);`, - ) - - // minified version - content = content.replace( - `throw Error('Dynamic require of "'+e+'" is not supported')`, - `return require.apply(this,arguments)`, - ) - fs.promises.writeFile(outfile, content) }) }, From 567f4eb1a34f6aa5deb7f5cfa383789b2f5faa24 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 15:00:44 -0400 Subject: [PATCH 6/8] Fix CS --- build.mjs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/build.mjs b/build.mjs index 4edff74e..b33ce5cc 100644 --- a/build.mjs +++ b/build.mjs @@ -89,11 +89,7 @@ let context = await esbuild.context({ entryPoints: [path.resolve(__dirname, './src/index.js')], outfile: path.resolve(__dirname, './dist/index.mjs'), format: 'esm', - plugins: [ - patchRecast(), - patchDynamicRequires(), - copyTypes(), - ], + plugins: [patchRecast(), patchDynamicRequires(), copyTypes()], }) await context.rebuild() From 40ff090accc8ded4c192ba349773302768c5736d Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 15:01:23 -0400 Subject: [PATCH 7/8] Rename plugin --- build.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.mjs b/build.mjs index b33ce5cc..ea360951 100644 --- a/build.mjs +++ b/build.mjs @@ -33,9 +33,9 @@ function patchRecast() { /** * @returns {import('esbuild').Plugin} */ -function patchDynamicRequires() { +function patchCjsInterop() { return { - name: 'patch-dynamic-requires', + name: 'patch-cjs-interop', setup(build) { build.onEnd(async () => { let outfile = './dist/index.mjs' @@ -89,7 +89,7 @@ let context = await esbuild.context({ entryPoints: [path.resolve(__dirname, './src/index.js')], outfile: path.resolve(__dirname, './dist/index.mjs'), format: 'esm', - plugins: [patchRecast(), patchDynamicRequires(), copyTypes()], + plugins: [patchRecast(), patchCjsInterop(), copyTypes()], }) await context.rebuild() From 69f2f4922b6deb6710bd2d980454d326285d8219 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 10 Aug 2023 15:25:08 -0400 Subject: [PATCH 8/8] Update changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abb3f80e..71b6623b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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