Sharing common code across pages with Browserify

后端 未结 1 1954
北荒
北荒 2021-02-02 00:36

I have a fairly large multi-page javascript applications that uses requirejs to organize code. I am researching moving to browserify because I like the simplicity that it offers

相关标签:
1条回答
  • 2021-02-02 00:52

    You can use factor-bundle to do exactly this. You will just need to split your code up into different entry points for each file.

    Suppose you have 3 pages, /a, /b, and /c. Each page corresponds to an entry point file of /browser/a.js, /browser.b.js, and /browser/c.js. With factor-bundle, you can do:

    browserify -p [ factor-bundle -o bundle/a.js -o bundle/b.js -o bundle/c.js ] \
      browser/a.js browser/b.js browser/c.js > bundle/common.js
    

    any files used by more than 1 of the entry points will be factored out into bundle/common.js, while all the page-specific code will be located in the page-specific bundle file. Now on each page you can put a script tag for the common bundle and a script tag for the page-specific bundle. For example, for /a you can put:

    <script src="bundle/common.js"></script>
    <script src="bundle/a.js"></script>
    

    If you don't want to use the command-line version, you can also use factor-bundle from the API:

    var browserify = require('browserify');
    var fs = require('fs');
    
    var files = [ './files/a.js', './files/b.js', './files/c.js' ];
    var b = browserify(files);
    b.plugin('factor-bundle', {
        outputs: [ 'bundle/a.js', 'bundle/b.js', 'bundle/c.js' ]
    });
    b.bundle().pipe(fs.createWriteStream('bundle/common.js'));
    
    0 讨论(0)
提交回复
热议问题