Jquery 在 webpack symfony encore 中定义版本

Jquery define version in webpack symfony 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 的版本?

根据这个回答 你可以:

  • 要么更改 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();

这是解决方案的尝试。