如何使 jQuery 在 Symfony Webpack Encore 外部工作?
How to make jQuery work externally with Symfony Webpack Encore?
我正在学习如何在法国网站 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.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.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
.
你得到这个错误是因为你试图将 jQuery 之类的东西导入到由 Webpack (require('jquery')
) 处理的文件中,而实际上你的 node_modules
文件夹中没有依赖项。
为了解决这个问题,您需要:
yarn add
(或npm install
)这些依赖项并从您的 Webpack 配置中删除您的 <script>
标记和 config.externals.jquery = 'jQuery'
行(这将导致 jQuery捆绑在您的 JS 文件中)
- 使用
Encore.addExternal()
(一种更简洁的config.externals.jquery = 'jQuery'
方式)告诉WebpackjQuery将由外部提供,不应该被捆绑
我遇到了同样的情况,经过搜索我发现上面提供了解决方案。
一次没看懂的就加这个
Encore
.addExternals({
jquery: 'jQuery'
})
我正在学习如何在法国网站 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.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.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
.
你得到这个错误是因为你试图将 jQuery 之类的东西导入到由 Webpack (require('jquery')
) 处理的文件中,而实际上你的 node_modules
文件夹中没有依赖项。
为了解决这个问题,您需要:
yarn add
(或npm install
)这些依赖项并从您的 Webpack 配置中删除您的<script>
标记和config.externals.jquery = 'jQuery'
行(这将导致 jQuery捆绑在您的 JS 文件中)- 使用
Encore.addExternal()
(一种更简洁的config.externals.jquery = 'jQuery'
方式)告诉WebpackjQuery将由外部提供,不应该被捆绑
我遇到了同样的情况,经过搜索我发现上面提供了解决方案。 一次没看懂的就加这个
Encore
.addExternals({
jquery: 'jQuery'
})