Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: less support "globalVars" #1465

Merged
merged 8 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ Specify the size limit of the assets file that needs to be converted to `base64`

### less

- Type: `{ modifyVars?: Record<string, string>, sourceMap?: { sourceMapFileInline?: boolean, outputSourceFiles?: boolean }, math?: "always" | "strict" | "parens-division" | "parens" | "strict-legacy" | number, plugins?: ([string, Record<string, any>]|string)[] }`
- Type: `{ modifyVars?: Record<string, string>, globalVars?: Record<string, string>, sourceMap?: { sourceMapFileInline?: boolean, outputSourceFiles?: boolean }, math?: "always" | "strict" | "parens-division" | "parens" | "strict-legacy" | number, plugins?: ([string, Record<string, any>]|string)[] }`
- Default: `{}`

Specify the less configuration.
Expand All @@ -387,6 +387,10 @@ e.g.
'primary-color': '#1DA57A',
'link-color': '#1DA57A',
},
globalVars: {
'primary-color': '#ffff00',
hack: 'true; @import "your-global-less-file.less";',
},
sourceMap: {
sourceMapFileInline: true,
outputSourceFiles: true,
Expand Down
6 changes: 5 additions & 1 deletion docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ e.g.

### less

- 类型:`{ modifyVars?: Record<string, string>, sourceMap?: { sourceMapFileInline?: boolean, outputSourceFiles?: boolean }, math?: "always" | "strict" | "parens-division" | "parens" | "strict-legacy" | number, plugins?: ([string, Record<string, any>]|string)[] }`
- 类型:`{ modifyVars?: Record<string, string>, globalVars?: Record<string, string>, sourceMap?: { sourceMapFileInline?: boolean, outputSourceFiles?: boolean }, math?: "always" | "strict" | "parens-division" | "parens" | "strict-legacy" | number, plugins?: ([string, Record<string, any>]|string)[] }`
- 默认值:`{}`

指定 less 配置。
Expand All @@ -388,6 +388,10 @@ e.g.
'primary-color': '#1DA57A',
'link-color': '#1DA57A',
},
globalVars: {
'primary-color': '#ffff00',
hack: 'true; @import "your-global-less-file.less";',
},
sourceMap: {
sourceMapFileInline: true,
outputSourceFiles: true,
Expand Down
12 changes: 12 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
mfsu: false,
mako: {},
lessLoader: {
globalVars: {
globalColorInConfig: '#ff0033',
hack: 'true; @import "./var.less";',
overrideInConfig: '#000012',
'@useAtPrefix': '#000013',
},
},
};
56 changes: 56 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

/** @type string */
let content = files["pages_index_tsx-async.css"];

// remove sourcemap
content = content.slice(0, content.indexOf("/*")).trim();

// 分割成单条 rule
// .a{...}
// .b{...}
let cssRules = content
.split(".")
.filter((item) => !!item)
.map((item) => "." + item.replace(/\s/g, ""));

/** @type {Record<string, string>} */
let cssDecls = cssRules.reduce((pre, cur) => {
let splitIndex = cur.indexOf("{");
let selector = cur.slice(0, splitIndex);
let decl = cur.slice(splitIndex);
pre[selector] = decl;

return pre;
}, {});

assert(
cssDecls[".normal"].includes("color:#ff0000;"),
"should available globalVars"
);
assert(
cssDecls[".override"].includes("color:#ff0022;"),
"should available override globalVars"
);
assert(
cssDecls[".inConfig"].includes("color:#ff0033;"),
"should available globalVars in .umirc config"
);
assert(
cssDecls[".overrideInConfig"].includes("color:#000012"),
"should available override globalVars in .umirc config"
);
assert(
cssDecls[".useAtPrefix"].includes("color:#000013"),
"should available use '@' prefix"
);
assert(
cssDecls[".normalA"].includes("color:#ff0000;"),
"should available globalVars in all less files"
);
assert(
cssDecls[".normalB"].includes("color:#ff0000;"),
"should available globalVars in all less files"
);
3 changes: 3 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/pages/a.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.normalA {
color: @primaryColor;
}
3 changes: 3 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/pages/b.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.normalB {
color: @primaryColor;
}
21 changes: 21 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/pages/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@testOverride: #ff0022;

.normal {
color: @primaryColor;
}

.override {
color: @testOverride;
}

.inConfig {
color: @globalColorInConfig;
}

.overrideInConfig {
color: @overrideInConfig;
}

.useAtPrefix {
color: @useAtPrefix;
}
3 changes: 3 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';
import './index.less';
export default () => <div>1</div>;
3 changes: 3 additions & 0 deletions e2e/fixtures.umi/config.less.globalVars/var.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@primaryColor: #ff0000;
@testOverride: #ff0011;
@overrideInConfig: #000011;
2 changes: 2 additions & 0 deletions packages/bundler-mako/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ function checkConfig(opts) {
.difference(Object.keys(opts.config.lessLoader), [
'javascriptEnabled',
'modifyVars',
'globalVars',
'math',
'plugins',
])
Expand Down Expand Up @@ -637,6 +638,7 @@ async function getMakoConfig(opts) {
forkTSChecker: !!forkTSChecker,
less: {
modifyVars: opts.config.lessLoader?.modifyVars || opts.config.theme,
globalVars: opts.config.lessLoader?.globalVars,
sourceMap: getLessSourceMapConfig(normalizedDevtool),
math: opts.config.lessLoader?.math,
plugins: opts.config.lessLoader?.plugins,
Expand Down
1 change: 1 addition & 0 deletions packages/mako/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function build(params: BuildParams) {
// built-in less-loader
let less = lessLoader(null, {
modifyVars: params.config.less?.modifyVars || {},
globalVars: params.config.less?.globalVars,
math: params.config.less?.math,
sourceMap: params.config.less?.sourceMap || false,
plugins: [
Expand Down
1 change: 1 addition & 0 deletions packages/mako/src/lessLoader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createParallelLoader } from './parallelLessLoader';

export interface LessLoaderOpts {
modifyVars: Record<string, string>;
globalVars?: Record<string, string>;
math:
| 'always'
| 'strict'
Expand Down
3 changes: 2 additions & 1 deletion packages/mako/src/lessLoader/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = async function render(param: {
filename: string;
opts: LessLoaderOpts;
}): Promise<{ content: string; type: 'css' }> {
const { modifyVars, math, sourceMap, plugins } = param.opts;
const { modifyVars, globalVars, math, sourceMap, plugins } = param.opts;
const input = fs.readFileSync(param.filename, 'utf-8');

const pluginInstances: Less.Plugin[] | undefined = plugins?.map((p) => {
Expand All @@ -26,6 +26,7 @@ module.exports = async function render(param: {
math,
plugins: pluginInstances,
modifyVars,
globalVars,
sourceMap,
rewriteUrls: 'all',
} as unknown as Less.Options)
Expand Down
1 change: 1 addition & 0 deletions scripts/mako.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function getMakoConfig() {
makoConfig.resolve.alias = makoConfig.resolve.alias || [];
makoConfig.less = {
modifyVars: makoConfig.less?.theme || {},
globalVars: makoConfig.less?.globalVars,
};
makoConfig.plugins = getPlugins();
makoConfig.resolve.alias.forEach((alias) => {
Expand Down
Loading