Skip to content

Commit 4204b39

Browse files
committed
feature #70 Allowing the controller name to be overridden by the package or the user (weaverryan)
This PR was merged into the main branch. Discussion ---------- Allowing the controller name to be overridden by the package or the user This is needed for live components, where the controller name needs to be simply `live`... else it will be super annoying to use :). Allowing the user to also override the controller name... isn't really needed... but if the user does this, I think they know they are on their own. This COULD actually replace #69... as a package could add, for example, `name: 'symfony/ux-typed'` to change the name to a shorter version. Commits ------- d8f6d5b Allowing the controller name to be overridden by the package or the user
2 parents 02420f0 + d8f6d5b commit 4204b39

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

Diff for: dist/webpack/loader.js

+7-1
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]) {

Diff for: src/webpack/create-controllers-module.ts

+10-2
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 || []) {

Diff for: test/fixtures/load-named-controller.json

+11
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+
}

Diff for: test/fixtures/module/package.json

+6
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
}

Diff for: test/fixtures/override-name.json

+12
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+
}

Diff for: test/index.test.ts

+1
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();

Diff for: test/webpack/create-controllers-module.test.ts

+20
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)