Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@ima/cli-plugin-less-constants
Advanced tools
Plugin for @ima/cli to enable sharing constants between LESS and JS source files.
Adds preprocessor which converts theme values defined in the JS file, to their LESS variable counterparts.
Can be used to share theme variables between JS and LESS files or even multiple npm packages to allow for easier overrides.
npm install @ima/cli-plugin-less-constants -D
// ./ima.config.js
const { LessConstantsPlugin } = require('@ima/cli-plugin-less-constants');
/**
* @type import('@ima/cli').ImaConfig
*/
module.exports = {
plugins: [
new LessConstantsPlugin({
entry: './app/config/theme.js'
})
],
};
Then export your LESS JS constants from the provided entry file, using the available units
helper functions, imported from the CLI plugin:
// ./app/config/theme.js
import { units, media } from '@ima/cli-plugin-less-constants/units';
export default {
bodyfontSize: units.rem(1),
headerHeight: units.px(120),
bodyWidth: units.vw(100),
greaterThanMobile: media.maxWidthMedia(360, 'screen'),
zIndexes: units.lessMap({
header: 100,
footer: 200,
body: 1,
}),
};
This produces the following output:
// ./build/less-constants/constants.less
@bodyfont-size: 1rem;
@header-height: 120px;
@body-width: 100vw;
@greater-than-mobile: ~"screen and (max-width: 360)";
@z-indexes: {
header: 100;
footer: 200;
body: 1;
}
constants.less
in globalsFinally don't forget to import the generated ./build/less-constants/constants.less
file in your ./app/less/globals.less
to have the variables available in all LESS files automatically without explicit import.
// ./app/less/globals.less
@import "../../build/less-constants/constants.less";
You can verify that your constants are actually used in your Less
files. The verify
option should include all directories that contain your Less files.
// ./ima.config.js
const { LessConstantsPlugin } = require('@ima/cli-plugin-less-constants');
/**
* @type import('@ima/cli').ImaConfig
*/
module.exports = {
plugins: [
new LessConstantsPlugin({
entry: './app/config/theme.js'
verify: ['./app']
})
],
};
Since every unit returns Unit
object, you can always access it's value through the .valueOf()
method or use the CSS interpreted value by calling .toString()
.
import { headerHeight } from 'app/config/theme.js';
export default function ThemeComponent({ children, title, href }) {
return (
<div>
Header height has an absolute value of: {headerHeight.valueOf()} {/* 120 */},
while it's CSS value is: {headerHeight.toString()} {/* 120px */}
</div>
);
}
Info: The constants are generated only in the
preProcess
which runs just ones before the compilation. So make sure to restart the built manually, when you add any new constants, to allow for the regeneration of theconstants.less
file.
new LessConstantsPlugin(options: {
entry: string;
output?: string;
});
string
Path to the LESS constants JS file.
string
Optional custom output path, defaults to ./build/less-constants/constants.less
.
The plugin provides unit functions for almost every unit available + some other helpers. Each helper returns Unit
object with following interface:
export interface Unit {
valueOf: () => string;
toString: () => string;
}
em
, ex
, ch
, rem
, lh
, rlh
, vw
, vh
, vmin
, vmax
, vb
, vi
, svw
, svh
, lvw
, lvh
, dvw
, dvh
, cm
, mm
, Q
, inches
, pc
, pt
, px
, percent
.hex
, rgb
, rgba
, hsl
, hsla
.maxWidthMedia
, minWidthMedia
, minAndMaxWidthMedia
, maxHeightMedia
, minHeightMedia
.lessMap
can be used to group together similar values in an "object-like" value.If you're missing any additional helpers, you can always define your own, either custom ones (as long as they adhere to the Unit
interface) or you can use the following helper:
import { asUnit } from '@ima/cli-plugin-less-constants/units';
function asUnit(
unit: string,
parts: (string | number)[],
template = '${parts}${unit}'
): Unit {
return {
__propertyDeclaration: true,
valueOf(): string {
return parts.length === 1 ? parts[0].toString() : this.toString();
},
toString(): string {
return template
.replace('${parts}', parts.join(','))
.replace('${unit}', unit);
},
};
}
For more information, take a look at the IMA.js documentation.
FAQs
Plugin for @ima/cli to enable sharing constants between LESS and JS source files.
We found that @ima/cli-plugin-less-constants demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.