From ef4e547e159099317171e1e6ee03c367c1c2ad2f Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Fri, 3 May 2024 13:47:37 -0700 Subject: [PATCH] unify vite module resolution --- package.json | 5 +- packages/lexical-devtools/wxt.config.ts | 2 +- packages/lexical-playground/vite.config.ts | 106 +++++++++--------- .../lexical-playground/vite.prod.config.ts | 52 +-------- packages/shared/viteModuleResolution.ts | 82 +++++++++++--- 5 files changed, 126 insertions(+), 121 deletions(-) diff --git a/package.json b/package.json index 97d4bb51e50..ced791107af 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "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 && npm run build-prod --prefix packages/lexical-playground", + "build-playground-dev": "npm run build && npm run build-dev --prefix packages/lexical-playground", + "build-playground-prod": "npm run build-prod && 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", @@ -89,7 +90,7 @@ "prettier": "prettier --list-different .", "ci-check": "npm-run-all --parallel tsc tsc-extension flow prettier lint", "prettier:fix": "prettier --write .", - "prepare-ci": "npm run build-dev --prefix packages/lexical-playground", + "prepare-ci": "npm run build-playground-dev", "prepare-ci-prod": "npm run build-playground-prod", "prepare-release": "npm run build-release && node ./scripts/npm/prepare-release.js && node ./scripts/validate-tsc-types.js", "prepare": "husky install", diff --git a/packages/lexical-devtools/wxt.config.ts b/packages/lexical-devtools/wxt.config.ts index b3cf5988b81..d6e9fb8dcfd 100644 --- a/packages/lexical-devtools/wxt.config.ts +++ b/packages/lexical-devtools/wxt.config.ts @@ -127,7 +127,7 @@ export default defineConfig({ find: 'lexicalOriginal', replacement: path.resolve('../lexical/src/index.ts'), }, - ...moduleResolution, + ...moduleResolution('preview'), ], }, }), diff --git a/packages/lexical-playground/vite.config.ts b/packages/lexical-playground/vite.config.ts index 66f11ba8d27..da1900251a5 100644 --- a/packages/lexical-playground/vite.config.ts +++ b/packages/lexical-playground/vite.config.ts @@ -19,60 +19,64 @@ import viteCopyEsm from './viteCopyEsm'; const require = createRequire(import.meta.url); // https://vitejs.dev/config/ -export default defineConfig({ - build: { - outDir: 'build', - rollupOptions: { - input: { - main: new URL('./index.html', import.meta.url).pathname, - split: new URL('./split/index.html', import.meta.url).pathname, - }, - onwarn(warning, warn) { - if ( - warning.code === 'EVAL' && - warning.id && - /[\\/]node_modules[\\/]@excalidraw\/excalidraw[\\/]/.test(warning.id) - ) { - return; - } - warn(warning); +export default defineConfig(({command}) => { + return { + build: { + outDir: 'build', + rollupOptions: { + input: { + main: new URL('./index.html', import.meta.url).pathname, + split: new URL('./split/index.html', import.meta.url).pathname, + }, + onwarn(warning, warn) { + if ( + warning.code === 'EVAL' && + warning.id && + /[\\/]node_modules[\\/]@excalidraw\/excalidraw[\\/]/.test( + warning.id, + ) + ) { + return; + } + warn(warning); + }, }, }, - }, - define: { - 'process.env.IS_PREACT': process.env.IS_PREACT, - }, - plugins: [ - replaceCodePlugin({ - replacements: [ - { - from: /__DEV__/g, - to: 'true', - }, - ], - }), - babel({ - babelHelpers: 'bundled', - babelrc: false, - configFile: false, - exclude: '/**/node_modules/**', - extensions: ['jsx', 'js', 'ts', 'tsx', 'mjs'], - plugins: [ - '@babel/plugin-transform-flow-strip-types', - [ - require('../../scripts/error-codes/transform-error-messages'), + define: { + 'process.env.IS_PREACT': process.env.IS_PREACT, + }, + plugins: [ + replaceCodePlugin({ + replacements: [ { - noMinify: true, + from: /__DEV__/g, + to: 'true', }, ], - ], - presets: ['@babel/preset-react'], - }), - react(), - viteCopyEsm(), - commonjs(), - ], - resolve: { - alias: moduleResolution, - }, + }), + babel({ + babelHelpers: 'bundled', + babelrc: false, + configFile: false, + exclude: '/**/node_modules/**', + extensions: ['jsx', 'js', 'ts', 'tsx', 'mjs'], + plugins: [ + '@babel/plugin-transform-flow-strip-types', + [ + require('../../scripts/error-codes/transform-error-messages'), + { + noMinify: true, + }, + ], + ], + presets: ['@babel/preset-react'], + }), + react(), + viteCopyEsm(), + commonjs(), + ], + resolve: { + alias: moduleResolution(command === 'serve' ? 'source' : 'development'), + }, + }; }); diff --git a/packages/lexical-playground/vite.prod.config.ts b/packages/lexical-playground/vite.prod.config.ts index 3cc3298163a..a1e5f9abd4c 100644 --- a/packages/lexical-playground/vite.prod.config.ts +++ b/packages/lexical-playground/vite.prod.config.ts @@ -9,60 +9,12 @@ 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 { - type ModuleExportEntry, - type NpmModuleExportEntry, - type PackageMetadata, -} from '../../scripts/shared/PackageMetadata'; +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((entry: NpmModuleExportEntry) => { - const [name, moduleExports] = entry; - // 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', moduleExports.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: name, - replacement, - }; - }), - ), - ...[packagesManager.getPackageByDirectoryName('shared')].flatMap( - (pkg: PackageMetadata) => - pkg.getPrivateModuleEntries().map((entry: ModuleExportEntry) => { - return { - find: entry.name, - replacement: pkg.resolve('src', entry.sourceFileName), - }; - }), - ), -]; - // https://vitejs.dev/config/ export default defineConfig({ build: { @@ -117,6 +69,6 @@ export default defineConfig({ commonjs(), ], resolve: { - alias: moduleResolution, + alias: moduleResolution('production'), }, }); diff --git a/packages/shared/viteModuleResolution.ts b/packages/shared/viteModuleResolution.ts index 9025d0d33ed..572e1e52dfd 100644 --- a/packages/shared/viteModuleResolution.ts +++ b/packages/shared/viteModuleResolution.ts @@ -8,33 +8,81 @@ import type { ModuleExportEntry, + NpmModuleExportEntry, PackageMetadata, } from '../../scripts/shared/PackageMetadata'; +import * as fs from 'node:fs'; import {createRequire} from 'node:module'; +import * as path from 'node:path'; const require = createRequire(import.meta.url); const {packagesManager} = require('../../scripts/shared/packagesManager') as typeof import('../../scripts/shared/packagesManager'); -function toAlias(pkg: PackageMetadata, entry: ModuleExportEntry) { - return { - find: entry.name, - replacement: pkg.resolve('src', entry.sourceFileName), - }; -} +const sourceModuleResolution = () => { + function toAlias(pkg: PackageMetadata, entry: ModuleExportEntry) { + return { + find: entry.name, + replacement: pkg.resolve('src', entry.sourceFileName), + }; + } + + return [ + ...packagesManager + .getPublicPackages() + .flatMap((pkg) => + pkg.getExportedNpmModuleEntries().map(toAlias.bind(null, pkg)), + ), + ...['shared'] + .map((name) => packagesManager.getPackageByDirectoryName(name)) + .flatMap((pkg) => + pkg.getPrivateModuleEntries().map(toAlias.bind(null, pkg)), + ), + ]; +}; -const moduleResolution = [ - ...packagesManager - .getPublicPackages() - .flatMap((pkg) => - pkg.getExportedNpmModuleEntries().map(toAlias.bind(null, pkg)), +const distModuleResolution = (environment: 'development' | 'production') => { + return [ + ...packagesManager.getPublicPackages().flatMap((pkg) => + pkg + .getNormalizedNpmModuleExportEntries() + .map((entry: NpmModuleExportEntry) => { + const [name, moduleExports] = entry; + const replacements = ([environment, 'default'] as const).map( + (condition) => pkg.resolve('dist', moduleExports.import[condition]), + ); + const replacement = replacements.find(fs.existsSync.bind(fs)); + if (!replacement) { + throw new Error( + `ERROR: Missing ./${path.relative( + '../..', + replacements[1], + )}. Did you run \`npm run build\` in the monorepo first?`, + ); + } + return { + find: name, + replacement, + }; + }), ), - ...['shared'] - .map((name) => packagesManager.getPackageByDirectoryName(name)) - .flatMap((pkg) => - pkg.getPrivateModuleEntries().map(toAlias.bind(null, pkg)), + ...[packagesManager.getPackageByDirectoryName('shared')].flatMap( + (pkg: PackageMetadata) => + pkg.getPrivateModuleEntries().map((entry: ModuleExportEntry) => { + return { + find: entry.name, + replacement: pkg.resolve('src', entry.sourceFileName), + }; + }), ), -]; + ]; +}; -export default moduleResolution; +export default function moduleResolution( + environment: 'source' | 'development' | 'production', +) { + return environment === 'source' + ? sourceModuleResolution() + : distModuleResolution(environment); +}