Skip to content

Commit

Permalink
make spacepackLite closer to real spacepack
Browse files Browse the repository at this point in the history
  • Loading branch information
adryd325 committed Dec 6, 2023
1 parent 4a2d2a0 commit fe17463
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Patcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import matchModule from "./matchModule";
import { getWpToolsFunc } from "./spacepackLite";
import { getSpacepack } from "./spacepackLite";

class ConfigValidationError extends Error {}

Expand Down Expand Up @@ -70,7 +70,7 @@ export default class Patcher {
name: "spacepack",
// This is sorta a scope hack.
// If we rewrap this function, it will lose its scope (in this case the match module import and the chunk object name)
run: getWpToolsFunc(this.chunkObject),
run: getSpacepack(this.chunkObject),
entry: true,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/spacepackEverywhere.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getWpToolsFunc } from "./spacepackLite";
import { getSpacepack } from "./spacepackLite";

function getWebpackVersion(chunkObject) {
if (chunkObject instanceof Array) {
Expand Down Expand Up @@ -37,7 +37,7 @@ function pushSpacepack(chunkObjectName) {
switch (version) {
case "modern":
chunkObject.__spacepack_everywhere_injected = true;
chunkObject.push([["spacepack"], { spacepack: getWpToolsFunc(chunkObjectName, true) }, onChunkLoaded]);
chunkObject.push([["spacepack"], { spacepack: getSpacepack(chunkObjectName, true) }, onChunkLoaded]);
break;
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/spacepackLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function getNamedRequire(webpackRequire) {
return namedRequireObj;
}

export function getWpToolsFunc(chunkObject, logSuccess = false) {
function wpTools(module, exports, webpackRequire) {
export function getSpacepack(chunkObject, logSuccess = false) {
function spacepack(module, exports, webpackRequire) {
if (logSuccess) {
console.log("[wpTools] spacepack loaded in " + chunkObject);
}
Expand All @@ -78,7 +78,7 @@ export function getWpToolsFunc(chunkObject, logSuccess = false) {
// modules require cache (exports): webpackRequire.c

// TODO: recurse in objects
function findModulesByExports(keysArg) {
function findByExports(keysArg) {
if (!webpackRequire.c) {
throw new Error("webpack runtime didn't export its moduleCache");
}
Expand All @@ -98,7 +98,7 @@ export function getWpToolsFunc(chunkObject, logSuccess = false) {
});
}

function findModulesByCode(search) {
function findByCode(search) {
return Object.entries(webpackRequire.m)
.filter(([moduleId, moduleFunc]) => {
const funcStr = Function.prototype.toString.apply(moduleFunc);
Expand Down Expand Up @@ -197,8 +197,8 @@ export function getWpToolsFunc(chunkObject, logSuccess = false) {
chunkObject: window[chunkObject],
name: chunkObject,

findModulesByCode,
findModulesByExports,
findByCode,
findByExports,
findObjectFromKey,
findObjectFromKeyValuePair,
findObjectFromValue,
Expand Down Expand Up @@ -231,7 +231,7 @@ export function getWpToolsFunc(chunkObject, logSuccess = false) {
}

// Mark as processed as to not loose scope if somehow passed to Patcher._patchModules()
wpTools.__wpt_processed = true;
spacepack.__wpt_processed = true;

return wpTools;
return spacepack;
}

0 comments on commit fe17463

Please sign in to comment.