Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

UMD imports doesn't resolve correctly with AMD output #114

Closed
inrix-vincent-jo opened this issue Sep 19, 2016 · 2 comments
Closed

UMD imports doesn't resolve correctly with AMD output #114

inrix-vincent-jo opened this issue Sep 19, 2016 · 2 comments

Comments

@inrix-vincent-jo
Copy link

Hello guys,

Last week experimented with rollup, rollup-commonjs, rollup-node-define package to build a all-in-one library package. I have 1 external dependency that I was trying to bundle up with my source code. The dependency was in UMD format. It did not resolve correctly, I tried many times to rebundle it by playing around with the configuration and turning off commonjs and only using node-define, etc. But none of that worked. Then, I switched to webpack and everything worked fine. The external library in question is 'jssha'.

Here is structure of the 'jssha' library that gets bundled up using the 3 tools above:

var sha = createCommonjsModule(function (module, exports) {
  'use strict';(function(X){function C(f,b,c){
   // library code
  "function" === typeof define && define.amd ? define(function(){return C}) : "undefined" !== typeof exports ? ( "undefined" !== typeof module && module.exports && (module.exports=C), exports=C): X.jsSHA=C 
  })(commonjsGlobal);
});

And again, my whole library is wrapped in AMD define function. So when my environment runs this bundled code, it basically returns the 'define(function(){return C})' inside of the wrapper which I believe causes it to break. Can you please look into this? I love the idea of rollup but this problem was the only obstacle that had me reeling towards webpack. I wish I can use rollup, it's so much simpler and compiling to ES6 a good performance factor for us too. Thanks, let me know if I can clear anything up.

@Rich-Harris
Copy link
Contributor

Can you provide a repro please? It's possible that this would be fixed by #147, but without more information it's not possible to say. Thanks

@clarencejychan
Copy link

clarencejychan commented Nov 22, 2019

So I'm actually running into a similar problem. I'm currently trying to bundle React into my code so that there's a copy of react inside of my project (specific reason as to why we need this). The output is an AMD module that can be consumed by RequireJS.

import postcss from 'rollup-plugin-postcss'
import resolve from 'rollup-plugin-node-resolve'
import commonJS from 'rollup-plugin-commonjs'
export default {
  input: 'src/index.js', // source entry point
  output: {
    file: "dist/amd/mjolnir-dist.js",
    format: 'amd',
  },
  plugins: [
    resolve(),
    commonJS(),
    postcss({
      extract: true,
      sourceMap: true,
      extensions: ['.css'],
      plugins: []
    })
  ]
};

In the resulting bundled file I can see

define(function () { 'use strict';
        ...
	var react = createCommonjsModule(function (module) {

	if (process.env.NODE_ENV === 'production') {
	  module.exports = react_production_min;
	} else {
	  module.exports = react_development;
	}
	});

       ....
	if (window.require) {
	  window.require.config({
	    map: {
	      '*': {
	        'mjolnir' : 'nbextensions/mjolnir/mjolnir'
	      }
	    }
	  });
	}

	function mjolnir() {
	  console.log("test");
	}

	return mjolnir;

});

I highly suspect for whatever reason, it consumes the modules, tries to output them out as commonjs modules, then it can't be consumed by requirejs (or anything that loads via AMD)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants