简体   繁体   English

react-starter-kit中的require('template.jade')

[英]require('template.jade') in react-starter-kit

Can someone maybe explain me, how this build-time require works? 有人可以向我解释一下,此构建时间require如何工作吗?

https://github.com/kriasoft/react-starter-kit/blob/feature/redux/src/server.js#L89 https://github.com/kriasoft/react-starter-kit/blob/feature/redux/src/server.js#L89

They are requiring a jade template, which package or configuration allows this, I seem unable to find it myself. 他们需要一个jade模板,该程序包或配置允许该模板,我似乎无法自己找到它。

const template = require('./views/index.jade')

I think is much more elegant then: 我认为再优雅一点:

import jade from 'jade'
const template = jade.compile('./views/index.jade')

As RGraham mentioned in his comment , the require call is being "intercepted" during webpack's compilation of the application bundle. 正如RGraham在他的评论中提到的那样 ,在webpack编译应用程序捆绑包期间, require调用被“拦截”了。 This is done using "loaders" that define particular behaviour for imports of a particular type: 这是通过使用“加载程序”完成的,这些加载程序为特定类型的导入定义了特定的行为:

Loaders allow you to preprocess files as you require() or “load” them. 加载程序允许您根据需要对文件进行预处理或“加载”文件。

In this particular case, the loader that does this modification could be one of these (or another that I didn't find in my search): 在这种特殊情况下,进行此修改的加载程序可以是其中一种(或在搜索中未找到的另一种):

Edit: looking at the project's own webpack configuration we can see it is the second link above: 编辑:查看项目自己的webpack配置,我们可以看到它是上面的第二个链接:

{
  test: /\.jade$/,
  loader: 'jade-loader',
}

jade-loader reads the content of the specified file, which make look something like this (Jade string): jade-loader读取指定文件的内容,该文件看起来像这样(Jade字符串):

h1 Hello, #{author}!

..and replaces that with a CommonJS JavaScript code similar to this (at compile time): ..并用与此类似的CommonJS JavaScript代码替换它(在编译时):

module.exports = function(data) {
  return `<h1>Hello, ${data.name}</h1>`;
};

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

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