Closed asdf2014 closed 5 years ago
是 gulp4 的 js 压缩存在问题,去掉了 minify-js 就可以了,,但是不知道本质原因是什么?
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
var imagemin = require('gulp-imagemin');
var babel = require('gulp-babel');
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true
}))
.on('error', function(err) {
console.log('html Error!', err.message);
this.end();
})
.pipe(gulp.dest('./public'))
});
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
gulp.task('minify-js', function() {
return gulp.src(['./public/js/**/*.js', '!./public/js/**/*.{min,mini}.js', '!./public/js/MathJax/**/*.js'])
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest('./public/js'));
});
gulp.task('minify-images', function() {
return gulp.src('./public/images/*.*')
.pipe(imagemin(
[imagemin.gifsicle({'optimizationLevel': 3}),
imagemin.jpegtran({'progressive': true}),
imagemin.optipng({'optimizationLevel': 8}),
imagemin.svgo()],
{'verbose': true}))
.pipe(gulp.dest('./public/images'))
});
// ,'minify-js' 会导致 https://github.com/theme-next/hexo-theme-next/issues/870
gulp.task('default', gulp.parallel('minify-html','minify-css','minify-images', function(done){
done();
}));
https://yuzhouwan.com/js/js.cookie.js
/*!
* JavaScript Cookie v2.2.0
* https://github.com/js-cookie/js-cookie
*
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
* Released under the MIT license
*/
Versions different?
@ivan-nginx Yep, I originally thought the version of js.cookie.js
is the reason. However, it is caused by minify-js
.
So, any suggestions to resolve it?
@ivan-nginx There is no idea at all. I just forbid minify-js
.
Remove gulp-htmlclean and have a try again.
@1v9 Okay, I will give a try.
I think gulp-htmlclean
only affects ./public/**/*.html
, it won't succeed.
I removed .pipe(babel())
and it worked.
Congrats 😁 and I have used the following configuration for a long time, basically no difference with yours.
const gulp = require('gulp');
const babel = require('gulp-babel');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
gulp.task('minify-html', () => {
return gulp.src('./public/**/*.html')
.pipe(htmlmin({
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
useShortDoctype: true,
collapseWhitespace: true,
conservativeCollapse: true,
collapseInlineTagWhitespace: true,
collapseBooleanAttributes: true,
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
}))
.pipe(gulp.dest('./public'));
});
gulp.task('minify-css', () => {
return gulp.src('./public/**/*.css', '!./public/**/*.min.css')
.pipe(cleanCSS({debug: true}, (details) => {
console.log(`${details.name}: ${details.stats.originalSize}`);
console.log(`${details.name}: ${details.stats.minifiedSize}`);
}))
.pipe(gulp.dest('./public'));
});
gulp.task('minify-js', () => {
return gulp.src(['./public/**/*.js', '!./public/**/*.min.js', '!./public/**/*.umd.js'])
.pipe(babel({ presets: ['@babel/env'] }))
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
gulp.task('minify-img', () => {
return gulp.src('./public/image/**/*.*')
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false}
]
})
]))
.pipe(gulp.dest('./public/image'))
});
gulp.task(
'default',
gulp.series(
'minify-html',
'minify-css',
'minify-js',
'minify-img'
)
);
I just found your dependency is outdated, maybe need upgrade.
@1v9 @ivan-nginx Aha, Thanks a lot. I will close this.
@1v9 Yep, I will upgrade the @babel/core
.
@1v9 @asdf2014 guys, can you create something like instructions for Gulp usage in NexT site? Basic configuration, etc., like Deploying.
@asdf2014 js.cookie.js
has been replaced with localstorage in v7.4.0 ~
I agree and want to create new issue
Expected behavior
正常打开网页
Actual behavior
Steps to reproduce the behavior
访问 https://yuzhouwan.com/posts/666/
Node.js and NPM Information
Package dependencies Information
Hexo Information
Hexo version
NexT Information
NexT Version:
NexT Scheme: