Skip to content

Commit

Permalink
unify vite module resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed May 3, 2024
1 parent 43e1adb commit ef4e547
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 121 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/lexical-devtools/wxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default defineConfig({
find: 'lexicalOriginal',
replacement: path.resolve('../lexical/src/index.ts'),
},
...moduleResolution,
...moduleResolution('preview'),
],
},
}),
Expand Down
106 changes: 55 additions & 51 deletions packages/lexical-playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
};
});
52 changes: 2 additions & 50 deletions packages/lexical-playground/vite.prod.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -117,6 +69,6 @@ export default defineConfig({
commonjs(),
],
resolve: {
alias: moduleResolution,
alias: moduleResolution('production'),
},
});
82 changes: 65 additions & 17 deletions packages/shared/viteModuleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit ef4e547

Please sign in to comment.