|
398 | 398 |
|
399 | 399 | /** |
400 | 400 | * Path matching the url from which the element was imported. |
| 401 | + * |
401 | 402 | * This path is used to resolve url's in template style cssText. |
402 | 403 | * The `importPath` property is also set on element instances and can be |
403 | 404 | * used to create bindings relative to the import path. |
404 | | - * Defaults to the path matching the url containing a `dom-module` element |
405 | | - * matching this element's static `is` property. |
| 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. |
| 410 | + * |
406 | 411 | * Note, this path should contain a trailing `/`. |
407 | 412 | * |
408 | 413 | * @return {string} The import path for this element class |
409 | 414 | */ |
410 | 415 | static get importPath() { |
411 | 416 | if (!this.hasOwnProperty(JSCompiler_renameProperty('_importPath', this))) { |
| 417 | + const meta = this.importMeta; |
| 418 | + if (meta) { |
| 419 | + this._importPath = Polymer.ResolveUrl.pathFromUrl(meta.url); |
| 420 | + } else { |
412 | 421 | 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; |
| 422 | + this._importPath = (module && module.assetpath) || |
| 423 | + Object.getPrototypeOf(/** @type {PolymerElementConstructor}*/ (this).prototype).constructor.importPath; |
| 424 | + } |
415 | 425 | } |
416 | 426 | return this._importPath; |
417 | 427 | } |
418 | 428 |
|
| 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 | + |
419 | 441 | constructor() { |
420 | 442 | super(); |
421 | 443 | /** @type {HTMLTemplateElement} */ |
|
447 | 469 | _initializeProperties() { |
448 | 470 | Polymer.telemetry.instanceCount++; |
449 | 471 | this.constructor.finalize(); |
450 | | - const importPath = this.constructor.importPath; |
451 | 472 | // note: finalize template when we have access to `localName` to |
452 | 473 | // avoid dependence on `is` for polyfilling styling. |
453 | 474 | this.constructor._finalizeTemplate(/** @type {!HTMLElement} */(this).localName); |
454 | 475 | super._initializeProperties(); |
455 | 476 | // set path defaults |
456 | 477 | this.rootPath = Polymer.rootPath; |
457 | | - this.importPath = importPath; |
| 478 | + this.importPath = this.constructor.importPath; |
458 | 479 | // apply property defaults... |
459 | 480 | let p$ = propertyDefaults(this.constructor); |
460 | 481 | if (!p$) { |
|
0 commit comments