How can I use npm for front-end dependencies?

前端 未结 5 1138
陌清茗
陌清茗 2020-12-08 10:07

I want to ask if it is possible (and generally a good idea) to use npm to handle front-end dependencies (Backbone, jQuery).

I have found that Backbone, jQuery and so

5条回答
  •  清歌不尽
    2020-12-08 10:20

    +1 for using Browserify. We use it here at diy.org and love it. The best introduction and reasoning behind Browserify can be found in the Browserify Handbook. Topics like CommonJS & AMD solutions, build pipelines and testing are covered there.

    The main reason Browserify works so well is it transparently works w/ NPM. As long as a module can be required it can be Browserified (though not all modules are made to work in the browser).

    Basics:

    npm install jquery-browserify
    

    main.js

    var $ = require('jquery-browserify');
    $("img[attr$='png']").hide();
    

    Then run:

    browserify main.js > bundle.js
    

    Then include bundle.js in your HTML doc and the code in main.js will execute.

提交回复
热议问题