如何使jQuery与Symfony Webpack Encore外部兼容?

看到

我正在关注有关如何在法国网站GraphikArt上使用Symfony创建网站的教程(因此我是新手)。我目前在“ Symfony Encore”部分,当我的Twig文件中的JS文件中需要jQuery时,我无法使其工作。

我正在使用Symfony 4.3.3和Yarn 1.17.3。正如那个家伙所显示的,我试图在我的末尾使该部分在外部起作用webpack.config.js

var config = Encore.getWebpackConfig();

config.externals.jquery = 'jQuery';

module.exports = config;

我也尝试了一些在此站点上可以找到的解决方案,但似乎没有任何效果。

我当前的文件:

webpack.config.js

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

// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if ( ! Encore.isRuntimeEnvironmentConfigured() ) {
    Encore.configureRuntimeEnvironment( process.env.NODE_ENV || 'dev' );
}

Encore
// directory where compiled assets will be stored
    .setOutputPath( 'public/build/' )
    // public path used by the web server to access the output path
    .setPublicPath( '/build' )
    // only needed for CDN's or sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
     */
    .addEntry( 'app', './assets/js/app.js' )
    //.addEntry('page1', './assets/js/page1.js')
    //.addEntry('page2', './assets/js/page2.js')

    // When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
    .splitEntryChunks()

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app
    .enableSingleRuntimeChunk()

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps( ! Encore.isProduction() )
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning( Encore.isProduction() )

    // enables @babel/preset-env polyfills
    .configureBabel( () => {
    }, {
        useBuiltIns: 'usage',
        corejs: 3
    } )

    // enables Sass/SCSS support
    .enableSassLoader()

    // uncomment if you use TypeScript
    //.enableTypeScriptLoader()

    // uncomment to get integrity="..." attributes on your script & link tags
    // requires WebpackEncoreBundle 1.4 or higher
    //.enableIntegrityHashes(Encore.isProduction())

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

// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;

var config = Encore.getWebpackConfig();

config.externals.jquery = 'jQuery';

module.exports = config;

app.js

/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you require will output into a single css file (app.css in this case)
require( '../css/app.css' );

// Need jQuery? Install it with "yarn add jquery", then uncomment to require it.
var $ = require( 'jquery' );
global.$ = global.jQuery = $;

require( 'select2' );

console.log( 'Hello Webpack Encore! Edit me in assets/js/app.js' );

base.html.twig JavaScript部分

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
{{ encore_entry_script_tags( 'app' ) }}

如本教程所示,我想app.jsbase.html.twig文件中链接jQuery使用

我的问题是,当我在中保存任何更改时app.js,都会收到以下消息:

Error: ./assets/js/app.js
Error: Can't resolve 'jquery' in 'C:\wamp\www\symfony4\assets\js'

教程(以及在线解决方案)如何能为其他人而不是我的人工作?

webpack.config.js文件中,而不是:

var config = Encore.getWebpackConfig();

config.externals.jquery = 'jQuery';

module.exports = config;

在该.autoProvidejQuery()之前,写:

.addExternals(
    {
        jquery: 'jQuery'
    }
)

而且,问题是我没有使用正确的命令行。使用yarn encore dev

里尔坎

之所以会出现此错误,是因为您试图将jQuery之类的内容导入由Webpack(require('jquery')处理的文件中,而实际上并没有在node_modules文件夹中包含依赖项

为了解决这个问题,您需要:

  • yarn add(或npm install)这些依赖项,<script>config.externals.jquery = 'jQuery'从Webpack配置中删除标签和行(这将导致jQuery捆绑在您的JS文件中)
  • 使用Encore.addExternal()(一种更清洁的方法config.externals.jquery = 'jQuery')告诉Webpack jQuery将在外部提供,不应捆绑在一起

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章