Skip to content

Commit

Permalink
Load wasm files as inline base64 data urls
Browse files Browse the repository at this point in the history
Will make the `vips.worker.js` file quite big (~17MB)
  • Loading branch information
swissspidy committed Nov 5, 2024
1 parent 391e94f commit d7d2fe3
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 33 deletions.
2 changes: 0 additions & 2 deletions packages/upload-media/src/store/utils/vips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const createVipsWorker = createWorkerFactory(
);
const vipsWorker = createVipsWorker();

void vipsWorker.setLocation( __webpack_public_path__ );

export async function vipsConvertImageFormat(
id: QueueItemId,
file: File,
Expand Down
10 changes: 0 additions & 10 deletions packages/vips/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,5 @@ _Returns_

- `Promise< { buffer: ArrayBuffer; width: number; height: number; originalWidth: number; originalHeight: number; } >`: Processed file data plus the old and new dimensions.

### setLocation

Dynamically sets the location / public path to use for loading the WASM files.

This is required when loading this module in an inline worker, where globals such as **webpack_public_path** are not available.

_Parameters_

- _newLocation_ `string`: Location, typically a base URL such as "<https://example.com/path/to/js/...">.


<!-- END TOKEN(Autogenerated API docs) -->
16 changes: 1 addition & 15 deletions packages/vips/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ interface EmscriptenModule {
setDelayFunction: ( fn: ( fn: () => void ) => void ) => void;
}

let location = '';

/**
* Dynamically sets the location / public path to use for loading the WASM files.
*
* This is required when loading this module in an inline worker,
* where globals such as __webpack_public_path__ are not available.
*
* @param newLocation Location, typically a base URL such as "https://example.com/path/to/js/...".
*/
export function setLocation( newLocation: string ) {
location = newLocation;
}

let cleanup: () => void;

let vipsInstance: typeof Vips;
Expand All @@ -70,7 +56,7 @@ async function getVips(): Promise< typeof Vips > {
fileName = VipsJxlModule;
}

return location + fileName;
return fileName;

Check warning on line 59 in packages/vips/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vips/src/index.ts#L59

Added line #L59 was not covered by tests
},
preRun: ( module: EmscriptenModule ) => {
// https://github.com/kleisauke/wasm-vips/issues/13#issuecomment-1073246828
Expand Down
7 changes: 1 addition & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ module.exports = {
},
{
test: /\.wasm$/,
type: 'asset/resource',
generator: {
// Use [contenthash:8] to help with cache busting.
filename: '[name].[contenthash:8].wasm',
publicPath: '',
},
type: 'asset/inline',
},
...defaultConfig.module.rules.slice( 1 ),
],
Expand Down

0 comments on commit d7d2fe3

Please sign in to comment.