gpt4 book ai didi

javascript - gulp-usemin 不输出 js 文件

转载 作者:行者123 更新时间:2023-11-30 15:52:50 25 4
gpt4 key购买 nike

我正在尝试使用 gulp-usemin 来捆绑我的 CSSjavascript 文件以用于生产。 gulp-usemin 在我的 CSS 文件上运行良好,但没有输出与我的 javascript 文件相关的任何内容。有人可以指出我做错了什么吗?

我遇到了类似于 uglified files are not rewritten in html file using gulp-usemin 的问题,但建议的解决方案不起作用。

作为引用,我的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 文件

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 block 可以工作,但 js block 却默默地失败了?

最佳答案

regexgulp-usemin 使用开头 <script> 之间不允许有任何空格和关闭</script>标签。它也不允许 type="text/javascript"属性。

参见 this regex101 page一个匹配和不匹配的例子。

所以这样:

<!-- 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 -->

关于javascript - gulp-usemin 不输出 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39045541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com