Using browserify with npm jQuery and non-npm plugins

后端 未结 3 1669
孤街浪徒
孤街浪徒 2021-02-01 07:09

I am using browserify to bundle front-end code. It\'s been great so far, but I\'ve been having difficulty mixing npm and non npm packages. For example, using the npm version of

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 07:49

    The browser directive is just an alias to specify what you want when you write jquery. The default for jquery is the path in node_modules, so your line:

    "jquery": "./node_modules/jquery/dist/jquery.js",
    

    ...is redundant and you could remove it, because when you write "depends": ["jquery"] in your Browserify Shim config, jquery already points to ./node_modules/jquery/dist/jquery.js without that line in your browser key. In fact, you could probably remove the browser directive entirely, you'd have to check the config in those jQuery plugins' package.json files but most likely they're already aliased as you have them, without the browser override.

    Otherwise I don't think there is a cleaner way to implement this. Like you said you need to use Browserify Shim to shim those non-CJS jQuery plugins and you're doing it the right way.

提交回复
热议问题