question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Javascript variables from html imports are inaccessible after becoming module imports (polymer redux example)

See original GitHub issue

I’m trying to follow Rob’s Redux Polycast while using this loader.

Maybe this is WAI but the javascript variables exposed from within html-imports are not accessible after they are made into module-imports.

This will fail with ‘PolymerRedux’ is undefined:

<link rel="import" href="../bower_components/polymer-redux/polymer-redux.html">

<script type="text/javascript">
  const Redux = require('redux');

  const initial = {message: 'Hello, World!'};
  const reducer = state => state;
  const store = Redux.createStore(reducer, initial);

  const ReduxMixin = PolymerRedux(store);
</script>

And this would fail with ‘ReduxMixin’ is undefined:

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="redux-store.html">

<dom-module id="app-main">
  <template>...</template>

  <script>
    class AppMain extends ReduxMixin(Polymer.Element) {

      static get is() { return 'app-main'; }

      static get properties() {
        return {};
      }
    }

    window.customElements.define(AppMain.is, AppMain);
  </script>
</dom-module>

My example project with a few quick attempts to make this work: https://github.com/zszafran/polymer-redux-webpack/blob/master/src/redux-store.html

I was able to make part of this work by doing this:

<link rel="import" href="../bower_components/polymer-redux/polymer-redux.html">

<script type="text/javascript">
  const Redux = require('redux');
  const initial = {message: 'Hello, World!'};
  const reducer = state => state;
  const store = Redux.createStore(reducer, initial);

  export const ReduxMixin = (Parent) => Parent;  // <--- added 'export'
</script>
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="redux-store.html" imports="ReduxMixin">  <!-- added 'imports' attribute -->

<dom-module id="app-main">
  <template>...</template>

  <script>
    class AppMain extends ReduxMixin(Polymer.Element) {
      ...
    }

    window.customElements.define(AppMain.is, AppMain);
  </script>
</dom-module>

After I modified the loader:

          if (ignoreLinks.indexOf(href) < 0 && ignoredFromPartial.length === 0) {
            var imports = (0, _dom.getAttribute)(linkNode, 'imports');
            var importStr = imports ? `{${imports}} from ` : '';
            source += `\nimport ${importStr}'${path.replace(/\\/g, '\\\\')}';\n`;
            lineCount += 2;
          }

But this hack only got me so far since I still need to modify polymer-redux to export the ‘PolymerRedux’ function. Maybe there is a FR in all this, but it seems like my best bet will be to load this all with <script> tags and somehow compile the polymer-redux source lib into it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NeilujDcommented, Mar 19, 2018

@ChadKillingsworth I am not sure about the provided solution. I think the issue is not about importing the ReduxMixin in the app-main.html element but importing the PolymerRedux mixin into the redux-store.html file. Am I right ?

1reaction
ChadKillingsworthcommented, Jul 28, 2017

I have not forgotten about this. Just have to find time to put together an example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript variables from html imports are inaccessible ...
This is expected behavior and by design. In WebPack everything is a module and variables declared at the root of a script are...
Read more >
Variable imported, but gives import error when used ...
I just created another dummy variable in actionTypes.js and then removed that variable. Everything remained same and the code executed ...
Read more >
polymerjs - npm Package Health Analysis
1. Dependencies: Polymer is a library around Web Components and thus it expects you to use HTML Imports for loading the dependencies for...
Read more >
Lets Build Web Components! Part 8: Mythbusters Edition
Today, we're going to review some pernicious myths about web components and their use which seem to have cropped up lately. Many of...
Read more >
The Shadow DOM. From Web Components in Action by…
Multiple ways to manage their own dependencies (ES2015 modules, templates, even the now obsolete HTML Imports); A user-defined API to control them either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found