How to build a less compilation chain (gulp -style) in Webpack v3?

老子叫甜甜 提交于 2019-12-10 15:01:41

问题


For a new project I am bound to keep things webpack-only, and thus need to find a way to efficiently compile our stylesheets. Basically in a very gulh-ish way:

  • gather all less-files including glob-patterns like src/**/*.less (css order may be arbitrary)
  • also allow import of css files like, say ../node_modules/vendor/**/*.css or 3rdparty/master.less
    • (If I have to setup a bogus.js entry point for that, fine...)

And with all of that, a typical gulp workflow:

  • transpile less to css
  • merge (concat) less and css
  • have cssnano do its optimization job, with specific css nano options like e.g. orderedValues: false, normalizeWhitespace: true ...
  • write styles.css as final output

And of course:

  • have source-maps survive that chain for a styles.css.map
  • efficient watching and the usual lazy/incremental compilation (as gulp and webpack have it)

Something I do not need is css modules (css imported into node modules for 'scoped' use, coming out as hash-scoped selectors...)

How can a 'typical' less|css-processing toolchain be done in Webpack?

This SO question has my first attempt where I got stuck in config hell right in the middle after combining...


considerations so far (helpful or not)

I know, to webpack, any ressource including css or even font and images is a "module"... Rather than merging my css 'modules' with with actual js (only to later painstakingly separate them again later again), it might be wiser, to have an entry point cssstub.js in parallel – also known as multi-compiler mode.

But that's really, where my wisdom ends... doing a sequence of $things on a set of files in webpack seems really hard (unless it's a connected javascript module graph). I did find something on globbing, but that's not enough (merge css, cssnano,...) and mostly I simply can't glue the pieces together...


回答1:


I have used gulp to build less and create corresponding maps like below:

First step compiles less and generated css in tmp folder

gulp.task('compile-less', function () {
    gulp.src('./*.less') // path to your file
    .pipe(less().on('error', function(err) {
        console.log(err);
    }))
    .pipe(gulp.dest('./tmp/'));
});

Second step minifies generated css and create map files

gulp.task('build-css', ['clean'], function() {
    return gulp.src('./tmp/**/*.css')
        .pipe(sourcemaps.init())
        .pipe(cachebust.resources())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('./compiled/css'));
});

If you want you can add additional step to conact generated css.



来源:https://stackoverflow.com/questions/46323410/how-to-build-a-less-compilation-chain-gulp-style-in-webpack-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!