-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69d4e81
commit 82cfc71
Showing
5 changed files
with
167 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { esmsh, jsdelivr } from './cdn.js'; | ||
|
||
/** | ||
* @param {import('../types.ts').ResolvedCompilerOptions} config | ||
* @param {import('../types.ts').PublicMethods} api | ||
*/ | ||
export async function compiler(config = {}, api) { | ||
const versions = config.versions || {}; | ||
|
||
const [babel, templatePlugin, { default: templateCompiler }, { Preprocessor }] = | ||
await esmsh.importAll(versions, [ | ||
'@babel/standalone', | ||
'babel-plugin-ember-template-compilation', | ||
'ember-source/dist/ember-template-compiler.js', | ||
'content-tag', | ||
// Failed to load (will need to PR for browser support), | ||
// so we have to use babel's decorator transforms, | ||
// which ... aren't great. | ||
// They force over-transforming of classes. | ||
// 'decorator-transforms', | ||
]); | ||
|
||
const preprocessor = new Preprocessor(); | ||
|
||
return { | ||
compile: async (text) => { | ||
let preprocessed = preprocessor.process(text, 'dynamic-repl.js'); | ||
let transformed = await transform({ babel, templatePlugin, templateCompiler }, preprocessed); | ||
|
||
return transformed.code; | ||
}, | ||
render: async (element, compiled, extra, compiler) => { | ||
Check failure on line 32 in packages/repl-sdk/src/compilers/glimmer-js.js GitHub Actions / autofix
|
||
element.innerHTML = compiled; | ||
}, | ||
}; | ||
} | ||
|
||
async function transform({ babel, templatePlugin, templateCompiler }, text) { | ||
console.log(templateCompiler, templatePlugin); | ||
return babel.transform(text, { | ||
filename: `dynamic-repl.js`, | ||
plugins: [ | ||
[ | ||
templatePlugin, | ||
{ | ||
compiler: templateCompiler, | ||
}, | ||
], | ||
// [babel.availablePlugins['proposal-decorators'], { legacy: true }], | ||
// [babel.availablePlugins['proposal-class-properties']], | ||
], | ||
presets: [ | ||
[ | ||
babel.availablePresets['env'], | ||
{ | ||
// false -- keeps ES Modules | ||
modules: false, | ||
targets: { esmodules: true }, | ||
forceAllTransforms: false, | ||
}, | ||
], | ||
], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Compiler } from 'repl-sdk'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
describe('ember', () => { | ||
describe('gjs', () => { | ||
test('template-only', async () => { | ||
let compiler = new Compiler(); | ||
let element = await compiler.compile( | ||
'gjs', | ||
` | ||
const name = 'world'; | ||
<template> | ||
<h1>Hello {{name}}!</h1> | ||
<style> | ||
h1 { color: red; } | ||
</style> | ||
</template> | ||
` | ||
); | ||
|
||
// getComputedStyle doesn't work without the element existing in the document | ||
document.body.appendChild(element); | ||
|
||
let h1 = element.querySelector('h1'); | ||
|
||
expect(h1).toBeTruthy(); | ||
expect(h1?.textContent).toContain('Hello world!'); | ||
expect(window.getComputedStyle(h1!).color).toBe('rgb(255, 0, 0)'); | ||
}); | ||
|
||
test('class component', async () => { | ||
let compiler = new Compiler(); | ||
let element = await compiler.compile( | ||
'gjs', | ||
` | ||
import Component from '@glimmer/component'; | ||
export default class Demo extends Component { | ||
name = 'world'; | ||
<template> | ||
<h1>Hello {{this.name}}!</h1> | ||
<style> | ||
h1 { color: red; } | ||
</style> | ||
</template> | ||
} | ||
` | ||
); | ||
|
||
// getComputedStyle doesn't work without the element existing in the document | ||
document.body.appendChild(element); | ||
|
||
let h1 = element.querySelector('h1'); | ||
|
||
expect(h1).toBeTruthy(); | ||
expect(h1?.textContent).toContain('Hello world!'); | ||
expect(window.getComputedStyle(h1!).color).toBe('rgb(255, 0, 0)'); | ||
}); | ||
}); | ||
}); |