Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define.withImports to reduce boilerplate a bit #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens with html imports that don't export anything because they don't have a script? it'd be nice to get their <dom-module> or document...

*
* @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>