Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142353

On webpack how can I import a script without evaluate it?

$
0
0

I'm recently working on some website optimization works, and I start using code splitting in webpack by using import statement like this:

import(/* webpackChunkName: 'pageB-chunk' */ './pageB')

Which correctly create the pageB-chunk.js, now let's say I want to prefetch this chunk in pageA, I can do it by add this statement in pageA:

import(/* webpackChunkName: 'pageB-chunk' */ /* webpackPrefetch: true */ './pageB')

Which will result in a

<link rel="prefetch" href="pageB-chunk.js">

being append to html's head, then browser will prefetch it, so far so good.

The problem is the import statement I use here not just prefetch the js file, but also evaluate the js file, means the code of that js file is parsed & compile to bytecodes, the top level code of that js is executed.

This is a very time consuming operation on mobile device and I want to optimize it, I only want the prefetch part, I don't want the evaluate & execute part, because later when some user interactions happen, I will trigger the parsing & evaluate myself

enter image description here

↑↑↑↑↑↑↑↑ I only want to trigger first two steps, picture come from: https://calendar.perfplanet.com/2011/lazy-evaluation-of-commonjs-modules/↑↑↑↑↑↑↑↑↑

Sure I can do this by adding the prefetch link myself, but this means I need to know which url I should put in the prefetch link, webpack definitely know this url, how can I get it from webpack?

Does webpack have any easy way to achieve this?


Viewing all articles
Browse latest Browse all 142353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>