简体   繁体   English

laravel 从 node_modules 中的包中混合编译资产?

[英]laravel mix compile assets from a package in node_modules?

I started using laravel-mix on my new Laravel 5.7 project to compile all the js/css into one file, which will appear in my public js/css directory (one for each page) like this:我开始在我的新 Laravel 5.7 项目中使用 laravel-mix 将所有 js/css 编译成一个文件,该文件将出现在我的公共 js/css 目录中(每个页面一个),如下所示:

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .js('resources/js/create.member.js', 'public/js/backend/members')
   .sass('resources/sass/create.member.scss', 'public/css/backend/members')
   .sourceMaps();

Like this I want to keep the views tidy and reduce the number of assets that have to be loaded.像这样,我想保持视图整洁并减少必须加载的资产数量。

Everything fine so far and I understand how to work with laravel-mix.到目前为止一切都很好,我了解如何使用 laravel-mix。 Now I installed some packages, specifically cropperjs and summernote, via npm, to my node_modules directory.现在我通过 npm 安装了一些包,特别是cropperjs 和summernote,到我的node_modules 目录。

It fails when I import/require their scss/js to the scripts which are going to be compiled and placed in public by laravel-mix because it seems that these scripts (cropper.js, summernote.scss, etc...) are referencing to other assets across their origin package directory within the node_modules directory (fonts, images, etc...)当我将他们的 scss/js 导入/要求他们的 scss/js 到将被 laravel-mix 编译并公开放置的脚本时,它会失败,因为这些脚本(cropper.js、summernote.scss 等)似乎正在引用到 node_modules 目录中原始包目录中的其他资产(字体、图像等...)

For JS I do:对于 JS,我这样做:

require('../../node_modules/cropperjs/dist/cropper.js');
require('../../node_modules/summernote/dist/summernote.js');

Fors SCSS I do:对于 SCSS 我做的:

@import '~cropperjs/src/css/cropper';
@import '~summernote/src/less/summernote-bs4';

My question is: What is good practice when using packages, installed with npm?我的问题是:使用 npm 安装的包时有什么好的做法?

Do I have to put the whole package directory to my public directory?我是否必须将整个包目录放到我的公共目录中? Do I link to the assets in the package directory which is located in the node_modules directory?我是否链接到位于 node_modules 目录中的包目录中的资产? Or is their any chance left to compile them together with my other assets via laravel-mix/webpack, maybe a flag I'm missing or so?或者他们是否有机会通过 laravel-mix/webpack 将它们与我的其他资产一起编译,也许是我缺少的标志? Do I have to use any additional software like bower to make things happen?我是否必须使用诸如 bower 之类的任何其他软件才能使事情发生?

For js I just use对于 js 我只使用

import 'vue'
import 'cropperjs'

Usually the package documentation will tell you how to import it.通常包文档会告诉你如何导入它。

你不应该在 webpack.mix 中编译节点资产......就像 barghouthi 说的,通常包文档会告诉你如何在你的 js 文件中导入包。

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

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