Skip to content

Commit

Permalink
Upgrade webpack-encore recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
MolloKhan committed Feb 2, 2021
1 parent edcad25 commit 2f35e0f
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 31 deletions.
12 changes: 12 additions & 0 deletions assets/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 import will output into a single css file (app.css in this case)
import './styles/app.css';

// start the Stimulus application
import './bootstrap';
5 changes: 5 additions & 0 deletions assets/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { startStimulusApp } from '@symfony/stimulus-bridge';
import '@symfony/autoimport';

// Registers Stimulus controllers from controllers.json and in the controllers/ directory
export const app = startStimulusApp(require.context('./controllers', true, /\.(j|t)sx?$/));
4 changes: 4 additions & 0 deletions assets/controllers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"controllers": [],
"entrypoints": []
}
16 changes: 16 additions & 0 deletions assets/controllers/hello_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from 'stimulus';

/*
* This is an example Stimulus controller!
*
* Any element with a data-controller="hello" attribute will cause
* this controller to be executed. The name "hello" comes from the filename:
* hello_controller.js -> "hello"
*
* Delete this file or adapt it for your use!
*/
export default class extends Controller {
connect() {
this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js';
}
}
3 changes: 3 additions & 0 deletions assets/styles/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: lightgray;
}
2 changes: 2 additions & 0 deletions config/packages/test/webpack_encore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#webpack_encore:
# strict_mode: false
30 changes: 23 additions & 7 deletions config/packages/webpack_encore.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
webpack_encore:
# The path where Encore is building the assets.
# This should match Encore.setOutputPath() in webpack.config.js.
# The path where Encore is building the assets - i.e. Encore.setOutputPath()
output_path: '%kernel.project_dir%/public/build'
# If multiple builds are defined (as shown below), you can disable the default build:
# output_path: false

# if using Encore.enableIntegrityHashes() specify the crossorigin attribute value (default: false, or use 'anonymous' or 'use-credentials')
# Set attributes that will be rendered on all script and link tags
script_attributes:
defer: true
# link_attributes:

# If using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
# crossorigin: 'anonymous'

# Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes).
# To enable caching for the production environment, creating a webpack_encore.yaml in the config/packages/prod directory with this value set to true
# Available in version 1.2
#cache: false
# Preload all rendered script and link tags automatically via the HTTP/2 Link header
# preload: true

# Throw an exception if the entrypoints.json file is missing or an entry is missing from the data
# strict_mode: false

# If you have multiple builds:
# builds:
# pass "frontend" as the 3rg arg to the Twig functions
# {{ encore_entry_script_tags('entry1', null, 'frontend') }}

# frontend: '%kernel.project_dir%/public/frontend/build'

# Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# Put in config/packages/prod/webpack_encore.yaml
# cache: true
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"devDependencies": {
"@symfony/stimulus-bridge": "^1.1.0",
"@symfony/webpack-encore": "^1.0.0",
"@fortawesome/fontawesome-free": "^5.11.2",
"@symfony/webpack-encore": "^0.28.0",
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"core-js": "^3.0.0",
"moment": "^2.24.0",
"regenerator-runtime": "^0.13.2",
"stimulus": "^2.0.0",
"webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
Expand Down
14 changes: 9 additions & 5 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -496,18 +496,22 @@
]
},
"symfony/webpack-encore-bundle": {
"version": "1.0",
"version": "1.9",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.0",
"ref": "d3b160b9799c91cbfc3df6f018c795f3bf88698a"
"version": "1.9",
"ref": "579d8de06df2ceb34d39e84e84c0c051b9b5ac68"
},
"files": [
"assets/css/app.css",
"assets/js/app.js",
"assets/app.js",
"assets/bootstrap.js",
"assets/controllers.json",
"assets/controllers/hello_controller.js",
"assets/styles/app.css",
"config/packages/assets.yaml",
"config/packages/prod/webpack_encore.yaml",
"config/packages/test/webpack_encore.yaml",
"config/packages/webpack_encore.yaml",
"package.json",
"webpack.config.js"
Expand Down
32 changes: 14 additions & 18 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Encore = require('@symfony/webpack-encore');
const 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.
Expand All @@ -17,15 +17,13 @@ Encore
/*
* 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')
.addEntry('app', './assets/app.js')

// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
.enableStimulusBridge('./assets/controllers.json')

// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
.splitEntryChunks()
Expand All @@ -47,15 +45,14 @@ Encore
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())

// enables @babel/preset-env polyfills
.configureBabel(() => {}, {
useBuiltIns: 'usage',
corejs: 3
.configureBabel((config) => {
config.plugins.push('@babel/plugin-proposal-class-properties');
})

.copyFiles({
from: './assets/images',
to: 'images/[path][name].[hash:8].[ext]'
// enables @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})

// enables Sass/SCSS support
Expand All @@ -64,16 +61,15 @@ Encore
// uncomment if you use TypeScript
//.enableTypeScriptLoader()

// uncomment if you use React
//.enableReactPreset()

// 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')
;

module.exports = Encore.getWebpackConfig();

0 comments on commit 2f35e0f

Please sign in to comment.