Skip to content

Commit e6a3b94

Browse files
committed
feat: support function for loader sunch as css-loader #58
1 parent 2e1dfff commit e6a3b94

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

example/webpack.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = (env = {}) => {
1212
poolTimeout: env.watch ? Infinity : 2000,
1313
};
1414
const workerPoolSass = {
15+
workerAllowedFunctions: ['cssLoaderGetLocalIdent'],
1516
workers: +env.threads,
1617
workerParallelJobs: 2,
1718
poolTimeout: env.watch ? Infinity : 2000,
@@ -50,6 +51,17 @@ module.exports = (env = {}) => {
5051
loader: path.resolve(__dirname, '../dist/index.js'),
5152
options: workerPoolSass,
5253
},
54+
{
55+
loader: 'css-loader',
56+
options: {
57+
modules: {
58+
getLocalIdent: function cssLoaderGetLocalIdent() {
59+
console.log('getLocalIdent called');
60+
return null;
61+
},
62+
},
63+
},
64+
},
5365
'css-loader',
5466
'sass-loader',
5567
].filter(Boolean),

src/WorkerPool.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class PoolWorker {
2121
this.activeJobs = 0;
2222
this.onJobDone = onJobDone;
2323
this.id = workerId;
24+
this.allowedFunctions = options.allowedFunctions;
2425

2526
workerId += 1;
2627
// Empty or invalid node args would break the child process
@@ -106,9 +107,29 @@ class PoolWorker {
106107
});
107108
}
108109

110+
getReplacer() {
111+
return (key, value) => {
112+
if (
113+
value instanceof Function &&
114+
Array.isArray(this.allowedFunctions) &&
115+
this.allowedFunctions.includes(value.name)
116+
) {
117+
return {
118+
__serialized_type: 'Function',
119+
fnBody: value.toString(),
120+
args: new Array(value.length).fill('a').map((i, dx) => `${i}${dx}`),
121+
};
122+
}
123+
return replacer(key, value);
124+
};
125+
}
126+
109127
writeJson(data) {
110128
const lengthBuffer = Buffer.alloc(4);
111-
const messageBuffer = Buffer.from(JSON.stringify(data, replacer), 'utf-8');
129+
const messageBuffer = Buffer.from(
130+
JSON.stringify(data, this.getReplacer()),
131+
'utf-8'
132+
);
112133
lengthBuffer.writeInt32BE(messageBuffer.length, 0);
113134
this.writePipe.write(lengthBuffer);
114135
this.writePipe.write(messageBuffer);
@@ -323,6 +344,7 @@ export default class WorkerPool {
323344
this.poolTimeout = options.poolTimeout;
324345
this.workerNodeArgs = options.workerNodeArgs;
325346
this.workerParallelJobs = options.workerParallelJobs;
347+
this.workerAllowedFunctions = options.workerAllowedFunctions;
326348
this.workers = new Set();
327349
this.activeJobs = 0;
328350
this.timeout = null;
@@ -389,6 +411,7 @@ export default class WorkerPool {
389411
{
390412
nodeArgs: this.workerNodeArgs,
391413
parallelJobs: this.workerParallelJobs,
414+
allowedFunctions: this.workerAllowedFunctions,
392415
},
393416
() => this.onJobDone()
394417
);

src/serializer.js

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export function reviver(_key, value) {
1515
if (value.__serialized_type === 'RegExp') {
1616
return new RegExp(value.source, value.flags);
1717
}
18+
// eslint-disable-next-line no-underscore-dangle
19+
else if (value.__serialized_type === 'Function') {
20+
// eslint-disable-next-line no-new-func
21+
return new Function(
22+
...value.args,
23+
` try {
24+
return (${value.fnBody})(${value.args.join(',')})
25+
} catch(err) {
26+
console.error("[Invalid Function call]", err);
27+
}
28+
`
29+
);
30+
}
1831
}
1932

2033
return value;

src/workerPools.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function getPool(options) {
2222
poolTimeout: options.poolTimeout || 500,
2323
poolParallelJobs: options.poolParallelJobs || 200,
2424
poolRespawn: options.poolRespawn || false,
25+
workerAllowedFunctions: options.workerAllowedFunctions || [],
2526
};
2627
const tpKey = JSON.stringify(workerPoolOptions);
2728

0 commit comments

Comments
 (0)