2

I am trying to get Web Components plus HTML Imports to work in Firefox and IE.

I followed the instructions as per the Web Components GitHub repo, installed the files via npm, and included it towards the head of my document.

I have a custom script that is called in the body of the document.

In firefox, the polyfill is loaded dynamically (synchronously) but transforms the script tag in the body from:

<script type="module" src="./src/scripts/init.js"></script>

to

<script src="/components/requirejs/require.js"></script>
<script>require(["./src/scripts/init.js"]);</script>

and I get the following error: ReferenceError: require is not defined.

I also tried following this StackOverflow answer and downloaded the polyfills separately:

(side note: is it advisable to copy/paste the raw code from a repo file? I don't know another way of doing this. I also find it very confusing actually locating the proper file as sometimes the file is located in the root folder, other times in 'src'. Am I missing something?)

I sequence the files in head like so:

<!-- <script src="src/scripts/helper/web-components-polyfill.js"></script> -->
  <script type="text/javascript" src="src/scripts/helper/html-imports-polyfill.js"></script>
  <script type="text/javascript" src="src/scripts/helper/custom-element-polyfill.js"></script>

Note: I comment out the "general" web components polyfill as I'm trying to follow the advice of the reference question.

In Firefox and IE, I get the same error: require is not defined. I get this extra goodness in Firefox: Firefox hell

I also tried using feature detection to load the polyfills as per WebComponents.org:

    <script type="text/javascript">
     (function() {
         if ('registerElement' in document
            && 'import' in document.createElement('link')
            && 'content' in document.createElement('template')) {
            // platform is good!
         } else {
            // polyfill the platform!
            console.log('loading the polyfills');
            var e = document.createElement('script');
            e.type = "text/javascript";
            e.src = './src/scripts/helper/html-imports-polyfill.js';
            document.head.appendChild(e);
            var f = document.createElement('script');
            f.src = './src/scripts/helper/custom-elements-polyfill.js';
            document.head.appendChild(f);
         }
      })();
   </script>

Again, I get a similar set of errors: More Firefox hellfire

The script that launches the app, init.js, which I call after the polyfills are "loaded", is set up to import HTML fragments and append them to the document. Here is the function I use for doing that:

   /**
     * Dynamically imports HTML into the Main file
     *
     * @param  {String} path The path to the document to import
     * @return {[type]}     [description]
     */
    function _importHTML(path) {
        console.log('importHTML called', path);

        let link = document.createElement('link');
        link.rel = 'import';
        link.href = path;

        link.onload = (e) => {
            // console.log('Successfully loaded', e.target.href);
        }

        link.onerror = (e) => {
            console.log('Error loading', e.target.href);
        }

        document.head.appendChild(link);

    }

It goes without saying, but everything works fine in Chrome.

Please help! :D

David Gaskin
  • 548
  • 7
  • 26
  • Have you updated the latest patches for IE and firefox ? – Ragavan Rajan Aug 29 '18 at 01:41
  • I'm not sure I understand. The goal is to define code that will work in most browsers so I won't have the luxury of updating other people's browsers. Am I understanding your comment correctly? – David Gaskin Aug 29 '18 at 01:44
  • yes you are right. What I wanted to ask is if the browser patches were never updated on the machine then it will create a problem. – Ragavan Rajan Aug 29 '18 at 01:48
  • I have faced the similar problem in angular framework. there is a file called "polyfills.ts". I updated the file like following `/** IE9, IE10 and IE11 requires all of the following polyfills.**/ import 'core-js/es6/symbol'; import 'core-js/es6/object'; import 'core-js/es6/function'; import 'core-js/es6/parse-int'; import 'core-js/es6/parse-float'; import 'core-js/es6/number'; import 'core-js/es6/math'; import 'core-js/es6/string'; import 'core-js/es6/date'; import 'core-js/es6/array'; import 'core-js/es6/regexp'; import 'core-js/es6/map'; import 'core-js/es6/set'; ` – Ragavan Rajan Aug 29 '18 at 01:49
  • I'm not sure if they were ever updated. I am using IE11 and Firefox 60+. I'm not sure how to update the patches but I will look into it. Thanks for commenting. – David Gaskin Aug 29 '18 at 01:52
  • Please note that HTML imports have been dropped in the specification. – connexo Aug 29 '18 at 16:59

1 Answers1

2

To make Custom Elements v1 work with Firefox and Edge, you just have to load the webcomponents-bundle.js file from the Web Components polyfill repository.

<script src="/your_path/webcomponents-bundle.js"></script>

To make HTML Imports work with Firefox, IE and Edge, you just have to load the html-imports.min.js file from HTML Imports polyfill repository.

<script src="/your_path/html-imports.min.js"></script>

To get the polyfills you can, on github, either :

  • download a file directly (right-click on the link then save the file),

  • copy/paste the raw content,

  • use the green button [Clone of Download] to clone the repository with GIT or download the repository as a ZIP file.


Using a polyfill with Internet Explorer to create a Custom Element v1 is more complicated because classes don't exist in Internet Explorer and cannot be fully polyfilled themselves.

However, it is possible to create Custom Elements v0 in Internet Explorer with a polyfill, but it's not recommended.

If you want to use Custom Elements then forget IE :-)

Supersharp
  • 29,002
  • 9
  • 92
  • 134
  • thanks for answering my questions. I added both the scripts as you advised but in firefox I get the following warnings and errors: `Warning: Loading failed for the – David Gaskin Sep 01 '18 at 23:03
  • @Peak where does require.js come from? do you use a some precompiler engine? – Supersharp Sep 02 '18 at 13:18
  • I just noticed a script tag I had in the body with the `type="module"` attr. When the script was active, the `webcomponents-loader.js` would rewrite the script tag in the body (in Firefox) from: `` to ` `. When I remove the `type="module"` or use the `nomodule` attr, that removes the error but I get another reference error coming from the init script: `Define is not defined`. The init script uses es6 including imports. – David Gaskin Sep 02 '18 at 20:15
  • I just swapped out my es6 version init script for a transpiled-to-es5 script and that removed the error. – David Gaskin Sep 02 '18 at 20:38