Gulp-usemin不输出js文件



我试图使用gulp-usemin来捆绑我的CSSjavascript文件用于生产。gulp-usemin在我的CSS文件上工作得很好,但没有输出任何与我的javascript文件相关的东西。有人能指出我哪里做错了吗?

我遇到了一个类似于使用gulp-usemin在html文件中重写丑化文件的问题,但提出的解决方案不起作用。

作为参考,我的node版本号是v0.12.1,我的gulp版本号如下:

"devDependencies": {
  "gulp": "^3.9.1",
  "gulp-minify-css": "^1.2.4",
  "gulp-rev": "^7.1.2",
  "gulp-uglify": "^2.0.0",
  "gulp-usemin": "^0.3.24"
}

index.html

<html>
<head>
  <meta charset="utf-8">
  <!-- build:css css/site.css -->
  <link rel="stylesheet" href="stylesheets/style.css" type="text/css" media="all" />
  <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css" type="text/css" media="all" />
  <!-- endbuild -->
  <!-- build:js js/site.js -->
  <script type="text/javascript" src='bower_components/gsap/src/minified/TweenMax.min.js'> </script>
  <script type="text/javascript" src='bower_components/d3/d3.js'> </script>
  <script type="text/javascript" src='bower_components/d3-tip/index.js'> </script>
  <script type="text/javascript" src='bower_components/moment/moment.js'> </script>
  <script type="text/javascript" src='chart.js'> </script>
  <!-- endbuild -->
</head>
<body>
</body>
</html>

gulp file

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var usemin = require('gulp-usemin');
var minifyCss = require('gulp-minify-css');
gulp.task('build', function() {
  return gulp.src(['index.html'])
    .pipe(usemin({
      css: [minifyCss()],
      js: [uglify()]
    }))
  .pipe(gulp.dest('dist/'));
});

运行gulp build后生成dist目录,如下所示

dist/
  css/
    site.css
  index.html
我生成的index。html是
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/site.css" media="all"/>
</head>
<body>
</body>
</html>

有什么明显的我错过了吗?为什么css块工作,而js块静默失败?

gulp-usemin使用的正则表达式不允许在开始的<script>和结束的</script>标记之间有任何空白。它也不允许type="text/javascript"属性。

有关匹配和非匹配的示例,请参阅regex101页。

所以这个:

<!-- build:js js/site.js -->
<script type="text/javascript" src='bower_components/gsap/src/minified/TweenMax.min.js'> </script>
<script type="text/javascript" src='bower_components/d3/d3.js'> </script>
<script type="text/javascript" src='bower_components/d3-tip/index.js'> </script>
<script type="text/javascript" src='bower_components/moment/moment.js'> </script>
<script type="text/javascript" src='chart.js'> </script>
<!-- endbuild -->

应该是这样的:

<!-- build:js js/site.js -->
<script src='bower_components/gsap/src/minified/TweenMax.min.js'></script>
<script src='bower_components/d3/d3.js'></script>
<script src='bower_components/d3-tip/index.js'></script>
<script src='bower_components/moment/moment.js'></script>
<script src='chart.js'></script>
<!-- endbuild -->

相关内容

  • 没有找到相关文章

最新更新