cli icon indicating copy to clipboard operation
cli copied to clipboard

define rewriting ignores my dependencies

Open ariovistus opened this issue 8 years ago • 2 comments

I'm submitting a feature request

  • Library Version: 0.30.1

Please tell us about your environment:

  • Operating System: Windows 10

  • Node Version: 8.1.2

  • NPM Version: 5.0.3
  • Browser: all

  • Language: all

Current behavior: I am trying to incorporate pikaday into an aurelia cli project, and I specify the following in my aurelia.json:

          {
            "name": "moment",
            "main": "./moment.js",
            "path": "../node_modules/moment",
            "resources": [],
            "export": "moment"
          },
          {
            "name": "pikaday",
            "main": "pikaday.js",
            "path": "../node_modules/pikaday",
            "resources": [
              "css/pikaday.css"
            ],
            "deps": ["moment"]
          },

pikaday does some obnoxious dynamic stuff trying to figure out what module system it should use:

    var moment;
    if (typeof exports === 'object') {
        // CommonJS module
        // Load moment.js as an optional dependency
        try { moment = require('moment'); } catch (e) {}
        module.exports = factory(moment);
    } else if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(function (req)
        {
            // Load moment.js as an optional dependency
            var id = 'moment';
            try { moment = req(id); } catch (e) {}
            return factory(moment);
        });
    } else {
        root.Pikaday = factory(root.moment);
    }

my project is set up with systemjs, and pikaday executes the AMD block, however require("moment") fails with

Error: Module moment not declared as a System.registerDynamic dependency of (pikaday)

The thing is, the define call is being rewritten by something in the bundler to

  define('pikaday/pikaday',['require'],function (req) {
    ...
  }

if I manually add moment to the dependency list, eg

  define('pikaday/pikaday',['require', 'moment'],function (req) {
    ...
  }

pikaday works and integrates with moment.

Also, if I manually remove the define call, the bundler will surround all of pikaday with

define('pikaday/pikaday',['require','exports','module','moment'],function (require, exports, module) {/*!

Expected/desired behavior: I would like the define-rewriting behavior to include dependencies that I manually specify in aurelia.json to be consistent with the wrapping behavior

  • What is the motivation / use case for changing the behavior? integrating pikaday with moment in an aurelia-cli project

ariovistus avatar Jul 18 '17 21:07 ariovistus

also, are the behaviors I described documented anywhere? I don't even know what to call them..

ariovistus avatar Jul 18 '17 21:07 ariovistus

The dynamic stuff to determine the module system isn't too uncommon. However,

        define(function (req)
        {
            // Load moment.js as an optional dependency
            var id = 'moment';
            try { moment = req(id); } catch (e) {}
            return factory(moment);
        });

This bit is probably why amodrotrace isn't able to detect that moment is a dependency, and therefore doesn't add it to the define call it creates. What i'd do is load pikaday through a CDN (a script tag) until we can get the bundler to deal with this scenario

JeroenVinke avatar Jul 29 '17 07:07 JeroenVinke