Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Laravel mix将模板文件/存根编译成JS变量_Javascript_Laravel_Laravel Mix - Fatal编程技术网

Javascript 使用Laravel mix将模板文件/存根编译成JS变量

Javascript 使用Laravel mix将模板文件/存根编译成JS变量,javascript,laravel,laravel-mix,Javascript,Laravel,Laravel Mix,我有一个文件夹/resources/js/stubs。该文件夹中有一些文件,比如User.stub、Controller.stub和Migration.stub。我希望通过这样做,在javascript中使用这些文件的内容 import Templates from './Templates.js' console.log(Templates.User) // The content of User.stub is printed 我不想使用这种方法 module.exports = name

我有一个文件夹
/resources/js/stubs
。该文件夹中有一些文件,比如
User.stub
Controller.stub
Migration.stub
。我希望通过这样做,在javascript中使用这些文件的内容

import Templates from './Templates.js'
console.log(Templates.User)
// The content of User.stub is printed
我不想使用这种方法

module.exports = name => `
...
`
我也可以使用后端将文件加载到视图中,但我不希望这样

所以这需要预处理。我能用Laravel mix做吗?如果没有,我的选项是什么,我需要向我的Laravel应用程序添加什么?

部分解决方案 感谢Diogo Sgrillo在评论中指出此解决方案

安装原始装载机
纱线添加毛坯加载器——开发

webpack.mix.js 添加此配置(在我的示例中,所有文件都将以
.stub
扩展名命名):

webpack.mix.js
中添加一个单独的管道,如下所示:

mix.js('src/templates/index.js', 'src/templates.js')
let templates = require('./templates.js')
console.log(templates['User.php'])
它将编译
index.js
文件中的模板列表,并将它们放入
templates.js

src/templates/index.js 以后可以这样使用:

mix.js('src/templates/index.js', 'src/templates.js')
let templates = require('./templates.js')
console.log(templates['User.php'])

请评论或添加另一个答案,说明如何更顺利地完成此操作。
导出/导出
之间有什么区别?我无法使用此方法使用
导入
,如果我尝试
导出默认值
(或删除
导出默认值

您可能想使用原始加载器,则仅需要,并且它会中断