Skip to content

[Bug] Removing unnecessary Promise in object of controllers to be loaded #81

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

Merged
merged 1 commit into from
Mar 20, 2023
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: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ automatically when you install [Symfony UX Packages](https://github.com/symfony/
Before you start, familiarize yourself with the basics of
[Stimulus](https://stimulus.hotwired.dev/).

Symfony UX Stimulus bridge is currently considered **experimental**.

## Installation

If you don't already have Webpack Encore installed, install it with:
Expand Down
4 changes: 1 addition & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ function startStimulusApp(context) {
if (!symfonyControllers.hasOwnProperty(controllerName)) {
continue;
}
symfonyControllers[controllerName].then((module) => {
application.register(controllerName, module.default);
});
application.register(controllerName, symfonyControllers[controllerName]);
}
return application;
}
Expand Down
30 changes: 14 additions & 16 deletions dist/webpack/lazy-controller-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,22 @@ var schemaUtils__namespace = /*#__PURE__*/_interopNamespace(schemaUtils);

function generateLazyController (controllerPath, indentationSpaces, exportName = 'default') {
const spaces = ' '.repeat(indentationSpaces);
return `${spaces}(function() {
${spaces} return class LazyController extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
return `class extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
${spaces} }
${spaces}})()`;
${spaces}}`;
}

// Reserved word lists for various dialects of the language
Expand Down
67 changes: 30 additions & 37 deletions dist/webpack/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,35 @@ var LoaderDependency__default = /*#__PURE__*/_interopDefaultLegacy(LoaderDepende

function generateLazyController (controllerPath, indentationSpaces, exportName = 'default') {
const spaces = ' '.repeat(indentationSpaces);
return `${spaces}(function() {
${spaces} return class LazyController extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
return `class extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
${spaces} }
${spaces}})()`;
${spaces}}`;
}

function createControllersModule(config) {
let controllerContents = 'export default {';
let autoImportContents = '';
let importStatementContents = '';
let hasLazyControllers = false;
const deprecations = [];
if ('undefined' !== typeof config['placeholder']) {
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.');
}
if ('undefined' === typeof config['controllers']) {
throw new Error('Your Stimulus configuration file (assets/controllers.json) lacks a "controllers" key.');
}
let controllerIndex = 0;
for (const packageName in config.controllers) {
let packageConfig;
try {
Expand All @@ -58,24 +56,19 @@ function createControllersModule(config) {
continue;
}
const controllerMain = packageName + '/' + controllerPackageConfig.main;
let fetchMode = 'eager';
if ('undefined' !== typeof controllerUserConfig.webpackMode) {
deprecations.push('The "webpackMode" config key is deprecated in controllers.json. Use "fetch" instead, set to either "eager" or "lazy".');
let fetchMode = controllerUserConfig.fetch || 'eager';
let moduleValueContents = ``;
if (fetchMode === 'eager') {
const controllerNameForVariable = `controller_${controllerIndex++}`;
importStatementContents += `import ${controllerNameForVariable} from '${controllerMain}';\n`;
moduleValueContents = controllerNameForVariable;
}
if ('undefined' !== typeof controllerUserConfig.fetch) {
if (!['eager', 'lazy'].includes(controllerUserConfig.fetch)) {
throw new Error(`Invalid "fetch" value "${controllerUserConfig.fetch}" in controllers.json. Expected "eager" or "lazy".`);
}
fetchMode = controllerUserConfig.fetch;
}
let moduleValueContents = `import(/* webpackMode: "eager" */ '${controllerMain}')`;
if (fetchMode === 'lazy') {
else if (fetchMode === 'lazy') {
hasLazyControllers = true;
moduleValueContents = `
new Promise((resolve, reject) => resolve({ default:
${generateLazyController(controllerMain, 6)}
}))
`.trim();
moduleValueContents = generateLazyController(controllerMain, 2);
}
else {
throw new Error(`Invalid fetch mode "${fetchMode}" in controllers.json. Expected "eager" or "lazy".`);
}
let controllerNormalizedName = controllerReference.substr(1).replace(/_/g, '-').replace(/\//g, '--');
if ('undefined' !== typeof controllerPackageConfig.name) {
Expand All @@ -87,7 +80,7 @@ ${generateLazyController(controllerMain, 6)}
controllerContents += `\n '${controllerNormalizedName}': ${moduleValueContents},`;
for (const autoimport in controllerUserConfig.autoimport || []) {
if (controllerUserConfig.autoimport[autoimport]) {
autoImportContents += "import '" + autoimport + "';\n";
importStatementContents += "import '" + autoimport + "';\n";
}
}
}
Expand All @@ -96,8 +89,8 @@ ${generateLazyController(controllerMain, 6)}
controllerContents = `import { Controller } from '@hotwired/stimulus';\n${controllerContents}`;
}
return {
finalSource: `${autoImportContents}${controllerContents}\n};`,
deprecations,
finalSource: `${importStatementContents}${controllerContents}\n};`,
deprecations: [],
};
}

Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export function startStimulusApp(context: __WebpackModuleApi.RequireContext) {
continue;
}

symfonyControllers[controllerName].then((module: any) => {
application.register(controllerName, module.default);
});
application.register(controllerName, symfonyControllers[controllerName]);
}

return application;
Expand Down
44 changes: 16 additions & 28 deletions src/webpack/create-controllers-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import generateLazyController from './generate-lazy-controller';

export default function createControllersModule(config: any) {
let controllerContents = 'export default {';
let autoImportContents = '';
let importStatementContents = '';
let hasLazyControllers = false;
const deprecations = [];

if ('undefined' !== typeof config['placeholder']) {
throw new Error(
Expand All @@ -27,6 +26,7 @@ export default function createControllersModule(config: any) {
throw new Error('Your Stimulus configuration file (assets/controllers.json) lacks a "controllers" key.');
}

let controllerIndex = 0;
for (const packageName in config.controllers) {
let packageConfig;
try {
Expand Down Expand Up @@ -55,32 +55,20 @@ export default function createControllersModule(config: any) {
}

const controllerMain = packageName + '/' + controllerPackageConfig.main;
let fetchMode = 'eager';
let fetchMode = controllerUserConfig.fetch || 'eager';

if ('undefined' !== typeof controllerUserConfig.webpackMode) {
deprecations.push(
'The "webpackMode" config key is deprecated in controllers.json. Use "fetch" instead, set to either "eager" or "lazy".'
);
}

if ('undefined' !== typeof controllerUserConfig.fetch) {
if (!['eager', 'lazy'].includes(controllerUserConfig.fetch)) {
throw new Error(
`Invalid "fetch" value "${controllerUserConfig.fetch}" in controllers.json. Expected "eager" or "lazy".`
);
}

fetchMode = controllerUserConfig.fetch;
}
let moduleValueContents = ``;
if (fetchMode === 'eager') {
const controllerNameForVariable = `controller_${controllerIndex++}`;
importStatementContents += `import ${controllerNameForVariable} from '${controllerMain}';\n`;

let moduleValueContents = `import(/* webpackMode: "eager" */ '${controllerMain}')`;
if (fetchMode === 'lazy') {
// simply use the imported controller
moduleValueContents = controllerNameForVariable;
} else if (fetchMode === 'lazy') {
hasLazyControllers = true;
moduleValueContents = `
new Promise((resolve, reject) => resolve({ default:
${generateLazyController(controllerMain, 6)}
}))
`.trim();
moduleValueContents = generateLazyController(controllerMain, 2);
} else {
throw new Error(`Invalid fetch mode "${fetchMode}" in controllers.json. Expected "eager" or "lazy".`);
}

// Normalize the controller name: remove the initial @ and use Stimulus format
Expand All @@ -97,7 +85,7 @@ ${generateLazyController(controllerMain, 6)}

for (const autoimport in controllerUserConfig.autoimport || []) {
if (controllerUserConfig.autoimport[autoimport]) {
autoImportContents += "import '" + autoimport + "';\n";
importStatementContents += "import '" + autoimport + "';\n";
}
}
}
Expand All @@ -108,7 +96,7 @@ ${generateLazyController(controllerMain, 6)}
}

return {
finalSource: `${autoImportContents}${controllerContents}\n};`,
deprecations,
finalSource: `${importStatementContents}${controllerContents}\n};`,
deprecations: [],
};
}
30 changes: 14 additions & 16 deletions src/webpack/generate-lazy-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
export default function(controllerPath: string, indentationSpaces: number, exportName = 'default') {
const spaces = ' '.repeat(indentationSpaces);

return `${spaces}(function() {
${spaces} return class LazyController extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
return `class extends Controller {
${spaces} constructor(context) {
${spaces} super(context);
${spaces} this.__stimulusLazyController = true;
${spaces} }
${spaces} initialize() {
${spaces} if (this.application.controllers.find((controller) => {
${spaces} return controller.identifier === this.identifier && controller.__stimulusLazyController;
${spaces} })) {
${spaces} return;
${spaces} }
${spaces} import('${controllerPath.replace(/\\/g, '\\\\')}').then((controller) => {
${spaces} this.application.register(this.identifier, controller.${exportName});
${spaces} });
${spaces} }
${spaces}})()`;
${spaces}}`;
}
11 changes: 0 additions & 11 deletions test/fixtures/deprecated-webpack-mode.json

This file was deleted.

Loading