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'])
请评论或添加另一个答案,说明如何更顺利地完成此操作。
导出/导出
之间有什么区别?我无法使用此方法使用导入
,如果我尝试导出默认值
(或删除导出默认值
,您可能想使用原始加载器,则仅需要,并且它会中断