简体   繁体   English

在webpack中异步加载

[英]async loading in webpack

I have the following situation. 我有以下情况。

EntryA 参赛作品A

require("./test.js");

EntryB 条目B

require("./test.js");

test.js test.js

module.exports = "something";

I want tuse webpack to compile these javascript files. 我想用webpack编译这些javascript文件。 On the html page i just want to include the EntryA.js. 在html页面上,我只想包含EntryA.js。 Every thing that is common between EntryA and EntryB should come in a sperate file. EntryA和EntryB之间的所有共同之处都应放在一个单独的文件中。 Now, when test.js is required, that common file should be downloaded from the net. 现在,当需要test.js时,应该从网上下载该通用文件。

is this possible and ghow should i proceed? 这可能吗,我应该如何进行?

Webpack offers two ways to split out your JS. Webpack提供了两种拆分JS的方法。

  1. Use multiple entry points (As you have done). 使用多个入口点 (已完成)。
  2. Define split points via require.ensure (or System.import in Webpack 2). 通过require.ensure (或Webpack 2中的System.import )定义拆分点

Approach 1 is really intended to be used by traditional 'multi' page websites and requires you to import the appropriate entry point on the correct page. 方法1实际上是供传统的“多”页面网站使用的,并且要求您在正确的页面上导入适当的入口点。 NOTE: You can use the CommonsChunkPlugin to extract out all the shared code but it still must be manually referenced. 注意:您可以使用CommonsChunkPlugin提取所有共享代码,但是仍然必须手动引用它。

Approach 2 is more geared towards single page apps and will automatically load in new scripts as necessary. 方法2更适合于单页应用程序,并将在必要时自动加载新脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM