URI.js icon indicating copy to clipboard operation
URI.js copied to clipboard

Support for requirejs compatible builds through npm

Open msmyers opened this issue 8 years ago • 5 comments

msmyers avatar Aug 25 '16 19:08 msmyers

thank you! Is this following up on #310?

What do you think of piping this through umd?

rodneyrehm avatar Aug 25 '16 20:08 rodneyrehm

I don't understand your question. I've never used UMD.

I forked and patched the project so I could use it in my requirejs setup.

msmyers avatar Aug 25 '16 20:08 msmyers

I'm new to the javascript/node/requirejs community. If you have suggestions on how to improve the build, I will gladly do the work, in order to learn.

msmyers avatar Aug 25 '16 20:08 msmyers

Yes, it resolves https://github.com/medialize/URI.js/issues/310

msmyers avatar Aug 25 '16 20:08 msmyers

I'm new to the javascript/node/requirejs community.

welcome :)

If you have suggestions on how to improve the build, I will gladly do the work, in order to learn.

I'll do my best to explain…

I've never used UMD.

UMD (Universal Module Definition) is a way to make modules compatible with AMD (what RequireJS uses), CommonJS (what Node uses) and globals (what you'd have in the browser using <script> elemenets, i.e. no module management).

The URI.js source is wrapped in UMD so everyone can use the code in whatever situation they happen to be in.

Your commit 047b363 makes the RequireJS Optimizer consume the UMD source and produce a file that does can't be loaded by RequireJS, as the following prints undefined:

require(['dist/urijs-1.18.1.min'], function(URI) {
  console.log(URI);
});

Also the RequireJS Optimizer does not remove the UMD wrappers of the individual files, so loading the dist file in node will complain about not being able to load ./punycode if you run

node -e 'var URI = require("./dist/urijs-1.18.1.min"); console.log("URI", URI);'

And if you load the file in a browser using <script src="dist/urijs-1.18.1.min.js"></script>, you'll find the globals URI, SecondLevelDomain, punycode and IPv6.

In other words, the Require Optimizer doesn't solve the problem.

But which problem is that anyway? If you are using a module loader, you can load src/URI.js and have your tool-chains optimizer (be that RequireJS, Browserify, Webpack, …) take care of the rest - there's no need to load minified/bundled sources, when you're going to run everything through your own build-pipeline again. You can load src/URI.js in Node as there's no need to optimize anything here. And for using <script> in the browser you can load src/URI.min.js, which exposes the modules as globals.

rodneyrehm avatar Aug 25 '16 21:08 rodneyrehm