简体   繁体   English

Symfony Webpack Encore:从捆绑包导入.js

[英]Symfony Webpack Encore : Import .js from Bundle

With Symfony, I created a bundle. 使用Symfony,我创建了一个捆绑包。 This bundle is correctly imported in /vendor . 此捆绑包已正确导入/vendor This bundle have a two public files : 该捆绑包有两个公共文件:

  • /MyBundle/Resources/Public/css/mybundle.scss
  • /MyBundle/Resources/Public/js/mybundle.js

How to import this files in my main projet with Wepack Encore ? 如何使用Wepack Encore在我的主要项目中导入此文件?

Currently i'm doing : 目前我正在做:

In assets/js/app.js : assets/js/app.js

import "../../vendor/gaylordp/mybundle/Resources/public/js/mybundle.js"; 导入“ ../../vendor/gaylordp/mybundle/Resources/public/js/mybundle.js”;

In assets/css/app.scss : assets/css/app.scss

@import "../../vendor/gaylordp/mybundle/Resources/public/css/mybundle.scss"; @import“ ../../vendor/gaylordp/mybundle/Resources/public/css/mybundle.scss”;

But I think it's not ideal solution ... do you have other idea ? 但是我认为这不是理想的解决方案……您还有其他想法吗?

You have to add an entry in your webpack.config.js . 您必须在webpack.config.js添加一个条目。

.addEntry('isotop', './vendor.../js/isotop.js')

after you do yarn run dev in the terminal to refresh the assets. yarn run devyarn run dev之后,在终端中yarn run dev以刷新资产。

tell us if it works 告诉我们是否可行

In my opinion you have two solutions: 我认为您有两种解决方案:

  1. Do it like @hoover_D tells. 就像@hoover_D告诉的那样。 Add two entries in your webpack.config.js. 在webpack.config.js中添加两个条目。

     // for the js .addEntry('js/mybundle', './vendor/.../mybundle.js') // for the scss .addStyleEntry('css/mybundle', './vendor/.../css/mybundle.scss') 
  2. If multiple files required the js code of your bundle, you can use the createSharedEntry . 如果多个文件需要捆绑包的js代码,则可以使用createSharedEntry Documentation is here . 文档在这里

     // for the js .createSharedEntry('js/mybundle', './vendor/.../mybundle.js') // for the scss .addStyleEntry('css/mybundle', './vendor/.../css/mybundle.scss') 

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

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