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

Try vips package as esm module #754

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,22 +494,29 @@ function enqueue_block_editor_assets(): void {
$asset['dependencies'] = $asset['dependencies'] ?? [];
$asset['version'] = $asset['version'] ?? '';

// wp_register_script_module(
// 'media-experiments-vips',
// plugins_url( 'build/vips.js', __DIR__ ),
// [],
// $asset['version']
// );

wp_enqueue_script(
'media-experiments',
plugins_url( 'build/media-experiments.js', __DIR__ ),
$asset['dependencies'],
$asset['version'],
array(
[
'strategy' => 'defer',
)
]
);

wp_set_script_translations( 'media-experiments', 'media-experiments' );

wp_enqueue_style(
'media-experiments-editor',
plugins_url( 'build/media-experiments.css', __DIR__ ),
array( 'wp-components' ),
[ 'wp-components' ],
$asset['version']
);

Expand Down
2 changes: 2 additions & 0 deletions packages/vips/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"engines": {
"node": ">=20"
},
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"exports": {
Expand Down
6 changes: 5 additions & 1 deletion packages/vips/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import Vips from 'wasm-vips';
import type Vips from 'wasm-vips';

// @ts-expect-error
// eslint-disable-next-line import/no-unresolved
Expand Down Expand Up @@ -60,6 +60,10 @@ async function getVips(): Promise< typeof Vips > {
return vipsInstance;
}

const { default: Vips } = await import(
/* webpackChunkName: "chunk-wasm-vips" */ 'wasm-vips'
);

vipsInstance = await Vips( {
locateFile: ( fileName: string ) => {
if ( fileName.endsWith( 'vips.wasm' ) ) {
Expand Down
50 changes: 44 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const { resolve, dirname, basename } = require( 'node:path' );
const { DefinePlugin } = require( 'webpack' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const { WebWorkerPlugin } = require( '@shopify/web-worker/webpack' );

/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { hasBabelConfig, hasArgInCLI } = require( '@wordpress/scripts/utils' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );

const isProduction = process.env.NODE_ENV === 'production';
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction;
Expand All @@ -30,7 +32,7 @@ const mediapipeCdnUrl = `https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmenta
const pdfJsCdnUrl = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${ pdfJsVersion }/build/pdf.worker.mjs`;
const ffmpegCdnUrl = `https://cdn.jsdelivr.net/npm/@ffmpeg/core@${ ffmpegVersion }/dist/ffmpeg-core.js`;

module.exports = {
const regular = {
...defaultConfig,
entry: {
'media-experiments': resolve(
Expand All @@ -50,10 +52,6 @@ module.exports = {
globalObject: 'self', // This is the default, but required for @shopify/web-worker.
},
resolve: {
// Ensure "require" has a higher priority when matching export conditions.
// https://webpack.js.org/configuration/resolve/#resolveconditionnames
// https://github.com/kleisauke/wasm-vips/issues/50#issuecomment-1664118137
conditionNames: [ 'require', 'import' ],
extensions: [ '.jsx', '.ts', '.tsx', '...' ],
fallback: {
crypto: false,
Expand Down Expand Up @@ -119,8 +117,17 @@ module.exports = {
},
plugins: [
...defaultConfig.plugins.filter(
( plugin ) => ! ( plugin instanceof MiniCSSExtractPlugin )
( plugin ) =>
! ( plugin instanceof MiniCSSExtractPlugin ) &&
! ( plugin instanceof DependencyExtractionWebpackPlugin )
),
new DependencyExtractionWebpackPlugin( {
requestToExternal( request ) {
if ( request === '@mexp/vips' ) {
return `import ${ request }`;
}
},
} ),
new WebWorkerPlugin(),
new MiniCSSExtractPlugin( {
filename: '[name].css',
Expand Down Expand Up @@ -178,3 +185,34 @@ module.exports = {
},
},
};

const modules = {
...regular,
entry: {
vips: resolve( __dirname, 'packages/vips/src/index.ts' ),
},
output: {
...regular.output,
module: true,
},
experiments: { outputModule: true },
plugins: [
...regular.plugins.filter(
( plugin ) =>
! [
'DependencyExtractionWebpackPlugin',
'WebWorkerPlugin',
].includes( plugin.constructor.name )
),
new DependencyExtractionWebpackPlugin( {
// With modules, use `requestToExternalModule`:
requestToExternalModule( request ) {
if ( request === '@mexp/vips' ) {
return request;
}
},
} ),
],
};

module.exports = [ regular ];
Loading