gpt4 book ai didi

css - 使用 Gulp 连接/重定位 CSS 文件

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:15 25 4
gpt4 key购买 nike

我正在寻找与 Gulp 一起使用的插件链,它提供:

  • 源映射支持
  • 缩小
  • 串联
  • URL 替换( rebase )以解决重定位/连接

我目前有前四个,但我找不到现有插件的组合也能给我最后一个(URL rebase )。我没有找到任何发出源映射的 URL rebase 插件。

需要说明的是,我的问题是,当我从我的元素开发文件夹中编译多个小 CSS 文件并将它们输出到一个公共(public)文件夹中时,连接导致的移动会破坏相对 URL,例如背景图像的 URL。

编辑:

听起来我目前使用的工具链已经打算解决这个问题了。所以,表面上这就是答案。然而,这引发了另一个问题,即所需的语法应该如何工作。

这个问题理论上有很多重复:

不幸的是,没有真正解释语法的答案,它们只是演示巫术;所以我不知道为什么以下内容不起作用。如果我能解决它,我会回到这里并标记此已接受,以表明此工具链确实可以满足我的需要。

源文件:

/assets
/site
styleInput1.less "url(../site/logo.png)"
logo.png
background.png
/application
styleInput2.less "url(../site/background.png)"

gulp 任务:

gulp.task(filename, function () {
return gulp.src(files)
.pipe(sourcemaps.init())
.pipe(less())
.pipe(minifyCss({ relativeTo: './compiled' }))
.pipe(concat(filename))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./compiled'));
});

输出,带有损坏的 URL。注意多个缺陷。尽管 CSS 相对于所需 Assets 提升了一个目录级别,但已添加了一个额外的父目录(!)。此外,其中一个 URL 的硬 Assets 文件夹名称已更改(!)。很奇怪:

/compiled
styleOutput1.css "url(../../compiled/logo.png)"
styleOutput2.css "url(../../site/background.png)"

我很抱歉继续使用巫术,但这是我的工作 gulp 管:

.pipe(minifyCss({ relativeTo: './compiled', target: './compiled' }))

正确的输出:

/compiled
styleOutput1.css "url(../assets/site/logo.png)"
styleOutput2.css "url(../assets/site/background.png)"

最佳答案

我个人使用 gulp-minify-css 并指定 relativeTo 属性。

gulp.task('css', function() {
gulp.src('./assets/css/main.css')
// Here I specify the relative path to my files
.pipe(minifyCSS({keepSpecialComments: 0, relativeTo: './assets/css/', processImport: true}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('./assets/css/dist/'))
.pipe(notify({
"title": "Should I Go?",
"subtitle": "Gulp Process",
"message": '<%= file.relative %> was successfully minified!',
"sound": "Pop",
"onLast": true
}));
});

如果这对您不起作用,他们还有很多其他参数来 rebase URL:https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-programmatically

值得注意的是:

  • rebase - 设置为 false 以跳过 URL rebase
  • relativeTo - 解析相对@import 规则和 URL 的路径
  • restructuring - 设置为 false 以禁用高级重组优化
  • root - 解析绝对@import 规则和 rebase 相对路径网址

关于css - 使用 Gulp 连接/重定位 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com