How to bundle multiple javascript libraries with browserify?

前端 未结 2 1117
死守一世寂寞
死守一世寂寞 2021-01-15 15:19

I\'m trying to use Browerifiy in the browser, if I use the standalone option it exposes one module. I don\'t want to do this. The website and documentation just seems to cut

2条回答
  •  再見小時候
    2021-01-15 15:55

    I got this working my own way without obfuscating the project too much with dependancies.

    In grunt I have

    browserify: {
          standalone: {
            src: [ '<%= yeoman.server %>/shared-components/exposed-modules.js' ],
            dest: '<%= yeoman.client %>/app/js/browserifed-shared-code.js',
            options: {
              browserifyOptions: {
                  standalone: 'Shared'
              }
            }
          },
        }
    
    //In-completed example.
    watch: {
          scripts: {
            files: ['<%= yeoman.server %>/shared-components/**/*.js'],
            tasks: ['browserify'],
            options: {
                spawn: false
            }
            },
    

    Then I exposed the modules I want using:

    'use strict';
    
    //This is the main entry point for all shared libraries.
    
    var Utility = require('./utility');
    var UrlController = require('./url-controller');
    
    var Shared = function(){};
    
    //Added new client modules to be exposed here.
    Shared.Utility = Utility;
    Shared.UrlController = UrlController;
    
    module.exports = Shared;
    

    In the browser I can now call

    shared.Utility.isAscii('test'); //Working.
    

    And anywhere I wanna reuse I just assign in the scope, so

    //Some scope.
    var Utility = shared.Utility;
    

提交回复
热议问题