Skip to content

Instantly share code, notes, and snippets.

@David-Tsui
Created November 16, 2018 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save David-Tsui/8b7bfef225954c96767fba4c3f7ee931 to your computer and use it in GitHub Desktop.
Save David-Tsui/8b7bfef225954c96767fba4c3f7ee931 to your computer and use it in GitHub Desktop.
在vue的webpack專案使用less引用全局變數
function getLessVariables(file) {
var themeContent = fs.readFileSync(file, 'utf-8')
var variables = {}
themeContent.split('\n').forEach(function (item) {
if (item.indexOf('//') > -1 || item.indexOf('/*') > -1) {
return
}
var _pair = item.split(':')
if (_pair.length < 2) return;
var key = _pair[0].replace('\r', '').replace('@', '')
if (!key) return;
var value = _pair[1].replace(';', '').replace('\r', '').replace(/^\s+|\s+$/g, '')
variables[key] = value
})
return variables
}
module.exports = {
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader",
options: {
loaders: {
css: "vue-style-loader!css-loader",
less: [
"vue-style-loader",
"css-loader",
{
loader: "less-loader",
options: {
globalVars: getLessVariables("./src/styles/global.less")
}
}
]
}
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment