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

有人可以解释我,这个build造时间require工作吗?

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

他们需要一个jade模板,哪个包或者configuration允许这个,我似乎无法自己find它。

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

那么我觉得更优雅了:

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

正如RGraham在他的评论中提到的那样 ,在webpack编译应用程序包的过程中, require调用被“拦截”了。 这是通过使用“加载器”来完成的,这些加载器为特定types的导入定义了特定的行为:

加载器允许您按需要预处理文件()或“加载”它们。

在这种情况下,做这种修改的加载器可能是其中的一个(或者我在search中没有find的另一个):

编辑:看看项目自己的webpackconfiguration,我们可以看到它是上面的第二个链接:

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

jade-loader读取指定文件的内容,这看起来像这样(Jadestring):

 h1 Hello, #{author}! 

..并用一个CommonJS的JavaScript代码(在编译时)replace它:

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