Skip to content

Commit 616f666

Browse files
committed
Add ability to define importMeta on legacy elements. Fixes #5163
1 parent 689ff72 commit 616f666

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

lib/legacy/legacy-element-mixin.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@
8686
this._applyListeners();
8787
}
8888

89+
/**
90+
* Forwards `importMeta` from the prototype (i.e. from the info object
91+
* passed to `Polymer({...})`) to the static API.
92+
*
93+
* @return {!Object} The `import.meta` object set on the prototype
94+
*/
95+
static get importMeta() {
96+
return this.prototype.importMeta;
97+
}
98+
8999
/**
90100
* Legacy callback called during the `constructor`, for overriding
91101
* by the user.

lib/mixins/element-mixin.html

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,13 @@
402402
* This path is used to resolve url's in template style cssText.
403403
* The `importPath` property is also set on element instances and can be
404404
* used to create bindings relative to the import path.
405-
* For elements defined in ES modules, users should implement `importMeta`
406-
* and this getter will return `import.meta.url`'s path. For elements
407-
* defined in HTML imports, this getter will return the path to the
408-
* document containing a `dom-module` element matching this element's
409-
* static `is` property.
405+
*
406+
* For elements defined in ES modules, users should implement
407+
* `static get importMeta() { return import.meta; }` and the default
408+
* implementation of `importPath` will return `import.meta.url`'s path.
409+
* For elements defined in HTML imports, this getter will return the path
410+
* to the document containing a `dom-module` element matching this
411+
* element's static `is` property.
410412
*
411413
* Note, this path should contain a trailing `/`.
412414
*
@@ -426,18 +428,6 @@
426428
return this._importPath;
427429
}
428430

429-
/**
430-
* When an element definition is being loaded from an ES module, users
431-
* may override this getter to return the `import.meta` object from that
432-
* module, which will be used to derive the `importPath` for the element.
433-
* When implementing `importMeta`, users should not implement `importPath`.
434-
*
435-
* @return {!Object} The `import.meta` object for the element's module
436-
*/
437-
static get importMeta() {
438-
return null;
439-
}
440-
441431
constructor() {
442432
super();
443433
/** @type {HTMLTemplateElement} */

test/unit/resolveurl.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@
8282

8383
test('Urls in styles and attributes', testStylesAndAttributes('p-r', 'sub'));
8484

85-
test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://foo.com/mymodule'));
85+
test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://class.com/mymodule'));
86+
87+
test('Urls in styles and attributes (importMeta, hybrid)', testStylesAndAttributes('p-r-hybrid', 'http://hybrid.com/mymodule'));
8688

8789
test('url changes via setting importPath/rootPath on element instance', function() {
8890
var el = document.createElement('p-r');

test/unit/sub/resolveurl-elements.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,26 @@
3939
}
4040
customElements.define(PR.is, PR);
4141

42-
class PRImportMeta extends PR {
42+
class PRImportMeta extends Polymer.Element {
43+
static get template() {
44+
return Polymer.DomModule.import('p-r', 'template').cloneNode(true);
45+
}
4346
static get importMeta() {
4447
// Idiomatically, this would be `return import.meta`, but for purposes
4548
// of stubbing the test without actual modules, it's shimmed
46-
return { url: 'http://foo.com/mymodule/index.js' }
49+
return { url: 'http://class.com/mymodule/index.js' };
4750
}
4851
}
4952
customElements.define('p-r-im', PRImportMeta);
53+
54+
const PRHybrid = Polymer({
55+
is: 'p-r-hybrid',
56+
_template: Polymer.DomModule.import('p-r', 'template').cloneNode(true),
57+
// Idiomatically, this would be `return import.meta`, but for purposes
58+
// of stubbing the test without actual modules, it's shimmed
59+
importMeta: { url: 'http://hybrid.com/mymodule/index.js' }
60+
});
61+
5062
</script>
5163

5264
<dom-module id="p-r-ap" assetpath="../../assets/"></dom-module>

0 commit comments

Comments
 (0)