From d899e473a9a929fe9a60ddb1bae4c2ec2bcf1815 Mon Sep 17 00:00:00 2001 From: Phil Olson Date: Fri, 1 Dec 2023 13:28:16 -0600 Subject: [PATCH 1/3] support cjs note: esbuild doesn't have a config like most tooling. They suggest instead to use their api. See https://github.com/evanw/esbuild/issues/952#issuecomment-796548330 --- .gitignore | 3 +++ build.js | 20 ++++++++++++++++++++ package.json | 10 ++++++++++ 3 files changed, 33 insertions(+) create mode 100644 build.js diff --git a/.gitignore b/.gitignore index 95855fb..8b34578 100755 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ pnpm-lock.yaml output.json deno.lock + +bundle.cjs +bundle.cjs.map diff --git a/build.js b/build.js new file mode 100644 index 0000000..4829ac8 --- /dev/null +++ b/build.js @@ -0,0 +1,20 @@ +import { build } from 'esbuild' +import fs from 'fs' + +const { dependencies } = JSON.parse(fs.readFileSync('./package.json', 'utf8')) +// we need esbuild to process esm dependencies while leaving cjs compatible ones +// out of the bundle +const esmDependencies = new Set(['bellajs']) +const externalDeps = Object.keys(dependencies) + .filter(dep => !esmDependencies.has(dep)) + +build({ + entryPoints: ['./src/main.js'], + bundle: true, + platform: 'node', + target: 'node16', + outfile: 'bundle.cjs', + minify: true, + sourcemap: true, + external: externalDeps, +}) diff --git a/package.json b/package.json index a16826e..ab979dd 100755 --- a/package.json +++ b/package.json @@ -10,6 +10,14 @@ "author": "@extractus", "main": "./src/main.js", "type": "module", + "exports": { + ".": { + "types": "./index.d.ts", + "import": "./src/main.js", + "require": "./bundle.cjs", + "default": "./src/main.js" + } + }, "imports": { "cross-fetch": "./src/deno/cross-fetch.js" }, @@ -23,6 +31,7 @@ "scripts": { "lint": "eslint .", "lint:fix": "eslint --fix .", + "build": "node build", "pretest": "npm run lint", "test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true", "eval": "node eval", @@ -35,6 +44,7 @@ "html-entities": "^2.4.0" }, "devDependencies": { + "esbuild": "^0.19.8", "eslint": "^8.53.0", "https-proxy-agent": "^7.0.2", "jest": "^29.7.0", From 18d9787228671001ce288653e5a6ce81556bc11a Mon Sep 17 00:00:00 2001 From: Phil Olson Date: Fri, 1 Dec 2023 13:34:56 -0600 Subject: [PATCH 2/3] deprecate cjs and add warning similar to vite --- README.md | 7 +++++++ build.js | 2 +- src/cjs-entry.js | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/cjs-entry.js diff --git a/README.md b/README.md index 7b4e668..442e816 100755 --- a/README.md +++ b/README.md @@ -48,6 +48,13 @@ import { extract } from 'https://esm.sh/@extractus/feed-extractor' Please check [the examples](https://github.com/extractus/feed-extractor/tree/main/examples) for reference. +## CJS Deprecated + +CJS is deprecated for this package. When calling `require('@extractus/feed-extractor')` a deprecation warning is now logged. You should update your code to use the ESM export. + +- You can ignore this warning via the environment variable `FEED_EXTRACTOR_CJS_IGNORE_WARNING=true` +- To see where the warning is coming from you can set the environment variable `FEED_EXTRACTOR_CJS_TRACE_WARNING=true` + ## APIs diff --git a/build.js b/build.js index 4829ac8..b917202 100644 --- a/build.js +++ b/build.js @@ -9,7 +9,7 @@ const externalDeps = Object.keys(dependencies) .filter(dep => !esmDependencies.has(dep)) build({ - entryPoints: ['./src/main.js'], + entryPoints: ['./src/cjs-entry.js'], bundle: true, platform: 'node', target: 'node16', diff --git a/src/cjs-entry.js b/src/cjs-entry.js new file mode 100644 index 0000000..e4bba06 --- /dev/null +++ b/src/cjs-entry.js @@ -0,0 +1,14 @@ +function warnCjsUsage () { + if (process.env.FEED_EXTRACTOR_CJS_IGNORE_WARNING?.toLowerCase() === 'true') return + const yellow = (str) => `\u001b[33m${str}\u001b[39m` + const log = process.env.FEED_EXTRACTOR_CJS_TRACE_WARNING?.toLowerCase() === 'true' ? console.trace : console.warn + log( + yellow( + 'The CJS build of @extractus/feed-extractor is deprecated. See https://github.com/extractus/feed-extractor#cjs-deprecated for details.' + ) + ) +} + +warnCjsUsage() + +export * from './main' From fa94d810a96848c6e5b724c6dd367989f575aca2 Mon Sep 17 00:00:00 2001 From: Phil Olson Date: Fri, 1 Dec 2023 18:34:49 -0600 Subject: [PATCH 3/3] add prepublishOnly script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index ab979dd..fc229b2 100755 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "lint": "eslint .", "lint:fix": "eslint --fix .", "build": "node build", + "prepublishOnly": "npm run build", "pretest": "npm run lint", "test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true", "eval": "node eval",