@@ -8,37 +8,35 @@ var LoaderDependency__default = /*#__PURE__*/_interopDefaultLegacy(LoaderDepende
88
99function generateLazyController ( controllerPath , indentationSpaces , exportName = 'default' ) {
1010 const spaces = ' ' . repeat ( indentationSpaces ) ;
11- return `${ spaces } (function() {
12- ${ spaces } return class LazyController extends Controller {
13- ${ spaces } constructor(context) {
14- ${ spaces } super(context);
15- ${ spaces } this.__stimulusLazyController = true;
16- ${ spaces } }
17- ${ spaces } initialize() {
18- ${ spaces } if (this.application.controllers.find((controller) => {
19- ${ spaces } return controller.identifier === this.identifier && controller.__stimulusLazyController;
20- ${ spaces } })) {
21- ${ spaces } return;
22- ${ spaces } }
23- ${ spaces } import('${ controllerPath . replace ( / \\ / g, '\\\\' ) } ').then((controller) => {
24- ${ spaces } this.application.register(this.identifier, controller.${ exportName } );
25- ${ spaces } });
11+ return `class extends Controller {
12+ ${ spaces } constructor(context) {
13+ ${ spaces } super(context);
14+ ${ spaces } this.__stimulusLazyController = true;
15+ ${ spaces } }
16+ ${ spaces } initialize() {
17+ ${ spaces } if (this.application.controllers.find((controller) => {
18+ ${ spaces } return controller.identifier === this.identifier && controller.__stimulusLazyController;
19+ ${ spaces } })) {
20+ ${ spaces } return;
2621${ spaces } }
22+ ${ spaces } import('${ controllerPath . replace ( / \\ / g, '\\\\' ) } ').then((controller) => {
23+ ${ spaces } this.application.register(this.identifier, controller.${ exportName } );
24+ ${ spaces } });
2725${ spaces } }
28- ${ spaces } })() `;
26+ ${ spaces } }`;
2927}
3028
3129function createControllersModule ( config ) {
3230 let controllerContents = 'export default {' ;
33- let autoImportContents = '' ;
31+ let importStatementContents = '' ;
3432 let hasLazyControllers = false ;
35- const deprecations = [ ] ;
3633 if ( 'undefined' !== typeof config [ 'placeholder' ] ) {
3734 throw new Error ( 'Your controllers.json file was not found. Be sure to add a Webpack alias from "@symfony/stimulus-bridge/controllers.json" to *your* controllers.json file.' ) ;
3835 }
3936 if ( 'undefined' === typeof config [ 'controllers' ] ) {
4037 throw new Error ( 'Your Stimulus configuration file (assets/controllers.json) lacks a "controllers" key.' ) ;
4138 }
39+ let controllerIndex = 0 ;
4240 for ( const packageName in config . controllers ) {
4341 let packageConfig ;
4442 try {
@@ -58,24 +56,19 @@ function createControllersModule(config) {
5856 continue ;
5957 }
6058 const controllerMain = packageName + '/' + controllerPackageConfig . main ;
61- let fetchMode = 'eager' ;
62- if ( 'undefined' !== typeof controllerUserConfig . webpackMode ) {
63- deprecations . push ( 'The "webpackMode" config key is deprecated in controllers.json. Use "fetch" instead, set to either "eager" or "lazy".' ) ;
59+ let fetchMode = controllerUserConfig . fetch || 'eager' ;
60+ let moduleValueContents = `` ;
61+ if ( fetchMode === 'eager' ) {
62+ const controllerNameForVariable = `controller_${ controllerIndex ++ } ` ;
63+ importStatementContents += `import ${ controllerNameForVariable } from '${ controllerMain } ';\n` ;
64+ moduleValueContents = controllerNameForVariable ;
6465 }
65- if ( 'undefined' !== typeof controllerUserConfig . fetch ) {
66- if ( ! [ 'eager' , 'lazy' ] . includes ( controllerUserConfig . fetch ) ) {
67- throw new Error ( `Invalid "fetch" value "${ controllerUserConfig . fetch } " in controllers.json. Expected "eager" or "lazy".` ) ;
68- }
69- fetchMode = controllerUserConfig . fetch ;
70- }
71- let moduleValueContents = `import(/* webpackMode: "eager" */ '${ controllerMain } ')` ;
72- if ( fetchMode === 'lazy' ) {
66+ else if ( fetchMode === 'lazy' ) {
7367 hasLazyControllers = true ;
74- moduleValueContents = `
75- new Promise((resolve, reject) => resolve({ default:
76- ${ generateLazyController ( controllerMain , 6 ) }
77- }))
78- ` . trim ( ) ;
68+ moduleValueContents = generateLazyController ( controllerMain , 2 ) ;
69+ }
70+ else {
71+ throw new Error ( `Invalid fetch mode "${ fetchMode } " in controllers.json. Expected "eager" or "lazy".` ) ;
7972 }
8073 let controllerNormalizedName = controllerReference . substr ( 1 ) . replace ( / _ / g, '-' ) . replace ( / \/ / g, '--' ) ;
8174 if ( 'undefined' !== typeof controllerPackageConfig . name ) {
@@ -87,7 +80,7 @@ ${generateLazyController(controllerMain, 6)}
8780 controllerContents += `\n '${ controllerNormalizedName } ': ${ moduleValueContents } ,` ;
8881 for ( const autoimport in controllerUserConfig . autoimport || [ ] ) {
8982 if ( controllerUserConfig . autoimport [ autoimport ] ) {
90- autoImportContents += "import '" + autoimport + "';\n" ;
83+ importStatementContents += "import '" + autoimport + "';\n" ;
9184 }
9285 }
9386 }
@@ -96,8 +89,8 @@ ${generateLazyController(controllerMain, 6)}
9689 controllerContents = `import { Controller } from '@hotwired/stimulus';\n${ controllerContents } ` ;
9790 }
9891 return {
99- finalSource : `${ autoImportContents } ${ controllerContents } \n};` ,
100- deprecations,
92+ finalSource : `${ importStatementContents } ${ controllerContents } \n};` ,
93+ deprecations : [ ] ,
10194 } ;
10295}
10396
0 commit comments