最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is `_dereq_()` inside React? - Stack Overflow

programmeradmin0浏览0评论

I'm searching through react-0.13.3.js and one thing I can't work out. At the very start of the source there's a bunch of dereq() calls.

'use strict';

var EventPluginUtils = _dereq_(19);
var ReactChildren = _dereq_(32);
var ReactComponent = _dereq_(34);
var ReactClass = _dereq_(33);

But nowhere do I see an implementation for that function. What does it do and how does it work? And most importantly, where is it declared?

I'm searching through react-0.13.3.js and one thing I can't work out. At the very start of the source there's a bunch of dereq() calls.

'use strict';

var EventPluginUtils = _dereq_(19);
var ReactChildren = _dereq_(32);
var ReactComponent = _dereq_(34);
var ReactClass = _dereq_(33);

But nowhere do I see an implementation for that function. What does it do and how does it work? And most importantly, where is it declared?

Share Improve this question edited Dec 13, 2022 at 23:48 Dmitry Shvedov 3,2864 gold badges40 silver badges56 bronze badges asked Jun 4, 2015 at 17:36 Morgan WildeMorgan Wilde 17.3k12 gold badges54 silver badges102 bronze badges 3
  • 1 See here: github.com/calvinmetcalf/derequire/issues/25. – glortho Commented Jun 5, 2015 at 1:41
  • That's the answer right there. If you could write it up, I would accept it :) – Morgan Wilde Commented Jun 5, 2015 at 1:43
  • Okay I'll add it to my answer below. – glortho Commented Jun 5, 2015 at 1:44
Add a comment  | 

4 Answers 4

Reset to default 9

This is likely derequire combined with browserify (or other bundler?) module ids. It's a way to rename require in your output bundle so as to avoid collisions.

Read here for more information: https://github.com/calvinmetcalf/derequire/issues/25

The basic idea is that different module loaders handle require differently. "Derequire" is a strategy for binding your requires to your context via a different keyword.

It has to do with amd module loader. _dereq_ is the first argument so it's the require function.

You need to expand line 4 in the source; it's require.

1: [function(_dereq_, module, exports) {

I have stumbled on this question, after some research, I think the main purpose of having this function is to build a similar npm like feature on your browser and handle module dependency on importing different functions.

if you go straight to the end of the IIFE (about 20k~ lines)

you will see a structure similar to below

(function e(t, n, r) {
    function s(o, u) {
       ...
    }

       ...
}) ({...  //blah blah blah, all the funcitons 
     }, {}, [1])


the variable t is all the functions modules
the variable n is an empty array
 the variable r is 1 here

so the require module is called in the right order, and is built from index 1, from there, it is pretty self-explanatory, all dependencies needs to be correctly programmed from the array on the outset

1: [function (_dereq_, module, exports) {....
}, {"106": 106, "23": 23, "35": 35, "45": 45, "63": 63}],
发布评论

评论列表(0)

  1. 暂无评论