吞噬中的任务依赖性

发布于 2025-02-02 00:23:40 字数 2057 浏览 3 评论 0原文

我试图使用以下gulpfile.js

const { watch, src, dest, parallel, series } = require('gulp')
const uglify = require('gulp-uglify')
const browserSync = require('browser-sync').create()
const htmlMin = require('gulp-htmlmin')
const cleanCSS = require('gulp-clean-css')
const imagemin = require('gulp-imagemin')
const hash = require('gulp-hash')                   // !!!
const references = require('gulp-hash-references')  // !!!
const del = require('del')
const path = require('path')

const manifestFile = 'asset-manifest.json'
const buildDir = './build'

function serve() {
    browserSync.init({
        server: {
            baseDir: "./"
        }
    })
    watch("./*.html").on('change', browserSync.reload)
    watch("./css/**/*.css").on('change', browserSync.reload)
    watch("./**/*.js").on('change', browserSync.reload)
}

function buildHTML(cb) {
    src('index.html')
        .pipe(references(manifestFile)) // replace file paths in index.html according to the manifest
        .pipe(htmlMin({
            collapseWhitespace: true
        }))
        .pipe(dest(buildDir))
    cb()
}

function buildCSS(cb) {
    src('./css/**/*.css')
        .pipe(cleanCSS())
        .pipe(hash())
        .pipe(dest(path.join(buildDir, 'css')))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'))
    cb()
}


function buildJS(cb) {
    src(['./**/*.js', '!./node_modules/**', '!./gulpfile.js'])
        .pipe(uglify())
        .pipe(hash())
        .pipe(dest('build/'))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'))
    cb()
}

function buildImages(cb) {
    src('./img/**/*.png')
        .pipe(imagemin([
            imagemin.optipng({ optimizationLevel: 7 })
        ]))
        .pipe(dest(path.join(buildDir, 'img')))
    cb()
}


exports.build = series(buildJS, buildCSS, buildHTML) // !!!
exports.default = serve

hash *.css和 *.js文件名,然后将其旧名称替换为build/index.html文件中的哈希名称。但是问题在于,函数“ buildhtml”开始工作早于创建的“资产 - manifest.json”文件。我在做什么错?

There is the following gulpfile.js

const { watch, src, dest, parallel, series } = require('gulp')
const uglify = require('gulp-uglify')
const browserSync = require('browser-sync').create()
const htmlMin = require('gulp-htmlmin')
const cleanCSS = require('gulp-clean-css')
const imagemin = require('gulp-imagemin')
const hash = require('gulp-hash')                   // !!!
const references = require('gulp-hash-references')  // !!!
const del = require('del')
const path = require('path')

const manifestFile = 'asset-manifest.json'
const buildDir = './build'

function serve() {
    browserSync.init({
        server: {
            baseDir: "./"
        }
    })
    watch("./*.html").on('change', browserSync.reload)
    watch("./css/**/*.css").on('change', browserSync.reload)
    watch("./**/*.js").on('change', browserSync.reload)
}

function buildHTML(cb) {
    src('index.html')
        .pipe(references(manifestFile)) // replace file paths in index.html according to the manifest
        .pipe(htmlMin({
            collapseWhitespace: true
        }))
        .pipe(dest(buildDir))
    cb()
}

function buildCSS(cb) {
    src('./css/**/*.css')
        .pipe(cleanCSS())
        .pipe(hash())
        .pipe(dest(path.join(buildDir, 'css')))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'))
    cb()
}


function buildJS(cb) {
    src(['./**/*.js', '!./node_modules/**', '!./gulpfile.js'])
        .pipe(uglify())
        .pipe(hash())
        .pipe(dest('build/'))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'))
    cb()
}

function buildImages(cb) {
    src('./img/**/*.png')
        .pipe(imagemin([
            imagemin.optipng({ optimizationLevel: 7 })
        ]))
        .pipe(dest(path.join(buildDir, 'img')))
    cb()
}


exports.build = series(buildJS, buildCSS, buildHTML) // !!!
exports.default = serve

I'm trying to hash *.css and *.js file names and replace their old names with hashed ones in the build/index.html file. But the problem is that the function "buildHTML" starts working earlier than the "asset-manifest.json" file created. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2025-02-09 00:23:40

您必须返回您的功能创建的流以确保它们以预期顺序运行并捕获任何可能的错误。无需使用回调:


function buildHTML() {
    return src('index.html')
        .pipe(references(manifestFile)) // replace file paths in index.html according to the manifest
        .pipe(htmlMin({
            collapseWhitespace: true
        }))
        .pipe(dest(buildDir));
}

function buildCSS() {
    return src('./css/**/*.css')
        .pipe(cleanCSS())
        .pipe(hash())
        .pipe(dest(path.join(buildDir, 'css')))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'));
}


function buildJS() {
    return src(['./**/*.js', '!./node_modules/**', '!./gulpfile.js'])
        .pipe(uglify())
        .pipe(hash())
        .pipe(dest('build/'))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'));
}

function buildImages() {
    return src('./img/**/*.png')
        .pipe(imagemin([
            imagemin.optipng({ optimizationLevel: 7 })
        ]))
        .pipe(dest(path.join(buildDir, 'img')));
}

You have to return the streams created by your functions to ensure that they run in the expected order and to catch any possible errors. No need to use a callback:


function buildHTML() {
    return src('index.html')
        .pipe(references(manifestFile)) // replace file paths in index.html according to the manifest
        .pipe(htmlMin({
            collapseWhitespace: true
        }))
        .pipe(dest(buildDir));
}

function buildCSS() {
    return src('./css/**/*.css')
        .pipe(cleanCSS())
        .pipe(hash())
        .pipe(dest(path.join(buildDir, 'css')))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'));
}


function buildJS() {
    return src(['./**/*.js', '!./node_modules/**', '!./gulpfile.js'])
        .pipe(uglify())
        .pipe(hash())
        .pipe(dest('build/'))
        .pipe(hash.manifest(manifestFile)) // generate a manifest file
        .pipe(dest('.'));
}

function buildImages() {
    return src('./img/**/*.png')
        .pipe(imagemin([
            imagemin.optipng({ optimizationLevel: 7 })
        ]))
        .pipe(dest(path.join(buildDir, 'img')));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文