1

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

我正在使用 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.twigJavaScript 部分

<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.js中链接时使用 jQuery。base.html.twig

我的问题是,当我保存任何更改时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.

4

2 回答 2

0

我遇到了同样的情况,经过搜索,我发现上面提供了解决方案。有一次看不懂的,补充一下

Encore
.addExternals({
    jquery: 'jQuery'
})
于 2019-11-08T15:53:45.733 回答
0

你得到这个错误是因为你试图将 jQuery 之类的东西导入到由 Webpack ( require('jquery')) 处理的文件中,而实际上你的node_modules文件夹中没有依赖项。

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

  • yarn add(或npm install)这些依赖项并从 Webpack 配置中删除您的<script>标签和config.externals.jquery = 'jQuery'行(这将导致 jQuery 捆绑在您的 JS 文件中)
  • 使用Encore.addExternal()(一种更简洁的方式config.externals.jquery = 'jQuery')告诉 Webpack jQuery 将由外部提供,不应捆绑
于 2019-08-06T13:35:31.207 回答