Gulp JS类型错误:imagemin不是函数和自动化帮助请求

s5a0g9ez  于 2022-12-08  发布在  Gulp
关注(0)|答案(1)|浏览(235)

我试图建立一个香草JS网站,我想让事情变得简单,所以我尝试了大口(我试图使用维特,但不能得到那么远的大口)。
我所要做的就是编译带有前缀的scss,并使用CssPurge清除未使用的css,然后将其缩小。我还使用npm导入SwiperJ,Jquery和一个js里的东西,我想把它们都编译成一个文件,使它向后兼容。同时压缩jpg和png,然后把它们转换成webp格式。如果可能的话,自动更改源文件夹中html格式的图像文件扩展名,并在dist文件夹中复制它们。
就像这样:

src/index.html --> <img src="picture.png" alt="">
dist/index.html --> <img src="picture.webp" alt="">

下面是我的Gulp文件:

const { src, dest, watch, series} = require('gulp');
  const sass = require('gulp-sass')(require('sass')); 
  const prefix = require('gulp-autoprefixer');
  const minify = require('gulp-clean-css');
  const terser = require('gulp-terser');
  const imagemin = import('gulp-imagemin');
  const imageminOptipng = require('imagemin-optipng')
  const imagewebp = require('gulp-webp');

  //compile, prefix, and min scss
  function compilescss() {
    return src('src/scss/style.scss') 
      .pipe(sass())
      .pipe(prefix('last 2 versions'))
      .pipe(minify())
      .pipe(dest('assets/css')) 
  };

  //optimize and move images
  function optimizeimg() {
    return src('src/images/*.{jpg,png}') 
      .pipe(imagemin([
          imageminOptipng({ quality: 80, progressive: true }),
        imageminOptipng({ optimizationLevel: 2 }),
      ]))
      .pipe(dest('src/images-optimized')) 
  };

  //optimize and move images
  function webpImage() {
    return src('src/images-optimized/*.{jpg,png}') 
      .pipe(imagewebp())
      .pipe(dest('dist/images')) 
  };

  // minify js
  function jsmin(){
    return src('src/js/*.js') 
      .pipe(terser())
      .pipe(dest('dist/js'));
  }

  //watchtask
  function watchTask(){
    watch('src/scss/**/*.scss', compilescss); 
    watch('src/js/*.js', jsmin); 
    watch('src/images/*', optimizeimg); 
    watch('src/images-optimized/*.{jpg,png}', webpImage); 
  }

  // Default Gulp task 
  exports.default = series(
    compilescss,
    jsmin,
    optimizeimg,
    webpImage,
    watchTask
  );

尝试初始化Gulp时出现错误

[00:21:18] 'optimizeimg' errored after 3.22 ms
[00:21:18] TypeError: imagemin is not a function
    at optimizeimg (C:\xampp\htdocs\project\gulpfile.js:22:17)
    at bound (domain.js:416:15)
    at runBound (domain.js:427:12)
    at asyncRunner (C:\xampp\htdocs\project\node_modules\async-done\index.js:55:18)
    at processTicksAndRejections (internal/process/task_queues.js:77:11)
[00:21:18] 'default' errored after 2.97 s

先谢谢你。

nbnkbykc

nbnkbykc1#

这里有简单的打字错误- const imagemin =导入(“吞咽-imagemin”);应该是const imagemin =必需的('gulp-imagemin');

相关问题