Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why webpack use jsonp to get chunk script

Tags:

jsonp

webpack

I don't think there's a CORS issue.

Why webpack use jsonp to get chunk script?

This is generated webpackBootstrap.

/******/    // install a JSONP callback for chunk loading
/******/    var parentJsonpFunction = window["webpackJsonp"];
/******/    window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules) {
/******/        // add "moreModules" to the modules object,
/******/        // then flag all "chunkIds" as loaded and fire callback
/******/        var moduleId, chunkId, i = 0, callbacks = [];
/******/        for(;i < chunkIds.length; i++) {
/******/            chunkId = chunkIds[i];
/******/            if(installedChunks[chunkId])
/******/                callbacks.push.apply(callbacks, installedChunks[chunkId]);
/******/            installedChunks[chunkId] = 0;
/******/        }
/******/        for(moduleId in moreModules) {
/******/            modules[moduleId] = moreModules[moduleId];
/******/        }
/******/        if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules);
/******/        while(callbacks.length)
/******/            callbacks.shift().call(null, __webpack_require__);
/******/    };
like image 617
mjkim Avatar asked Nov 23 '16 08:11

mjkim


1 Answers

My thought here is,

Whether webpack uses JSON or JSONP, it has to append the loaded chunk file into the document.

So webpack team might have though that instead of getting the script file first (JSON) and then appending with the document, append the script tag first (JSONP) and then let the script tag load the file.

Nice explanation here on JSON and JSONP

like image 54
Thaadikkaaran Avatar answered Oct 21 '22 03:10

Thaadikkaaran