Bower+grunt-wiredep自动注入包到html

以安装jquery为例

1、假设已经通过npm安装好了bower和grunt-wiredep,以及grunt-contrib-watch(用来观察文件变动)

在gruntfile.js文件中增加任务

wiredep: {
      target: {
        src:['public/index.html'] //指定要将bower下载的包添加到的html文件
      }
    }
2、在watch中观察bower.json文件,如果该文件变动,则执行wiredep任务,将下载的包插入html

watch: {
      gruntfile: {
        files: '<%= jshint.gruntfile.src %>',
        tasks: ['jshint:gruntfile']
      },
      lib_test: {
        files: '<%= jshint.lib_test.src %>',
        tasks: ['jshint:lib_test', 'qunit']
      },
      bower: {                 //为观察bower.json
        files: ['bower.json'],
        tasks: ['wiredep']
      }

    }
3、加载包,并设为默认启动
grunt.loadNpmTasks('grunt-wiredep');

  // Default task.
  grunt.registerTask('default', ['jshint','concat', 'uglify','wiredep', 'watch']);
4、指定包在html的插入位置,在html文件中添加注释,包会被插入到注释中

  <!-- bower:js -->  js插入位置
  <!-- endbower -->

  <!-- bower:css --> css插入位置
  <!-- endbower -->

5、在控制台输入命令运行grunt

grunt

6、另打开一个控制台,下载jquery,运行以下命令

bower install jquery --save
可以看到bower.json如下

"dependencies": {
    "socket.io-client": "^2.0.1",
    "bootstrap": "^3.3.7",
    "jquery": "^3.2.1"
  }
index.htm自动插入了jquery

<!-- bower:js -->
    <script src="../bower_components/jquery/dist/jquery.js"></script>
    <script src="../bower_components/bootstrap/dist/js/bootstrap.js"></script>
<!-- endbower -->









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值