You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a function in params, it is not defined for the first time that file is processed, however, on subsequent run (such as the second time using webpack watch), it is defined correctly.
We use the 'thread-loader', and removing that does seem to make this work fine.
However, with it enabled, the first time the file is processed, it produces and error.
I can tell it works on subsequent processing with "watch" because putting a console.log in the function prints to console.
The error is:
includeID is not defined
at eval (eval at ifComparator (node_modules\webpack-preprocessor-loader\dist\filter.js:134:24), <anonymous>:3:1)
As far as I can tell, this is only the case for functions, and normal values are perfectly fine.
Some other things which might affect it:
The webpack config is done in TS not JS.
We use 'thread-loader', and removing that works.
The text was updated successfully, but these errors were encountered:
tech-meppem
changed the title
Functions in params are not defined for the first time
Functions in params are not defined for the first time with thread-loader
Nov 30, 2022
TL;DR thread-loader is not getting along well with some loader options with non-JSON-serializable values, ie functions.
I spent some time reading the source code from thread-loader, not all of them, only to find things getting more confusing. The options you provide to the webpack-preprocessor-loader is JSON.stringifyed in the spawning session of thread-loader, which means the includeID is lost during the process.
So it is clear that the trick you used should not be working at all when combined with thread-loader, hence leading to the error you mentioned at the beginning.
Sorry for the delay but I am afraid it is an implementation limitation from the thread-loader and nothing more I can do for that. Maybe you should take another route if thread-loader is really important for you:
constConfig={units: ["Foo","Baz"]}constdynamicParams=Config.units.reduce((obj,id)=>Object.assign(obj,{[id]: true}),{})// In option...{
params: dynamicParams// If controlling block of codes with `#!if`,
directives: dynamicParams// If controlling single line of code with `#!foo`,}// In code...// #!if foo == trueimport{}from'units/Foo';// #!endif// #!barimport{}from'units/Bar';
OR (not tested, should works)
constConfig={units: ["Foo","Baz"]}// In option...{params: {myPreciousIDs: Config.units}}// In code...// #!if (function(ids){ return ids.includes("foo"); })(myPreciousIDs)import{}from'units/Foo';// #!endif
When using a function in params, it is not defined for the first time that file is processed, however, on subsequent run (such as the second time using webpack watch), it is defined correctly.
We use the 'thread-loader', and removing that does seem to make this work fine.
However, with it enabled, the first time the file is processed, it produces and error.
I can tell it works on subsequent processing with "watch" because putting a console.log in the function prints to console.
The error is:
As far as I can tell, this is only the case for functions, and normal values are perfectly fine.
So, for example, we have this:
And then the file looks like this:
Some other things which might affect it:
The webpack config is done in TS not JS.
We use 'thread-loader', and removing that works.
The text was updated successfully, but these errors were encountered: