Thursday, 2 June 2022

How to integrate own library with WebPack which needs to span WebWorkers and AudioWorklets

Goal I am the author of a JavaScript library which can be consumed through AMD or ESM within various runtime environments (Browser, Node.js, Dev Servers). My library needs to spawn WebWorkers and AudioWorklets using the file it is contained. The library detects in which context it is running and sets up the required stuff for the execution context.

This works fine as long users (user=integrator of my library) do not bring bundlers like WebPack into the game. To spawn the WebWorker and AudioWorklet I need the URL to file in which my library is contained in and I need to ensure the global initialization routines of my library are called.

I would prefer to do as much as possible of the heavy lifting within my library, and not require users to do a very specialized custom setup only for using my library. Offloading this work to them typically backfires instantly and people open issues asking for help on integrating my library into their project.

Problem 1: I am advising my users to ensure my library is put into an own chunk. Users might setup the chunks based on their own setup as long the other libs don't cause any troubles or side effects in the workers. Especially modern web frameworks like React, Angular and Vue.js are typical problem children here, but also people tried to bundle my library with jQuery and Bootstrap. All these libraries cause runtime errors when included in Workers/Worklets.

The chunking is usually done with some WebPack config like:

config.optimization.splitChunks.cacheGroups.alphatab = {
  chunks: 'all',
  name: 'chunk-mylib',
  priority: config.optimization.splitChunks.cacheGroups.defaultVendors.priority + 10,
  test: /.*node_modules.*mylib.*/
};

The big question mylib now has: What is the absolute URL of the generated chunk-mylib.js as this is now the quasi-entrypoint to my library now with bundling and code splitting in place:

  • document.currentScript points usually to some entry point like an app.js and not the chunks.
  • __webpack_public_path__ is pointing to whatever the user sets it to in the webpack config.
  • __webpack_get_script_filename__ could be used if the chunk name would be known but I haven't found a way to get the name of the chunk my library is contained in.
  • import.meta.url is pointing to some absolute file:// url of the original .mjs of my library.
  • new URL(import.meta.url, import.meta.url) causes WebPack to generate an additional .mjs file with some hash. This additional file is not desired and also the generated .mjs contains some additional code breaking its usage in browsers.

I was already thinking to maybe create a custom WebPack plugin which can resolve the chunk my library is contained in so I can use it during runtime. I would prefer to use as much built-in features as possible.

Problem 2: Assuming that problem 1 is solved I could now spawn a new WebWorker and AudioWorklet with the right file. But as my library is wrapped into a WebPack module my initialization code will not be executed. My library only lives in a "chunk" and is not an entry and I wouldn't know that this splitting would allow mylib to run some code after the chunk was loaded by the browser.

Here I am rather clueless. Maybe chunks are not the right way of splitting for this purpose. Maybe some other setup is needed I am not yet aware of that its possible?

Maybe also this could be done best with a custom WebPack plugin.

Visual Representation of the problem: With the proposed chunking rule we get an output as shown in the blocks. Problem 1 is the red part (how to get this URL) and Problem 2 is the orange part (how to ensure my startup logic is called when the background worker/worklet starts)

visual example

Actual Project I want to share my actual project for better understanding of my use case. I am talking about my project alphaTab, a music notation rendering and playback library. On the Browser UI thread (app.js) people integrate the component into the UI and they get an API object to interact with the component. One WebWorker does the layouting and rendering of the music sheet, a second one synthesizes the audio samples for playback and the AudioWorklet sends the buffered samples to the audio context for playback.



from How to integrate own library with WebPack which needs to span WebWorkers and AudioWorklets

Find boundary points of xy coordinates

I have a text file with xy-coordinates called xy.txt.

29.66150677   -98.39336541
29.66150677   -98.39337576
29.66150651   -98.39336541
29.66150328   -98.39337576
29.66150677   -98.39336475
29.66150677   -98.39338611
29.66150393   -98.39338611
29.66150677   -98.39339646
29.66150659   -98.39339646
29.66150677   -98.39339693
29.66151576   -98.39334472
29.66151576   -98.39335506
29.66151511   -98.39334472
29.66151058   -98.39335506
29.66151576   -98.39334322
29.66151576   -98.39336541
29.66151576   -98.39337576
29.66151576   -98.39338611
29.66151576   -98.39339646
29.66151576   -98.39340681
29.66151067   -98.39340681
29.66151576   -98.39341515
29.66152475   -98.39332402
29.66152475   -98.39333437
29.66152443   -98.39332402
29.66151973   -98.39333437
29.66152475   -98.39332332
29.66152475   -98.39334472
29.66152475   -98.39335506
29.66152475   -98.39336541
29.66152475   -98.39337576
29.66152475   -98.39338611
29.66152475   -98.39339646
29.66152475   -98.39340681
29.66152475   -98.39341716
29.66151699   -98.39341716
29.66152475   -98.39342722
29.66153375   -98.39331367
29.66153375   -98.39332402
29.6615302    -98.39331367
29.66153375   -98.3933086
29.66153375   -98.39333437
29.66153375   -98.39334472
29.66153375   -98.39335506
29.66153375   -98.39336541
29.66153375   -98.39337576
29.66153375   -98.39338611
29.66153375   -98.39339646
29.66153375   -98.39340681
29.66153375   -98.39341716
29.66153375   -98.39342751
29.66152507   -98.39342751
29.66153375   -98.39343443
29.66154274   -98.39330332
29.66154274   -98.39331367
29.66153745   -98.39330332
29.66154274   -98.39329625
29.66154274   -98.39332402
29.66154274   -98.39333437
29.66154274   -98.39334472
29.66154274   -98.39335506
29.66154274   -98.39336541
29.66154274   -98.39337576
29.66154274   -98.39338611
29.66154274   -98.39339646
29.66154274   -98.39340681
29.66154274   -98.39341716
29.66154274   -98.39342751
29.66154274   -98.39343786
29.66153992   -98.39343786
29.66154274   -98.3934387
29.66155173   -98.39329297
29.66155173   -98.39330332
29.6615457    -98.39329297
29.66155173   -98.39328644
29.66155173   -98.39331367
29.66155173   -98.39332402
29.66155173   -98.39333437
29.66155173   -98.39334472
29.66155173   -98.39335506
29.66155173   -98.39336541
29.66155173   -98.39337576
29.66155173   -98.39338611
29.66155173   -98.39339646
29.66155173   -98.39340681
29.66155173   -98.39341716
29.66155173   -98.39342751
29.66155173   -98.39343786
29.66155173   -98.39344106
29.66156073   -98.39328262
29.66156073   -98.39329297
29.66155555   -98.39328262
29.66156073   -98.39327744
29.66156073   -98.39330332
29.66156073   -98.39331367
29.66156073   -98.39332402
29.66156073   -98.39333437
29.66156073   -98.39334472
29.66156073   -98.39335506
29.66156073   -98.39336541
29.66156073   -98.39337576
29.66156073   -98.39338611
29.66156073   -98.39339646
29.66156073   -98.39340681
29.66156073   -98.39341716
29.66156073   -98.39342751
29.66156073   -98.39343786
29.66156073   -98.39344196
29.66156972   -98.39327227
29.66156972   -98.39328262
29.66156651   -98.39327227
29.66156972   -98.39326964
29.66156972   -98.39329297
29.66156972   -98.39330332
29.66156972   -98.39331367
29.66156972   -98.39332402
29.66156972   -98.39333437
29.66156972   -98.39334472
29.66156972   -98.39335506
29.66156972   -98.39336541
29.66156972   -98.39337576
29.66156972   -98.39338611
29.66156972   -98.39339646
29.66156972   -98.39340681
29.66156972   -98.39341716
29.66156972   -98.39342751
29.66156972   -98.39343786
29.66156972   -98.393442
29.66157871   -98.39327227
29.66157871   -98.39328262
29.66157871   -98.39326327
29.66157871   -98.39329297
29.66157871   -98.39330332
29.66157871   -98.39331367
29.66157871   -98.39332402
29.66157871   -98.39333437
29.66157871   -98.39334472
29.66157871   -98.39335506
29.66157871   -98.39336541
29.66157871   -98.39337576
29.66157871   -98.39338611
29.66157871   -98.39339646
29.66157871   -98.39340681
29.66157871   -98.39341716
29.66157871   -98.39342751
29.66157871   -98.39343786
29.66157871   -98.39344084
29.66158771   -98.39326192
29.66158771   -98.39327227
29.66158097   -98.39326192
29.66158771   -98.39325788
29.66158771   -98.39328262
29.66158771   -98.39329297
29.66158771   -98.39330332
29.66158771   -98.39331367
29.66158771   -98.39332402
29.66158771   -98.39333437
29.66158771   -98.39334472
29.66158771   -98.39335506
29.66158771   -98.39336541
29.66158771   -98.39337576
29.66158771   -98.39338611
29.66158771   -98.39339646
29.66158771   -98.39340681
29.66158771   -98.39341716
29.66158771   -98.39342751
29.66158771   -98.39343786
29.66158771   -98.39343926
29.66159226   -98.39343786
29.6615967    -98.39326192
29.6615967    -98.39327227
29.6615967    -98.39325426
29.6615967    -98.39328262
29.6615967    -98.39329297
29.6615967    -98.39330332
29.6615967    -98.39331367
29.6615967    -98.39332402
29.6615967    -98.39333437
29.6615967    -98.39334472
29.6615967    -98.39335506
29.6615967    -98.39336541
29.6615967    -98.39337576
29.6615967    -98.39338611
29.6615967    -98.39339646
29.6615967    -98.39340681
29.6615967    -98.39341716
29.6615967    -98.39342751
29.6615967    -98.39343623
29.66160569   -98.39325157
29.66160569   -98.39326192
29.66160564   -98.39325157
29.66160569   -98.39325156
29.66160569   -98.39327227
29.66160569   -98.39328262
29.66160569   -98.39329297
29.66160569   -98.39330332
29.66160569   -98.39331367
29.66160569   -98.39332402
29.66160569   -98.39333437
29.66160569   -98.39334472
29.66160569   -98.39335506
29.66160569   -98.39336541
29.66160569   -98.39337576
29.66160569   -98.39338611
29.66160569   -98.39339646
29.66160569   -98.39340681
29.66160569   -98.39341716
29.66160569   -98.39342751
29.66160569   -98.39343291
29.66161468   -98.39325157
29.66161468   -98.39326192
29.66161468   -98.39324921
29.66161468   -98.39327227
29.66161468   -98.39328262
29.66161468   -98.39329297
29.66161468   -98.39330332
29.66161468   -98.39331367
29.66161468   -98.39332402
29.66161468   -98.39333437
29.66161468   -98.39334472
29.66161468   -98.39335506
29.66161468   -98.39336541
29.66161468   -98.39337576
29.66161468   -98.39338611
29.66161468   -98.39339646
29.66161468   -98.39340681
29.66161468   -98.39341716
29.66161468   -98.39342751
29.66161468   -98.39342823
29.66161592   -98.39342751
29.66162368   -98.39325157
29.66162368   -98.39326192
29.66162368   -98.39324697
29.66162368   -98.39327227
29.66162368   -98.39328262
29.66162368   -98.39329297
29.66162368   -98.39330332
29.66162368   -98.39331367
29.66162368   -98.39332402
29.66162368   -98.39333437
29.66162368   -98.39334472
29.66162368   -98.39335506
29.66162368   -98.39336541
29.66162368   -98.39337576
29.66162368   -98.39338611
29.66162368   -98.39339646
29.66162368   -98.39340681
29.66162368   -98.39341716
29.66162368   -98.39342302
29.66163267   -98.39325157
29.66163267   -98.39326192
29.66163267   -98.39324642
29.66163267   -98.39327227
29.66163267   -98.39328262
29.66163267   -98.39329297
29.66163267   -98.39330332
29.66163267   -98.39331367
29.66163267   -98.39332402
29.66163267   -98.39333437
29.66163267   -98.39334472
29.66163267   -98.39335506
29.66163267   -98.39336541
29.66163267   -98.39337576
29.66163267   -98.39338611
29.66163267   -98.39339646
29.66163267   -98.39340681
29.66163267   -98.39341716
29.66163267   -98.39341722
29.66163275   -98.39341716
29.66164166   -98.39325157
29.66164166   -98.39326192
29.66164166   -98.39324588
29.66164166   -98.39327227
29.66164166   -98.39328262
29.66164166   -98.39329297
29.66164166   -98.39330332
29.66164166   -98.39331367
29.66164166   -98.39332402
29.66164166   -98.39333437
29.66164166   -98.39334472
29.66164166   -98.39335506
29.66164166   -98.39336541
29.66164166   -98.39337576
29.66164166   -98.39338611
29.66164166   -98.39339646
29.66164166   -98.39340681
29.66164166   -98.39341103
29.66164749   -98.39340681
29.66165066   -98.39325157
29.66165066   -98.39326192
29.66165066   -98.39324533
29.66165066   -98.39327227
29.66165066   -98.39328262
29.66165066   -98.39329297
29.66165066   -98.39330332
29.66165066   -98.39331367
29.66165066   -98.39332402
29.66165066   -98.39333437
29.66165066   -98.39334472
29.66165066   -98.39335506
29.66165066   -98.39336541
29.66165066   -98.39337576
29.66165066   -98.39338611
29.66165066   -98.39339646
29.66165066   -98.39340447
29.66165965   -98.39325157
29.66165965   -98.39326192
29.66165965   -98.39324479
29.66165965   -98.39327227
29.66165965   -98.39328262
29.66165965   -98.39329297
29.66165965   -98.39330332
29.66165965   -98.39331367
29.66165965   -98.39332402
29.66165965   -98.39333437
29.66165965   -98.39334472
29.66165965   -98.39335506
29.66165965   -98.39336541
29.66165965   -98.39337576
29.66165965   -98.39338611
29.66165965   -98.39339646
29.66165965   -98.39339783
29.6616615    -98.39339646
29.66166864   -98.39325157
29.66166864   -98.39326192
29.66166864   -98.39324424
29.66166864   -98.39327227
29.66166864   -98.39328262
29.66166864   -98.39329297
29.66166864   -98.39330332
29.66166864   -98.39331367
29.66166864   -98.39332402
29.66166864   -98.39333437
29.66166864   -98.39334472
29.66166864   -98.39335506
29.66166864   -98.39336541
29.66166864   -98.39337576
29.66166864   -98.39338611
29.66166864   -98.39339119
29.66167552   -98.39338611
29.66167764   -98.39325157
29.66167764   -98.39326192
29.66167764   -98.3932437
29.66167764   -98.39327227
29.66167764   -98.39328262
29.66167764   -98.39329297
29.66167764   -98.39330332
29.66167764   -98.39331367
29.66167764   -98.39332402
29.66167764   -98.39333437
29.66167764   -98.39334472
29.66167764   -98.39335506
29.66167764   -98.39336541
29.66167764   -98.39337576
29.66167764   -98.39338455
29.66168663   -98.39325157
29.66168663   -98.39326192
29.66168663   -98.39324315
29.66168663   -98.39327227
29.66168663   -98.39328262
29.66168663   -98.39329297
29.66168663   -98.39330332
29.66168663   -98.39331367
29.66168663   -98.39332402
29.66168663   -98.39333437
29.66168663   -98.39334472
29.66168663   -98.39335506
29.66168663   -98.39336541
29.66168663   -98.39337576
29.66168663   -98.39337791
29.66168954   -98.39337576
29.66169562   -98.39325157
29.66169562   -98.39326192
29.66169562   -98.39324277
29.66169562   -98.39327227
29.66169562   -98.39328262
29.66169562   -98.39329297
29.66169562   -98.39330332
29.66169562   -98.39331367
29.66169562   -98.39332402
29.66169562   -98.39333437
29.66169562   -98.39334472
29.66169562   -98.39335506
29.66169562   -98.39336541
29.66169562   -98.39337127
29.66170356   -98.39336541
29.66170462   -98.39325157
29.66170462   -98.39326192
29.66170462   -98.39324245
29.66170462   -98.39327227
29.66170462   -98.39328262
29.66170462   -98.39329297
29.66170462   -98.39330332
29.66170462   -98.39331367
29.66170462   -98.39332402
29.66170462   -98.39333437
29.66170462   -98.39334472
29.66170462   -98.39335506
29.66170462   -98.39336463
29.66171361   -98.39325157
29.66171361   -98.39326192
29.66171361   -98.39324213
29.66171361   -98.39327227
29.66171361   -98.39328262
29.66171361   -98.39329297
29.66171361   -98.39330332
29.66171361   -98.39331367
29.66171361   -98.39332402
29.66171361   -98.39333437
29.66171361   -98.39334472
29.66171361   -98.39335506
29.66171361   -98.39335799
29.66171758   -98.39335506
29.6617226    -98.39325157
29.6617226    -98.39326192
29.6617226    -98.393242
29.6617226    -98.39327227
29.6617226    -98.39328262
29.6617226    -98.39329297
29.6617226    -98.39330332
29.6617226    -98.39331367
29.6617226    -98.39332402
29.6617226    -98.39333437
29.6617226    -98.39334472
29.6617226    -98.39335135
29.66173159   -98.39334472
29.6617316    -98.39325157
29.6617316    -98.39326192
29.6617316    -98.393242
29.6617316    -98.39327227
29.6617316    -98.39328262
29.6617316    -98.39329297
29.6617316    -98.39330332
29.6617316    -98.39331367
29.6617316    -98.39332402
29.6617316    -98.39333437
29.6617316    -98.39334471
29.66174059   -98.39325157
29.66174059   -98.39326192
29.66174059   -98.393242
29.66174059   -98.39327227
29.66174059   -98.39328262
29.66174059   -98.39329297
29.66174059   -98.39330332
29.66174059   -98.39331367
29.66174059   -98.39332402
29.66174059   -98.39333437
29.66174059   -98.39333807
29.66174561   -98.39333437
29.66174958   -98.39325157
29.66174958   -98.39326192
29.66174958   -98.39324293
29.66174958   -98.39327227
29.66174958   -98.39328262
29.66174958   -98.39329297
29.66174958   -98.39330332
29.66174958   -98.39331367
29.66174958   -98.39332402
29.66174958   -98.39333143
29.66175858   -98.39325157
29.66175858   -98.39326192
29.66176663   -98.39325157
29.66176757   -98.39326192
29.66175858   -98.39324585
29.66175858   -98.39327227
29.66175858   -98.39328262
29.66175858   -98.39329297
29.66175858   -98.39330332
29.66175858   -98.39331367
29.66175858   -98.39332402
29.66175858   -98.39332427
29.6617589    -98.39332402
29.66176757   -98.39327227
29.66177412   -98.39326192
29.66177656   -98.39327227
29.66176757   -98.3932525
29.66176757   -98.39328262
29.66176757   -98.39329297
29.66176757   -98.39330332
29.66176757   -98.39331367
29.66177543   -98.39330332
29.66176974   -98.39331367
29.66176757   -98.3933162
29.66177656   -98.39328262
29.66177775   -98.39327227
29.66177872   -98.39328262
29.66177656   -98.39326599
29.66177656   -98.39329297
29.66177855   -98.39329297
29.66177656   -98.39330028

I read the file using

import numpy as np
xy =  np.loadtxt('xy.txt')
x, y = xy[:, 0], xy[:, 1]

and I can plot the points with

import matplotlib.pyplot as plt
plt.plot(x, y, 'o', color='black', markersize=6)
plt.show()

Visually the data looks as follows:

enter image description here

I want to retrieve the xy-coordinates of the points that form the boundary of the shape. Answers on similar questions suggest to use Concave Hull. With the help of this blog I write the following code:

from scipy import spatial
hull = spatial.ConvexHull(xy, incremental=False, qhull_options='Qt')
hull_indices = hull.vertices

boundary_x = []
boundary_y = []

for i in range(len(hull_indices)):
    index = hull_indices[i]
    boundary_x.append(xy[index, 0].astype('float32'))
    boundary_y.append(xy[index, 1].astype('float32'))

plt.plot(boundary_x, boundary_y, 'o', color='red', markersize=6)
plt.show()

The output looks like

enter image description here

Clearly the red points are not the boundary points. Some are enclosed by other points and they all are different from the original points. How do I define the boundary points in terms of the original points? Please advice



from Find boundary points of xy coordinates

Jetpack Compose: The layout inspector is not showing the menu for recomposition counts

Android Studio Bumblebee Patch 3

From this page, it says that I can see recomposition counts: https://developer.android.com/jetpack/compose/tooling#recomposition-counts

It does not show the menu option to me. Only the device name and "Stop inspector"



from Jetpack Compose: The layout inspector is not showing the menu for recomposition counts