amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

🚀 Insert empty ESM export to ensure file is parsed as module

Open jridgewell opened this issue 4 years ago • 5 comments

From discussions with the Chrome team, apparently using <link rel=preload> and pointing it at a script that's used as type=module will cause a double parse of the script. Once as a regular Script, and once as an ESM Module. Adding in an ESM export {} ensures that the file cannot be parsed as a Script (because export is only valid in ESM Modules), thus saving us from processing this file twice.

This means using <script src="foo.mjs"> without setting type=module will fail!

jridgewell avatar Mar 17 '21 21:03 jridgewell

Funny enough the module/nomodule tests are failing with this PR.

kristoferbaxter avatar Mar 18 '21 00:03 kristoferbaxter

Just for verification, did we confirm that terser and friends do not remove this export?

I tried on the terser website (and it stays), but not our actual dist output. After checking, it did remove it 🤦 .

jridgewell avatar Mar 18 '21 01:03 jridgewell

Does this affect both v0.mjs and amp4ads-v0.mjs? I'm cool with this, but we need some time to fix the transformer since it still uses <script src=amp4ads.mjs. Can we hold on merging this if it affects amp4ads-v0.mjs?

powerivq avatar Mar 18 '21 04:03 powerivq

Does this affect both v0.mjs and amp4ads-v0.mjs?

Yes.

Can we hold on merging this if it affects amp4ads-v0.mjs?

That's why you're included on the PR. But this is the reason I opened the CL yesterday, we're trying to optimize our ESM code and that requires treating it like a real module.

jridgewell avatar Mar 18 '21 04:03 jridgewell

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jul 22 '22 09:07 CLAassistant

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 15 '23 16:10 stale[bot]