Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #848 from stealjs/serviveworker
bundle assets in slim loader
- Loading branch information
Showing
18 changed files
with
564 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background-image: url(./images/logo.png); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require("./main.css"); | ||
|
||
var xhr = new XMLHttpRequest(); | ||
|
||
xhr.open("GET", "dist/images/logo.png"); | ||
|
||
xhr.onload = function(){ | ||
window.MODULE = {}; | ||
}; | ||
|
||
xhr.send(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<script src="./dist/bundles/main.js" async></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<script src="./dist/steal.production.js" main="main"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* naive version of the css plugin for the slim loader */ | ||
module.exports = function(moduleId, config) { | ||
return new CssModule(config.paths[config.bundles[moduleId]]).injectLink(); | ||
}; | ||
|
||
function CssModule(address) { | ||
this.address = address; | ||
} | ||
|
||
// timeout in seconds | ||
CssModule.waitTimeout = 60; | ||
|
||
CssModule.prototype.linkExists = function() { | ||
var styleSheets = document.styleSheets; | ||
var anchor = document.createElement("a"); | ||
anchor.href = this.address; | ||
var href = anchor.href; | ||
|
||
for (var i = 0; i < styleSheets.length; ++i) { | ||
if (href === styleSheets[i].href) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
CssModule.prototype.injectLink = function() { | ||
if (this._loadPromise) { | ||
return this._loadPromise; | ||
} | ||
|
||
if (this.linkExists()) { | ||
this._loadPromise = Promise.resolve(""); | ||
return this._loadPromise; | ||
} | ||
|
||
// inspired by https://github.com/filamentgroup/loadCSS | ||
var link = (this.link = document.createElement("link")); | ||
link.type = "text/css"; | ||
link.rel = "stylesheet"; | ||
link.href = this.address; | ||
|
||
// wait until the css file is loaded | ||
this._loadPromise = new Promise(function(resolve, reject) { | ||
var timeout = setTimeout(function() { | ||
reject("Unable to load CSS"); | ||
}, CssModule.waitTimeout * 1000); | ||
|
||
var linkEventCallback = function(event) { | ||
clearTimeout(timeout); | ||
|
||
link.removeEventListener("load", linkEventCallback); | ||
link.removeEventListener("error", linkEventCallback); | ||
|
||
if (event && event.type === "error") { | ||
reject("Unable to load CSS"); | ||
} else { | ||
resolve(""); | ||
} | ||
}; | ||
|
||
link.addEventListener("load", linkEventCallback); | ||
link.addEventListener("error", linkEventCallback); | ||
|
||
document.head.appendChild(link); | ||
}); | ||
|
||
return this._loadPromise; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
steal.config({ | ||
ext: { | ||
"css": "steal-css" | ||
}, | ||
paths: { | ||
"steal-css": "../steal-css.js" | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var asap = require("pdenodeify"), | ||
assert = require("assert"), | ||
comparify = require("comparify"), | ||
fs = require("fs-extra"), | ||
multiBuild = require("../index").build, | ||
optimize = require("../index").optimize, | ||
rmdir = require("rimraf"), | ||
path = require("path"), | ||
testHelpers = require("./helpers"); | ||
|
||
var find = testHelpers.find; | ||
var open = testHelpers.open; | ||
|
||
describe("bundleAssets", function(){ | ||
this.timeout(50000); | ||
|
||
before(function(done){ | ||
asap(rmdir)(__dirname + "/bundle_assets/dist") | ||
.then(function(){ | ||
done(); | ||
}); | ||
}); | ||
|
||
it("works with steal-tools build", function(done){ | ||
multiBuild({ | ||
main: "main", | ||
config: path.join(__dirname, "bundle_assets", "stealconfig.js") | ||
}, { | ||
quiet: true, | ||
minify: false, | ||
bundleAssets: { | ||
infer: true, | ||
glob: [ | ||
path.join(__dirname, "bundle_assets", "assets", "*") | ||
] | ||
} | ||
}).then(function(){ | ||
open("test/bundle_assets/prod.html", function(browser, close){ | ||
find(browser, "MODULE", function(module){ | ||
const logo = path.join(__dirname, "bundle_assets", "dist", "images", "logo.png"); | ||
assert(fs.pathExistsSync(logo), "image was copied"); | ||
|
||
const json = path.join(__dirname, "bundle_assets", "dist", "assets", "some.json"); | ||
assert(fs.pathExistsSync(json), "asset was copied"); | ||
|
||
assert(true, "page loaded correctly"); | ||
close(); | ||
}, close); | ||
}, done); | ||
}); | ||
}); | ||
|
||
it("works with steal-tools optimize", function (done) { | ||
optimize({ | ||
main: "main", | ||
config: path.join(__dirname, "bundle_assets", "stealconfig.js") | ||
}, { | ||
quiet: true, | ||
minify: false, | ||
bundleAssets: { | ||
infer: true, | ||
glob: [ | ||
path.join(__dirname, "bundle_assets", "assets", "*") | ||
] | ||
} | ||
}).then(function() { | ||
open("test/bundle_assets/prod-slim.html", function(browser, close){ | ||
find(browser, "MODULE", function(module){ | ||
const logo = path.join(__dirname, "bundle_assets", "dist", "images", "logo.png"); | ||
assert(fs.pathExistsSync(logo), "image was copied"); | ||
|
||
const json = path.join(__dirname, "bundle_assets", "dist", "assets", "some.json"); | ||
assert(fs.pathExistsSync(json), "asset was copied"); | ||
|
||
assert(true, "page loaded correctly"); | ||
close(); | ||
}, close); | ||
}, done); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("test"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "serviceworker", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
|
||
} | ||
} |
Oops, something went wrong.