From c5d0811bc98d10602eeca26a2fb8eaad47e1d806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 17 Dec 2024 10:01:00 +0100 Subject: [PATCH] Build `@php-wasm` packages as dual ESM + CJS (#2087) --- packages/php-wasm/scopes/package.json | 11 ++++++++++- packages/php-wasm/scopes/vite.config.ts | 2 +- packages/php-wasm/util/package.json | 11 +++++++++++ .../php-wasm/web-service-worker/package.json | 14 +++++++++++--- .../php-wasm/web-service-worker/project.json | 17 +++++++++++------ .../php-wasm/web-service-worker/vite.config.ts | 16 ++++++++++++++++ .../data-liberation/blueprints-library | 2 +- 7 files changed, 61 insertions(+), 12 deletions(-) diff --git a/packages/php-wasm/scopes/package.json b/packages/php-wasm/scopes/package.json index 9ce9e858a0..452d7ab695 100644 --- a/packages/php-wasm/scopes/package.json +++ b/packages/php-wasm/scopes/package.json @@ -20,8 +20,17 @@ "directory": "../../../dist/packages/php-wasm/scopes" }, "license": "GPL-2.0-or-later", + "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./package.json": "./package.json", + "./README.md": "./README.md" + }, "type": "module", - "main": "index.js", + "main": "./index.cjs", + "module": "./index.js", "types": "index.d.ts", "gitHead": "2f8d8f3cea548fbd75111e8659a92f601cddc593", "engines": { diff --git a/packages/php-wasm/scopes/vite.config.ts b/packages/php-wasm/scopes/vite.config.ts index 63f0fa757b..2d1fcfdfd6 100644 --- a/packages/php-wasm/scopes/vite.config.ts +++ b/packages/php-wasm/scopes/vite.config.ts @@ -32,7 +32,7 @@ export default defineConfig({ entry: 'src/index.ts', name: 'php-wasm-scope', fileName: 'index', - formats: ['es'], + formats: ['es', 'cjs'], }, rollupOptions: { // External packages that should not be bundled into your library. diff --git a/packages/php-wasm/util/package.json b/packages/php-wasm/util/package.json index 22ecca652c..74d1350fd1 100644 --- a/packages/php-wasm/util/package.json +++ b/packages/php-wasm/util/package.json @@ -13,6 +13,17 @@ "access": "public", "directory": "../../../dist/packages/php-wasm/util" }, + "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./package.json": "./package.json", + "./README.md": "./README.md" + }, + "type": "module", + "main": "./index.cjs", + "module": "./index.js", "gitHead": "2f8d8f3cea548fbd75111e8659a92f601cddc593", "engines": { "node": ">=18.18.0", diff --git a/packages/php-wasm/web-service-worker/package.json b/packages/php-wasm/web-service-worker/package.json index 3c422bd88c..1f9cc2195b 100644 --- a/packages/php-wasm/web-service-worker/package.json +++ b/packages/php-wasm/web-service-worker/package.json @@ -19,10 +19,18 @@ "access": "public", "directory": "../../../dist/packages/php-wasm/web-service-worker" }, - "license": "GPL-2.0-or-later", + "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./package.json": "./package.json", + "./README.md": "./README.md" + }, "type": "module", - "main": "src/index.js", - "types": "src/index.d.ts", + "main": "./index.cjs", + "module": "./index.js", + "license": "GPL-2.0-or-later", "gitHead": "2f8d8f3cea548fbd75111e8659a92f601cddc593", "engines": { "node": ">=18.18.0", diff --git a/packages/php-wasm/web-service-worker/project.json b/packages/php-wasm/web-service-worker/project.json index 9d8c51de03..32bb5a575b 100644 --- a/packages/php-wasm/web-service-worker/project.json +++ b/packages/php-wasm/web-service-worker/project.json @@ -12,15 +12,20 @@ "buildTarget": "php-wasm-web-service-worker:build:bundle:production" } }, + "build:package-json": { + "executor": "@wp-playground/nx-extensions:package-json", + "options": { + "tsConfig": "packages/php-wasm/web-service-worker/tsconfig.lib.json", + "outputPath": "dist/packages/php-wasm/web-service-worker", + "buildTarget": "php-wasm-web-service-worker:build:bundle:production" + } + }, "build:bundle": { - "executor": "@nx/js:tsc", + "executor": "@nx/vite:build", "outputs": ["{options.outputPath}"], "options": { - "outputPath": "dist/packages/php-wasm/web-service-worker", - "main": "packages/php-wasm/web-service-worker/src/index.ts", - "tsConfig": "packages/php-wasm/web-service-worker/tsconfig.lib.json", - "assets": ["packages/php-wasm/web-service-worker/*.md"], - "updateBuildableProjectDepsInPackageJson": true + "emptyOutDir": false, + "outputPath": "dist/packages/php-wasm/web-service-worker" } }, "lint": { diff --git a/packages/php-wasm/web-service-worker/vite.config.ts b/packages/php-wasm/web-service-worker/vite.config.ts index d5df5f4767..d1a26ed5d1 100644 --- a/packages/php-wasm/web-service-worker/vite.config.ts +++ b/packages/php-wasm/web-service-worker/vite.config.ts @@ -6,6 +6,8 @@ import { join } from 'path'; // eslint-disable-next-line @nx/enforce-module-boundaries import { viteTsConfigPaths } from '../../vite-extensions/vite-ts-config-paths'; +// eslint-disable-next-line @nx/enforce-module-boundaries +import { getExternalModules } from '../../vite-extensions/vite-external-modules'; export default defineConfig({ cacheDir: '../../../node_modules/.vite/php-wasm-web-service-worker', @@ -22,6 +24,20 @@ export default defineConfig({ }), ], + build: { + lib: { + // Could also be a dictionary or array of multiple entry points. + entry: 'src/index.ts', + name: 'php-wasm-web-service-worker', + fileName: 'index', + formats: ['es', 'cjs'], + }, + rollupOptions: { + // External packages that should not be bundled into your library. + external: getExternalModules(), + }, + }, + test: { globals: true, cache: { diff --git a/packages/playground/data-liberation/blueprints-library b/packages/playground/data-liberation/blueprints-library index c7ed61ba6b..966e692625 160000 --- a/packages/playground/data-liberation/blueprints-library +++ b/packages/playground/data-liberation/blueprints-library @@ -1 +1 @@ -Subproject commit c7ed61ba6bcd4be8defa1614d5cf479c98594656 +Subproject commit 966e6926256dc56c8473c6257d0d474be0f20811