jQuery在webpack symfony encore中定义版本

时间:2019-03-08 11:40:50

标签: jquery symfony webpack-encore

我有一个旧版symfony项目,我想慢慢地迁移到webpack,在documentation中它说我们需要包含这样的jquery

Encore
// you can use this method to provide other common global variables,
// such as '_' for the 'underscore' library
.autoProvideVariables({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery',
})

问题是加载的jquery的版本始终为3,但是在我的旧项目中,我需要jquery的版本2。 如何定义要加载的jQuery版本?

1 个答案:

答案 0 :(得分:0)

根据此答案install jquery 2.1.1 您可以:

  • 可以更改package.json文件中的版本号,例如
{
    "devDependencies": {
        "@symfony/webpack-encore": "^0.22.0",
        "jquery": "2.2.4",
        "webpack-notifier": "^1.6.0"
    },
    ...
    ...
}

并使用以下命令进行安装:

npm install
  • 可以使用以下命令直接在您的项目目录中安装:
npm install jquery@2.2.4 --save-dev

然后在webpack配置中,您可以使用以下配置示例:

var Encore = require('@symfony/webpack-encore');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')

    //.setManifestKeyPrefix('build/')

    .addEntry('app', './assets/js/app.js')
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())

    // uncomment if you're having problems with a jQuery plugin
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

这是解决方案的尝试。

相关问题