From ac34cf668749bf915fd6fdf8c4c7d0a847ea0ef5 Mon Sep 17 00:00:00 2001 From: gossi Date: Thu, 15 Apr 2021 09:27:47 +0200 Subject: [PATCH 1/2] first try at html-first .gc syntax --- index.js | 3 ++ lib/gc-preprocessor-plugin.js | 42 +++++++++++++++++++ tests/dummy/app/components/gc-no-class.gc | 13 ++++++ .../dummy/app/components/gc-template-only.gc | 1 + tests/dummy/app/components/gc-test.gc | 39 +++++++++++++++++ tests/dummy/app/templates/application.hbs | 13 ++++-- 6 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 lib/gc-preprocessor-plugin.js create mode 100644 tests/dummy/app/components/gc-no-class.gc create mode 100644 tests/dummy/app/components/gc-template-only.gc create mode 100644 tests/dummy/app/components/gc-test.gc diff --git a/index.js b/index.js index 8e2b235..888b09a 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,9 @@ module.exports = { setupPreprocessorRegistry(type, registry) { if (type === 'parent') { + let GlimmerComponentPreprocessor = require('./lib/gc-preprocessor-plugin'); + registry.add('js', new GlimmerComponentPreprocessor()); + let TemplateImportPreprocessor = require('./lib/preprocessor-plugin'); registry.add( 'js', diff --git a/lib/gc-preprocessor-plugin.js b/lib/gc-preprocessor-plugin.js new file mode 100644 index 0000000..50533b2 --- /dev/null +++ b/lib/gc-preprocessor-plugin.js @@ -0,0 +1,42 @@ +const stew = require('broccoli-stew'); + +const IMPORT = `import { hbs } from 'ember-template-imports';`; + +module.exports = class GlimmerComponentPreprocessor { + constructor() { + this.name = 'glimmer-component-preprocessor'; + } + + toTree(tree) { + let compiled = stew.map(tree, `**/*.gc`, (contents) => { + const scriptRegex = /([\s\S]*?)<\/script>/i; + const scriptTag = contents.match(scriptRegex); + + if (scriptTag) { + const template = contents.replace(scriptRegex, ''); + let script = scriptTag[1].trim(); + + // need to check for class here + const signatureRegex = /export default class [^{]*? {/gi; + const signature = script.match(signatureRegex); + + if (signature) { + script = script.replace( + signature, + `${signature}\n static template = hbs\`${template}\`;\n` + ); + + return `${IMPORT} ${script}`; + } + + return `${IMPORT} ${script} export default hbs\`${template}\`;`; + } + + return `${IMPORT} export default hbs\`${contents}\`;`; + }); + + return stew.rename(compiled, (name) => { + return name.replace(/\.gc$/, '.js'); + }); + } +}; diff --git a/tests/dummy/app/components/gc-no-class.gc b/tests/dummy/app/components/gc-no-class.gc new file mode 100644 index 0000000..3351064 --- /dev/null +++ b/tests/dummy/app/components/gc-no-class.gc @@ -0,0 +1,13 @@ + + +Hello, I am + + bold + + +2 + 1 = {{plusOne 2}} +! diff --git a/tests/dummy/app/components/gc-template-only.gc b/tests/dummy/app/components/gc-template-only.gc new file mode 100644 index 0000000..7606281 --- /dev/null +++ b/tests/dummy/app/components/gc-template-only.gc @@ -0,0 +1 @@ +Hello, I am bold! diff --git a/tests/dummy/app/components/gc-test.gc b/tests/dummy/app/components/gc-test.gc new file mode 100644 index 0000000..5e4cf79 --- /dev/null +++ b/tests/dummy/app/components/gc-test.gc @@ -0,0 +1,39 @@ + + +Hello, I am + + bold + +! + +2 + 1 = {{plusOne 2}} + +Counter: {{this.counter}} + + diff --git a/tests/dummy/app/templates/application.hbs b/tests/dummy/app/templates/application.hbs index b4aed8b..9af5ac6 100644 --- a/tests/dummy/app/templates/application.hbs +++ b/tests/dummy/app/templates/application.hbs @@ -1,6 +1,11 @@ -{{page-title "Dummy"}} +{{page-title 'Dummy'}} -

Welcome to Ember

+

+ Welcome to Ember +

- - + + + + + \ No newline at end of file From 5f09e59626afb16e2442a36036bb4d33a8c26bc5 Mon Sep 17 00:00:00 2001 From: gossi Date: Thu, 15 Apr 2021 09:34:29 +0200 Subject: [PATCH 2/2] cleanup template --- tests/dummy/app/components/gc-test.gc | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/dummy/app/components/gc-test.gc b/tests/dummy/app/components/gc-test.gc index 5e4cf79..a560f3e 100644 --- a/tests/dummy/app/components/gc-test.gc +++ b/tests/dummy/app/components/gc-test.gc @@ -1,9 +1,9 @@ -Hello, I am - - bold - -! - 2 + 1 = {{plusOne 2}} Counter: {{this.counter}}