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

Add CommonJS/Node/AMD snippet to end of compiled output #50

Closed
@rtfeldman

Description

See elm/compiler#1029 for discussion that led to this.

The following should be incorporated into the output JS of compiled Elm files.

(function() {
  var Elm = ... // current emitted code goes here

  if (typeof define === "function" && define.amd) {
    define([], function() {
      return Elm;
    });
  } else if (typeof module === "object") {
    module.exports = Elm;
  } else {
    if (typeof this.Elm === "undefined") {
      this.Elm = Elm;
    } else {
      throw new Error("This page is trying to import multiple compiled Elm programs using the same `Elm` global object, which would cause conflicts. This can be resolved by using a module loader like RequireJS to import the compiled Elm programs into different objects.")
    }
  }
}).call(this);

PREVIOUS VERSION:

This version is more "stock" and will silently overwrite any previous Elm globals defined on the page...which seems far less desirable than crashing early with a helpful message.

(function() {
  var Elm = ... // current emitted code goes here

  if (typeof define === "function" && define.amd) {
    define([], function() {
      return Elm;
    });
  } else if (typeof module === "object") {
    module.exports = Elm;
  } else {
    this.Elm = Elm;
  }
}).call(this);

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions