diff --git a/pywebpack/bundle.py b/pywebpack/bundle.py index eef9d55..eab304d 100644 --- a/pywebpack/bundle.py +++ b/pywebpack/bundle.py @@ -21,6 +21,7 @@ def __init__( devDependencies=None, peerDependencies=None, aliases=None, + copy=None, ): """Initialize webpack bundle. @@ -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 {} @@ -41,3 +43,4 @@ def __init__( "peerDependencies": peerDependencies or {}, } self.aliases = aliases or {} + self.copy = copy or [] diff --git a/pywebpack/project.py b/pywebpack/project.py index 9892452..b306b7c 100644 --- a/pywebpack/project.py +++ b/pywebpack/project.py @@ -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 diff --git a/tests/test_pywebpack.py b/tests/test_pywebpack.py index 5a0eb27..3ef9b80 100644 --- a/tests/test_pywebpack.py +++ b/tests/test_pywebpack.py @@ -226,6 +226,7 @@ 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, @@ -233,6 +234,7 @@ def test_bundleproject(builddir, bundledir, destdir): "lodash": "~4", }, aliases=aliases, + copy=copy, ) project = WebpackBundleProject( working_dir=destdir, @@ -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",