Skip to content

Commit ed6b964

Browse files
committed
[FEATURE] Ability to configure automatic alias creation
1 parent 472ea29 commit ed6b964

File tree

7 files changed

+104
-23
lines changed

7 files changed

+104
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ You can provide additional configuration to the plugin, for example:
7676
file to provide entrypoints from extensions.
7777
- `entrypointIgnorePatterns` (array, default: `["**/node_modules/**", "**/.git/**"]`): Files
7878
that should be ignored when using wildcards in `ViteEntrypoints.json`.
79+
- `aliases` (`true`, `false`, `@` or `EXT`): If set to true, both aliases starting with `EXT:`
80+
and `@` are created for all available extension folders. If set to `@`, only `@` aliases
81+
are created, if set to `EXT`, only `EXT:` aliases are created. If set to `false`, alias
82+
creation is skipped altogether.
7983

8084
### Fixing CORS issues
8185

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface UserConfig {
33
entrypointFile?: string;
44
entrypointIgnorePatterns?: string[];
55
debug?: boolean;
6+
aliases?: AliasConfig;
67
}
78

89
export interface PluginConfig<T extends ComposerContext> extends UserConfig {
@@ -11,6 +12,7 @@ export interface PluginConfig<T extends ComposerContext> extends UserConfig {
1112
composerContext: T;
1213
entrypointIgnorePatterns: string[];
1314
debug: boolean;
15+
aliases: AliasConfig;
1416
}
1517

1618
export interface ComposerContext {
@@ -33,3 +35,5 @@ export interface Typo3ProjectContext extends ComposerContext {
3335
}
3436

3537
export type PluginTarget = "project" | "extension";
38+
39+
export type AliasConfig = true | "EXT" | "@" | false;

src/typo3extension.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ export default function typo3extension(
6565

6666
// Add path alias for extension
6767
config.resolve ??= {};
68-
config.resolve.alias = addAliases(config.resolve.alias, [
69-
extension,
70-
]);
68+
config.resolve.alias = addAliases(
69+
config.resolve.alias,
70+
[extension],
71+
pluginConfig.aliases,
72+
);
7173

7274
// Find all vite entrypoints in extension
7375
entrypoints = findEntrypointsInExtensions(
@@ -101,6 +103,7 @@ export default function typo3extension(
101103
entrypoints,
102104
pluginConfig.composerContext,
103105
logger,
106+
pluginConfig.aliases,
104107
);
105108
}
106109
},

src/typo3project.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default function typo3project(
9999
config.resolve.alias = addAliases(
100100
config.resolve.alias,
101101
availableExtensions,
102+
pluginConfig.aliases,
102103
);
103104

104105
// Find all vite entrypoints in relevant TYPO3 extensions
@@ -144,6 +145,7 @@ export default function typo3project(
144145
entrypoints,
145146
pluginConfig.composerContext,
146147
logger,
148+
pluginConfig.aliases,
147149
);
148150
}
149151
},

src/utils.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
UserConfig,
1717
Typo3ProjectContext,
1818
Typo3ExtensionContext,
19+
AliasConfig,
1920
} from "./types.js";
2021

2122
export function initializePluginConfig<T extends ComposerContext>(
@@ -42,6 +43,7 @@ export function initializePluginConfig<T extends ComposerContext>(
4243
entrypointIgnorePatterns: ["**/node_modules/**", "**/.git/**"],
4344
debug: false,
4445
composerContext,
46+
aliases: true,
4547
...userConfig,
4648
};
4749
}
@@ -180,15 +182,14 @@ export function outputDebugInformation(
180182
entrypoints: string[],
181183
composerContext: ComposerContext,
182184
logger: Logger,
185+
aliasConfig: AliasConfig = true,
183186
): void {
184187
if (availableExtensions.length) {
185188
const extensionList = availableExtensions.map(
186189
(extension) => extension.extensionKey,
187190
);
188-
const aliasList = extensionList.reduce(
189-
(aliasList: string[], extensionKey) =>
190-
aliasList.concat(["@" + extensionKey, "EXT:" + extensionKey]),
191-
[],
191+
const aliasList = createAliases(availableExtensions, aliasConfig).map(
192+
(alias) => alias.find,
192193
);
193194
logger.info(
194195
`The following TYPO3 extensions have been recognized: ${colors.green(extensionList.join(", "))}`,
@@ -231,31 +232,40 @@ export function addRollupInputs(
231232
export function addAliases(
232233
alias: AliasOptions | undefined,
233234
extensions: Typo3ExtensionContext[],
235+
config: AliasConfig = true,
234236
): AliasOptions {
235-
const additionalAliases = extensions.reduce(
236-
(aliases: Alias[], extension) => {
237-
const replacement = extension.path.endsWith("/")
238-
? extension.path
239-
: extension.path + "/";
240-
aliases.push({ find: "@" + extension.extensionKey, replacement });
241-
aliases.push({
242-
find: "EXT:" + extension.extensionKey,
243-
replacement,
244-
});
245-
return aliases;
246-
},
247-
[],
248-
);
249-
250237
alias ??= [];
251238
if (!Array.isArray(alias)) {
252239
alias = Object.entries(alias).map((entry) => ({
253240
find: entry[0],
254241
replacement: entry[1],
255242
}));
256243
}
244+
return alias.concat(createAliases(extensions, config));
245+
}
257246

258-
return alias.concat(additionalAliases);
247+
export function createAliases(
248+
extensions: Typo3ExtensionContext[],
249+
config: AliasConfig,
250+
) {
251+
if (config === false) {
252+
return [];
253+
}
254+
return extensions.reduce((aliases: Alias[], extension) => {
255+
const replacement = extension.path.endsWith("/")
256+
? extension.path
257+
: extension.path + "/";
258+
if (config === "@" || config === true) {
259+
aliases.push({ find: "@" + extension.extensionKey, replacement });
260+
}
261+
if (config === "EXT" || config === true) {
262+
aliases.push({
263+
find: "EXT:" + extension.extensionKey,
264+
replacement,
265+
});
266+
}
267+
return aliases;
268+
}, []);
259269
}
260270

261271
export function readJsonFile(file: string): any {

tests/unit/addAliases.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,59 @@ describe("addAliases", () => {
114114
},
115115
]);
116116
});
117+
test("set no aliases", () => {
118+
expect(
119+
addAliases(
120+
undefined,
121+
[
122+
{
123+
type: "typo3-cms-extension",
124+
extensionKey: "test_extension",
125+
path: "/path/to/dummy/extension1",
126+
},
127+
],
128+
false,
129+
),
130+
).toEqual([]);
131+
});
132+
test("set only @ aliases", () => {
133+
expect(
134+
addAliases(
135+
undefined,
136+
[
137+
{
138+
type: "typo3-cms-extension",
139+
extensionKey: "test_extension",
140+
path: "/path/to/dummy/extension1",
141+
},
142+
],
143+
"@",
144+
),
145+
).toEqual([
146+
{
147+
find: "@test_extension",
148+
replacement: "/path/to/dummy/extension1/",
149+
},
150+
]);
151+
});
152+
test("set only EXT aliases", () => {
153+
expect(
154+
addAliases(
155+
undefined,
156+
[
157+
{
158+
type: "typo3-cms-extension",
159+
extensionKey: "test_extension",
160+
path: "/path/to/dummy/extension1",
161+
},
162+
],
163+
"EXT",
164+
),
165+
).toEqual([
166+
{
167+
find: "EXT:test_extension",
168+
replacement: "/path/to/dummy/extension1/",
169+
},
170+
]);
171+
});
117172
});

tests/unit/initializePluginConfig.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe("initializePluginConfig", () => {
1616
entrypointFile: "Configuration/ViteEntrypoints.json",
1717
entrypointIgnorePatterns: ["**/node_modules/**", "**/.git/**"],
1818
debug: false,
19+
aliases: true,
1920
composerContext: {
2021
type: "project",
2122
path: "/path/to/fixtures/composerProject",
@@ -35,6 +36,7 @@ describe("initializePluginConfig", () => {
3536
entrypointFile: "viteEntrypoints.json",
3637
entrypointIgnorePatterns: ["**/node_modules/**", "**/.git/**"],
3738
debug: true,
39+
aliases: true,
3840
composerContext: {
3941
type: "project",
4042
path: "/path/to/fixtures/composerProject",
@@ -54,6 +56,7 @@ describe("initializePluginConfig", () => {
5456
entrypointFile: "Configuration/ViteEntrypoints.json",
5557
entrypointIgnorePatterns: ["**/node_modules/**", "**/.git/**"],
5658
debug: false,
59+
aliases: true,
5760
composerContext: {
5861
type: "typo3-cms-extension",
5962
path: "/path/to/fixtures/composerProject/packages/composerExtension/",

0 commit comments

Comments
 (0)