t2ym / i18n-element

I18N Base Element for Lit and Polymer
Other
9 stars 1 forks source link

[Polymer 2.4] Support Polymer.html`<tag></tag>` #57

Closed t2ym closed 5 years ago

t2ym commented 6 years ago

[Polymer 2.4] Support Polymer.html``

Notes

Example Code

class I18nSubclassElement extends BaseElements.I18nElement {
  static get is() { return 'i18n-subclass-element'; }
  static get template() {
    return this._i18nPreprocess(Polymer.html`
      <span id="label1">Subclass UI label 1</span><br>
      <span id="label2">Subclass UI label 2</span><br>
      <span id="label3">Subclass UI label 3</span><br>
    `);
  }
}
customElements.define(I18nSubclassElement.is, I18nSubclassElement);
t2ym commented 6 years ago

Script to generate <dom-module> for gulp-i18n-preprocess from Polymer.html templates

unmodularize.js

#!/usr/bin/env node
/*
@license https://github.com/t2ym/thin-hook/blob/master/LICENSE.md
Copyright (c) 2018, Tetsuya Mori <t2y3141592@gmail.com>. All rights reserved.
*/

const fs = require('fs');
const path = require('path');

if (process.argv.length <= 2) {
  let cmd = path.basename(process.argv[1]);
  console.log(
`Unmodularize Polymer.html templates for gulp-i18n-preprocess

Usage: ${cmd} --htmlTemplate='{htmlTemplate}' my-custom-element.js ...

Options:
  --htmlTemplate={htmlTemplate} : HTML template. Default value: '<link rel="import" href="../../../i18n-element.html"><dom-module>'

Outputs: my-custom-element.html ...
(Target extensions are converted from .js to .html)
`);
}

let htmlTemplate = `<link rel="import" href="../../../i18n-element.html"><dom-module>`;

for (let i = 2; i < process.argv.length; i++) {
  let match = process.argv[i].match(/^--htmlTemplate=(.*)$/);
  if (match) {
    htmlTemplate = match[1];
    continue;
  }
  match = process.argv[i].match(/^(.*)[.]js$/)
  if (match) {
    let code = fs.readFileSync(process.argv[i], 'UTF-8');
    let outPath = match[1] + '.html';
    let name = path.basename(match[1]);
    let template = code.match(/Polymer[.]html`([^`]*)`/);
    if (!template) {
      console.error('Cannot find template in ' + process.argv[i]);
      continue;
    }
    let html = htmlTemplate.replace('<dom-module>', `<dom-module id="${name}"><template>${template[1]}</template></dom-module>\n`);
    fs.writeFileSync(outPath, html);
  }
}

Example Command

./unmodularize.js --htmlTemplate='<link rel="import" href="../../../../i18n-element.html"><dom-module>' i18n-subclass-element.js

Example Input

class I18nSubclassElement extends BaseElements.I18nElement {
  static get is() { return 'i18n-subclass-element'; }
  static get template() {
    return this._i18nPreprocess(Polymer.html`
      <span id="label1">Subclass UI label 1</span><br>
      <span id="label2">Subclass UI label 2</span><br>
      <span id="label3">Subclass UI label 3</span><br>
    `);
  }
}
customElements.define(I18nSubclassElement.is, I18nSubclassElement);

Example Output

<link rel="import" href="../../../../i18n-element.html"><dom-module id="i18n-subclass-element"><template>
      <span id="label1">Subclass UI label 1</span><br>
      <span id="label2">Subclass UI label 2</span><br>
      <span id="label3">Subclass UI label 3</span><br>
    </template></dom-module>

Extracted Core Logic to reuse in custom build processes

let htmlTemplate = `<link rel="import" href="../../../i18n-element.html"><dom-module>`;
let code = '**JavaScript with Polymer.html**';
let template = code.match(/Polymer[.]html`([^`]*)`/);
let html = htmlTemplate.replace('<dom-module>', 
  `<dom-module id="${name}"><template>${template[1]}</template></dom-module>\n`);

Notes

t2ym commented 5 years ago

Supported in 3.0.0-rc.1