-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
🐛Parcel cannot require a UMD #959
Comments
Duplicate of #766 But thanks for bringing it up. We should definitely make sure that UMD support get’s added 👍 |
Hi, @davidnagli — thanks for the response! I'm not sure this is exactly a duplicate, though, since it looks like #766 is about generating UMDs, whereas this is about consuming them. Maybe I could've titled the issue more accurately. |
Thanks for the clarification:) Totally agree that this is something that we should work on 👍 |
@markandrus I’ve been looking into this, and by the looks of it the UMD should be perfectly compatible with CommonJS - and by extension, Parcel. Can you please provide an example of a UMD module that doesn’t work? |
For anybody else that is interested in looking into this, and for general reference (since I already did the research anyway), dependencies get collected here: Lines 89 to 91 in 0dc062f
parcel/src/visitors/dependencies.js Lines 1 to 110 in 0dc062f
That pretty much sums up how Parcel gathers JavaScript dependencies :) By the looks of it, this should all be perfectly compatible with UMD, but we’ll have to see what @markandrus answers about the example. For more info on how visitors work: Here’s a useful explanation from Babel’s plugin handbook |
@davidnagli you can build the module using OP's instruction, here is (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Foo = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
},{}],2:[function(require,module,exports){
require('./bar')
},{"./bar":1}]},{},[2])(2)
}); So why does it works with Webpack and not Parcel and Browserify? Because of how we parse the code, Webpack checks if
function require(name) {
console.log('require', name)
}
require('anything') Boom :
Babel offers neat APIs to check this, I'll try to make a PR. |
🎛 Configuration (.babelrc, package.json, cli command)
I setup a project where
index.js
depends on a UMD. The source of the UMD lives inlib/
. It will then be browserified and placed indist/
.I do this by executing the following:
Then, I try to run
parcel
onindex.js
.🤔 Expected Behavior
Parcel is able to bundle the UMD.
😯 Current Behavior
Parcel fails to bundle the UMD.
💁 Possible Solution
Webpack succeeds in bundling the UMD, so I think this is doable. I don't know the solution, though.
Interestingly, Browserify seems unable to consume its own UMD, which seems like a separate bug.
🔦 Context
I'm trying to replace Webpack with Parcel in a client-side application my team created. We have one or more dependencies that are UMDs.
💻 Code Sample
See above.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: