meteor-webpack icon indicating copy to clipboard operation
meteor-webpack copied to clipboard

dynamic import at top level fails

Open s7dhansh opened this issue 6 years ago • 5 comments

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?

s7dhansh avatar Apr 02 '19 11:04 s7dhansh

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.

abecks avatar Apr 02 '19 16:04 abecks

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?

s7dhansh avatar Apr 02 '19 17:04 s7dhansh

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 [email protected] 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 .

abecks avatar Apr 02 '19 17:04 abecks

@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.

ardatan avatar Apr 03 '19 01:04 ardatan

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

s7dhansh avatar Apr 03 '19 05:04 s7dhansh