我想使用文件加载器生成版本较少的文件导入
<link rel="stylesheet/less" type="text/css" href="/static/styles.<VERSION/HASH>.less" />
我希望我可以
import '@/assets/styles.less'
在中main.js
。
我只需要版本控制(每次修改styles.less
文件时自动重命名文件)就可以了,我不想在编译期间进行较少的编译,因为我less.modifyVars
在运行时使用。所以我正在使用
import '@/assets/less.min.js'
搜索rel="stylesheet/less"
标签并对其进行编译。当我在index.html中手动添加时
<link rel="stylesheet/less" type="text/css" href="/static/styles.less" />
所有的作品,但我需要版本控制才能持续交付
我试过的是向装载程序添加下一条规则:
{
test: /\.less$/,
loader: 'file-loader',
},
但是在编译过程中我得到:
ERROR Failed to compile with 1 errors 13:05:04
This dependency was not found:
* @/assets/styles.less in ./src/main.js
To install it, you can run: npm install --save @/assets/styles.less
波纹管的配置如您所愿。您可以在github上找到此示例。
src / main.js:
import a from "@/assets/styles.less"
console.log(`You don't need to import less file here at all, only in index.ejs but if you need the reference, there you have it: ${a}`);
资产/样式
// Variables
@link-color: #428bca; // sea blue
@link-color-hover: darken(@link-color, 10%);
// Usage
a,
.link {
color: @link-color;
}
a:hover {
color: @link-color-hover;
}
.widget {
color: #fa0;
background: @link-color;
}
webpack.config.js:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require('path')
module.exports = {
entry: ['./src/main.js'],
plugins: [
new HtmlWebpackPlugin({template: 'src/index.ejs'}),
],
devtool: '#source-map',
resolve: {
extensions: ['.less', '.js'], // not require
alias: {'@': path.resolve(__dirname, 'src')}
},
module: {
rules: [
{
test: /\.less$/,
loader: 'file-loader',
options: {
publicPath: '', // you can override this to put less output in different folder
name: 'static/[name].[ext]?[sha512:hash:base64:6]' // I guess you want the filename to stay the same after it's modified
}
},
],
},
};
src / index.ejs:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet/less" type="text/css" href="<%= require('@/assets/styles.less') %>" />
</head>
<body>
</body>
</html>
请注意,下面的异常提示您找不到文件@/assets/styles.less
。请确保指定了正确的路径,并且如果您使用别名而不是相对路径,则您在中具有正确的别名webpack.config.js
。
错误编译失败,出现1个错误
13:05:04找不到此依赖项:
- ./src/main.js中的@ / assets / styles.less
要安装它,您可以运行:npm install --save @ / assets / styles.less
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句