Skip to content

Commit

Permalink
Add tests for --typescript
Browse files Browse the repository at this point in the history
Update dependency manipulation

use npx, because I don't have globally installed things

Use a separate fixture directory for the TS output

Silly linting

More gts -- compat with hbs and TS can be later

TS tests pass

Split JS and TS files so they're easier to grok

Update files-override lints

Another test port 0
  • Loading branch information
NullVoxPopuli committed Sep 26, 2024
1 parent 1743eb5 commit d28f119
Show file tree
Hide file tree
Showing 32 changed files with 732 additions and 7 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export default [
pluginJs.configs.recommended,
eslintPluginPrettierRecommended,
{
ignores: ['tests/fixture/*'],
ignores: ['tests/fixture/*', 'tests/fixture-ts/*'],
},
];
2 changes: 1 addition & 1 deletion files-override/js/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

let d = window.define;
const d = window.define;

for (const [name, module] of Object.entries(compatModules)) {
d(name, function () {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions files-override/ts/app/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Application from '@ember/application';
// This can be removed if you have no need for compatModules
// @ts-expect-error TODO: add types to @embroider/core/entrypoint
import compatModules from '@embroider/core/entrypoint';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

// This can be removed if you have no need for compatModules
// @ts-expect-error window does not have define
const d = window.define;

for (const [name, module] of Object.entries(compatModules)) {
d(name, function () {
return module;
});
}

export default class App extends Application {
modulePrefix = config.modulePrefix;
podModulePrefix = config.podModulePrefix;
Resolver = Resolver;
}

loadInitializers(App, config.modulePrefix);
14 changes: 14 additions & 0 deletions files-override/ts/tests/test-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Application from '<%= name %>/app';
import config from '<%= name %>/config/environment';
import * as QUnit from 'qunit';
import { setApplication } from '@ember/test-helpers';
import { setup } from 'qunit-dom';
import { start as qunitStart } from 'ember-qunit';

export function start() {
setApplication(Application.create(config.APP));

setup(QUnit.assert);

qunitStart();
}
46 changes: 46 additions & 0 deletions files-override/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"extends": "@tsconfig/ember/tsconfig.json",
"include": [
"app/**/*",
"tests/**/*",
],
"glint": {
"environment": [
"ember-loose",
"ember-template-imports"
]
},
"compilerOptions": {
"allowJs": true,
/**
https://www.typescriptlang.org/tsconfig#noEmitOnError
Do not block emit on TS errors.
*/
"noEmitOnError": false,

"declaration": false,
"declarationMap": false,

/**
https://www.typescriptlang.org/tsconfig#allowImportingTsExtensions
We want our tooling to know how to resolve our custom files so the appropriate plugins
can do the proper transformations on those files.
*/
"allowImportingTsExtensions": true,
"paths": {
"<%= name %>/tests/*": [
"./tests/*"
],
"<%= name %>/*": [
"./app/*"
],
"*": [
"./types/*"
]
},
"types": [
"ember-source/types"
]
},
}
3 changes: 3 additions & 0 deletions files/ts/app/config/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import loadConfigFromMeta from '@embroider/config-meta-loader';

export default loadConfigFromMeta('<%= name %>');
18 changes: 18 additions & 0 deletions files/ts/app/templates/application.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Route from 'ember-route-template';
import { pageTitle } from 'ember-page-title';
<% if (welcome) {%>import { WelcomePage } from 'ember-welcome-page';<% } %>

export default Route(
<template>
{{pageTitle "<%= namespace %>"}}
<% if (welcome) { %>
{{outlet}}

{{! The following component displays Ember's default welcome message. }}
<WelcomePage />
{{! Feel free to remove this! }}<% } else { %>
<h2 id="title">Welcome to Ember</h2>

{{outlet}}<% } %>
</template>
);
50 changes: 50 additions & 0 deletions files/ts/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const {
babelCompatSupport,
templateCompatSupport,
} = require('@embroider/compat/babel');

module.exports = {
plugins: [
[
'@babel/plugin-transform-typescript',
{
allExtensions: true,
onlyRemoveTypeImports: true,
allowDeclareFields: true,
},
],
[
'babel-plugin-ember-template-compilation',
{
compilerPath: 'ember-source/dist/ember-template-compiler.js',
enableLegacyModules: [
'ember-cli-htmlbars',
'ember-cli-htmlbars-inline-precompile',
'htmlbars-inline-precompile',
],
transforms: [...templateCompatSupport()],
},
],
[
'module:decorator-transforms',
{
runtime: {
import: require.resolve('decorator-transforms/runtime-esm'),
},
},
],
[
'@babel/plugin-transform-runtime',
{
absoluteRuntime: __dirname,
useESModules: true,
regenerator: false,
},
],
...babelCompatSupport(),
],

generatorOpts: {
compact: false,
},
};
29 changes: 29 additions & 0 deletions files/ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AppTemplate</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

{{content-for "head"}}

<link integrity="" rel="stylesheet" href="/@embroider/core/vendor.css">
<link integrity="" rel="stylesheet" href="/assets/<%= name %>.css">

{{content-for "head-footer"}}
</head>
<body>
{{content-for "body"}}

<script src="/@embroider/core/vendor.js"></script>
<script type="module">
import Application from './app/app';
import environment from './app/config/environment';

Application.create(environment.APP);
</script>

{{content-for "body-footer"}}
</body>
</html>
64 changes: 64 additions & 0 deletions files/ts/vite.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { defineConfig } from 'vite';
import {
resolver,
hbs,
scripts,
templateTag,
optimizeDeps,
compatPrebuild,
assets,
contentFor,
} from '@embroider/vite';
import { babel } from '@rollup/plugin-babel';

const extensions = [
'.mjs',
'.gjs',
'.js',
'.mts',
'.gts',
'.ts',
'.hbs',
'.json',
];

export default defineConfig(({ mode }) => {
return {
resolve: {
extensions,
},
plugins: [
hbs(),
templateTag(),
scripts(),
resolver(),
compatPrebuild(),
assets(),
contentFor(),

babel({
babelHelpers: 'runtime',
extensions,
}),
],
optimizeDeps: optimizeDeps(),
server: {
port: 4200,
},
build: {
outDir: 'dist',
rollupOptions: {
input: {
main: 'index.html',
...(shouldBuildTests(mode)
? { tests: 'tests/index.html' }
: undefined),
},
},
},
};
});

function shouldBuildTests(mode) {
return mode !== 'production' || process.env.FORCE_BUILD_TESTS;
}
Loading

0 comments on commit d28f119

Please sign in to comment.