Skip to content

Commit d5ba369

Browse files
committed
1 parent 1d4927f commit d5ba369

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/zimscraperlib/rewriting/js.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
from zimscraperlib.rewriting.url_rewriting import ArticleUrlRewriter, ZimPath
3030

3131
# The regex used to rewrite `import ...` in module code.
32-
IMPORT_MATCH_RX = re.compile(
33-
r"""^\s*?import(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s])(?:.*?)['"\s]""",
32+
IMPORT_EXPORT_MATCH_RX = re.compile(
33+
r"""(^|;)\s*?(?:im|ex)port(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s])(?:.*?)['"\s]""",
3434
)
3535

3636
# A sub regex used inside `import ...` rewrite to rewrite http url imported
37-
IMPORT_HTTP_RX = re.compile(
38-
r"""(import(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s]))((?:https?|[./]).*?)(['"\s])""",
37+
IMPORT_EXPORT_HTTP_RX = re.compile(
38+
r"""((?:im|ex)port(?:['"\s]*(?:[\w*${}\s,]+from\s*)?['"\s]?['"\s]))((?:https?|[./]).*?)(['"\s])""",
3939
)
4040

4141
# This list of global variables we want to wrap.
@@ -312,8 +312,8 @@ def sub_funct(match: re.Match[str]) -> str:
312312
f"{match.group(3)}"
313313
)
314314

315-
return IMPORT_HTTP_RX.sub(sub_funct, m_object[0])
315+
return IMPORT_EXPORT_HTTP_RX.sub(sub_funct, m_object[0])
316316

317317
return func
318318

319-
return (IMPORT_MATCH_RX, rewrite_import())
319+
return (IMPORT_EXPORT_MATCH_RX, rewrite_import())

tests/rewriting/test_js_rewriting.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ def wrap_import(text: str) -> str:
310310
export { a };
311311
""",
312312
),
313+
# rewrite import same line
314+
ImportTestContent(
315+
input_='import{A, B} from "https://example.com/";'
316+
'import{C, D} from "https://example.org"',
317+
expected='import{A, B} from "../../../example.com/";'
318+
'import{C, D} from "../../../example.org/"',
319+
),
320+
# rewrite import / export same line
321+
ImportTestContent(
322+
input_='import{A, B} from "https://example.com/";'
323+
'export{C, D} from "/another/path/to/file"',
324+
expected='import{A, B} from "../../../example.com/";'
325+
'export{C, D} from "../../another/path/to/file"',
326+
),
313327
# rewrite ESM module import
314328
ImportTestContent(
315329
input_='import "https://example.com/file.js"',

0 commit comments

Comments
 (0)