Skip to content

Commit

Permalink
feat: support function for loader sunch as css-loader #58
Browse files Browse the repository at this point in the history
  • Loading branch information
BUPTlhuanyu committed Dec 30, 2021
1 parent 2e1dfff commit e6a3b94
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = (env = {}) => {
poolTimeout: env.watch ? Infinity : 2000,
};
const workerPoolSass = {
workerAllowedFunctions: ['cssLoaderGetLocalIdent'],
workers: +env.threads,
workerParallelJobs: 2,
poolTimeout: env.watch ? Infinity : 2000,
Expand Down Expand Up @@ -50,6 +51,17 @@ module.exports = (env = {}) => {
loader: path.resolve(__dirname, '../dist/index.js'),
options: workerPoolSass,
},
{
loader: 'css-loader',
options: {
modules: {
getLocalIdent: function cssLoaderGetLocalIdent() {
console.log('getLocalIdent called');
return null;
},
},
},
},
'css-loader',
'sass-loader',
].filter(Boolean),
Expand Down
25 changes: 24 additions & 1 deletion src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PoolWorker {
this.activeJobs = 0;
this.onJobDone = onJobDone;
this.id = workerId;
this.allowedFunctions = options.allowedFunctions;

workerId += 1;
// Empty or invalid node args would break the child process
Expand Down Expand Up @@ -106,9 +107,29 @@ class PoolWorker {
});
}

getReplacer() {
return (key, value) => {
if (
value instanceof Function &&
Array.isArray(this.allowedFunctions) &&
this.allowedFunctions.includes(value.name)
) {
return {
__serialized_type: 'Function',
fnBody: value.toString(),
args: new Array(value.length).fill('a').map((i, dx) => `${i}${dx}`),
};
}
return replacer(key, value);
};
}

writeJson(data) {
const lengthBuffer = Buffer.alloc(4);
const messageBuffer = Buffer.from(JSON.stringify(data, replacer), 'utf-8');
const messageBuffer = Buffer.from(
JSON.stringify(data, this.getReplacer()),
'utf-8'
);
lengthBuffer.writeInt32BE(messageBuffer.length, 0);
this.writePipe.write(lengthBuffer);
this.writePipe.write(messageBuffer);
Expand Down Expand Up @@ -323,6 +344,7 @@ export default class WorkerPool {
this.poolTimeout = options.poolTimeout;
this.workerNodeArgs = options.workerNodeArgs;
this.workerParallelJobs = options.workerParallelJobs;
this.workerAllowedFunctions = options.workerAllowedFunctions;
this.workers = new Set();
this.activeJobs = 0;
this.timeout = null;
Expand Down Expand Up @@ -389,6 +411,7 @@ export default class WorkerPool {
{
nodeArgs: this.workerNodeArgs,
parallelJobs: this.workerParallelJobs,
allowedFunctions: this.workerAllowedFunctions,
},
() => this.onJobDone()
);
Expand Down
13 changes: 13 additions & 0 deletions src/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export function reviver(_key, value) {
if (value.__serialized_type === 'RegExp') {
return new RegExp(value.source, value.flags);
}
// eslint-disable-next-line no-underscore-dangle
else if (value.__serialized_type === 'Function') {
// eslint-disable-next-line no-new-func
return new Function(
...value.args,
` try {
return (${value.fnBody})(${value.args.join(',')})
} catch(err) {
console.error("[Invalid Function call]", err);
}
`
);
}
}

return value;
Expand Down
1 change: 1 addition & 0 deletions src/workerPools.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function getPool(options) {
poolTimeout: options.poolTimeout || 500,
poolParallelJobs: options.poolParallelJobs || 200,
poolRespawn: options.poolRespawn || false,
workerAllowedFunctions: options.workerAllowedFunctions || [],
};
const tpKey = JSON.stringify(workerPoolOptions);

Expand Down

0 comments on commit e6a3b94

Please sign in to comment.