Skip to content

Commit f7672da

Browse files
committed
Add importMeta getter to derive importPath from modules. Fixes #5163
1 parent 569ff37 commit f7672da

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

lib/mixins/element-mixin.html

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,33 @@
409409
*/
410410
static get importPath() {
411411
if (!this.hasOwnProperty(JSCompiler_renameProperty('_importPath', this))) {
412+
const meta = this.importMeta;
413+
if (meta) {
414+
this._importPath = meta.url.slice(0, meta.url.lastIndexOf('/') + 1);
415+
} else {
412416
const module = Polymer.DomModule && Polymer.DomModule.import(/** @type {PolymerElementConstructor} */ (this).is);
413-
this._importPath = module ? module.assetpath : '' ||
414-
Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.importPath;
417+
if (module) {
418+
this._importPath = module ? module.assetpath : '';
419+
} else {
420+
this._importPath = Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.importPath;
421+
}
422+
}
415423
}
416424
return this._importPath;
417425
}
418426

427+
/**
428+
* When an element definition is being loaded from an ES module, users
429+
* may override this getter to return the `import.meta` object from that
430+
* module, which will be used to derive the `importPath` for the element.
431+
* When implementing `importMeta`, users should not implement `importPath`.
432+
*
433+
* @return {!Object} The `import.meta` object for the element's module
434+
*/
435+
static get importMeta() {
436+
return null;
437+
}
438+
419439
constructor() {
420440
super();
421441
/** @type {HTMLTemplateElement} */
@@ -447,14 +467,13 @@
447467
_initializeProperties() {
448468
Polymer.telemetry.instanceCount++;
449469
this.constructor.finalize();
450-
const importPath = this.constructor.importPath;
451470
// note: finalize template when we have access to `localName` to
452471
// avoid dependence on `is` for polyfilling styling.
453472
this.constructor._finalizeTemplate(/** @type {!HTMLElement} */(this).localName);
454473
super._initializeProperties();
455474
// set path defaults
456475
this.rootPath = Polymer.rootPath;
457-
this.importPath = importPath;
476+
this.importPath = this.constructor.importPath;
458477
// apply property defaults...
459478
let p$ = propertyDefaults(this.constructor);
460479
if (!p$) {

test/unit/resolveurl.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
<script>
5353
suite('ResolveUrl', function() {
5454

55-
test('Urls in styles and attributes', function() {
56-
var el = document.createElement('p-r');
55+
const testStylesAndAttributes = (elementName, folder) => () => {
56+
var el = document.createElement(elementName);
5757
document.body.appendChild(el);
58-
var resolvedUrl = /sub\/foo\.z/;
58+
var resolvedUrl = new RegExp(`${folder}/foo\\.z`);
5959
var styleHashUrl = /url\('#bar'\)/;
6060
var styleAbsUrl = /url\('\/zot'\)/;
6161
var style = el.shadowRoot.querySelector('style') || document.querySelector('style[scope=p-r]');
@@ -78,7 +78,11 @@
7878
assert.equal(el.$.absolute.getAttribute('href'), '/foo.z', 'absolute urls should not be resolved');
7979
assert.equal(el.$.protocol.getAttribute('href'), 'data:foo.z', 'urls with other protocols should not be resolved');
8080
document.body.removeChild(el);
81-
});
81+
}
82+
83+
test('Urls in styles and attributes', testStylesAndAttributes('p-r', 'sub'));
84+
85+
test('Urls in styles and attributes (importMeta)', testStylesAndAttributes('p-r-im', 'http://foo.com/mymodule'));
8286

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

test/unit/sub/resolveurl-elements.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
static get is() { return 'p-r'; }
3939
}
4040
customElements.define(PR.is, PR);
41+
42+
class PRImportMeta extends PR {
43+
static get importMeta() {
44+
// Idiomatically, this would be `return import.meta`, but for purposes
45+
// of stubbing the test without actual modules, it's shimmed
46+
return { url: 'http://foo.com/mymodule/index.js' }
47+
}
48+
}
49+
customElements.define('p-r-im', PRImportMeta);
4150
</script>
4251

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

0 commit comments

Comments
 (0)