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...
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 mycomponent = (
<MyComponent
className={styles.nav}
tintColor={jsStyle['@tint-color']}
/>
);
@import "../../../themes/theme.web.less";
@tint-color: grey;
.nav {
background-color: @tint-color;
}
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' },
],
});
As I mentioned in my comment 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With