Skip to content

Commit

Permalink
Make bundle globs work with the modlet pattern
Browse files Browse the repository at this point in the history
This makes it so the following can be used:

```json
"bundle": [
  "pages/**/"
]
```

Which will find modules like:

- pages/home/home
- pages/cart/cart

But will not pick up

- pages/cart/cart-test
  • Loading branch information
matthewp committed Nov 5, 2018
1 parent 672cbc9 commit 444cb85
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
49 changes: 41 additions & 8 deletions lib/loader/find_bundle.js
Expand Up @@ -34,20 +34,31 @@ function findBundles(loader) {

var pattern = [];
if (globs.length > 0) {
var out = [];

pattern = globs.map(function (bundlePattern) {
return substitute(bundlePattern, loader);
});

bundles = glob(pattern, Object.assign({}, globbyOptions)).map(function (bundle) {
bundle = minusJS(bundle);
if(loader.npmContext) {
var app = loader.npmContext.pkgInfo[0];
bundle = app.name + "/" + bundle;
}
return bundle;
pattern = groupPatterns(pattern);

pattern.forEach(pattern => {
var trailingSlash = typeof pattern === "string" && pattern.endsWith("/");

bundles = glob(pattern, Object.assign({}, globbyOptions)).forEach(function (bundle) {
bundle = minusJS(bundle);
if(trailingSlash && !isModlet(bundle)) {
return;
}
if(loader.npmContext) {
var app = loader.npmContext.pkgInfo[0];
bundle = app.name + "/" + bundle;
}
out.push(bundle);
});
});

return [].concat(plain, bundles);
return [].concat(plain, out);

} else {
return plain;
Expand All @@ -60,6 +71,10 @@ function minusJS(name) {
return idx === -1 ? name : name.substr(0, idx);
}

function isModlet(name) {
return /\/(.+)\/\1$/.test(name);
}

function startsWith(prefix, path) {
return path.substr(0,prefix.length) === prefix;
}
Expand All @@ -72,6 +87,24 @@ function split(bundle, plain, globs) {
}
}

function groupPatterns(pattern) {
var current, last, i = 0, out = [];
while(i < pattern.length) {
current = pattern[i];

if(last && current[0] === "!") {
out.pop();
out.push([last, current]);
} else {
out.push(current);
}

last = current;
i++;
}
return out;
}

function substitute(bundle, loader) {
if(loader.npmContext) {
var negation = false;
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/bundle/pages/home/home-test.js
@@ -0,0 +1 @@
module.exports = "test";
2 changes: 2 additions & 0 deletions test/bundle/pages/home/home.js
@@ -0,0 +1,2 @@

module.exports = "home";
29 changes: 25 additions & 4 deletions test/bundle_test.js
Expand Up @@ -26,6 +26,31 @@ describe("find bundle", function () {
'app_d'
], bundles);
});

it("wildcard with extension", function() {
fakeLoader["bundle"] = ["pages/*.component"];
var bundles = findbundle(fakeLoader);
assert.deepEqual([
'pages/cart.component',
], bundles);
});

it("the steal convention trailing slash", function() {
fakeLoader["bundle"] = ["pages/**/"];
var bundles = findbundle(fakeLoader);
assert.deepEqual([
'pages/home/home',
], bundles);
});

it("trailing slash and wildcard extension", function() {
fakeLoader["bundle"] = ["pages/**/", "pages/*.component"];
var bundles = findbundle(fakeLoader);
assert.deepEqual([
'pages/home/home',
'pages/cart.component'
], bundles);
});
});

describe("bundle", function () {
Expand Down Expand Up @@ -139,7 +164,3 @@ describe("bundle", function () {
});

});




0 comments on commit 444cb85

Please sign in to comment.