简体   繁体   English

代码拆分如何与导入/导出以及babel和webpack一起使用?

[英]how code splitting works with import/export and babel and webpack?

I am trying to answer, 我想回答

when to use import/export and when to use require()/module.exports ? 何时使用import / export以及何时使用require()/ module.exports But as I try to dig, it seems to get complicated. 但是,当我尝试挖掘时,它似乎变得复杂起来。

Here's my understanding 这是我的理解

  • require()/module.exports: this is nodejs implementation of the module system. require()/ module.exports:这是模块系统的nodejs实现。 This loads the modules syncronously. 这将同步加载模块。
  • with es6, we can use import/export. 使用es6,我们可以使用导入/导出。 the docs says 文档

The import statement is used to import bindings which are exported by another module. import语句用于导入由另一个模块导出的绑定。 Imported modules are in strict mode whether you declare them as such or not. 不管您是否声明导入模块,都处于严格模式。 The import statement cannot be used in embedded scripts unless such script has a type="module". 除非嵌入式脚本具有type =“ module”,否则无法在嵌入式脚本中使用import语句。

Ques1 : How does this work with babel or webpack or browsers in general? Ques1通常如何与babel或webpack或浏览器一起使用?

As I was exploring I came across stuff like CommonJs, requireJs, Asynchronous Module Definition (AMD) 在探索过程中,我遇到了诸如CommonJ,requireJ,异步模块定义(AMD)之类的东西。

Ques2 : I am more interested in knowing the timeline as how these things evolved in javascript ? Ques2 :我更想知道时间表,因为这些事情在javascript中是如何演变的?

How does this work with babel or webpack or browsers in general? 通常,如何与babel或webpack或浏览器一起使用?

Babel and Webpack follow the ES spec and transpile the import / export statement to one single file. Babel和Webpack遵循ES规范,并将import / export语句转换为一个文件。 As they also support the require syntax, they usually transpile the import statements to require() calls and the export statements to module exports , and then ship with a custom loader for modules., If you got for example: 因为它们还支持require语法,所以它们通常将import语句转换为require()调用,将export语句转换为module exports export ,然后随模块一起提供自定义加载程序。

 // A.js
 export default function() { }

 // B.js
 import A from "./A";
 A();

Then it gets transpiled to the following require syntax: 然后将其转换为以下require语法:

 //A.js
 exports.default = function() {};

 //B.js
 var A = require("./A").default;
 A();

That could then get wrapped to something like: 然后可以将其包装为:

 (function() { // prevent leaking to global scope
   // emulated loader:
   var modules = {};

   function require(name) { return modules[name]; }

   function define(name, fn) {
     var module = modules[name] = { exports: {} };
     fn(module, module.exports, require);
   }


  // The code:
  define("A", function(module, exports, require) {
      // A.js
     exports.default = function() { };
  });

  define("B", function(module, exports, require) { 
    // B.js
   var A = require("A").default;
    A();
  });
 })();

how these things evolved in javascript ? 这些东西在javascript中是如何演变的?

A few years ago, writing JS was restricted to browsers, and the only way to load multiple js sources was to use multiple <script> tags and use the global object to exchange functionality. 几年前,写JS仅限于浏览器,而加载多个js源的唯一方法是使用多个<script>标记并使用全局对象交换功能。 That was ugly. 那太丑了。

Then Nodejs was invented and they needed a better way to work with modules and invented the require() thing. 然后发明了Node.js,他们需要一种更好的方法来处理模块并发明了require()东西。

The writers of the spec saw a need for a native syntax for that, so import / export were introduced. 规范的作者看到了为此的本机语法的需要,因此引入了import / export

Babel and others then wrote transpilers. 然后,Babel和其他人写了翻译。

What webpack the bundler does is the following: 捆绑程序执行的webpack如下:

  1. You specify an input file in the config 您在配置中指定输入文件
  2. You specify an output file the config 您可以在配置文件中指定一个输出文件

Webpack will look at all the files which the input file requires (commomJS module system) or imports (ES6 module system). Webpack将查看输入文件requires所有文件(commomJS模块系统)或imports (ES6模块系统)。 It then funnels the code based on file name extention through loaders. 然后,它通过加载程序根据文件名扩展来合并代码。 Loaders can transpile the individual files to code the browser can understand. 装载机可以transpile单个文件的代码浏览器可以理解的。 An example of a loader is babel or the sass/scss compiler. 加载程序的一个示例是babel或sass / scss编译器。

After the different files are transpiled with loaders, the plugins can work at the transform the bundle of generated code into something else. 在使用加载程序对不同的文件进行编译之后, 插件可以将生成的代码捆绑转换为其他内容。 The bundle is just a bunch of code which together forms piece of functionality 捆绑包只是一堆代码,一起构成了功能

In won't go into detail in the internals of webpack too deeply, but the most important thing to understand is: 不会太深入地介绍webpack的内部细节,但是最重要的要了解的是:

You use webpack so you can use split up your code in multiple files, which makes them more maintainable and easier to work with. 使用webpack可以将代码拆分成多个文件,从而使它们更易于维护和使用。 However then requesting all these files by the client would be horrible for performance (many HTTP requests overhead). 但是,然后由客户端请求所有这些文件对于性能而言将是可怕的(许多HTTP请求开销)。 Therefore, we bundle the files into one file, or a couple so this overhead is reduced. 因此,我们将文件捆绑为一个或几个文件,从而减少了开销。

Generally, you should write all modern code with import/export syntax if you are using a bundler like webpack, or translating with Babel... npm modules may favor require/module syntax but you can still import them. 通常,如果您使用捆绑程序(例如webpack)或使用Babel进行翻译,则应使用import / export语法编写所有现代代码,npm模块可能更喜欢require / module语法,但您仍然可以导入它们。

Also worth noting is the import() method which returns a promise that should resolve to the root module being imported asynchronously. 同样值得注意的是import()方法,该方法返回一个承诺,该承诺应解析为异步导入的根模块。 Webpack may bundle these as asynchronous modules if you have it configured to do so. 如果已配置Webpack,则可以将它们捆绑为异步模块。

In practice the resolution through tooling like babel and webpack will fallback to node-style behaviors against the node_modules lookup, where the standard is transport paths, favoring relative paths. 在实践中,通过babel和webpack之类的工具进行的解析将回退到针对node_modules查找的节点样式行为,其中的标准是传输路径,而相对路径更受青睐。 Additional support is per environment. 每个环境都提供其他支持。

You can experiment with esm support in modern browsers and in current node (behind a flag as of this answer). 您可以在现代浏览器和当前节点(此答案后面的标记后面)中尝试使用esm支持。 The behaviors are somewhat inconsistent, but well defined. 行为有些不一致,但定义明确。 When in doubt, experiment and try. 如有疑问,请尝试并尝试。

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

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