From f591d141c98491fc867f19fabc0282f3678e9a59 Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Fri, 3 May 2024 12:30:02 -0700 Subject: [PATCH] Use build artifacts not typescript sources for playground build --- package.json | 2 +- .../lexical-playground/vite.prod.config.ts | 40 ++++++++++++++++++- scripts/shared/PackageMetadata.js | 4 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 42e5135d822..97d4bb51e50 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "start-test-server": "npm run preview --prefix packages/lexical-playground -- --port 4000", "build": "node scripts/build.js", "build-prod": "npm run clean && npm run build -- --prod", - "build-playground-prod": "npm run build-prod && npm run build-prod --prefix packages/lexical-playground", + "build-playground-prod": "npm run build && npm run build-prod --prefix packages/lexical-playground", "build-release": "npm run build-prod -- --release", "build-www": "npm run clean && npm run build -- --www && npm run build -- --www --prod && npm run prepare-www", "build-types": "tsc -p ./tsconfig.build.json && node ./scripts/validate-tsc-types.js", diff --git a/packages/lexical-playground/vite.prod.config.ts b/packages/lexical-playground/vite.prod.config.ts index d9a498df283..6a57c68bae9 100644 --- a/packages/lexical-playground/vite.prod.config.ts +++ b/packages/lexical-playground/vite.prod.config.ts @@ -9,12 +9,49 @@ import babel from '@rollup/plugin-babel'; import commonjs from '@rollup/plugin-commonjs'; import react from '@vitejs/plugin-react'; +import * as fs from 'node:fs'; +import {createRequire} from 'node:module'; +import * as path from 'node:path'; import {defineConfig} from 'vite'; import {replaceCodePlugin} from 'vite-plugin-replace'; -import moduleResolution from '../shared/viteModuleResolution'; import viteCopyEsm from './viteCopyEsm'; +const require = createRequire(import.meta.url); +const {packagesManager} = + require('../../scripts/shared/packagesManager') as typeof import('../../scripts/shared/packagesManager'); + +const moduleResolution = [ + ...packagesManager.getPublicPackages().flatMap((pkg) => + pkg.getNormalizedNpmModuleExportEntries().map(([find, exports]) => { + // Prefer the development esm version because we want nice errors and + // introspection on the playground! + const replacements = (['development', 'default'] as const).map((k) => + pkg.resolve('dist', exports.import[k]), + ); + const replacement = replacements.find((fn) => fs.existsSync(fn)); + if (!replacement) { + throw new Error( + `ERROR: Missing ./${path.relative( + '../..', + replacements[1], + )}. Did you run \`npm run build\` in the monorepo first?`, + ); + } + return { + find, + replacement, + }; + }), + ), + ...[packagesManager.getPackageByDirectoryName('shared')].flatMap((pkg) => + pkg.getPrivateModuleEntries().map(({name, sourceFileName}) => ({ + find: name, + replacement: pkg.resolve('src', sourceFileName), + })), + ), +]; + // https://vitejs.dev/config/ export default defineConfig({ build: { @@ -40,6 +77,7 @@ export default defineConfig({ compress: { toplevel: true, }, + keep_classnames: true, }, }, define: { diff --git a/scripts/shared/PackageMetadata.js b/scripts/shared/PackageMetadata.js index fcc9b0861ff..f8a3b351145 100644 --- a/scripts/shared/PackageMetadata.js +++ b/scripts/shared/PackageMetadata.js @@ -34,7 +34,9 @@ const npmToWwwName = require('../www/npmToWwwName'); */ /** - * @typedef {readonly [string, Record<'import'|'require', Object>]} NpmModuleExportEntry + * @typedef {Record<'types' | 'development' | 'production' | 'node' | 'default', string>} ImportCondition + * @typedef {Record<'types' | 'development' | 'production' | 'default', string>} RequireCondition + * @typedef {readonly [string, { import: ImportCondition; require: RequireCondition }]} NpmModuleExportEntry */ /**