Closed fireflysemantics closed 3 years ago
The errors you're seeing indicate that there are no DOM typings available in the web worker's compilation unit. I believe that is expected; the web worker is compiled using its own tsconfig.worker.json
. It appears that the import of @fireflysemantics/csv
is reaching into @angular/cdk
(or some of its entry-points) through (transitive) imports, but this library is not meant to be compiled for web workers/without DOM typings so you get the errors.
I can't tell how this could have worked before; I believe this should be failing as it currently does. Perhaps you recently updated @fireflysemantics/csv
in a way where it started to depend on @angular/cdk
. Or perhaps the tsconfig.worker.json
was modified. I can't possibly tell without a reproduction. I understand that the library is private and can't be used for reproduction, so then the quest becomes to reproduce this with a minimal library instead. That is the recommendation for reproductions either way, as the process of creating such minimal reproductions will likely show exactly what scenario is needed to break it.
@JoostK That makes sense. I put components as well in the library, and so now the library depends on CDK.
However the readCSV
function that I'm importing into the web worker does not depend on CDK (Or have any CDK imports in it. I did the following refactoring of the worker and now it compiles fine. Basically I just moved the pieces from the library directly into the worker:
/// <reference lib="webworker" />
import { parse, ParseResult } from "papaparse"
addEventListener('message', ({ data }) => {
const result: ParseResult<any> = readCSV(data)
postMessage(result);
});
export function readCSV(csv: string, config = CSV_DEFAULT_CONFIG): ParseResult<any> {
return parse(csv.trim(), config);
}
/**
* Default configuration
* commonly used when we parse
* CSV.
*
* It maps the rows to javascript
* objects and uses commas as the delimiter.
*/
export const CSV_DEFAULT_CONFIG = {
delimiter: ",",
header: true,
dynamicTyping: true,
skipEmptyLines: true,
transformHeader: function (h) {
return h.trim();
}
}
/*
import { ParseResult } from "papaparse";
import { readCSV } from '@fireflysemantics/csv';
addEventListener('message', ({ data }) => {
const result: ParseResult<any> = readCSV(data)
postMessage(result);
});
*/
I guess I could break out the components from the @fireflysemantics/csv
library ... but It seems a bit odd to have to do this just to get a worker to compile... It seems like we should be able to import code into workers as long as the code being imported can be tree shaken and the part that's being used does not have any of the disallowed constructs.
Would it be possible for the worker compilation to just compile the way everything else compiles...and then if there is a problem the browser should create the error and we can see it in the web developer console?
This is really good to know though. I'll just break up the library for now. Thanks so much for the input!
Closing as this is working as expected.
A library entrypoint is a single compilation unit, if you import a single function from a library the entire library needs to be compatible with the current compilation type whether it’s a Web Worker, Browser or Node.Js.
The error happens when TypeScript compiler tries to valid the type definitions of the depend library, which happens way before tree-shaking and any JavaScript optimizations.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.
🐞 bug report
Is this a regression?
It works in this version:
Description
I have a web worker that looks like this. This used to work fine in previous Angular versions:
If I comment out most of the worker:
Then it builds fine. If I comment in the line:
Then all these errors are generated:
The
readCsv()
Method is exported from an Angular Package Format library an looks like this:🔬 Minimal Reproduction
I can't reproduce this because it depends on an internal library.
🌍 Your Environment
Angular Version: