Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typings #86

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions plugins/drop-import-sync.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type { NodePath } from '@babel/traverse';
import type { Program, ImportDeclaration, CallExpression } from '@babel/types';
import type * as b from '@babel/types';

export function dropImportSync(addons: string[]) {
return function dropImportSyncPlugin(babel) {
const { types: t } = babel;
return function dropImportSyncPlugin(babel: any) {
const { types: t } = babel as { types: typeof b };
let cnt = 0;
type State = {
file: {
opts: {
filename: string;
};
};
importsToAppend?: ImportDeclaration[];
};
function keyValue() {
return `_$key${cnt++}`;
}
const shouldContinue = (state) => {
const shouldContinue = (state: State) => {
const fName = state.file.opts.filename;
return (
fName.includes('node_modules') &&
Expand All @@ -17,7 +29,7 @@ export function dropImportSync(addons: string[]) {
name: 'drop-import-sync', // not required
visitor: {
Program: {
exit(path, state) {
exit(path: NodePath<Program>, state: State) {
if (!shouldContinue(state)) {
return;
}
Expand All @@ -35,24 +47,42 @@ export function dropImportSync(addons: string[]) {
}
},
},
ImportDeclaration(path, state) {
ImportDeclaration(path: NodePath<ImportDeclaration>, state: State) {
if (!shouldContinue(state)) {
return;
}
if (path.node.source.value === '@embroider/macros') {
path.node.specifiers = path.node.specifiers.filter((el) => {
return el.imported.name !== 'importSync';
if (
t.isImportDefaultSpecifier(el) ||
t.isImportNamespaceSpecifier(el)
) {
return true;
}
if (t.isStringLiteral(el.imported)) {
return;
}
return el.imported && el.imported.name !== 'importSync';
});
if (path.node.specifiers.length === 0) {
path.remove();
}
}
},
CallExpression(path, state) {
CallExpression(path: NodePath<CallExpression>, state: State) {
if (!shouldContinue(state)) {
return;
}
if (path.node.callee && path.node.callee.name === 'importSync') {
if (t.isV8IntrinsicIdentifier(path.node.callee)) {
return;
}
if (
path.node.callee &&
t.isIdentifier(path.node.callee) &&
path.node.callee.name === 'importSync' &&
path.node.arguments[0] &&
t.isStringLiteral(path.node.arguments[0])
) {
path.node.callee.name = '_$importSync';
state.importsToAppend = state.importsToAppend || [];
const literalName = keyValue();
Expand Down
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
48 changes: 38 additions & 10 deletions plugins/remove-legacy-layout.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import type { NodePath } from '@babel/traverse';
import type {
Program,
ImportDeclaration,
ObjectProperty,
ClassProperty,
} from '@babel/types';
import type * as b from '@babel/types';

export function removeLegacyLayout(addons: string[]) {
return function removeLegacyLayoutPlugin (babel) {
const shouldContinue = (state) => {
return function removeLegacyLayoutPlugin(babel: any) {
type State = {
file: {
opts: {
filename: string;
};
};
shouldAppendLayout?: boolean;
};
const shouldContinue = (state: State) => {
const fName = state.file.opts.filename;
return (
fName.includes('node_modules') &&
addons.some((el) => fName.includes(`/${el}/`))
);
};
const { types: t } = babel;
const { types: t } = babel as { types: typeof b };
return {
name: 'remove-layout',
visitor: {
Program: {
exit(path, state) {
exit(path: NodePath<Program>, state: State) {
if (state.shouldAppendLayout) {
path.node.body.push(
t.variableDeclaration('var', [
Expand All @@ -25,7 +42,7 @@ export function removeLegacyLayout(addons: string[]) {
}
},
},
ImportDeclaration(path, state) {
ImportDeclaration(path: NodePath<ImportDeclaration>, state: State) {
if (!shouldContinue(state)) {
return;
}
Expand All @@ -46,21 +63,33 @@ export function removeLegacyLayout(addons: string[]) {
state.shouldAppendLayout = true;
}
},
ObjectProperty(path, state) {
ObjectProperty(path: NodePath<ObjectProperty>, state: State) {
if (!shouldContinue(state)) {
return;
}
if (
!t.isIdentifier(path.node.key) ||
!t.isIdentifier(path.node.value)
) {
return;
}
if (
path.node.value.name === 'layout' &&
path.node.key.name === 'layout'
) {
path.remove();
}
},
ClassProperty(path, state) {
ClassProperty(path: NodePath<ClassProperty>, state: State) {
if (!shouldContinue(state)) {
return;
}
if (
!t.isIdentifier(path.node.key) ||
!t.isIdentifier(path.node.value)
) {
return;
}
if (
path.node.value &&
path.node.value.name === 'layout' &&
Expand All @@ -87,6 +116,5 @@ export function removeLegacyLayout(addons: string[]) {
// },
},
};
}

}
};
}
Loading