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

feat: add $prepend and $append APIs to imports #124

Merged
merged 1 commit into from
Aug 27, 2024
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
2 changes: 1 addition & 1 deletion src/helpers/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function addVitePlugin(
insertPluginIntoConfig(plugin, config);
}

magicast.imports.$add({
magicast.imports.$prepend({
from: plugin.from,
local: plugin.constructor,
imported: plugin.imported || "default",
Expand Down
28 changes: 23 additions & 5 deletions src/proxy/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@
return imports;
};

const updateImport = (key: string, value: ImportItemInput) => {
const updateImport = (
key: string,
value: ImportItemInput,
order: "prepend" | "append",
) => {
const imports = getAllImports();
const item = imports.find((i) => i.local === key);
const local = value.local || key;
Expand All @@ -156,12 +160,20 @@
(i) => i.from === value.from,
)?.$declaration;
if (declaration) {
// TODO: insert after the last import maybe?
declaration.specifiers.push(specifier as any);
} else {
} else if (order === "prepend" || imports.length === 0) {
root.body.unshift(
b.importDeclaration([specifier], b.stringLiteral(value.from)) as any,
);
} else {
// The `imports` length is already checked above, so `at(-1)` will exist
const lastImport = imports.at(-1)!.$declaration;

Check warning on line 170 in src/proxy/imports.ts

View workflow job for this annotation

GitHub Actions / ci

Forbidden non-null assertion
const lastImportIndex = root.body.indexOf(lastImport);
root.body.splice(
lastImportIndex + 1,
0,
b.importDeclaration([specifier], b.stringLiteral(value.from)) as any,
);
}
return true;
};
Expand All @@ -185,7 +197,13 @@
{
$type: "imports",
$add(item: ImportItemInput) {
proxy[item.local || item.imported] = item as any;
updateImport(item.local || item.imported, item, "prepend");
},
$prepend(item: ImportItemInput) {
updateImport(item.local || item.imported, item, "prepend");
},
$append(item: ImportItemInput) {
updateImport(item.local || item.imported, item, "append");
},
get $items() {
return getAllImports();
Expand All @@ -203,7 +221,7 @@
return getAllImports().find((i) => i.local === prop);
},
set(_, prop, value) {
return updateImport(prop as string, value);
return updateImport(prop as string, value, "prepend");
},
deleteProperty(_, prop) {
return removeImport(prop as string);
Expand Down
3 changes: 3 additions & 0 deletions src/proxy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export type ProxifiedModule<T extends object = Record<string, any>> =
export type ProxifiedImportsMap = Record<string, ProxifiedImportItem> &
ProxyBase & {
$type: "imports";
/** @deprecated Use `$prepend` instead */
$add: (item: ImportItemInput) => void;
$prepend: (item: ImportItemInput) => void;
$append: (item: ImportItemInput) => void;
$items: ProxifiedImportItem[];
};

Expand Down
4 changes: 2 additions & 2 deletions test/function-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("function-calls", () => {
const installVuePlugin = (mod: ProxifiedModule<any>) => {
// Inject export default if not exists
if (!mod.exports.default) {
mod.imports.$add({
mod.imports.$prepend({
imported: "defineConfig",
from: "vite",
});
Expand All @@ -58,7 +58,7 @@ describe("function-calls", () => {
: mod.exports.default;

// Inject vue plugin import
mod.imports.$add({
mod.imports.$prepend({
imported: "default",
local: "vuePlugin",
from: "@vitejs/plugin-vue",
Expand Down
30 changes: 25 additions & 5 deletions test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,45 @@ foo: []
});"
`);

mod.imports.$add({
mod.imports.$prepend({
from: "foo",
imported: "default",
local: "Foo",
});
mod.imports.$add({
mod.imports.$prepend({
from: "star",
imported: "*",
local: "Star",
});
mod.imports.$add({
mod.imports.$prepend({
from: "vite",
imported: "Good",
});
mod.imports.$append({
from: "append-foo",
imported: "default",
local: "AppendFoo",
});
mod.imports.$append({
from: "append-star",
imported: "*",
local: "AppendStar",
});
mod.imports.$append({
from: "vite",
imported: "AppendGood",
});

expect(await generate(mod)).toMatchInlineSnapshot(`
"import * as Star from "star";
import Foo from "foo";
import { defineConfig, Good } from "vite";
import { defineConfig, Good, AppendGood } from "vite";
import VuePlugin from "@vitejs/plugin-vue";
import * as path2 from "path";

import AppendFoo from "append-foo";
import * as AppendStar from "append-star";

export default defineConfig({
foo: [],
});"
Expand All @@ -117,10 +134,13 @@ foo: []
"import { defineConfig } from "vitest/config";
import * as Star from "star";
import Foo from "foo";
import { Good } from "vite";
import { Good, AppendGood } from "vite";
import VuePlugin from "@vitejs/plugin-vue";
import * as path2 from "path";

import AppendFoo from "append-foo";
import * as AppendStar from "append-star";

export default defineConfig({
foo: [],
});"
Expand Down