如何在Symfony应用程序中通过Webpack Encore添加外部库



在Symfony应用程序中,我们使用Webpack&Encore用于管理我们的自定义javascript库。

然而,我们也想添加一些外部库,如web3.js.

如何做到这一点?

我们的webpack.config.js看起来像这样:

Encore
.setOutputPath('public/build/')
.addEntry('apiPlatform', './assets/apiPlatformCustomize.js')
.addEntry('shop-entry', './assets/shop/entry.js')
.disableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.enableSassLoader()
.autoProvidejQuery()
;

对于注入CCD_ 1,由于采用了一种方便的方法,它非常容易。但是,如何使用npm install web3安装其他外部库呢?

我见过类似addExternals的东西,但找不到关于它的任何信息,也找不到如何正确使用它。

非常感谢您的帮助!

我想说:不要在webpack配置中导入它。

添加条目是您的";js";您的应用程序的入口文件。这意味着,如果你安装了想要使用访问的外部库

npm install alibrary

你会把它导入你的内心"main.js";文件

这里有一个例子:

webpack.config.js内部

.addEntry('website', './assets/website.js')
.splitEntryChunks() // this line is important to avoid multiple entry for css and js but require a website.css to work properly

然后在你的主要分支文件:

{{ encore_entry_link_tags('website') }} // inside <header> tag for css
{% block javascripts %} // at the bottom of your <body>
{{ encore_entry_script_tags('website') }} for.js
{% endblock %}

现在,您的网站.js已正确导入到您的应用程序中。现在,如果你想让jquery安装它:

npm i jquery

然后进入您的网站.js

import 'jquery-form/dist/jquery.form.min'
import $ from "jquery";

现在jquery可以在website.js和您的应用程序中使用。

根据您想要的库,有时只需要基本导入。

import $ from "jquery";

是特定于jquery的,以便使其工作

最新更新