Skip to content

Commit

Permalink
module rewrite fix: support dynamic import in module and non module s…
Browse files Browse the repository at this point in the history
…cripts (#112)

* module rewrite fix: (fix for issue found in webrecorder/wombat#116)
- support dynamic 'import()' in module or non-module with different base url (presence of import doesn't imply script is in module)
- simplify isModule check logic
- if script not in module, rewrite dynamic import to '__wb_rewrite_import_('', url), no base resolution
- if script is in module, rewrite dynamic import to '__wb_rewrite_import_(import.meta.url, ...)' to pass curr script url as base url
- remove duplicate rewrite call!
bump to 2.15.4
  • Loading branch information
ikreymer committed Mar 28, 2023
1 parent 098b6c2 commit 2360177
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions dist/sw.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wombat.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@webrecorder/wabac",
"version": "2.15.3",
"version": "2.15.4",
"main": "index.js",
"type": "module",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@peculiar/asn1-ecc": "^2.3.4",
"@peculiar/asn1-schema": "^2.3.3",
"@peculiar/x509": "^1.9.2",
"@webrecorder/wombat": "^3.4.6",
"@webrecorder/wombat": "^3.5.0",
"auto-js-ipfs": "^2.1.0",
"base64-js": "^1.5.1",
"brotli": "^1.3.3",
Expand Down
28 changes: 13 additions & 15 deletions src/rewrite/jsrewriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ const createJSRules = () => {
};
}

// mark as module as side-effect
function replaceMarkModule(src, target) {
function replaceImport(src, target) {
return (x, opts) => {
opts.isModule = true;
return x.replace(src, target);
let res = x.replace(src, target);
// if not module, add empty string, otherwise, import.meta.url
res += (opts.isModule ? "import.meta.url, " : "\"\", ");
return res;
};
}

Expand Down Expand Up @@ -119,7 +120,7 @@ const createJSRules = () => {
[/[^|&][|&]{2}\s*this\b\s*(?![|\s&.$](?:[^|&]|$))/, replaceThis()],

// esm dynamic import, if found, mark as module
[/[^$.]\bimport\s*\(/, replaceMarkModule("import", "____wb_rewrite_import__")]
[/[^$.]\bimport\s*\(/, replaceImport("import", "____wb_rewrite_import__")]
];
};

Expand Down Expand Up @@ -158,11 +159,7 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
return `import { ${localDecls.join(", ")} } from "${prefix}__wb_module_decl.js";\n`;
}

isModule(text, opts) {
if (opts && opts.isModule) {
return true;
}

detectIsModule(text) {
if (text.indexOf("import") >= 0 && text.match(IMPORT_RX)) {
return true;
}
Expand All @@ -175,11 +172,14 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re
}

rewrite(text, opts) {
const isModule = this.isModule(text, opts);
opts = opts || {};
if (opts.isModule === undefined || opts.isModule === null) {
opts.isModule = this.detectIsModule(text, opts);
}

let rules = DEFAULT_RULES;

if (isModule) {
if (opts.isModule) {
rules = [...rules, this.getESMImportRule()];
}

Expand All @@ -193,14 +193,12 @@ if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; re

let newText = super.rewrite(text, opts);

if (isModule || opts.isModule) {
if (opts.isModule) {
return this.getModuleDecl(GLOBAL_OVERRIDES, opts.prefix) + newText;
}

const wrapGlobals = GLOBALS_RX.exec(text);

newText = super.rewrite(text, opts);

if (wrapGlobals) {
newText = this.firstBuff + newText + this.lastBuff;
}
Expand Down
8 changes: 4 additions & 4 deletions test/rewriteJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ a = _____WB$wombat$check$this$function_____(this).location\
`);


// dynamic import rewrite
test(rewriteJSImport,
// dynamic import rewrite (non-module)
test(rewriteJS,
"await import (somefile);",
"await ____wb_rewrite_import__ (somefile);"
"await ____wb_rewrite_import__ (\"\", somefile);"
);


Expand Down Expand Up @@ -203,7 +203,7 @@ import {X, Y} from "http://localhost:8080/prefix/20201226101010esm_/https://exam
import {E, F, G} from "http://localhost:8080/prefix/20201226101010esm_/https://example.com/path.js";
import { Z } from "http://localhost:8080/prefix/20201226101010esm_/https://example.com/path.js";
B = await ____wb_rewrite_import__(somefile);
B = await ____wb_rewrite_import__(import.meta.url, somefile);
`
);

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@
resolved "https://registry.yarnpkg.com/@webpack-cli/serve/-/serve-1.5.2.tgz#ea584b637ff63c5a477f6f21604b5a205b72c9ec"
integrity sha512-vgJ5OLWadI8aKjDlOH3rb+dYyPd2GTZuQC/Tihjct6F9GpXGZINo3Y/IVuZVTM1eDQB+/AOsjPUWH/WySDaXvw==

"@webrecorder/wombat@^3.4.6":
version "3.4.6"
resolved "https://registry.yarnpkg.com/@webrecorder/wombat/-/wombat-3.4.6.tgz#48c8a03b1d47789fd799f6d9d49ec1ca6c610a1f"
integrity sha512-YMGKT78B6nDOIV8irIvIPtcWGOa44/FxFXN6LqeUM8WwJrvLL3YI+pXfKYd7GYJFULNqir3Ao+nSNNrjy9PtJg==
"@webrecorder/wombat@^3.5.0":
version "3.5.0"
resolved "https://registry.yarnpkg.com/@webrecorder/wombat/-/wombat-3.5.0.tgz#e48e580161a0c82f39bb92fc0c81db04c5098b8a"
integrity sha512-ID9tbsqdyhWTBukBF/epztpmgqBkjCoKc3dws7VbUDUMnvkUOIRb9uLBzvKNSg9paGJKhIYzZBtjIQLFw8gFDg==
dependencies:
warcio "^2.0.1"

Expand Down

0 comments on commit 2360177

Please sign in to comment.