Skip to content

Commit

Permalink
fix: restore playground esm build
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Mar 29, 2024
1 parent 4c37c10 commit 46e001c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/lexical-playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import react from '@vitejs/plugin-react';
import {defineConfig} from 'vite';
import {replaceCodePlugin} from 'vite-plugin-replace';

import viteCopyEsm from './viteCopyEsm';
import moduleResolution from './viteModuleResolution';

// https://vitejs.dev/config/
Expand Down Expand Up @@ -54,6 +55,7 @@ export default defineConfig({
presets: ['@babel/preset-react'],
}),
react(),
viteCopyEsm(),
],
resolve: {
alias: moduleResolution,
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-playground/vite.prod.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import react from '@vitejs/plugin-react';
import {defineConfig} from 'vite';
import {replaceCodePlugin} from 'vite-plugin-replace';

import viteCopyEsm from './viteCopyEsm';
import moduleResolution from './viteModuleResolution';

// https://vitejs.dev/config/
Expand Down Expand Up @@ -53,6 +54,7 @@ export default defineConfig({
presets: ['@babel/preset-react'],
}),
react(),
viteCopyEsm(),
],
resolve: {
alias: moduleResolution,
Expand Down
41 changes: 41 additions & 0 deletions packages/lexical-playground/viteCopyEsm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import fs from 'fs';
import path from 'path';
import copy from 'rollup-plugin-copy';

function parseImportMapImportEntries() {
const m = /<script type="importmap">([\s\S]+?)<\/script>/g.exec(
fs.readFileSync('./esm/index.html', 'utf8'),
);
if (!m) {
throw new Error('Could not parse importmap from esm/index.html');
}
return Object.entries<string>(JSON.parse(m[1]).imports);
}

// Fork modules are only produced by the build script
export default function viteCopyEsm() {
return copy({
hook: 'writeBundle',
targets: [
{dest: './build/esm/', src: './esm/*'},
{dest: './build/', src: ['./*.png', './*.ico']},
...parseImportMapImportEntries().map(([mod, fn]) => ({
dest: path.join('./build/esm/dist', fn),
src: path.join(
`../${mod.replace(/^@/, '').replace(/\//g, '-')}`,
// Fork modules are only produced by build-release, which is not run
// in CI, so we don't need to worry about choosing dev or prod
fn,
),
})),
],
verbose: true,
});
}

0 comments on commit 46e001c

Please sign in to comment.