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
Bad module design #295
Comments
You don't need the last bullet point:
Browserify users who do |
Yes, sorry, you are right. |
No problem :) |
Thanks folks, PR is welcome! Sent from my iPad
|
What's the plan here? If we change package.json to {main: 'src/htmlminifier.js'} and leave grunt->concat->dist/htmlminifier.js alone will that fix Browserify, or break any other package systems? Or does src/htmlminifier.js need to be changed to support Browserify to replace concat? I'm kinda stuck here. |
IMHO the design should be:
Period. But honestly, a good library cannot rely on concatenating files into a single one. |
What's wrong with concat?
Why is it bad specifically for "browser-suitable" library? And what do you mean by "browser-suitable"? |
Building a library with "concat" is like doing copy&paste. Those files have no sense by them alone. Even worse: most probably they are syntactically incorrect, so you cannot process them with JSHint/JSLint. Concatenating files is not the way to do nothing in JavaScript nowadays. Again: code it as if it was a pure Node project, make your project "export" something (via
A Node project is composed of external modules (libraries) and internal modules (which in fact are other files under the After the Node project is done, a Grunt o gulp task can easily run browserify on it to generate a single JavaScrip file which contains all the dependencies in it. This is NOT a simple "concat" mechanism at all. In fact, the resulting "bundle" still keeps all the calls to the function
"Suitable for a browser" means that you can use it as usual within your HTML:
Take a look to this project in which I'm co-author: JsSIP. For a simpler example, take a look to this other project: https://github.com/ibc/args2object (but it is not a good example since its source file is a single JS file, but let's imagine there are more files):
This is, I do NOT need to do
(oppss, error because D.js depends on a global I've learnt all this stuff about Node (and building browser suitable libraries from a Node project) recently, and I cannot live without it now.I strongly think this is the way to go today. For example, all the stuff you do at https://github.com/kangax/fabric.js/blob/master/dist/fabric.require.js is much easier (and robust) if you code as I suggest above and them use browserify to build such a bundle. |
I don't care whether or not you use concat. What I care about is that when I require("html-minifier") and bundle my code with Browserify, it reads in your concatted ./dist/htmlminifier.js file because package.json says main: ./dist/htmlminifier.js, but when it gets to line 580 of ./dist/htmlminifier.js Browserify throws an error because require is in fact a function and ./dist/htmlparser.js doesn't exist. If you are publishing a package to NPM package.json "main" should point to a file that can be required as a CommonJS module. I believe there are other properties of package.json that can point to bundled code appropriate for direct inclusion on the client side. It's a fine idea to use Browserify to bundle ./dist/htmlparser.js because it would save you from having to check whether or not require is a function all the time. But for my purposes, all I care is that htmlparser.js exists when your code require()s it. |
@kangax I've forked the project and will try to make a PR with a new "Node-based" design. However the fact that test units require "qunit" (which internally requires "pantomjs") annoys me. Test units in this library are supposed to take a string, produce another string and match them with a expected string. No more. There is no need for having a browser emulation within it. Is there something wrong in using any other testunit library that does not depend on Phantomjs? Phantomjs is not relieable, it fails to load some JS files (for example all those generated with |
Bad:
"main": "./dist/htmlminifier.js"
in package.json.How should it be?:
"main": "index.js"
in package.json, and in "index.js" call torequire('lib/htmlparser')
, etc.browserify
(via a Grunt task) to generate a bundle file suitable for the browser, and store it in dist/htmlminifier.js."browser": "dist/htmlminifier.js"
.The text was updated successfully, but these errors were encountered: