Skip to content

Commit

Permalink
Merge pull request #119 from olsonpm/support-cjs
Browse files Browse the repository at this point in the history
Support cjs
  • Loading branch information
ndaidong authored Dec 2, 2023
2 parents 7abcabc + fa94d81 commit 306b847
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pnpm-lock.yaml

output.json
deno.lock

bundle.cjs
bundle.cjs.map
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -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/cjs-entry.js'],
bundle: true,
platform: 'node',
target: 'node16',
outfile: 'bundle.cjs',
minify: true,
sourcemap: true,
external: externalDeps,
})
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -23,6 +31,8 @@
"scripts": {
"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",
Expand All @@ -35,6 +45,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",
Expand Down
14 changes: 14 additions & 0 deletions src/cjs-entry.js
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 306b847

Please sign in to comment.