Gulp Parceljs在src而不是dist中编译css

1bqhqjot  于 2023-05-15  发布在  Gulp
关注(0)|答案(1)|浏览(216)

我在一个HTML项目,这将是集成PHP工作。我已经创建了一个结构的js,scss,字体,图像和清单的。html文件内的src文件夹。但是当运行index.html时,它实际上是从dist文件夹渲染的,这非常复杂。我希望结构保持不变。因此其他开发人员可以相应地使用它。唯一的变化,我需要的是额外的CSS文件夹,使图像的路径是采取相对。
包裹捆绑在dist文件夹内的所有东西使它更脏。我不想捆绑我需要我的工作目录直接structre。这可能吗
我的src文件夹结构。

  • SCSS
  • 字体
  • js
  • 图像
  • index.html

package.json

{
  "name": "project_name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "parcel serve src/index.html",
    "build": "parcel build src/index.html —no-minify"
  },
  "repository": {
    "type": "git",
    "url": "git url....."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "...."
  },
  "staticFiles": {
    "staticPath": ["src"],
    "staticOutDir" : ["public"]
  },
  "homepage": "....,
  "devDependencies": {
    "@parcel/transformer-sass": "^2.8.3",
    "lodash": "^4.17.21",
    "parcel": "^2.8.3",
    "parcel-bundler": "^1.12.5"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "^6.4.0",
    "@popperjs/core": "^2.11.7",
    "bootstrap": "^5.2.3",
    "lodash.escaperegexp": "^4.1.2",
    "parcel-plugin-static-files-copy": "^2.6.0",
    "replace-in-file": "^6.3.5"
  }
}

我需要完全相同的结构在区文件夹以及。我也用过parcel-plugin-static-files-copy npm包。它只是将文件夹从src复制到dist,但不改变style.css中的路径,也不改变复制root中所有文件的dist文件夹行为。请帮帮我

n6lpvg4x

n6lpvg4x1#

虽然这不是问题的答案,但实际上是我想要实现的另一种解决方案。
我切换回 Gulp 修复所有错误。如果有人在这里寻找相同的代码是package.json和gulpfile.js
package.json

{
  "name": "gulpexapp",
  "version": "1.0.0",
  "description": "Example app using Gulp",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "browser-sync": "^2.29.1",
    "gulp": "^4.0.2",
    "gulp-concat": "^2.6.1",
    "gulp-imagemin": "^3.2.0",
    "gulp-sass": "^5.1.0",
    "gulp-uglify": "^2.1.2",
    "sass": "^1.62.1"
  },
  "dependencies": {
    "bootstrap": "^5.2.3"
  }
}

gulpfile.js

const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const uglify = require('gulp-uglify');
const sass = require('gulp-sass')(require('sass'));
// const sass = require('sass');
const concat = require('gulp-concat');
const browserSync = require('browser-sync').create();

// var spritesmith = require('gulp.spritesmith');
/*
  -- TOP LEVEL FUNCTIONS 
  gulp.task - Define tasks
  gulp.src - Point to files to use
  gulp.dest - Points to folder to output
  gulp.watch - Watch files and folders for changes
*/

// Logs Message
gulp.task('message',  gulp.series(async function() {
  return console.log('Gulp is running...');
}));

// Copy All HTML files
gulp.task('copyHtml',  gulp.series(async function() {
  gulp.src('src/*.html')
      .pipe(gulp.dest('dist'))
      .pipe(browserSync.stream());

}));
gulp.task('copyFonts',  gulp.series(async function() {
  gulp.src('src/fonts/*')
      .pipe(gulp.dest('dist/fonts'))
      .pipe(browserSync.stream());

}));
// Optimize Images
gulp.task('imageMin', () =>
    gulp.src('src/images/**/**/*')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'))
);

// Minify JS
// gulp.task('minify',  gulp.series(async function() {
//   gulp.src('src/js/*.js')
//       .pipe(uglify())
//       .pipe(gulp.dest('dist/js'));
// });

// Compile Sass
gulp.task('sass',  gulp.series(async function() {
  gulp.src('src/scss/*.scss')
  .pipe(sass({
    includePaths: ['node_modules/bootstrap/scss/']
  }))
      .pipe(sass({errLogToConsole: true}))
      // .pipe(sass().on('error', sass.logError))
      .pipe(gulp.dest('dist/css'))
      .pipe(browserSync.stream());
}));

// Scripts
gulp.task('scripts',  gulp.series( async function() {
  gulp.src('src/js/*.js')
      .pipe(concat('main.js'))
      // .pipe(uglify())
      .pipe(gulp.dest('dist/js'));
}));

// gulp.task('sprite', function () {
//   var spriteData = gulp.src('images/home/spr/*.png')
//       .pipe(spritesmith({
//           imgName: '../images/home/sprite.png',
//           cssName: '../sass/_sprite.scss' // <<-- just need to change this line
//       }));
//   spriteData.img.pipe(gulp.dest('images'));
//   spriteData.css.pipe(gulp.dest('sass'));
// });




gulp.task('watch',  gulp.series(function(){
  gulp.watch('src/js/*.js', gulp.series("scripts"));
  gulp.watch('src/images/*', gulp.series("imageMin"));
  gulp.watch('src/scss/*.scss',gulp.series("sass"));
  gulp.watch('src/*.html', gulp.series("copyHtml"));
  gulp.watch('src/fonts/*', gulp.series("copyFonts"));
})
);

gulp.task('serve', gulp.series('sass' , function() {
  browserSync.init({
      server: {
      baseDir: '/Users/santoshshah/projects/amlhtml2.0/dist',
        debugInfo: false            
      }
  });
}));
gulp.task('default', gulp.series(gulp.parallel('serve','watch','message', 'copyHtml','copyFonts', 'imageMin', 'sass', 'scripts')));

注意gulpfile.js的最后一行

gulp.task('default', gulp.series(gulp.parallel('serve','watch','message', 'copyHtml','copyFonts', 'imageMin', 'sass', 'scripts')));

在终端中,我们只需要运行gulp,它将自动运行这个任务default,它有一系列的任务,这些任务将从右到左运行,从脚本开始,以serve结束。

相关问题