Skip to content

Commit

Permalink
Create a post bundle script for preview-api
Browse files Browse the repository at this point in the history
To workaround vercel/next.js#57962

cc @ndelangen. Not sure we want to merge this but it gets us through the demo for now.
  • Loading branch information
tmeasday authored and shilman committed Dec 2, 2023
1 parent 7125e92 commit a3583c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion code/lib/preview-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"bundler": {
"entries": [
"./src/index.ts"
]
],
"post": "./rewrite-modules.ts"
},
"gitHead": "e6a7fd8a655c69780bc20b9749c2699e44beae17"
}
30 changes: 30 additions & 0 deletions code/lib/preview-api/rewrite-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types="node" />

// rewrite code from dist that triggers this bug: https://github.com/vercel/next.js/issues/57962
import { join, relative } from 'path';
import { cwd } from 'process';
import { promises } from 'fs';

const DIST = relative(cwd(), './dist');

async function go() {
const filenames = await promises.readdir(DIST);

await Promise.all(
filenames.map(async (filename) => {
if (filename.endsWith('.mjs')) {
const fullFilename = join(DIST, filename);

const content = await promises.readFile(fullFilename, 'utf-8');

const newContent = content
.replace(/\(exports, module\)/g, '(exports, mod)')
.replace(/module.exports = /g, 'mod.exports = ');

await promises.writeFile(fullFilename, newContent);
}
})
);
}

go();

0 comments on commit a3583c8

Please sign in to comment.