Skip to content

Commit d8f6d5b

Browse files
committed
Allowing the controller name to be overridden by the package or the user
1 parent 02420f0 commit d8f6d5b

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

dist/webpack/loader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function createControllersModule(config) {
4949
}
5050
for (const controllerName in config.controllers[packageName]) {
5151
const controllerReference = packageName + '/' + controllerName;
52-
const controllerNormalizedName = controllerReference.substr(1).replace(/_/g, '-').replace(/\//g, '--');
5352
if ('undefined' === typeof packageConfig.symfony.controllers[controllerName]) {
5453
throw new Error('Controller "' + controllerReference + '" does not exist in the package and cannot be compiled.');
5554
}
@@ -78,6 +77,13 @@ ${generateLazyController(controllerMain, 6)}
7877
}))
7978
`.trim();
8079
}
80+
let controllerNormalizedName = controllerReference.substr(1).replace(/_/g, '-').replace(/\//g, '--');
81+
if ('undefined' !== typeof controllerPackageConfig.name) {
82+
controllerNormalizedName = controllerPackageConfig.name;
83+
}
84+
if ('undefined' !== typeof controllerUserConfig.name) {
85+
controllerNormalizedName = controllerUserConfig.name;
86+
}
8187
controllerContents += `\n '${controllerNormalizedName}': ${moduleValueContents},`;
8288
for (const autoimport in controllerUserConfig.autoimport || []) {
8389
if (controllerUserConfig.autoimport[autoimport]) {

src/webpack/create-controllers-module.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ export default function createControllersModule(config) {
3939

4040
for (const controllerName in config.controllers[packageName]) {
4141
const controllerReference = packageName + '/' + controllerName;
42-
// Normalize the controller name: remove the initial @ and use Stimulus format
43-
const controllerNormalizedName = controllerReference.substr(1).replace(/_/g, '-').replace(/\//g, '--');
4442

4543
// Find package config for the controller
4644
if ('undefined' === typeof packageConfig.symfony.controllers[controllerName]) {
@@ -85,6 +83,16 @@ ${generateLazyController(controllerMain, 6)}
8583
`.trim();
8684
}
8785

86+
// Normalize the controller name: remove the initial @ and use Stimulus format
87+
let controllerNormalizedName = controllerReference.substr(1).replace(/_/g, '-').replace(/\//g, '--');
88+
// allow the package or user config to override name
89+
if ('undefined' !== typeof controllerPackageConfig.name) {
90+
controllerNormalizedName = controllerPackageConfig.name;
91+
}
92+
if ('undefined' !== typeof controllerUserConfig.name) {
93+
controllerNormalizedName = controllerUserConfig.name;
94+
}
95+
8896
controllerContents += `\n '${controllerNormalizedName}': ${moduleValueContents},`;
8997

9098
for (const autoimport in controllerUserConfig.autoimport || []) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"controllers": {
3+
"@symfony/mock-module": {
4+
"mock_named": {
5+
"fetch": "eager",
6+
"enabled": true
7+
}
8+
}
9+
},
10+
"entrypoints": []
11+
}

test/fixtures/module/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"importedStyles": {
1111
"@symfony/mock-module/dist/style.css": true
1212
}
13+
},
14+
"mock_named": {
15+
"main": "dist/named_controller.js",
16+
"name": "custom_name",
17+
"webpackMode": "eager",
18+
"enabled": true
1319
}
1420
}
1521
}

test/fixtures/override-name.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"controllers": {
3+
"@symfony/mock-module": {
4+
"mock": {
5+
"name": "overridden_name",
6+
"fetch": "eager",
7+
"enabled": true
8+
}
9+
}
10+
},
11+
"entrypoints": []
12+
}

test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { startStimulusApp } from './dist/index';
1414
describe('startStimulusApp', () => {
1515
it('must start the app', async () => {
1616
const app = startStimulusApp();
17+
app.debug = false;
1718

1819
// Wait for controllers to be loaded
1920
await app.start();

test/webpack/create-controllers-module.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,24 @@ export default {
103103
);
104104
});
105105
});
106+
107+
describe('load-named-controller.json', () => {
108+
it('must register the custom name from package.json', () => {
109+
// eslint-disable-next-line @typescript-eslint/no-var-requires
110+
const config = require('../fixtures/load-named-controller.json');
111+
expect(createControllersModule(config).finalSource).toEqual(
112+
"export default {\n 'custom_name': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/named_controller.js'),\n};"
113+
);
114+
});
115+
});
116+
117+
describe('override-name.json', () => {
118+
it('must return file with no autoimport', () => {
119+
// eslint-disable-next-line @typescript-eslint/no-var-requires
120+
const config = require('../fixtures/override-name.json');
121+
expect(createControllersModule(config).finalSource).toEqual(
122+
"export default {\n 'overridden_name': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/controller.js'),\n};"
123+
);
124+
});
125+
});
106126
});

0 commit comments

Comments
 (0)