Skip to content

Commit

Permalink
Merge pull request #1090 from stealjs/bundle-wildcard
Browse files Browse the repository at this point in the history
Make bundle globs work with the modlet pattern
  • Loading branch information
matthewp committed Nov 5, 2018
2 parents 672cbc9 + fa1ad6f commit e298a5c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 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 () {
});

});




2 changes: 1 addition & 1 deletion test/helpers.js
Expand Up @@ -12,7 +12,7 @@ exports.find = function(browser, property, callback, done){
} else if(new Date() - start < 2000){
setTimeout(check, 20);
} else {
done("failed to find "+property+" in "+browser.window.location.href);
done(new Error("failed to find "+property+" in "+browser.window.location.href));
}
};
check();
Expand Down
19 changes: 14 additions & 5 deletions test/multibuild_test.js
Expand Up @@ -1507,6 +1507,7 @@ describe("multi build", function(){


it("should not include src/dep and jqueryt into the bundled file", function(done){
this.timeout(15000);
setup(function(error) {
if (error) {
return done(error);
Expand All @@ -1529,22 +1530,30 @@ describe("multi build", function(){
assert.equal(data.loader.envs['window-production'].map.jqueryt, '@empty', 'ignore modules must declare as @empty');

open("test/bundle_false/prod.html",function(browser, close){
var complete = 0;
var findDone = function() {
complete++;
if(complete === 2) {
close();
}
};

find(browser,"MODULE", function(module){
//assert.ok(module);
assert.equal(typeof module.name, "undefined", "depending Module shouldn't have been loaded");
}, close);
findDone();
}, findDone);

find(browser,"$", function(jqueryt){
// jqueryt is mapped to @empty
assert.ok(jqueryt);
var jversion = false;
try{
jversion = jqueryt.fn.version;
}catch(e){}
} catch(e) {}

assert.strictEqual(jversion, false, "jqueryt Module shouldn't have been loaded");
close();
}, close);
findDone();
}, findDone);

}, done);
}).catch(done);
Expand Down

0 comments on commit e298a5c

Please sign in to comment.