Mar*_*arc 10 frontend sass laravel laravel-blade laravel-mix
在我的项目中,我在 SASS 和 Blade 中使用了一些资源(主要是图像)。另外,我有一些资源只在 SASS 中使用,有些只在 Blade 中使用。
例如,我可以mix('images/logo.png')
在 Blade 文件和background: url('../images/logo.png')
SASS 文件中使用。
至于我的目录结构,我做了以下事情:
- resources
- js
- sass
- images // All images used by Blade, Sass, or both
- fonts
Run Code Online (Sandbox Code Playgroud)
为了编译我的资源并将它们放在public
文件夹中,我使用以下内容webpack.mix.js
:
mix.copy('resources/images/**/*.*', 'public/images');
mix.copy('resources/fonts/**/*.*', 'public/fonts');
mix.version('public/images/**/*.*');
mix.version('public/fonts/**/*.*');
mix.js('resources/js/app.js', 'public/js')
.js('resources/js/vendor.js', 'public/js')
.scripts([ // Old not ES6 JS
'resources/js/tpl/core.min.js'
], 'public/js/core.min.js')
.sass('resources/sass/app.scss', 'public/css')
.sourceMaps()
.version();
Run Code Online (Sandbox Code Playgroud)
结果,我在 app.css 中得到了那个 URL:
background: url(/images/logo.png?0e567ce87146d0353fe7f19f17b18aca);
Run Code Online (Sandbox Code Playgroud)
虽然我在渲染的 HTML 中得到另一个:
src="/images/logo.png?id=4d4e33eae039c367c8e9"
Run Code Online (Sandbox Code Playgroud)
它们被视为 2 种不同的资源,这不是我所期望的......
潜在的解决方法
我发现 SASS 生成的 CSS 文件使用版本化 URL,即使我没有version()
在webpack.mix.js
. 所以我想知道也许我可以使用一些技巧,比如这个:
const sass = require('sass');
// Custom SASS function to get versioned file name
// Uses Mix version md5 hash
const functions = {
'versioned($uri)': function(uri, done) {
uri = uri && uri.getValue() || uri;
const version = File.find(path.join(Config.publicPath, uri)).version();
done(new sass.types.String(`${uri}?id=${version}`));
}
};
mix.sass('resources/sass/all.scss', 'public/css', {
sassOptions: {
functions
}
})
.options({ // Do not process URLs anymore
processCssUrls: false
});
Run Code Online (Sandbox Code Playgroud)
并在 SASS 中使用它,如下所示:
background-image: url(versioned('/images/logo.png'));
Run Code Online (Sandbox Code Playgroud)
但是这个方案有很多缺点,我versioned
每次都不得不使用该功能,我的源代码在没有该webpack.mix.js
功能的其他项目中无法轻松运行,而且我必须编辑我在资源文件夹中使用的每个文件才能使用功能。
其他解决方案?
我认为我的问题的根源可能来自我构建文件的方式,我有一个resources/images
文件夹,其中包含 SASS 使用的图像,但 Blade 也使用它。
SASS 中使用的图像将被复制到,public/images
因为这是 SASS 与 webpack 一起工作的方式,并且这些图像也将被第二次复制,因为我使用了mix.copy()
(因为我需要其他文件位于公共文件夹中,以便在 Blade 中访问/HTML)。
我很确定我在某个地方弄错了,我在互联网上查看了在 Laravel 中使用 SASS 和 Blade 资源的正确方法,但我没有找到任何相关的内容。
也许我应该考虑另一种文件结构?但哪一个?
我发现即使我没有在 webpack.mix.js 中指定 version(),SASS 生成的 CSS 文件也会使用版本化 URL。
在样式表中重写url()
是webpack 的一项功能,它将计算出的文件 MD5 哈希值附加到 URL 中。mix.version()
另一方面,由于这些行,生成了不同的哈希值:
/**
* Read the file's contents.
*/
read() {
return fs.readFileSync(this.path(), 'utf8');
}
/**
* Calculate the proper version hash for the file.
*/
version() {
return md5(this.read()).substr(0, 20);
}
Run Code Online (Sandbox Code Playgroud)
Laravel Mix 将文件作为字符串(而不是缓冲区)读取,对其进行哈希处理并仅提取前 20 个字符。我无法找到一种简单的方法来覆盖这种行为,一个快速而肮脏的解决方法是重新定义函数hash
:
const mix = require('laravel-mix');
let md5 = require('md5');
let fs = require('fs-extra');
Mix.manifest.hash = function (file) {
let f = new File(path.join(Config.publicPath, file));
let hash = md5(fs.readFileSync(f.path()));
let filePath = this.normalizePath(file);
this.manifest[filePath] = filePath + '?' + hash;
return this;
}
Run Code Online (Sandbox Code Playgroud)
更好的方法是扩展 Laravel Mix并定义自己的versionMD5()
方法,您可以从这个扩展中复制一些代码。
归档时间: |
|
查看次数: |
1555 次 |
最近记录: |