Skip to content

Commit

Permalink
fix: normalize injected import paths on windows (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Jul 31, 2023
1 parent 226d562 commit 3affd17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-radios-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Normalize injected import paths for builds done on Windows machines.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Plugin } from 'esbuild';
import { build } from 'esbuild';
import { mkdir } from 'node:fs/promises';
import { dirname, join, relative, resolve } from 'node:path';
import { normalizePath } from '../../utils';

/**
* Builds a file using esbuild.
Expand All @@ -17,9 +18,11 @@ export async function buildFile(
filePath: string,
{ relativeTo }: Omit<RelativePathOpts, 'from'> = {},
) {
const relativeNopDistPath = join(
getRelativePathToAncestor({ from: filePath, relativeTo }),
'__next-on-pages-dist__',
const relativeNopDistPath = normalizePath(
join(
getRelativePathToAncestor({ from: filePath, relativeTo }),
'__next-on-pages-dist__',
),
);

await mkdir(dirname(filePath), { recursive: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { buildFile, getRelativePathToAncestor } from './build';
import {
addLeadingSlash,
copyFileWithDir,
normalizePath,
replaceLastSubstringInstance,
validateFile,
} from '../../utils';
Expand Down Expand Up @@ -185,7 +186,9 @@ async function buildFunctionFile(
from: newFnLocation,
relativeTo: nopDistDir,
});
const importPath = join(relativeImportPath, addLeadingSlash(path));
const importPath = normalizePath(
join(relativeImportPath, addLeadingSlash(path)),
);

functionImports += `import { ${keys} } from '${importPath}';\n`;
});
Expand Down Expand Up @@ -233,7 +236,9 @@ async function prependWasmImportsToCodeBlocks(
for (const identifier of wasmImports) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const { newDest } = identifierMaps.wasm.get(identifier)!;
const wasmImportPath = join(relativeImportPath, newDest as string);
const wasmImportPath = normalizePath(
join(relativeImportPath, newDest as string),
);
functionImports += `import ${identifier} from "${wasmImportPath}";\n`;
}

Expand Down Expand Up @@ -274,7 +279,7 @@ async function processImportIdentifier(

const oldPath = join(dirname(entrypoint), importPath);
const newPath = join(nopDistDir, type, importPathWithoutType);
info.newDest = relative(workerJsDir, newPath);
info.newDest = normalizePath(relative(workerJsDir, newPath));

await copyFileWithDir(oldPath, newPath);
}
Expand All @@ -283,7 +288,7 @@ async function processImportIdentifier(
from: newFnLocation,
relativeTo: nopDistDir,
});
const newImportPath = join(relativeImportPath, info.newDest);
const newImportPath = normalizePath(join(relativeImportPath, info.newDest));

const newVal = `import ${identifier} from "${newImportPath}";`;
updatedContents = replaceLastSubstringInstance(
Expand Down Expand Up @@ -339,7 +344,7 @@ async function processCodeBlockIdentifier(
if (!info.newDest) {
const identTypeDir = join(nopDistDir, type);
newFilePath = join(identTypeDir, `${info.groupedPath ?? identifier}.js`);
info.newDest = relative(workerJsDir, newFilePath);
info.newDest = normalizePath(relative(workerJsDir, newFilePath));

// Record the wasm identifiers used in the code block.
wasmIdentifierKeys
Expand Down

0 comments on commit 3affd17

Please sign in to comment.