Skip to content

Commit

Permalink
Split JS and TS files so they're easier to grok
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 24, 2024
1 parent 8edb4e5 commit 5af9afc
Show file tree
Hide file tree
Showing 24 changed files with 311 additions and 23 deletions.
73 changes: 73 additions & 0 deletions files-override/js/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

module.exports = {
overrides: [
{
files: ['*.{js,mjs,cjs}'],
plugins: ['ember'],
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
requireConfigFile: false,
babelOptions: {
plugins: [
[
'@babel/plugin-proposal-decorators',
{ decoratorsBeforeExport: true },
],
],
},
},
extends: ['eslint:recommended', 'plugin:ember/recommended'],
},
{
files: ['*.gjs'],
parser: 'ember-eslint-parser',
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:ember/recommended-gjs',
],
},
{
files: ['tests/**/*-test.js'],
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:qunit/recommended',
'plugin:ember/recommended',
],
},
{
files: ['tests/**/*-test.gjs'],
parser: 'ember-eslint-parser',
plugins: ['ember'],
extends: [
'eslint:recommended',
'plugin:ember/recommended',
'plugin:ember/recommended-gjs',
],
},
// node files
{
files: [
'./.eslintrc.{js,cjs}',
'./.prettierrc.{js,cjs}',
'./.stylelintrc.{js,cjs}',
'./.template-lintrc.{js,cjs}',
'./ember-cli-build.js',
'./testem.js',
'./config/**/*.js',
'*.mjs',
'*.cjs',
],
env: {
browser: false,
node: true,
},
extends: ['plugin:n/recommended'],
},
],
};
21 changes: 21 additions & 0 deletions files-override/js/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Application from '@ember/application';
import compatModules from '@embroider/core/entrypoint';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

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);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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();
}
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions files/js/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const {
babelCompatSupport,
templateCompatSupport,
} = require('@embroider/compat/babel');

module.exports = {
plugins: [
[
'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,
},
};
File renamed without changes.
File renamed without changes.
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 %>');
File renamed without changes.
File renamed without changes.
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;
}
74 changes: 53 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ const fs = require('fs');
const { join } = require('path');
const emberCliUpdate = require('./lib/ember-cli-update');
const copyWithTemplate = require('./lib/copy-with-template');
const { rm, readFile, writeFile } = require('fs/promises');
const { rm, readFile } = require('fs/promises');

const appBlueprint = Blueprint.lookup('app');

module.exports = {
locals(options) {
return appBlueprint.locals(options);
},

filesPath(options) {
if (options.typescript) {
return join(this.path, 'files/ts');
}

return join(this.path, 'files/js');
},
beforeInstall(options) {
if (!appBlueprint) {
throw new Error('Cannot find app blueprint for generating test-app!');
Expand All @@ -27,16 +35,6 @@ module.exports = {
let manifestBuffer = await readFile(manifestPath);
let manifest = JSON.parse(manifestBuffer.toString());

manifest.scripts['test'] += ' --prefixColors auto';
manifest.scripts['lint'] += ' --prefixColors auto';
manifest.scripts['lint:fix'] += ' --prefixColors auto';

if (manifest.scripts['lint:types']) {
manifest.scripts['lint:types'] = 'glint';
}

await writeFile(manifestPath, JSON.stringify(manifest, null, 2));

let existingDeps = [
...Object.keys(manifest.dependencies || {}),
...Object.keys(manifest.devDependencies || {}),
Expand Down Expand Up @@ -127,6 +125,7 @@ module.exports = {
'decorator-transforms',

// Dependencies out of date from upstream
// (or waiting for the release train to catch up)
'@ember/string',
'@ember/test-helpers',
'ember-resolver',
Expand All @@ -143,14 +142,18 @@ module.exports = {

// TypeScript
// Note that Vite supports TypeScript with 0 configuration on the user's part
'@babel/plugin-transform-typescript',
'typescript',
'@glint/core',
'@glint/environment-ember-loose', // currently required :(
'@glint/environment-ember-template-imports',
'@glint/template',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
...(options.typescript
? [
'@babel/plugin-transform-typescript',
'typescript',
'@glint/core@unstable',
'@glint/environment-ember-loose@unstable', // currently required :(
'@glint/environment-ember-template-imports@unstable',
'@glint/template@unstable',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
]
: []),
],
packageManager: options.packageManager,
});
Expand All @@ -167,25 +170,46 @@ module.exports = {
// replaced with .prettierrc.cjs
'.prettierrc.js',
// We don't set up a template-registry, because we can be entirely gjs/gts
// TODO: when using the --compatibility flag (to be implemented), don't remove
// this folder
'types',
// ember-data / warp-drive doesn't want folks using models
'app/models',
// We don't need these with gjs/gts
'app/helpers',
];

console.log(options);
if (!options.typescript) {
filesToDelete.push('tsconfig.json');
}

for (let file of filesToDelete) {
await rm(join(options.target, file), { recursive: true });
await rm(join(options.target, file), { recursive: true, force: true });
}

// there doesn't seem to be a way to tell ember-cli to not prompt to override files that were added in the beforeInstall
// so I'm just copying a few over at this stage
copyWithTemplate(
join(__dirname, 'files-override'),
join(__dirname, 'files-override/shared'),
options.target,
options,
);

if (options.typescript) {
copyWithTemplate(
join(__dirname, 'files-override/ts'),
options.target,
options,
);
} else {
copyWithTemplate(
join(__dirname, 'files-override/js'),
options.target,
options,
);
}

let packageJson = join(options.target, 'package.json');
let json = JSON.parse(fs.readFileSync(packageJson));

Expand All @@ -196,6 +220,14 @@ module.exports = {
'test:ember': 'vite build --mode test && ember test --path dist',
};

json.scripts['test'] += ' --prefixColors auto';
json.scripts['lint'] += ' --prefixColors auto';
json.scripts['lint:fix'] += ' --prefixColors auto';

if (json.scripts['lint:types']) {
json.scripts['lint:types'] = 'glint';
}

json['ember-addon'] = {
type: 'app',
version: 2,
Expand Down
Loading

0 comments on commit 5af9afc

Please sign in to comment.