Skip to content

Commit

Permalink
improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Dec 9, 2023
1 parent 9b3d27f commit 5f1e40d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
12 changes: 7 additions & 5 deletions plugins/ember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import hbsResolver from './hbs-resolver';
import gtsResolver from './gts-resolver';
import i18nLoader from './i18n-loader';
import { babelHotReloadPlugin } from './hot-reload';
import type { ASTv1 } from '@glimmer/syntax';


const selfPath = import.meta.url + '/../..';

Expand All @@ -30,7 +32,7 @@ export function App(mode: string) {

addon.babelPlugins = [
hbsResolver(isProd),
gtsResolver(isProd),
gtsResolver(),
i18nLoader(),
!isDev
? babel({
Expand Down Expand Up @@ -94,7 +96,7 @@ export function emberAppConfig(
}

function addonBabelConfig(
plugins = [],
plugins: unknown[] = [],
isProd: boolean,
enableSourceMaps = false
) {
Expand Down Expand Up @@ -255,7 +257,7 @@ export function Addon(name: string, mode: string) {
}

export function defaultBabelConfig(
plugins = [],
plugins: unknown[] = [],
isProd: boolean,
enableSourceMaps = false
) {
Expand Down Expand Up @@ -317,11 +319,11 @@ function templateCompilationPlugin(isProd: boolean) {
refBucketTransform,
isProd
? // eslint-disable-next-line @typescript-eslint/no-unused-vars
function sampleTransform(env) {
function sampleTransform() {
return {
name: 'skip-prod-test-selectors',
visitor: {
ElementNode(node) {
ElementNode(node: ASTv1.ElementNode) {
node.attributes = node.attributes.filter(
(el) => !el.name.startsWith('data-test-')
);
Expand Down
9 changes: 6 additions & 3 deletions plugins/gts-resolver.js → plugins/gts-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { Preprocessor } from 'content-tag';
import type { Plugin } from 'vite';

const p = new Preprocessor();
const fileRegex = /\.(gts|gjs)$/;

function tpl(raw, id) {
function tpl(raw: string, id: string) {
return p.process(raw, id);
}

export default function hbsResolver() {
export default function hbsResolver(): Plugin {
return {
name: 'gts-resolver',
enforce: 'pre',
transform(src, id) {
if (fileRegex.test(id, id)) {
if (fileRegex.test(id)) {
return {
code: tpl(src, id),
map: null, // provide source map if available
};
} else {
return void 0;
}
},
};
Expand Down
13 changes: 9 additions & 4 deletions plugins/hbs-resolver.js → plugins/hbs-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Plugin } from 'vite';

const fileRegex = /\.(hbs)$/;

function tpl(raw, id, isProd) {
function tpl(raw: string, id: string, isProd: boolean) {
const code = raw.split('`').join('\\`');

const moduleName = id.includes('node_modules')
? id.split('node_modules/')[1]
: id.split('src').pop();

const name = moduleName.split('/components/').pop().split('.')[0];
const name =
moduleName?.split('/components/').pop()?.split('.')[0] ?? 'unknown';
// we always want to return a template-only component
// and corner-cases handled by patched ember-source, and glimmer

Expand All @@ -26,16 +29,18 @@ function tpl(raw, id, isProd) {
// `.trim();
}

export default function hbsResolver(isProd) {
export default function hbsResolver(isProd: boolean): Plugin {
return {
name: 'hbs-resolver',
enforce: 'pre',
transform(src, id) {
if (fileRegex.test(id, id)) {
if (fileRegex.test(id)) {
return {
code: tpl(src, id, isProd),
map: null, // provide source map if available
};
} else {
return void 0;
}
},
};
Expand Down
3 changes: 2 additions & 1 deletion plugins/i18n-loader.js → plugins/i18n-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import YAML from 'yaml';
import { readdir, access, readFile } from 'fs/promises';
import { basename, extname, join } from 'path';
import { constants } from 'fs';
import type { Plugin } from 'vite';

const translationsFileName = 'ember-intl/translations';
const translationsDir = 'translations';

export default function i18nLoader() {
export default function i18nLoader(): Plugin {
return {
name: 'i18n-loader',
enforce: 'pre',
Expand Down

0 comments on commit 5f1e40d

Please sign in to comment.