Skip to content

Commit

Permalink
global: allow setting of copy instructions per bundle
Browse files Browse the repository at this point in the history
* because copy instructions may vary based on the exact JS code in
  question (e.g. TinyMCE and PDF.js), they are placed in bundles
  • Loading branch information
max-moser committed Sep 26, 2024
1 parent ff0277e commit 5757415
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pywebpack/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
devDependencies=None,
peerDependencies=None,
aliases=None,
copy=None,
):
"""Initialize webpack bundle.
Expand All @@ -32,6 +33,7 @@ def __init__(
:param devDependencies: npm dev dependencies.
:param peerDependencies: npm peer dependencies.
:param aliases: Webpack resolver aliases.
:param copy: Instructions to copy assets somewhere else.
"""
self.path = path
self.entry = entry or {}
Expand All @@ -41,3 +43,4 @@ def __init__(
"peerDependencies": peerDependencies or {},
}
self.aliases = aliases or {}
self.copy = copy or []
19 changes: 18 additions & 1 deletion pywebpack/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,28 @@ def entry(self):
entries["entries"].update(bundle.entry)
return entries["entries"]

@property
def copy(self):
"""Get instructions for copying assets around."""
copy_instructions = []
for bundle in self.bundles:
for copy in bundle.copy:
if set(copy.keys()) != {"from", "to"}:
raise RuntimeError(
f"Invalid copy instruction: {copy}. "
"Requires exactly 'to' and 'from' keys to be present."
)

# NOTE: the validation check is performed in the JS build step
copy_instructions.append(copy)

return copy_instructions

@property
def config(self):
"""Inject webpack entry points from bundles."""
config = super(WebpackBundleProject, self).config
config.update({"entry": self.entry, "aliases": self.aliases})
config.update({"entry": self.entry, "aliases": self.aliases, "copy": self.copy})
return config

@property
Expand Down
9 changes: 8 additions & 1 deletion tests/test_pywebpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,15 @@ def test_bundleproject(builddir, bundledir, destdir):
"""Test bundle project."""
entry = {"app": "./index.js"}
aliases = {"@app": "index.js"}
copy = [{"from": "./source/", "to": "./target/"}]
bundle = WebpackBundle(
bundledir,
entry=entry,
dependencies={
"lodash": "~4",
},
aliases=aliases,
copy=copy,
)
project = WebpackBundleProject(
working_dir=destdir,
Expand All @@ -243,7 +245,12 @@ def test_bundleproject(builddir, bundledir, destdir):

assert project.bundles == [bundle]
assert project.entry == entry
assert project.config == {"entry": entry, "test": True, "aliases": aliases}
assert project.config == {
"entry": entry,
"test": True,
"aliases": aliases,
"copy": copy,
}
assert project.dependencies == {
"dependencies": {
"lodash": "~4",
Expand Down

0 comments on commit 5757415

Please sign in to comment.