Skip to content

Commit

Permalink
remove use of astring (#188)
Browse files Browse the repository at this point in the history
* remove use of astring

resolves #151
  • Loading branch information
dario-piotrowicz authored Apr 25, 2023
1 parent b07e3bc commit bddbe04
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
16 changes: 16 additions & 0 deletions .changeset/pretty-goats-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@cloudflare/next-on-pages': patch
---

remove astring dependency

remove the `astring` dependency and by doing so basically just create and edit
javascript code via string manipulations.

this should speed up the experimental minification (since we don't generate js code
from ASTs anymore) and avoid potential bugs in the `astring` library (like #151)

note that this is not the cleanest solution and that we should look into implementing
more robust and stable javascript code handling via AST visiting and manipulations
(but currently that has proven quite problematic since modern javascript libraries that
allow such code modding have turned out to be very slow, significantly impacting DX)
18 changes: 2 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"dependencies": {
"acorn": "^8.8.0",
"ast-types": "^0.14.2",
"astring": "^1.8.4",
"chalk": "^5.2.0",
"chokidar": "^3.5.3",
"cookie": "^0.5.0",
Expand Down
66 changes: 31 additions & 35 deletions src/buildApplication/generateFunctionsMap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFile, writeFile, mkdir, rm, readdir, copyFile } from 'fs/promises';
import { exit } from 'process';
import { dirname, join, relative, resolve } from 'path';
import type { Node } from 'acorn';
import { parse } from 'acorn';
import { generate } from 'astring';
import {
formatRoutePath,
normalizePath,
Expand Down Expand Up @@ -290,6 +290,7 @@ function extractWebpackChunks(
extractedWebpackChunks: Map<number, string>;
} {
const webpackChunks = new Map<number, string>();
const webpackChunksCodeReplaceMap = new Map<string, string>();

const parsedContents = parse(functionContents, {
ecmaVersion: 'latest',
Expand All @@ -298,53 +299,48 @@ function extractWebpackChunks(

const chunks = parsedContents.body.flatMap(getWebpackChunksFromStatement);

for (const chunk of chunks) {
chunks.forEach(chunk => {
const key = (chunk.key as AST.NumericLiteralKind).value;

if (key in existingWebpackChunks) {
if (existingWebpackChunks.get(key) !== generate(chunk.value)) {
cliError(
`
const chunkExpressionCode = functionContents.slice(
(chunk.value as Node).start,
(chunk.value as Node).end
);

if (
existingWebpackChunks.has(key) &&
existingWebpackChunks.get(key) !== chunkExpressionCode
) {
cliError(
`
ERROR: Detected a collision with '--experimental-minify'.
Try removing the '--experimental-minify' argument.
`,
{ spaced: true }
);
exit(1);
}
{ spaced: true }
);
exit(1);
}

webpackChunks.set(key, generate(chunk.value));
webpackChunks.set(key, chunkExpressionCode);

const chunkFilePath = join(tmpWebpackDir, `${key}.js`);

const newValue = {
type: 'MemberExpression',
object: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require',
},
arguments: [
{
type: 'Literal',
value: chunkFilePath,
raw: JSON.stringify(chunkFilePath),
},
],
},
property: {
type: 'Identifier',
name: 'default',
},
};
const newChunkExpressionCode = `require(${JSON.stringify(
chunkFilePath
)}).default`;

(chunk as unknown as { value: unknown }).value = newValue;
}
webpackChunksCodeReplaceMap.set(
chunkExpressionCode,
newChunkExpressionCode
);
});

webpackChunksCodeReplaceMap.forEach((newChunkCode, chunkCode) => {
functionContents = functionContents.replace(chunkCode, newChunkCode);
});

return {
updatedFunctionContents: generate(parsedContents),
updatedFunctionContents: functionContents,
extractedWebpackChunks: webpackChunks,
};
}
Expand Down

0 comments on commit bddbe04

Please sign in to comment.