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: disable parallel less loader when on linux ant node version < 20.12.0 #1184

Merged
merged 7 commits into from
May 21, 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
2 changes: 1 addition & 1 deletion packages/mako/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
"@umijs/mako-linux-x64-gnu": "0.4.17"
},
"repository": "[email protected]:umijs/mako.git"
}
}
19 changes: 16 additions & 3 deletions packages/mako/src/lessLoader/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import url from 'url';
import { compile, terminatePool } from './parallelLessLoader';
/**
* When on platform linux ant node version is lower then 20,
* the worker pool may exit unexpectedly, we need to disable it.
* This may be is problem of nodejs, may related issue: https://github.com/nodejs/node/issues/51129.
*/
const [NodeMajorVersion, NodeMirrorVersion] = process.versions.node
.split('.')
.map((v) => parseInt(v));
const DisableParallelLess =
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
process.platform === 'linux' &&
(NodeMajorVersion < 20 ||
(NodeMajorVersion === 20 && NodeMirrorVersion < 12));

export interface LessLoaderOpts {
modifyVars: Record<string, string>;
Expand Down Expand Up @@ -32,7 +43,9 @@ function lessLoader(fn: Function | null, opts: LessLoaderOpts) {
return;
}
if (pathname?.endsWith('.less')) {
return compile(pathname, opts);
return DisableParallelLess
? require('./render').render(pathname, opts)
: require('./parallelLessLoader').render(pathname, opts);
} else {
// TODO: remove this
fn && fn(filePath);
Expand All @@ -41,7 +54,7 @@ function lessLoader(fn: Function | null, opts: LessLoaderOpts) {
}

lessLoader.terminate = () => {
terminatePool();
!DisableParallelLess && require('./parallelLessLoader').terminatePool();
};

export { lessLoader };
42 changes: 2 additions & 40 deletions packages/mako/src/lessLoader/lessLoader.worker.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
import fs from 'fs';
import less from 'less';
import workerpool from 'workerpool';
import { LessLoaderOpts } from '.';
import { render } from './render';

const lessLoader = {
render: async function (
filePath: string,
opts: LessLoaderOpts,
): Promise<string> {
const { modifyVars, math, sourceMap, plugins } = opts;
const input = fs.readFileSync(filePath, 'utf-8');

const pluginInstances: Less.Plugin[] | undefined = plugins?.map((p) => {
if (Array.isArray(p)) {
const pluginModule = require(p[0]);
const PluginClass = pluginModule.default || pluginModule;
return new PluginClass(p[1]);
} else {
return require(p);
}
});

const result = await less
.render(input, {
filename: filePath,
javascriptEnabled: true,
math,
plugins: pluginInstances,
modifyVars,
sourceMap,
rewriteUrls: 'all',
} as unknown as Less.Options)
.catch((err) => {
throw new Error(err.toString());
});

return result.css;
},
};

workerpool.worker(lessLoader);
workerpool.worker({ render });
6 changes: 3 additions & 3 deletions packages/mako/src/lessLoader/parallelLessLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export function terminatePool() {
pool = undefined;
}

export async function compile(
export async function render(
filePath: string,
opts: LessLoaderOpts,
): Promise<{ content: string; type: string }> {
createPool();

const css = await pool!.exec('render', [filePath, opts]);
const res = await pool!.exec('render', [filePath, opts]);

return { content: css, type: 'css' };
return res;
}
37 changes: 37 additions & 0 deletions packages/mako/src/lessLoader/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from 'fs';
import less from 'less';
import { LessLoaderOpts } from '.';

export async function render(
filePath: string,
opts: LessLoaderOpts,
): Promise<{ content: string; type: 'css' }> {
const { modifyVars, math, sourceMap, plugins } = opts;
const input = fs.readFileSync(filePath, 'utf-8');

const pluginInstances: Less.Plugin[] | undefined = plugins?.map((p) => {
if (Array.isArray(p)) {
const pluginModule = require(p[0]);
const PluginClass = pluginModule.default || pluginModule;
return new PluginClass(p[1]);
} else {
return require(p);
}
});

const result = await less
.render(input, {
filename: filePath,
javascriptEnabled: true,
math,
plugins: pluginInstances,
modifyVars,
sourceMap,
rewriteUrls: 'all',
} as unknown as Less.Options)
.catch((err) => {
throw new Error(err.toString());
});

return { content: result.css, type: 'css' };
}
Loading