Browserify - How to call function bundled in a file generated through browserify in browser

前端 未结 11 2227
-上瘾入骨i
-上瘾入骨i 2020-11-29 17:18

I am new to nodejs and browserify. I started with this link .

I have file main.js which contains this code

var unique = require(\'uniq\');

var data          


        
11条回答
  •  不知归路
    2020-11-29 17:43

    You have a few options:

    1. Let plugin browserify-bridge auto-export the modules to a generated entry module. This is helpful for SDK projects or situations where you don't have to manually keep up with what is exported.

    2. Follow a pseudo-namespace pattern for roll-up exposure:

    First, arrange your library like this, taking advantage of index look-ups on folders:

    /src
    --entry.js
    --/helpers
    --- index.js
    --- someHelper.js
    --/providers
    --- index.js
    --- someProvider.js
    ...
    

    With this pattern, you define entry like this:

    exports.Helpers = require('./helpers');
    exports.Providers = require('./providers');
    ...
    

    Notice the require automatically loads the index.js from each respective sub-folder

    In your subfolders, you can just include a similar manifest of the available modules in that context:

    exports.SomeHelper = require('./someHelper');
    

    This pattern scales really well and allows for contextual (folder by folder) tracking of what to include in the rolled-up api.

提交回复
热议问题