Skip to content

Commit

Permalink
Fix REPL in dev (#4964)
Browse files Browse the repository at this point in the history
* Fix REPL in dev

* Maybe improve test stability
  • Loading branch information
lukastaegert committed Apr 28, 2023
1 parent da74c59 commit 6cc3fef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
13 changes: 10 additions & 3 deletions build-plugins/replace-browser-modules.ts
@@ -1,6 +1,6 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Plugin } from 'rollup';
import type { Plugin } from 'vite';

const resolve = (path: string) => fileURLToPath(new URL(`../${path}`, import.meta.url));

Expand All @@ -14,10 +14,10 @@ const REPLACED_MODULES = [
'resolveId'
];

export const resolutions: ReadonlyMap<string, string> = new Map(
const resolutions: ReadonlyMap<string, string> = new Map(
REPLACED_MODULES.flatMap(module => {
const originalId = resolve(`src/utils/${module}`);
const replacementId = resolve(`browser/src//${module}.ts`);
const replacementId = resolve(`browser/src/${module}.ts`);
return [
[originalId, replacementId],
[`${originalId}.ts`, replacementId]
Expand All @@ -27,11 +27,18 @@ export const resolutions: ReadonlyMap<string, string> = new Map(

export default function replaceBrowserModules(): Plugin {
return {
apply: 'serve',
enforce: 'pre',
name: 'replace-browser-modules',
resolveId(source, importer) {
if (importer && source[0] === '.') {
return resolutions.get(join(dirname(importer), source));
}
},
transformIndexHtml(html) {
// Unfortunately, picomatch sneaks as a dedendency into the dev bundle.
// This fixes an error.
return html.replace('</head>', '<script>window.process={}</script></head>');
}
};
}
18 changes: 2 additions & 16 deletions docs/.vitepress/config.ts
@@ -1,7 +1,7 @@
import alias from '@rollup/plugin-alias';
import { defineConfig } from 'vitepress';
import { moduleAliases } from '../../build-plugins/aliases';
import { resolutions } from '../../build-plugins/replace-browser-modules';
import replaceBrowserModules from '../../build-plugins/replace-browser-modules';
import '../declarations.d';
import { examplesPlugin } from './create-examples';
import { renderMermaidGraphsPlugin } from './mermaid';
Expand Down Expand Up @@ -135,21 +135,7 @@ export default defineConfig({
vite: {
plugins: [
renderMermaidGraphsPlugin(),
{
apply: 'serve',
enforce: 'pre',
name: 'replace-browser-modules',
resolveId(source, importer) {
if (importer && source.startsWith('/@fs')) {
return resolutions.get(source.slice(4));
}
},
transformIndexHtml(html) {
// Unfortunately, picomatch sneaks as a dedendency into the dev bundle.
// This fixes an error.
return html.replace('</head>', '<script>window.process={}</script></head>');
}
},
replaceBrowserModules(),
{
apply: 'build',
enforce: 'pre',
Expand Down
2 changes: 1 addition & 1 deletion test/watch/index.js
Expand Up @@ -1263,7 +1263,7 @@ describe('rollup.watch', () => {

it('rebuilds immediately by default', async () => {
await copy('test/watch/samples/basic', 'test/_tmp/input');
await wait(200);
await wait(300);
watcher = rollup.watch({
input: 'test/_tmp/input/main.js',
output: {
Expand Down

0 comments on commit 6cc3fef

Please sign in to comment.