Skip to content

v5: JS constant declaration / minification #20011

@pvdlg

Description

@pvdlg

Most of the constant in javascript modules are declared as properties of constant objects, i.e.:

const ClassName = {
  BACKDROP : 'dropdown-backdrop',
  DISABLED : 'disabled',
  OPEN     : 'open'
}

Once minified, these constants are not mangled (for example search for 'BACKDROP' in bootstrap.min.js) because uglify, by default doesn't mangle object keys (I didn't find a safe way to have Uglify mangling objects keys).

Defining a variable as a constant make it a read-only reference, however defining an object as a constant make this object reference read-only, but not its keys. See const.
As all the variables declared this way in Bootstrap are read but never reassigned, therefore really used as constants, I would imagine that the original intent was to make the actual variable a constant, rather than its wrapping object.

Declaring these constants directly rather than as object key would:

  • allow mangling of the variables declaration and their references (smaller minified file, faster parsing)
  • be more consistent with the intention (as I understand it) of declaring read-only variable

Is there a reason for which the constants are declared as object keys rather than directly as a constant ?

If not, I would like to propose a PR that would change the code example above into:

const CN_BACKDROP = 'dropdown-backdrop'
const CN_DISABLED = 'disabled'
const CN_OPEN = 'open'

The CN_ naming convention for Class name (or S_ for selectors, E_ for events) would maintain the readability of the current implementation (I imagine readability might be the reason for this choice ?).

Am I missing/misunderstanding something ? Would you consider such PR ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions