prepack-webpack-plugin icon indicating copy to clipboard operation
prepack-webpack-plugin copied to clipboard

Code splits limitation

Open FranckFreiburger opened this issue 7 years ago • 1 comments
trafficstars

Detailed Description

Since code split creates multiple independent chunks, prepack can only apply optimizations to chunk "shells", not modules contained in these chunks (maybe except for entry chunk). I wondering if there is any workaround for this ?

Possible Implementation

call __optimize(module) for each module in a chunk eg: https://prepack.io/repl.html#GYVwdgxgLglg9mABAWzgExAGwKYEYAUAlIgN4BQiioksCVccRpFliATtlCG0gOTANeAbhYBfFtWjwkAIwCGbJuVbtO3PvLbCxLDlx71GxANSJNREeLIB9a3AAOsZDABe2fKgw5chMkA

FranckFreiburger avatar Nov 02 '18 11:11 FranckFreiburger

    compiler.hooks.thisCompilation.tap('PrepackPlugin', compilation => {

      compilation.chunkTemplate.hooks.render.tap(
        {
          name: 'PrepackPlugin',
          stage: 9999, // stage:9999 // need to be at latest stage
        },
        (source, chunk, module) => {

          const jsonpFunction = compilation.chunkTemplate.outputOptions.jsonpFunction;
          const globalObject = compilation.chunkTemplate.outputOptions.globalObject;

          source.add(`;(function(){
            var chunkModuleList = ${globalObject}[${JSON.stringify(jsonpFunction)}].slice(-1)[0][1];
            for (var id in chunkModuleList)
              __optimize(chunkModuleList[id]);
          })()`);
        }
      )
    });

FranckFreiburger avatar Nov 02 '18 18:11 FranckFreiburger