简体   繁体   English

使用Webpack要求显示模块

[英]Using Webpack to require revealing modules

I've just started looking into all the fancy stuff Javascript has to offer mainly Webkit . 我刚刚开始研究Javascript主要提供Webkit所有奇特功能。 I've got a decent sized application which uses the revealing module pattern. 我有一个使用揭示模块模式的大小合适的应用程序。 These modules make calls to public functions of other modules to make calculations and return results. 这些模块调用其他模块的公共功能以进行计算并返回结果。

Using this pattern was working great until I started to contemplate having dozens of script includes in my html pages. 直到我开始考虑在我的html页面中包含数十个脚本之前,使用这种模式的效果很好。 So, here we are... 所以,我们在这里

For the purposes of the question, I have made a much simpler application to demonstrate what is going wrong for me. 出于这个问题的目的,我提出了一个简单得多的应用程序来说明问题出在我身上。

I have two javascript modules contained within their own files: 我在自己的文件中包含两个javascript模块:

// one.js 
require("babel-loader?two!./two.js"); 
var one = (function(){
  two.sayHello(); 
})() 

// two.js
var two = (function(){
  var sayHello = function(){
    console.log('Hello World'); 
  }
  return {
    sayHello: sayHello
  }
})()

What I'm trying to do is use sayHello function from two.js within one.js . 我想做的是在one.js使用two.js中的sayHello函数。

So, firstly, I installed Webpack and exports-loader and created the following configuration file: 因此,首先,我安装了Webpackexports-loader并创建了以下配置文件:

module.exports = {
  entry: './index.js', 
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx$/,

        loader: "babel-loader",
        query: {
          presets: ['es2015'] 
        }
      }
    ]
  }
}

index.js is my entry file and simply includes the following single line: index.js是我的输入文件,仅包含以下一行:

require('./one.js'); 

Now, when trying to run this code, I'm getting the following error in the console: 现在,当尝试运行此代码时,控制台中出现以下错误:

Uncaught ReferenceError: two is not defined 未捕获的ReferenceError:未定义两个

With a little more digging, I found that my compiled bundle.js file was throwing the following Error when trying to import two.js : 经过进一步的挖掘,我发现我的编译的bundle.js文件在尝试导入two.js时抛出以下错误:

throw new Error("Module parse failed: \\testing_env\\webpack\\two.js 'import' and 'export' may only appear at the top level (2:2)\\nYou may need an appropriate loader to handle this file type. 引发新错误(“模块解析失败:\\ testing_env \\ webpack \\ two.js'import'和'export'可能仅出现在顶层(2:2)\\ n您可能需要适当的加载程序来处理此文件类型。

Obviously i'm doing something wrong, I'm just not sure what. 显然我做错了,我不确定。 I've tried both exports-loader and babel-loader but with no joy. 我已经尝试了exports-loaderbabel-loader但没有任何乐趣。

Which loader should I be using to parse module dependencies? 我应该使用哪个加载器来解析模块依赖性?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Out of the box webpack has support for CommonJS (which is the same module system that Nodejs implements ) . 开箱即用的webpack支持CommonJS (与Nodejs实现的模块系统相同) This means that you need to use the require/export syntax to make use of the modules. 这意味着您需要使用require / export语法来使用这些模块。

To export something in a module you simply do: 要导出模块中的内容,您只需执行以下操作:

// a.js
function a() { console.log("I'm a module '); }

module.exports = a;

and in your other module you do: 然后在其他模块中执行以下操作:

// b.js
const a = require('./a.js');

a(); // I'm a module

require() will simply returns the exports object. require()将仅返回exports对象。 Whatever you put on it you will be able to retrieve in another module. 无论您穿什么,都可以在另一个模块中进行检索。

This is why webpack is a module bundler, it comes with a built in module system. 这就是为什么webpack是模块捆绑器,它带有内置的模块系统。 The revelaing module pattern is more practical if you load your JS files directly in the browser without a module bundler, and you want to incapsulate your data (in "modules"). 如果您在没有模块捆绑器的情况下直接在浏览器中加载JS文件,并且想要封装数据(在“模块”中),那么转载模块模式更为实用。

The Revelaing module pattern is useful when you don't have a module system and you simply load up your JS files in the browser. 如果您没有模块系统,而只是在浏览器中加载JS文件,那么Revelaing模块模式就很有用。 Till recenlty when ES Modules were introduced in the browser, JavaScript didn't have a module system of it's own. 当浏览器中引入ES模块时,JavaScript才没有它自己的模块系统。 This is why people came up with the module pattern, to have a way to incapsulate logic in the browser. 这就是为什么人们想出模块模式,以便在浏览器中封装逻辑的原因。 But now because of tools like webpack and now we have natively the ES Modules system, we can have a more better way to incapsulate our logic. 但是现在由于有了webpack之类的工具,并且现在有了ES Modules系统本身,我们可以有一种更好的方法来封装我们的逻辑。

Does this make sense? 这有意义吗?

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

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