为什么Webpack使用jsonp获取块脚本 [英] Why webpack use jsonp to get chunk script

查看:1552
本文介绍了为什么Webpack使用jsonp获取块脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为不存在CORS问题.

为什么webpack使用jsonp获取块脚本?

这是生成的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__);
/******/    };
 

解决方案

我的想法是,

无论webpack使用的是JSON还是JSONP,它都必须将已加载的块文件附加到文档中.

因此,Webpack团队可能会这样做,而不是先获取脚本文件(JSON)然后附加文档,而不是先获取脚本标签,然后再附加脚本标签(JSONP),然后让脚本标签加载文件. /p>

关于JSON和JSONP的此处的很好的解释

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__);
/******/    };

解决方案

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

这篇关于为什么Webpack使用jsonp获取块脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆