Skip to content

Commit c835d1c

Browse files
committed
Add Stimulus in Webpack Encore recipe
1 parent 12765b0 commit c835d1c

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

symfony/webpack-encore-bundle/1.0/assets/app.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@
88
// any CSS you import will output into a single css file (app.css in this case)
99
import './styles/app.css';
1010

11-
// Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
12-
// import $ from 'jquery';
13-
14-
console.log('Hello Webpack Encore! Edit me in assets/app.js');
11+
// start the Stimulus application
12+
import './bootstrap';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { startStimulusApp } from '@symfony/stimulus-bridge';
2+
import '@symfony/autoimport';
3+
4+
// Registers Stimulus controllers from controllers.json and in the controllers/ directory
5+
export const app = startStimulusApp(require.context('./controllers', true, /\.(j|t)sx?$/));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"controllers": [],
3+
"entrypoints": []
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Controller } from 'stimulus';
2+
3+
/*
4+
* This is an example Stimulus controller!
5+
*
6+
* Any element with a data-controller="hello" attribute will cause
7+
* this controller to be executed. The name "hello" comes from the filename:
8+
* hello_controller.js -> "hello"
9+
*
10+
* Delete this file or adapt it for your use!
11+
*/
12+
export default class extends Controller {
13+
connect() {
14+
this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js';
15+
}
16+
}

symfony/webpack-encore-bundle/1.0/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"devDependencies": {
3-
"@symfony/webpack-encore": "^0.31.0",
3+
"@symfony/stimulus-bridge": "^1.0.0",
4+
"@symfony/webpack-encore": "^0.32.0",
45
"core-js": "^3.0.0",
6+
"stimulus": "^1.1.1",
57
"regenerator-runtime": "^0.13.2",
68
"webpack-notifier": "^1.6.0"
79
},

symfony/webpack-encore-bundle/1.0/webpack.config.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ Encore
1717
/*
1818
* ENTRY CONFIG
1919
*
20-
* Add 1 entry for each "page" of your app
21-
* (including one that's included on every page - e.g. "app")
22-
*
2320
* Each entry will result in one JavaScript file (e.g. app.js)
2421
* and one CSS file (e.g. app.css) if your JavaScript imports CSS.
2522
*/
2623
.addEntry('app', './assets/app.js')
27-
//.addEntry('page1', './assets/page1.js')
28-
//.addEntry('page2', './assets/page2.js')
24+
25+
// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
26+
.enableStimulusBridge('./assets/controllers.json')
2927

3028
// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
3129
.splitEntryChunks()
@@ -47,6 +45,10 @@ Encore
4745
// enables hashed filenames (e.g. app.abc123.css)
4846
.enableVersioning(Encore.isProduction())
4947

48+
.configureBabel((config) => {
49+
config.plugins.push('@babel/plugin-proposal-class-properties');
50+
})
51+
5052
// enables @babel/preset-env polyfills
5153
.configureBabelPresetEnv((config) => {
5254
config.useBuiltIns = 'usage';
@@ -59,16 +61,15 @@ Encore
5961
// uncomment if you use TypeScript
6062
//.enableTypeScriptLoader()
6163

64+
// uncomment if you use React
65+
//.enableReactPreset()
66+
6267
// uncomment to get integrity="..." attributes on your script & link tags
6368
// requires WebpackEncoreBundle 1.4 or higher
6469
//.enableIntegrityHashes(Encore.isProduction())
6570

6671
// uncomment if you're having problems with a jQuery plugin
6772
//.autoProvidejQuery()
68-
69-
// uncomment if you use API Platform Admin (composer req api-admin)
70-
//.enableReactPreset()
71-
//.addEntry('admin', './assets/admin.js')
7273
;
7374

7475
module.exports = Encore.getWebpackConfig();

0 commit comments

Comments
 (0)