evancz / elm-markdown

Markdown parsing within Elm
http://package.elm-lang.org/packages/evancz/elm-markdown/latest
Other
88 stars 7 forks source link

Fix webpack compatibility #15

Closed ento closed 8 years ago

ento commented 8 years ago

Problem:

When webpacked, Markdown.toHtml fails with:

Uncaught ReferenceError: marked is not defined

Clone this repo and follow the readme to see it in action.

Cause:

When webpacked, the whole compiled Elm code is evaluated within a CommonJS environment.

The marked library detects the module object and exports itself as a proper CommonJS module should. As a result, the native Markdown module loses access to a global marked function.

Fix:

Nest the marked library inside our own CommonJS-compatible environment and capture the exported object as a local variable.

var marked = (function() {
  var module = {};
  var exports = module.exports = {};

  // insert marked library here

  return module.exports;
})();

QA

  1. The aforementioned project should work
  2. Compiling the project through elm-make src/Main.elm --output main.html should work too
eeue56 commented 8 years ago

Beautiful! @evancz, this is of interest and looks good.

evancz commented 8 years ago

Thanks @ento! Can you remind me to do a patch release on Monday?

ento commented 8 years ago

@evancz Thanks for the merge && pinging for a patch release :)