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: support function for loader sunch as css-loader #58 #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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