ardatan / meteor-webpack

https://medium.com/@ardatan/meteor-with-webpack-in-2018-faster-compilation-better-source-handling-benefit-from-bc5ccc5735ef
MIT License
123 stars 29 forks source link

dynamic import at top level fails #64

Open s7dhansh opened 5 years ago

s7dhansh commented 5 years ago

A)

async function main() {
    const [
        React,
        {hydrate},
    ] = await Promise.all([
        import('react'),
        import('react-dom'),
    ]);

    hydrate(<div>asdfasdf</div>, document.getElementById('root'));
}

main();

fails with an error:

Uncaught ReferenceError: exports is not defined
    at 0.js:1
(anonymous) @ 0.js:1
app.js:817 Uncaught (in promise) Error: Loading chunk 0 failed.

If I remove the Promises, it works: B)

import React from 'react';
import {hydrate} from 'react-dom';
hydrate(<div>asdfasdf</div>, document.getElementById('root'));

Ironically enough, if I replace (B) with (A) now (while meteor is running), the app starts working. It fails again when I restart meteor. Can someone guide me on fixing/debugging this?

abecks commented 5 years ago

I don't believe Meteor's dynamic import functionality will currently work with this package. Some more work is likely required. You might be better off using webpack's code splitting functionality.

s7dhansh commented 5 years ago

How do I do that? Webpack's code splitting with dynamic imports also uses the same syntax, right? Should I remove dynamic-import from meteor packages?

abecks commented 5 years ago

To be honest I'm not sure, as I haven't used it before. If you have any luck, please let me know.

On Tue, Apr 2, 2019 at 10:30 AM Sudhanshu notifications@github.com wrote:

How do I do that? Webpack's code splitting with dynamic imports also uses the same syntax, right? Should I remove dynamic-import from meteor packages?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ardatan/meteor-webpack/issues/64#issuecomment-479108707, or mute the thread https://github.com/notifications/unsubscribe-auth/AB-PgQwxB9aWlO-nf-mn-IEpFIzgJmKwks5vc5O3gaJpZM4cXwWi .

ardatan commented 5 years ago

@s7dhansh @abecks Meteor-Webpack supports dynamic imports by serving chunks as assets; https://github.com/ardatan/meteor-webpack/blob/1f6796743d226c6fb65e5cf4f5c16e24ef3c3c71/atmosphere-packages/webpack/plugin.js#L129

I think there is a problem in dev-middleware package about calculation of output files. https://github.com/ardatan/meteor-webpack/commit/68898dc2d42222869a8aaddad15830ed524a3a82#diff-8f10efed2c00dab752c268b55cfd62a1 The problem may be related to this commit, on each compilation the assets to be served must be recalculated.

s7dhansh commented 5 years ago

@ardatan sorry I have very limited understanding of what you are saying. Can you let me know a fix that I can try?