jQuery not exposed
See original GitHub issueI have problem with providing jQuery
as var to global scope for legacy js code.
main.js
var $ = require('jquery');
window.$ = window.jQuery = $;
webpack-config.js
var Encore = require('@symfony/webpack-encore');
Encore
// directory where all compiled assets will be stored
.setOutputPath('mainsites/build/')
// what's the public path to this directory (relative to your project's document root dir)
.setPublicPath('/build')
// empty the outputPath dir before each build
.cleanupOutputBeforeBuild()
// will output as web/build/app.js
.addEntry('app', './assets/js/main.js')
// allow legacy applications to use $/jQuery as a global variable
.autoProvidejQuery()
.enableSourceMaps(!Encore.isProduction())
// create hashed filenames (e.g. app.abc123.css)
.enableVersioning()
;
// export the final configuration
module.exports = Encore.getWebpackConfig();
package.js
{
"name": "schoolm",
"version": "0.0.1",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"jquery": "^2.1.4",
},
"devDependencies": {
"@symfony/webpack-encore": "github:symfony/webpack-encore"
}
}
html
<?php
$webpackConfig = json_decode(file_get_contents(__DIR__ . '/../mainsites/build/manifest.json'), true);
?>
<script src="<?php echo $webpackConfig['build/app.js']; ?>"></script>
chrome console
> jQuery
VM117:1 Uncaught ReferenceError: jQuery is not defined
at <anonymous>:1:1
(anonymous) @ VM117:1
> $
function ( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.…
Issue Analytics
- State:
- Created 6 years ago
- Comments:24 (15 by maintainers)
Top Results From Across the Web
Exposing jQuery not working · Issue #20 - GitHub
In my webpack config I have the following line to expose jQuery on the global scope { test: require.resolve('jquery'), loader: 'expose?$!expose?
Read more >Expose jQuery to real Window object with Webpack
Looks like the window object is exposed in all modules. Why not just import/require JQuery and put: window.$ = window.JQuery = JQuery;.
Read more >#14549 (npm jQuery does not expose the jQuery function, but ...
Even after fixing ticket #14548 myself, I was unable to use jQuery to select my DOM elements. E.g. when I did. var $...
Read more >Expose - jQuery TOOLS
Expose is a JavaScript tool that exposes selected HTML elements on the page so that the surrounding elements will gradually fade out. The...
Read more >jquery script on view not working when filtering with (ajax on ...
Thanks to Clive answer i have solved out the problem embedding to my theme script file : Drupal.behaviors.betterExposedFilters = { attach: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@gimler you need to explicitly expose the jQuery variables. To do so:
1/ you need to install the expose-loader 2/ update you webpack config
@weaverryan what about having a
exposeJquery()
method ?Ok got it to work. When using
window.jQuery
you must remove the.autoProvidejQuery()
from webpack config.main.js
webpack.config.js