-
Notifications
You must be signed in to change notification settings - Fork 92
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
feat: esm and cjs builds for node and browser #81
Open
diegomura
wants to merge
10
commits into
master
Choose a base branch
from
dm/browser-bundle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5cfe323
feat: move node file to src dir
diegomura c3878b4
feat: rename file to index
diegomura 90e099c
feat: avoid using fs in browser
diegomura 81dd1cd
feat: use Buffer.alloc
diegomura 59b72a8
refactor: remove license from code (already in file)
diegomura 892e977
feat: build with rollup
diegomura df770c9
feat: list files to publish
diegomura 258d234
refactor: rollup cleanup
diegomura decd6ba
feat: use nested conditions
diegomura 61f9388
feat: bring back browser
diegomura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/node_modules/ | ||
playground/ | ||
.vscode | ||
coverage | ||
coverage | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,57 @@ | |
"name": "png-js", | ||
"description": "A PNG decoder in JavaScript", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "./lib/png-js.cjs", | ||
"module": "./lib/png-js.js", | ||
"browser": { | ||
"./lib/png-js.js": "./lib/png-js.browser.js", | ||
"./lib/png-js.cjs": "./lib/png-js.browser.cjs" | ||
}, | ||
"exports": { | ||
".": { | ||
"node": { | ||
"import": "./lib/png-js.js", | ||
"require": "./lib/png-js.cjs" | ||
}, | ||
"import": "./lib/png-js.browser.js", | ||
"require": "./lib/png-js.browser.cjs" | ||
} | ||
}, | ||
"author": { | ||
"name": "Devon Govett", | ||
"email": "[email protected]", | ||
"url": "http://badassjs.com/" | ||
}, | ||
"files": [ | ||
"lib", | ||
"png.js", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/devongovett/png.js.git" | ||
}, | ||
"bugs": "http://github.com/devongovett/png.js/issues", | ||
"dependencies": { | ||
"browserify-zlib": "^0.2.0" | ||
}, | ||
"devDependencies": { | ||
"jest": "^24.1.0", | ||
"prettier": "^1.16.4" | ||
"@rollup/plugin-alias": "5.1.0", | ||
"@rollup/plugin-node-resolve": "15.2.0", | ||
"@rollup/plugin-replace": "5.0.0", | ||
"jest": "24.1.0", | ||
"prettier": "1.16.4", | ||
"rimraf": "2.6.3", | ||
"rollup": "4.9.0", | ||
"rollup-plugin-ignore": "1.0.10" | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"test": "yarn build && jest", | ||
"build": "rimraf ./lib && rollup -c", | ||
"prettier": "prettier test/**/*.js png-node.js png.js --write" | ||
}, | ||
"main": "png-node.js", | ||
"engine": [ | ||
"node >= v0.6.0" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import replace from '@rollup/plugin-replace'; | ||
import ignore from 'rollup-plugin-ignore'; | ||
import alias from '@rollup/plugin-alias'; | ||
import nodeResolve from '@rollup/plugin-node-resolve'; | ||
|
||
import pkg from './package.json' assert { type: 'json' }; | ||
|
||
const cjs = { | ||
exports: 'named', | ||
format: 'cjs', | ||
interop: 'compat', | ||
}; | ||
|
||
const esm = { | ||
format: 'es', | ||
}; | ||
|
||
const getCJS = (override) => Object.assign({}, cjs, override); | ||
const getESM = (override) => Object.assign({}, esm, override); | ||
|
||
const input = 'src/index.js'; | ||
|
||
|
||
const getExternal = ({ browser }) => | ||
browser | ||
? Object.keys(pkg.dependencies) | ||
: ['fs', ...Object.keys(pkg.dependencies)]; | ||
|
||
const getPlugins = ({ browser }) => [ | ||
...(browser | ||
? [ | ||
ignore(['fs']), | ||
alias({ | ||
entries: [{ find: 'zlib', replacement: 'browserify-zlib' }], | ||
}), | ||
nodeResolve({ browser, preferBuiltins: !browser }), | ||
] | ||
: []), | ||
replace({ | ||
preventAssignment: true, | ||
values: { | ||
BROWSER: JSON.stringify(browser), | ||
}, | ||
}), | ||
]; | ||
|
||
const serverConfig = { | ||
input, | ||
output: [ | ||
getESM({ file: 'lib/png-js.js' }), | ||
getCJS({ file: 'lib/png-js.cjs' }), | ||
], | ||
external: getExternal({ browser: false }), | ||
plugins: getPlugins({ browser: false }), | ||
}; | ||
|
||
const browserConfig = { | ||
input, | ||
output: [ | ||
getESM({ file: 'lib/png-js.browser.js' }), | ||
getCJS({ file: 'lib/png-js.browser.cjs' }), | ||
], | ||
external: getExternal({ browser: true }), | ||
plugins: getPlugins({ browser: true }), | ||
}; | ||
|
||
export default [serverConfig, browserConfig]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using non standard browser field, use nested conditions in exports field:
https://nodejs.org/api/packages.html#nested-conditions
IMO, for browser should have only import (es module)
This should be future proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sgtm! Thanks for the input @blikblum . Done in decd6ba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might still need the browser field for backward compatibility though, eg for old tools that don't support exports. Otherwise we'll have to release this in a major version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I also wanted to remove
Buffer
usage in favor of Uint8Array (separately) if that sounds good for you guys. I think that should be breaking, but up to you 😄