Skip to content

Commit

Permalink
define.withImports to reduce boilerplate a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian MacLeod committed Feb 26, 2015
1 parent e6ea0ce commit 70a4c0a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/lib/module.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,60 @@
*/
define.amd = {};

// `define.withImports`

/**
* Define an AMD module in a HTML import, and reference any dependencies that
* are expressed via <link rel="import"> elements.
*
* This is a simple wrapper around `define` that reduces module boilerplate:
*
* <link rel="import" href="./foo.html">
* <link rel="import" href="../bar.html">
* &lt;script&gt;
* define(['./foo.html', '../bar.html'], function(foo, bar) {
* // ...
* });
* &lt;/script&gt;
*
* To something a little more convenient:
*
* <link rel="import" href="./foo.html">
* <link rel="import" href="../bar.html">
* &ltscript&gt;
* define(function(foo, bar) {
* // ...
* });
* &lt/script&gt;
*
* **All** import link elements will be declared as dependencies for the
* defined module, and they will be declared in the same order.
*
* @param {string=} id The id of the module being defined. If not provided,
* one will be given to the module based on the document it was called in.
* @param {function(...*)|*} factory A function that is given the exported
* values for `dependencies`, in the same order. Alternatively, you can
* pass the exported value directly.
*/
function defineWithImports(id, factory) {
var importDoc = document.currentScript && document.currentScript.ownerDocument;
if (!importDoc) {
throw new Error('defineWithImports must be called within an imported document');
}

var imports = importDoc.querySelectorAll('link[rel=import]');
var dependencies = Array.prototype.map.call(imports, function(link) {
return link.href;
});

if (arguments.length === 1) {
define(dependencies, id);
} else {
define(id, dependencies, factory);
}
}
define.withImports = defineWithImports;

// Utility

/** @return {string} A module id inferred from the current document/import. */
Expand Down
24 changes: 24 additions & 0 deletions test/assets/modules/fancy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="one.html">
<link rel="import" href="sub/three.html">

<script>

define.withImports(function(one, three) {
return {
one: one,
me: 'module 2',
three: three,
};
});

</script>
1 change: 0 additions & 1 deletion test/assets/modules/one.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../../src/lib/module.html">

<script>
Expand Down
1 change: 0 additions & 1 deletion test/assets/modules/sub/three.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../../../src/lib/module.html">

<script>
Expand Down
1 change: 0 additions & 1 deletion test/assets/modules/two.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../../../src/lib/module.html">
<link rel="import" href="one.html">
<link rel="import" href="sub/three.html">
Expand Down
14 changes: 14 additions & 0 deletions test/unit/module.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<link rel="import" href="../../src/lib/module.html">
<link rel="import" href="../assets/modules/one.html">
<link rel="import" href="../assets/modules/two.html">
<link rel="import" href="../assets/modules/fancy.html">
</head>
<body>

Expand Down Expand Up @@ -114,6 +115,19 @@

});

suite('define.withImports()', function() {

test('loads imported modules for you', function(done) {
define(['../assets/modules/fancy.html'], function(two) {
assert.equal(two.one, 'module 1');
assert.equal(two.me, 'module 2');
assert.equal(two.three, 'module 3');
done();
});
});

});

</script>
</body>
</html>

0 comments on commit 70a4c0a

Please sign in to comment.