Created
November 16, 2018 03:36
-
-
Save David-Tsui/8b7bfef225954c96767fba4c3f7ee931 to your computer and use it in GitHub Desktop.
在vue的webpack專案使用less引用全局變數
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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