Skip to content

Commit

Permalink
Refactor (#4887)
Browse files Browse the repository at this point in the history
* Remove unused loop

* Use Map

* Chain method calls

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Mar 1, 2023
1 parent 96f2eb1 commit 25bdc12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
27 changes: 11 additions & 16 deletions build-plugins/replace-browser-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ import type { Plugin } from 'rollup';

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

export const resolutions = {
[resolve('src/utils/crypto')]: resolve('browser/src/crypto.ts'),
[resolve('src/utils/fs')]: resolve('browser/src/fs.ts'),
[resolve('src/utils/hookActions')]: resolve('browser/src/hookActions.ts'),
[resolve('src/utils/path')]: resolve('browser/src/path.ts'),
[resolve('src/utils/performance')]: resolve('browser/src/performance.ts'),
[resolve('src/utils/process')]: resolve('browser/src/process.ts'),
[resolve('src/utils/resolveId')]: resolve('browser/src/resolveId.ts')
};

for (const [key, value] of Object.entries(resolutions)) {
resolutions[`${key}.ts`] = value;
}
export const resolutions: ReadonlyMap<string, string> = new Map([
[resolve('src/utils/crypto'), resolve('browser/src/crypto.ts')],
[resolve('src/utils/fs'), resolve('browser/src/fs.ts')],
[resolve('src/utils/hookActions'), resolve('browser/src/hookActions.ts')],
[resolve('src/utils/path'), resolve('browser/src/path.ts')],
[resolve('src/utils/performance'), resolve('browser/src/performance.ts')],
[resolve('src/utils/process'), resolve('browser/src/process.ts')],
[resolve('src/utils/resolveId'), resolve('browser/src/resolveId.ts')]
]);

export default function replaceBrowserModules(): Plugin {
return {
name: 'replace-browser-modules',
resolveId(source, importer) {
if (importer && source[0] === '.') {
const resolved = join(dirname(importer), source);
if (resolutions[resolved]) {
return resolutions[resolved];
}

return resolutions.get(resolved);
}
}
};
Expand Down
6 changes: 2 additions & 4 deletions src/utils/renderChunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ async function transformChunksAndGenerateContentHashes(
};
const { code } = transformedChunk;
if (hashPlaceholder) {
const hash = createHash();
// To create a reproducible content-only hash, all placeholders are
// replaced with the same value before hashing
const { containedPlaceholders, transformedCode } =
replacePlaceholdersWithDefaultAndGetContainedPlaceholders(code, placeholders);
hash.update(transformedCode);
const hash = createHash().update(transformedCode);
const hashAugmentation = pluginDriver.hookReduceValueSync(
'augmentChunkHash',
'',
Expand Down Expand Up @@ -290,8 +289,7 @@ function generateFinalHashes(
do {
// In case of a hash collision, create a hash of the hash
if (finalHash) {
hash = createHash();
hash.update(finalHash);
hash = createHash().update(finalHash);
}
finalHash = hash.digest('hex').slice(0, placeholder.length);
finalFileName = replaceSinglePlaceholder(fileName, placeholder, finalHash);
Expand Down

0 comments on commit 25bdc12

Please sign in to comment.