首页 > 解决方案 > Symfony Webpack Encore:从 Bundle 导入 .js

问题描述

使用 Symfony,我创建了一个包。此捆绑包已正确导入/vendor. 此捆绑包有两个公共文件:

如何使用 Wepack Encore 在我的主项目中导入这些文件?

目前我正在做:

assets/js/app.js

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

assets/css/app.scss

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

但是我认为这不是理想的解决方案……您还有其他想法吗?

标签: symfonywebpackwebpack-encore

解决方案


在我看来,您有两种解决方案:

  1. 像@hoover_D 所说的那样做。在 webpack.config.js 中添加两个条目。

    // for the js  
    .addEntry('js/mybundle', './vendor/.../mybundle.js')  
    
    // for the scss  
    .addStyleEntry('css/mybundle', './vendor/.../css/mybundle.scss')  
    
  2. 如果多个文件需要你的包的 js 代码,你可以使用createSharedEntry. 文档在这里

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

推荐阅读