Skip to content

Commit

Permalink
Merge af13119 into b78bc74
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed May 20, 2020
2 parents b78bc74 + af13119 commit 4e69ed5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -24,6 +24,7 @@ env:
- REQUIREMENTS=devel

python:
- "2.7"
- "3.6"
- "3.7"
- "3.8"
Expand Down
5 changes: 4 additions & 1 deletion pywebpack/bundle.py
Expand Up @@ -16,7 +16,8 @@ class WebpackBundle(object):
"""Webpack bundle."""

def __init__(self, path, entry=None, dependencies=None,
devDependencies=None, peerDependencies=None):
devDependencies=None, peerDependencies=None,
aliases=None):
"""Initialize webpack bundle.
:param path: Absolute path to the folder where the assets are
Expand All @@ -26,6 +27,7 @@ def __init__(self, path, entry=None, dependencies=None,
:param dependencies: npm dependencies.
:param devDependencies: npm dev dependencies.
:param peerDependencies: npm peer dependencies.
:param aliases: Webpack resolver aliases.
"""
self.path = path
self.entry = entry or {}
Expand All @@ -34,3 +36,4 @@ def __init__(self, path, entry=None, dependencies=None,
'devDependencies': devDependencies or {},
'peerDependencies': peerDependencies or {},
}
self.aliases = aliases or {}
22 changes: 21 additions & 1 deletion pywebpack/project.py
Expand Up @@ -200,9 +200,29 @@ def entry(self):
def config(self):
"""Inject webpack entry points from bundles."""
config = super(WebpackBundleProject, self).config
config.update({'entry': self.entry})
config.update({'entry': self.entry, 'aliases': self.aliases})
return config

@property
def aliases(self):
"""Get webpack resolver aliases from bundles."""
aliases = dict(aliases=dict(), paths=dict())
error = 'Duplicated alias for `{0}:{1}` in bundle `{2}` and ' \
'`{3}:{4}` in bundle `{5}`. Please choose another alias name.'

for bundle in self.bundles:
for alias, path in bundle.aliases.items():
# Check that there are no duplicated aliases
if alias in aliases['aliases']:
prev_path, prev_bundle_path = aliases['paths'][alias]
raise RuntimeError(error.format(
alias, prev_path, prev_bundle_path,
alias, path, bundle.path))
aliases['paths'][alias] = (path, bundle.path)

aliases['aliases'].update(bundle.aliases)
return aliases['aliases']

@property
@cached
def dependencies(self):
Expand Down
27 changes: 15 additions & 12 deletions pywebpack/storage.py
Expand Up @@ -11,8 +11,9 @@

from __future__ import absolute_import, print_function

from os import makedirs, remove, scandir, symlink, walk
from os.path import dirname, exists, getmtime, islink, join, realpath, relpath
from os import listdir, makedirs, remove, symlink, walk
from os.path import dirname, exists, getmtime, isfile, islink, join, \
realpath, relpath
from shutil import copy


Expand All @@ -30,19 +31,21 @@ def iter_paths(folder, root=None, depth=None):
root = root or folder # needed to compute the relative name

if depth is None: # yield all paths
yield from iter_files(folder)
for result in iter_files(folder):
yield result
elif depth == 0:
yield folder, relpath(folder, root)
else:
for entry in scandir(folder):
if entry.is_file(): # always yield files no matter the depth
f = join(folder, entry.name)
for entry in listdir(folder):
if isfile(join(folder, entry)):
# Always yield files no matter the depth
f = join(folder, entry)
yield f, relpath(f, root)
else:
yield from iter_paths(
join(folder, entry.name),
root=root, depth=(depth - 1)
)
for result in iter_paths(
join(folder, entry), root=root, depth=(depth - 1)
):
yield result


class FileStorage(object):
Expand Down Expand Up @@ -81,9 +84,9 @@ def run(self, force=None):
class LinkStorage(FileStorage):
"""Storage class that link files."""

def __init__(self, *args, depth=None, **kwargs):
def __init__(self, *args, **kwargs):
"""Initialize storage."""
self.depth = depth
self.depth = kwargs.pop('depth', None)
super(LinkStorage, self).__init__(*args, **kwargs)

def __iter__(self):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_pywebpack.py
Expand Up @@ -136,12 +136,14 @@ def test_templateproject_buildall(templatedir, destdir):
def test_bundleproject(builddir, bundledir, destdir):
"""Test bundle project."""
entry = {'app': './index.js'}
aliases = {'@app': 'index.js'}
bundle = WebpackBundle(
bundledir,
entry=entry,
dependencies={
'lodash': '~4',
}
},
aliases=aliases,
)
project = WebpackBundleProject(
working_dir=destdir,
Expand All @@ -152,7 +154,7 @@ def test_bundleproject(builddir, bundledir, destdir):

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

0 comments on commit 4e69ed5

Please sign in to comment.