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..a560f3e --- /dev/null +++ b/tests/dummy/app/components/gc-test.gc @@ -0,0 +1,33 @@ + + +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