How to override a HTML file using a custom module?

Working solution.

Just create or edit requirejs-config.js file from below path.

/app/code/Namespace/Module/view/frontend/requirejs-config.js

And place below code in requirejs-config.js

var config = {
    map: {
        '*': {
          'Magento_Payment/template/payment/cc-form.html': 
              'Namespace_Module/template/payment/cc-form.html'
        }
  }
};

So we can override any html file in this way.


You can just add your cc-form.html file inside your theme payment module.

<mage_dir>/app/design/frontend/{Package}/{themename}/Magento_Payment/web/template/payment/cc-form.html

You can change according to your requirements at above place.

Remove var folder from root and remove pub/static/frontend folder.

You must have run command php bin/magento setup:static-content:deploy

Clear your browser cache and check.


Accepted solution is right, but I copy here the full @AntonGuz answer from the "Stack Overflow" (very well explained):

Yes, there is. You can look in pub static to see how path to static asset constructed.

How it works

Every asset is accessible from the page by itenter code heres "RequireJS ID". It similar to real path, but varied.

For example file http://magento.vg/static/adminhtml/Magento/backend/en_US/Magento_Theme/favicon.ico.

It's real path is /app/code/Magento/Theme/view/adminhtml/web/favicon.ico. It's RequireJS ID is Magento_Theme/favicon.ico. This means that file could be accessible via require("text!Magento_Theme/favicon.ico") or similar command.

You can find that RequireJS ID consist with module name and useful part of path (after folder web).

How can I replace a file

So you have file
vendor/magento/module-payment/view/frontend/web/template/payment/cc-form.html

On the page it loaded with src as
http://magento.vg/static/frontend/Magento/luma/en_US/Magento_Payment/template/payment/cc-form.html

So its RequireJS ID is
Magento_Payment/template/payment/cc-form.html

Side note: Inside UI components stuff it equals to Magento_Payment/payment/cc-form. Words "template" and ".html" are added automatically.

And now you can replace this file for application via RequireJS config

var config = {
  "map": {
    "*": {
      "Magento_Payment/template/payment/cc-form.html": 
          "<OwnBrand>_<OwnModule>/template/payment/cc-form.html"
    }
  }
};

This code snippet you place in requirejs-config.js file in your module. That is all.

Perhaps it will help somebody to understand as it occurs.