Polymer / pwa-starter-kit

Starter templates for building full-featured Progressive Web Apps from web components.
https://pwa-starter-kit.polymer-project.org
2.36k stars 431 forks source link

Import after other import doesn't work when builded #190

Closed abdonrd closed 5 years ago

abdonrd commented 6 years ago

When I edit:

https://github.com/Polymer/pwa-starter-kit/blob/b50a93748677c8be0bdcdc5ed71a03747628fc57/src/actions/app.js#L31-L36

To:

    case 'view1':
      import('../components/my-view1.js').then((module) => {
        // Put code in here that you want to run every time when
        // navigating to view1 after my-view1.js is loaded.
        import('../components/my-view2.js');
        import('../components/my-view3.js');
      });
      break;

The website works with npm start. But it doesn't work with npm run build:static && npm run serve:static:

screen shot 2018-07-07 at 11 50 36 screen shot 2018-07-07 at 11 50 45
keanulee commented 6 years ago

Seems to be an issue with the bundler when there are multiple import() statements for the same fragment. Filed https://github.com/Polymer/tools/issues/568

evayde commented 6 years ago

It does seem that multiple dynamic imports are really kinda bad. For anyone else struggling with doing something like this:

    case 'view1':
      import('../components/my-view1.js').then((module) => {
        import('../components/my-view2.js');
      });
      break;

where my-view2 is not imported in any other place (e.g. you want to create a container-page my-view1 which imports my-view2. My-view1 might include a global navigation and my-view2 is the content for a page, hence you dont need it at any other place, but you want to make sure that my-view1 is loaded before my-view2), then you will probably also get an error. But this error is unrelated to the error above. If you try to dynamically import my-view2 you also have to add fragments to your polymer.json, like so:

  "fragments": [
    "src/components/my-view1.js",
    "src/components/my-view2.js"
  ]

Again: This error is unrelated. As you "hide" your components from the bundler inside the then block. You could also encounter this problem when using variables inside your imports.

To stress the point: Above error only happens when the same file is imported twice. Which may also be bad in general (why would you have 2 same dynamic imports in the first place?). If you have something that needs to be imported more than once then it might be a static import INSIDE of one or more dynamically loaded files.

frankiefu commented 5 years ago

Fixed by https://github.com/Polymer/tools/pull/722