thlorenz / browserify-shim

📩 Makes CommonJS incompatible files browserifyable.
MIT License
933 stars 87 forks source link

Fail to attach element on window #193

Closed lelea2 closed 8 years ago

lelea2 commented 8 years ago

Have issue with browserify-shim latest version using with grunt-browserify. So basically, i have shim configured in package.json

 "browser": {
    "jquery": "./node_modules/couponscom-js-lib/lib/jquery.js",
    "underscore": "./node_modules/couponscom-js-lib/lib/underscore.js",
    "backbone": "./node_modules/couponscom-js-lib/lib/backbone.js",
    "pubsub": "./node_modules/couponscom-js-lib/lib/PubSub.js",
    "dust": "./node_modules/dustjs-linkedin/lib/dust.js",
    "dust-compiler": "./node_modules/dustjs-linkedin/lib/compiler.js",
    "dust-helpers": "./node_modules/dustjs-helpers/lib/dust-helpers.js",
  },
  "browserify-shim": "./tasks/shim/shim.js",
  "browserify": {
    "global-transform": [
      "browserify-shim"
    ]
  }

under tasks/shim/shim.js

module.exports = {
    'jquery': {
        'exports': 'jQuery'
    },
    'underscore': {
        'exports': '_'
    },
    'backbone': {
        'exports': 'Backbone',
        'depends': {
            'jquery': '$',
            'underscore': '_'
        }
    },
    'pubsub': {
        'exports': 'PubSub',
        'depends': {
            'backbone': 'Backbone'
        }
    },
    'dust': {
        'exports': 'dust'
    },
    'dust-compiler': {
        'depends': {
            'dust': 'dust'
        }
    },
    'dust-helpers': {
        'depends': {
            'dust': 'dust'
        }
    }
}

Doing some googling and realize i have to duplicate shim on grunt file

var fs = require('fs'),
    shims = require('../shim/shim.js'),
    alias = require('../shim/browser.js'),
    sharedModules = Object.keys(shims).concat([
      // place all modules you want in the lib build here
    ]);

module.exports = function browserify(grunt) {

    // Load task
    grunt.loadNpmTasks('grunt-browserify');
    var browserifyProduction = {
            options: {
                browserifyOptions: {
                    debug: false
                },
                require: sharedModules,
                preBundleCB: function (b) {
                    b.transform('browserify-shim')
                }
            },
            files: [{
                expand: true,
                cwd: 'public/js/',
                src: '*.js',
                dest: 'build/js'
            }]
        }

However, after building, jQuery doesnt get export as window.jQuery. Same as other object. Did i do anything wrong here?

Thank you Kareen

bendrucker commented 8 years ago

We're going to need a tighter reproducible example to look into this. Please exclude Grunt and everything but jQuery.

lelea2 commented 8 years ago

Same as this issue actually https://github.com/thlorenz/browserify-shim/issues/188

bendrucker commented 8 years ago

Ok, let's track it there then