admin管理员组文章数量:1305082
How can I read less variables in javascript like less-vars-to-js?
I'm working on a React project(webpack2、less etc), but not SSR(node environment),so I can't use fs
or node-glob
module.
Some people suggest me writting a webpack loader myself :( i'm not really familiar with that... And I already used less-loader...
javascript
import lessToJs from 'less-vars-to-js';
import styles from './style.less';
const jsStyle = lessToJs(styles); => Uncaught TypeError: sheet.match is not a function
const myponent = (
<MyComponent
className={styles.nav}
tintColor={jsStyle['@tint-color']}
/>
);
less
@import "../../../themes/theme.web.less";
@tint-color: grey;
.nav {
background-color: @tint-color;
}
webpack
config.module.rules.push({
test: /\.less$/,
exclude: /node_modules/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
},
},
{ loader: 'postcss-loader' },
{ loader: 'less-loader' },
],
});
How can I read less variables in javascript like less-vars-to-js?
I'm working on a React project(webpack2、less etc), but not SSR(node environment),so I can't use fs
or node-glob
module.
Some people suggest me writting a webpack loader myself :( i'm not really familiar with that... And I already used less-loader...
javascript
import lessToJs from 'less-vars-to-js';
import styles from './style.less';
const jsStyle = lessToJs(styles); => Uncaught TypeError: sheet.match is not a function
const myponent = (
<MyComponent
className={styles.nav}
tintColor={jsStyle['@tint-color']}
/>
);
less
@import "../../../themes/theme.web.less";
@tint-color: grey;
.nav {
background-color: @tint-color;
}
webpack
config.module.rules.push({
test: /\.less$/,
exclude: /node_modules/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
},
},
{ loader: 'postcss-loader' },
{ loader: 'less-loader' },
],
});
Share
Improve this question
edited May 19, 2017 at 7:39
Kim
asked May 19, 2017 at 7:15
KimKim
5,4457 gold badges41 silver badges64 bronze badges
2
- 4 Under normal circumstances, the LESS will have been piled to CSS before being sent to the browser, at which point the LESS variables no longer exist, so you can't read them. – Quentin Commented May 19, 2017 at 7:17
- 1 Implement a custom webpack loader that extracts variables from less files. – Yury Tarabanko Commented May 19, 2017 at 7:18
1 Answer
Reset to default 7As I mentioned in my ment you could implement custom loader. Something like this (haven't tested)
//utils/less-var-loader.js
const lessToJs = require('less-vars-to-js')
module.exports = function(content) {
return `module.exports = ${JSON.stringify(lessToJs(content))}`
}
and then
import * as styles from '!!./utils/less-var-loader!./style.less'
Double bang !!
to ignore preconfigured loaders.
本文标签: reactjsHow can I read Less variables in JavascriptStack Overflow
版权声明:本文标题:reactjs - How can I read Less variables in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741797595a2398028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论