Skip to content

Commit

Permalink
Merge pull request #5180 from Polymer/5163-kschaaf-importMeta-legacy
Browse files Browse the repository at this point in the history
Add ability to define importMeta on hybrid elements. Fixes #5163
  • Loading branch information
kevinpschaaf authored Apr 6, 2018
2 parents 689ff72 + 61ca60e commit 0d8b34e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
12 changes: 12 additions & 0 deletions lib/legacy/legacy-element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@
this._applyListeners();
}

/**
* Forwards `importMeta` from the prototype (i.e. from the info object
* passed to `Polymer({...})`) to the static API.
*
* @return {!Object} The `import.meta` object set on the prototype
* @suppress {missingProperties} `this` is always in the instance in
* closure for some reason even in a static method, rather than the class
*/
static get importMeta() {
return this.prototype.importMeta;
}

/**
* Legacy callback called during the `constructor`, for overriding
* by the user.
Expand Down
2 changes: 1 addition & 1 deletion lib/legacy/polymer.dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
* This method facades to `Polymer.enqueueDebouncer`.
*
* @memberof Polymer.dom
* @param {Polymer.Debouncer} debouncer Debouncer to enqueue
* @param {!Polymer.Debouncer} debouncer Debouncer to enqueue
*/
Polymer.dom.addDebouncer = Polymer.enqueueDebouncer;
})();
Expand Down
25 changes: 8 additions & 17 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,18 @@
* This path is used to resolve url's in template style cssText.
* The `importPath` property is also set on element instances and can be
* used to create bindings relative to the import path.
* For elements defined in ES modules, users should implement `importMeta`
* and this getter will return `import.meta.url`'s path. For elements
* defined in HTML imports, this getter will return the path to the
* document containing a `dom-module` element matching this element's
* static `is` property.
*
* For elements defined in ES modules, users should implement
* `static get importMeta() { return import.meta; }`, and the default
* implementation of `importPath` will return `import.meta.url`'s path.
* For elements defined in HTML imports, this getter will return the path
* to the document containing a `dom-module` element matching this
* element's static `is` property.
*
* Note, this path should contain a trailing `/`.
*
* @return {string} The import path for this element class
* @suppress {missingProperties}
*/
static get importPath() {
if (!this.hasOwnProperty(JSCompiler_renameProperty('_importPath', this))) {
Expand All @@ -426,18 +429,6 @@
return this._importPath;
}

/**
* When an element definition is being loaded from an ES module, users
* may override this getter to return the `import.meta` object from that
* module, which will be used to derive the `importPath` for the element.
* When implementing `importMeta`, users should not implement `importPath`.
*
* @return {!Object} The `import.meta` object for the element's module
*/
static get importMeta() {
return null;
}

constructor() {
super();
/** @type {HTMLTemplateElement} */
Expand Down
2 changes: 1 addition & 1 deletion lib/mixins/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
READ_ONLY: '__readOnly'
};

/** @const {string} */
/** @const {RegExp} */
const capitalAttributeRegex = /[A-Z]/;

/**
Expand Down
4 changes: 3 additions & 1 deletion test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@

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

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

test('Urls in styles and attributes (importMeta, hybrid)', testStylesAndAttributes('p-r-hybrid', 'http://hybrid.com/mymodule'));

test('url changes via setting importPath/rootPath on element instance', function() {
var el = document.createElement('p-r');
Expand Down
34 changes: 24 additions & 10 deletions test/unit/sub/resolveurl-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,33 @@
</template>
</dom-module>
<script>
class PR extends Polymer.Element {
static get is() { return 'p-r'; }
}
customElements.define(PR.is, PR);
window.addEventListener('WebComponentsReady', () => {
class PR extends Polymer.Element {
static get is() { return 'p-r'; }
}
customElements.define(PR.is, PR);

class PRImportMeta extends PR {
static get importMeta() {
class PRImportMeta extends Polymer.Element {
static get template() {
return Polymer.DomModule.import('p-r', 'template').cloneNode(true);
}
static get importMeta() {
// Idiomatically, this would be `return import.meta`, but for purposes
// of stubbing the test without actual modules, it's shimmed
return { url: 'http://class.com/mymodule/index.js' };
}
}
customElements.define('p-r-im', PRImportMeta);

const PRHybrid = Polymer({
is: 'p-r-hybrid',
_template: Polymer.DomModule.import('p-r', 'template').cloneNode(true),
// Idiomatically, this would be `return import.meta`, but for purposes
// of stubbing the test without actual modules, it's shimmed
return { url: 'http://foo.com/mymodule/index.js' }
}
}
customElements.define('p-r-im', PRImportMeta);
importMeta: { url: 'http://hybrid.com/mymodule/index.js' }
});

});
</script>

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

0 comments on commit 0d8b34e

Please sign in to comment.