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

fix: use relative assets url in overrides.less #11843

Merged
merged 4 commits into from
Nov 10, 2023
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
5 changes: 5 additions & 0 deletions packages/preset-umi/fixtures/overrides/less/index.less
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
@import "foo/foo.less";
@import "./styles/other.less";

@red: green;
.a {
aaa: @red;
bbb: @primary-color;
}

.img {
border-image: url('./assets/img.png');
}
3 changes: 3 additions & 0 deletions packages/preset-umi/fixtures/overrides/less/styles/other.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.img {
background: url('../assets/img.png');
}
2 changes: 1 addition & 1 deletion packages/preset-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"enhanced-resolve": "5.9.3",
"fast-glob": "3.2.12",
"html-webpack-plugin": "5.5.0",
"less-plugin-resolve": "1.0.0",
"less-plugin-resolve": "1.0.2",
"path-to-regexp": "1.7.0",
"postcss": "^8.4.21",
"postcss-prefix-selector": "1.16.0",
Expand Down

This file was deleted.

17 changes: 17 additions & 0 deletions packages/preset-umi/src/features/overrides/compileLess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { execa } from '@umijs/utils';
import { join } from 'path';

test('normal', async () => {
try {
const result = await execa.execa(
'tsx',
[join(__dirname, './compileLess.testScript.ts')],
{
stdio: 'inherit',
},
);
expect(result.exitCode).toEqual(0);
} catch {
throw new Error('compileLess.testScript.ts failed');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from 'fs';
import { join } from 'path';
import { compileLess } from './compileLess';

const fixturesDir = join(__dirname, '../../../fixtures');

const run = async () => {
const filePath = join(fixturesDir, 'overrides/less/index.less');
const targetPath = join(fixturesDir, 'overrides/less/output/index.css');
const modifyVars = {
'primary-color': '#1DA57A',
};
const alias = {
barbar: join(filePath, '../node_modules/bar'),
};
const content = fs.readFileSync(filePath, 'utf-8');
const result = await compileLess({
lessContent: content,
filePath,
modifyVars,
alias,
targetPath,
});
const expectContained = `
.bar {
color: red;
}
.foo {
color: red;
}
.img {
background: url('../assets/img.png');
}
.a {
aaa: green;
bbb: #1DA57A;
}
.img {
border-image: url('../assets/img.png');
}
`.trim();
if (!result.includes(expectContained)) {
throw new Error(
`result not contains the expect content, result: ${result}`,
);
}
};

run().catch((e) => {
console.error(e);
process.exit(1);
});
31 changes: 20 additions & 11 deletions packages/preset-umi/src/features/overrides/compileLess.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import less from '@umijs/bundler-utils/compiled/less';
import { winPath } from '@umijs/utils';

export async function compileLess(
lessContent: string,
filePath: string,
modifyVars: Record<string, string> = {},
alias: Record<string, string> = {},
) {
export async function compileLess(opts: {
lessContent: string;
filePath: string;
modifyVars: Record<string, string>;
alias: Record<string, string>;
targetPath: string;
}) {
const {
lessContent,
filePath,
modifyVars = {},
alias = {},
targetPath,
} = opts;
const resolvePlugin = new (require('less-plugin-resolve') as any)({
aliases: alias,
urlRewriteTargetPath: winPath(targetPath),
});
const result = await less.render(lessContent, {
filename: filePath,
plugins: [
new (require('less-plugin-resolve') as any)({
aliases: alias,
}),
],
plugins: [resolvePlugin],
javascriptEnabled: true,
modifyVars,
});
Expand Down
15 changes: 9 additions & 6 deletions packages/preset-umi/src/features/overrides/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ export default (api: IApi) => {
const filePath = api.appData.overridesCSS[0];
let content = readFileSync(filePath, 'utf-8');
if (content === cachedContent) return;
const subPath = 'core/overrides.css';
const targetPath = join(api.paths.absTmpPath!, subPath);
const isLess = filePath.endsWith('.less');
if (isLess) {
content = await compileLess(
content,
content = await compileLess({
lessContent: content,
filePath,
{
modifyVars: {
...api.config.theme,
...api.config.lessLoader?.modifyVars,
},
api.config.alias,
);
alias: api.config.alias,
targetPath,
});
}
content = await transform(content, filePath);
api.writeTmpFile({
path: 'core/overrides.css',
path: subPath,
content: content || '/* empty */',
noPluginDir: true,
});
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading