Enable Dark Mode!
how-to-manage-javascript-and-scss-asset-bundles-in-odoo-20.jpg
By: Ayana R

How to Manage JavaScript and SCSS Asset Bundles in Odoo 20

Technical Odoo 20 Odoo Enterprises Odoo Community

The Odoo framework provides an asset management system to bundle our JavaScript, SCSS, CSS, and XML files. These assets have been declared by the developers using the assets dictionary of the manifest file (the __manifest__.py file) of any module. This manifest file approach has been employed for the management of assets in Odoo from earlier versions to the current version of Odoo, which is Odoo 20.

This blog will help you to manage asset bundles of JavaScript and SCSS files in Odoo 20.

1. Understanding the Odoo Asset Management System

Odoo divides the frontend assets into asset bundles. An asset bundle refers to the files that must be loaded together for a specific application.

The following are three types of assets available in Odoo:

  • JavaScript (JS): File format used to store business logic in the frontend.
  • Stylesheets (CSS/SCSS): File format used to control appearance and layout.
  • XML: File format used to define the templates of JavaScript components in the frontend.

Once JS files are added to the asset bundles, Odoo will take care of processing these files based on the structure of the JavaScript module. Odoo converts the native JavaScript modules into Odoo modules. In most cases, when it comes to production mode, JavaScript and CSS assets are minified and concatenated whenever possible.

The SCSS files will be compiled to CSS files before delivery to the browser.

The created asset files are saved in the attachments and delivered using proper asset URLs.

2. Defining Asset Bundles in the Module Manifest

In Odoo 20, assets can be added to the __manifest__.py file of the module using assets.

In the assets dictionary, there is a Python dictionary where keys are bundle names, and values are paths to the list of files.

For example, suppose we have a module named my_custom_module and its directory structure is as follows:

static/
    +-- src/
        +-- js/
        ¦   +-- custom.js
        ¦
        +-- scss/
        ¦   +-- custom.scss
        ¦
        +-- xml/
            +-- custom_templates.xml

A manifest file for the import of files in the Odoo Backend can be made in the following way:

{
    'name': 'My Custom Module',
    'version': '20.0.1.0.0',
    'depends': ['web'],
    'assets': {
        'web.assets_backend': [
            'my_custom_module/static/src/js/custom.js',
            'my_custom_module/static/src/scss/custom.scss',
            'my_custom_module/static/src/xml/custom_templates.xml',
        ],
    },
}

In this instance, web.assets_backend will serve as a bundle for the Odoo back-end interface.

Following the installation or upgrade of the module, Odoo will process the assets, and the files will be bundled accordingly.

Important note: In order for the bundle to be created, the module with the asset declaration should be installed, along with its dependencies.

3. Using Glob Patterns to Include Multiple Assets

It is tough to add each JavaScript and SCSS file individually because it would be a headache to manage the manifest file whenever the number of front-end items inside the module increases.

The Odoo system uses the glob pattern for adding several files together.

Example:

'assets': {
    'web.assets_backend': [
        'my_custom_module/static/src/js/**/*',
        'my_custom_module/static/src/scss/**/*',
        'my_custom_module/static/src/xml/**/*',
    ],
}

The **/* pattern matches all the files located in the designated directory as well as its sub-directories.

Some of the common patterns are the following:

PatternPurpose
*.jsMatches JavaScript files in the specified directory
*.scssMatches SCSS files in the specified directory
**/*Matches files recursively inside subdirectories
js/**/*Includes files under the JS directory and its subdirectories

For example, if there are several JavaScript files in the static/src/js folder of the module, then the below-mentioned statement will include them all at once.

'web.assets_backend': [
    'my_custom_module/static/src/js/**/*',
],

These patterns allow elimination of repetitions in statements and make asset management possible in case of adding new files.

However, caution should be exercised in using these patterns. For instance, a broad pattern may include files that do not belong to a particular bundle.

4. Managing Asset Loading Order Using Directives

In some cases, merely including the files in the asset bundle is not sufficient. For example, there could be a specific JavaScript or SCSS file which should be loaded either before or after another file.

In this case, for example, a customized SCSS file might need to be loaded after the Bootstrap default SCSS file to override the variables and styles.

Odoo provides asset directives that can be used to manage the inclusion, exclusion, or replacement of files in a bundle.

These directives include:

  • include
  • prepend
  • before
  • after
  • remove
  • replace

Let us learn about the significance of these directives through some examples.

A. Include

Using inclusion by means of the include statement, a developer can include one asset bundle inside another asset bundle.

This feature proves useful when one wants to use the same assets in several asset bundles.

Example:

'assets': {
    'my_custom_module._custom_assets': [
        'my_custom_module/static/src/js/custom.js',
        'my_custom_module/static/src/scss/custom.scss',
    ],
    'web.assets_backend': [
        ('include', 'my_custom_module._custom_assets'),
    ],
}

In this case, the sub-bundle holding the custom JavaScript and SCSS files is named _custom_assets.

The bundle web.assets_backend refers to this sub-bundle using the include statement.

It is common practice to have an underscore at the beginning of reusable sub-bundles.

B. Prepend

The prepend command will add an asset to a bundle right from the start.

The prepend command is used in cases where an asset must be loaded before other assets within the bundle.

Example:

'assets': {
    'web.assets_backend': [
        (
            'prepend',
            'my_custom_module/static/src/scss/custom.scss'
        ),
    ],
}

In this case, the custom.scss file will reside at the beginning of the backend asset bundle.

This may be required in instances when a specific style sheet must be loaded before others.

C. Before

The before directive allows for adding a file before a specific target file.

It requires three parameters: directive name, target file, and file name. 

For example:

'assets': {
    'web.assets_backend': [
        (
            'before',
            'web/static/src/css/list_view.scss',
            'my_custom_module/static/src/scss/custom.scss'
        ),
    ],
}

In this scenario, the custom SCSS file is included before the provided list view file.

Such a directive is quite useful when there is a need to have greater control compared to what is provided by the above directive.

D. After

This directive includes the provided file after a specific target file.

Example:

'assets': {
    'web.assets_backend': [
        (
            'after',
            'web/static/src/css/list_view.scss',
            'my_custom_module/static/src/scss/custom.scss'
        ),
    ],
}

In this scenario, the custom style sheet is applied after the chosen list view style sheet.

Its importance becomes evident when the custom styles need to take precedence over the existing styles.

E. Remove

Remove command removes an existing file from an asset bundle.

For example:

'assets': {
    'web.assets_backend': [
        (
            'remove',
            'web/static/src/js/some_file.js'
        ),
    ],
}

This will exclude the given JavaScript file from the backend bundle.

It can be useful in case you want to exclude an asset from a particular bundle.

F. Replace

Replace Directive

The replace directive is used to replace the pre-existing asset through the addition of another file.

Example:

'assets': {
    'web.assets_backend': [
        (
            'replace',
            'web/static/src/js/some_file.js',
            'my_custom_module/static/src/js/custom_file.js'
        ),
    ],
}

Here, our customized implementation replaces the existing .js file.

This is useful for us in the case where we wish to replace an existing resource but not affect its position in the order of loading the resources.

Please note that the resource file specified in before, after, remove, and replace should have already been declared in the bundle, and the module responsible for doing the replacing depends on that module declaring the resource.

5. Understanding the Main Asset Bundles in Odoo 20

Odoo makes use of different types of asset bundles depending on the particular application of the interfaces. It is therefore important to pick the correct bundle because sometimes a file in one bundle may not always be available in all parts of Odoo.

These are some of the asset bundles.

Asset BundlePurpose
web.assets_backendJavaScript, SCSS, and XML resources used by the backend web client
web.assets_frontendAssets used by public website pages, portal, and eCommerce
web.assets_unit_testsJavaScript unit testing resources, including tests, helpers, and mocks
point_of_sale._assets_posAssets specific to the Point of Sale interface
web._assets_primary_variablesPrimary SCSS variables such as colors, fonts, and spacing

We need to understand the applicability of each bundle.

A. web.assets_backend

Web.assets_backend is an asset bundle that is used to load assets of the Odoo backend interface.

This bundle has all the required assets for the web client, including views, action managers, and front-end components.

If a custom module installs a new JS component or modifies the appearance of a backend form, the corresponding assets will be placed here.

'assets': {
    'web.assets_backend': [
        'my_custom_module/static/src/js/backend.js',
        'my_custom_module/static/src/scss/backend.scss',
        'my_custom_module/static/src/xml/backend_templates.xml',
    ],
}

B. web.assets_frontend

Web Assets Front End Bundle 

This particular bundle will be used for the public website.

It works perfectly for customizations such as website pages, portal pages, eCommerce, and any other front-end functionality.

Let us take an example where a module will customize the appearance of a website product page. These files will be in the web assets front-end bundle.

'assets': {
    'web.assets_frontend': [
        'my_custom_module/static/src/js/website.js',
        'my_custom_module/static/src/scss/website.scss',
    ],
}

C. web.assets_unit_tests

JavaScript files needed for testing purposes are contained within the web.assets_unit_tests package.

Test helpers, mocks, and other testing resources are contained within this package.

Having a separate package for tests allows test files not to be loaded during normal application operation.

Example:

'assets': {
    'web.assets_unit_tests': [
        'my_custom_module/static/tests/**/*',
    ],
}

D. point_of_sale._assets_pos

This point_of_sale._assets_pos package will be utilized to declare JavaScript, SCSS, and XML assets that are specific to the Point of Sale module.

For example, if you are developing a custom POS module wherein you have added a new button or made some modification to the product view or interface of POS, then you can declare those assets under this package. Your custom module should depend on point_of_sale, because that way, this POS bundle will get loaded while compiling the assets.

Points to be noted: The underscore present in point_of_sale._assets_pos signifies that it is a private bundle. It is meant only for POS assets and not for general backend assets.

'assets': {
    'point_of_sale._assets_pos': [
        'my_custom_module/static/src/js/pos.js',
        'my_custom_module/static/src/scss/pos.scss',
        'my_custom_module/static/src/xml/pos_templates.xml',
    ],
}

6. Managing SCSS Variables and Stylesheet Loading Order

SCSS is an improved CSS technology that has more features such as variables, nesting, mixins, and reusable styles.

Odoo uses SCSS to style the back-end, website, and other interfaces in Odoo.

One important thing about customizing the application with the help of SCSS is the order in which the variables and stylesheets are imported.

For example, consider the case in which a developer intends to change the main color used on the Odoo interface. Then, instead of having to override the CSS properties in various files, he/she just needs to override the SCSS variable.

But the variable needs to be declared before importing the stylesheets.

Understanding SCSS Variable Bundles

SCSS variables and styles have been developed by Odoo through different modules.

Some important modules are:

  • web._assets_primary_variables: Specifies the primary theming variables.
  • web._assets_secondary_variables: Specifies secondary variables which are extended from the primary variables.
  • web._assets_frontend_helpers: Contains the frontend Bootstrap override before compiling frontend SCSS.

These modules help in maintaining the correct order in rendering the style sheets.

Example: Overriding SCSS Variables

If any developer wants to set the primary color for the website theme,

then he/she can create a file named as primary_variables.scss inside the custom module.

File: static/src/scss/primary_variables.scss

$o-brand-primary: #875A7B;

The above file can then be included in the set of main variables using the module manifest:

'assets': {
    'web._assets_primary_variables': [
        'my_custom_module/static/src/scss/primary_variables.scss',
    ],
}

It enables the use of the custom variable in all the stylesheets that use the main variable settings.

Why Does Loading Order Matter?

Consider the following example:

  • The custom SCSS file changes one of the Bootstrap variables.
  • Bootstrap stylesheets are compiled before the custom variable definition.
  • As a result, the CSS will use the outdated value of the variable.
  • Therefore, in this case, it will not be possible to achieve the desired result.

To avoid this problem, it is suggested that you define variables in the required variable bundle and compile dependent stylesheets afterwards.

Important: Defining a variable in a bundle is different from including a stylesheet in web.assets_backend. Variables have to be defined while compiling SCSS.

7. Debugging JavaScript and SCSS Assets in Odoo 20

One of the most frequent problems while doing frontend Odoo development is understanding the reason why some JavaScript or SCSS customization doesn't work.

For instance:

  • The JavaScript file is not being included.
  • A custom button is not displayed.
  • There is no effect of SCSS customization on the UI.
  • The browser console contains JavaScript errors.
  • The custom template is not available.

Odoo offers a special debugging mode named debug=assets.

Enabling Debug Assets Mode

For debugging your assets, you need to use the following parameter in the Odoo URL:

?debug=assets

For example:

http://localhost:8020/odoo?debug=assets

This is when the following changes happen with regard to how the assets are distributed on the frontend side:

  • No minifying of JavaScript files, as it is usually done in production.
  • No minifying of CSS files, as it is usually done in production.
  • Source maps are present.
  • Source code can be easily traced during debugging.

Debugging JavaScript Files

In normal production mode, to load the JavaScript files, several files are merged together.

It becomes difficult to figure out which file holds the error, which message.Debug=assets will help the users to check each file individually using Developer Tools in the web browser.

The following is the process that should be followed to debug the JavaScript error:

  1. Start the Odoo application.
  2. Add debug=assets at the end of the URL.
  3. Open the web browser's Developer Tools.
  4. Switch to the Sources tab.
  5. Find the JavaScript custom file.
  6. Analyze the code using breakpoints.

Debugging SCSS and CSS Files

The very same debug mode can be utilized in dealing with styling problems. There is an opportunity to investigate the CSS that has been created in the Developer Tools of the browser and discover what stylesheets or selectors caused the style.

For example, if the custom background color does not apply:

  1. Debug=assets mode needs to be turned on.
  2. Access the Odoo window where the problem is.
  3. Investigate the HTML tag.
  4. Check the Styles tab.
  5. Find out if the custom CSS style is applied.
  6. Determine if some other selector overrides the custom one.

Asset management is one of the essential topics in Odoo 20 frontend development. Asset management in the form of asset bundling is a very effective approach for managing resources such as JavaScript, SCSS, CSS, and XML for the application in Odoo.

The custom assets can be registered in the required bundle using the assets dictionary in the manifest file of the module. Such directives as include, prepend, before, after, remove, and replace provide an opportunity to control the assets loading and manipulation. All of these concepts are essential in performing the customizations on the frontend side.

To read more about How to Load JS Function in Menu Item Click in Odoo 19, refer to our blog, How to Load JS Function in Menu Item Click in Odoo 19.


Frequently Asked Questions

Where could the declarations of the JavaScript and SCSS files be located in Odoo 20?

The declarations of JavaScript and SCSS files are located in the assets section of the __manifest__.py file of the module. The developer should select an asset bundle where he would load the files.

What is the difference between web.assets_backend and web.assets_frontend?

web.assets_backend contains the resources necessary for the Odoo backend UI (views and web client). web.assets_frontend contains resources required for websites, portals, and e-commerce pages. It is necessary to choose an appropriate asset bundle depending on the customization place.

Why should we use debug=assets in Odoo?

The debug=assets mode helps the developer easily solve frontend issues. In this mode, the JavaScript and CSS assets are provided without any production minification, and source maps are generated. It makes it possible to investigate every source file and make JavaScript or styling debugging.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp