Adding new recipes for Encore, Stimulus & AssetMapper (#1202)

* Adding new recipes for Encore, Stimulus & AssetMapper

* StimulusBundle 1.0 -> 2.9

* updating namespace

* moving encore recipe changes to 2.0

* conflict with older version of flex

* adding ux_controller_link_tags() to recipe

* adding documentation to importmap

* updating message to match core

* Adding ux translator

* fixing translations path

* Documenting the new add-lines configurator

* Fixing translator initial version

* shorten to just var

* remove ux alias - there is no one package to install UX packages currently

* fixing wrong package name
This commit is contained in:
Ryan Weaver
2023-05-27 01:35:12 +02:00
committed by GitHub
parent 6d4e120add
commit c590fdaa8e
29 changed files with 459 additions and 1 deletions
+38
View File
@@ -313,6 +313,44 @@ colors`_ are supported too:
* <fg=blue>Read</> the documentation at <comment>https://symfony.com/doc</>
``add-lines`` Configurator
~~~~~~~~~~~~~~~~~~~~~~~~~~
If no other configurators can meet your needs, the ``add-lines`` configurator
can add entire lines to files, either at the top, bottom or after a target:
.. code-block:: json
"add-lines": [
{
"file": "webpack.config.js",
"content": "\nenables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)\n .enableStimulusBridge('./assets/controllers.json')",
"position": "after_target",
"target": ".splitEntryChunks()"
},
{
"file": "assets/app.js",
"content": "import './bootstrap.js';",
"position": "top",
"warn_if_missing": true
},
{
"file": "assets/translator.js",
"content": "export * from '../var/translations';",
"position": "bottom",
"requires": "symfony/webpack-encore-bundle"
}
]
Each item needs ``file``, ``content`` and ``position``, which can be
``top``, ``bottom`` or ``after_target``. If ``after_target`` is used, a
``target`` must also be specified, which is a string that will be searched for
in the file.
If ``warn_if_missing`` is set to ``true``, a warning will be shown to the
user if the ``file`` or ``target`` isn't found. If ``requires`` is set, the
rule will only be applied if the given package is installed.
Validation
----------
+7
View File
@@ -0,0 +1,7 @@
/*
* Welcome to your app's main JavaScript file!
*
* This file will be included onto the page via the importmap() Twig function,
* which should already be in your base.html.twig.
*/
console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉')
@@ -0,0 +1,3 @@
body {
background-color: skyblue;
}
@@ -0,0 +1,5 @@
framework:
asset_mapper:
# The paths to make available to the asset mapper.
paths:
- assets/
+21
View File
@@ -0,0 +1,21 @@
<?php
/**
* Returns the import map for this application.
*
* - "path" is a path inside the asset mapper system. Use the
* "debug:asset-map" command to see the full list of paths.
*
* - "preload" set to true for any modules that are loaded on the initial
* page load to help the browser download them earlier.
*
* The "importmap:require" command can be used to add new entries to this file.
*
* This file has been auto-generated by the importmap commands.
*/
return [
'app' => [
'path' => 'app.js',
'preload' => true,
],
];
+32
View File
@@ -0,0 +1,32 @@
{
"copy-from-recipe": {
"assets/": "assets/",
"config/": "%CONFIG_DIR%/",
"importmap.php": "importmap.php"
},
"aliases": ["asset-mapper", "importmap"],
"gitignore": [
"/%PUBLIC_DIR%/assets/"
],
"add-lines": [
{
"file": "templates/base.html.twig",
"content": " {{ importmap() }}",
"position": "after_target",
"target": "{% block javascripts %}",
"warn_if_missing": true
},
{
"file": "templates/base.html.twig",
"content": " <link rel=\"stylesheet\" href=\"{{ asset('styles/app.css') }}\">",
"position": "after_target",
"target": "{% block stylesheets %}",
"warn_if_missing": true
}
],
"conflict": {
"symfony/framework-bundle": "<6.3",
"symfony/twig-bundle": "<6.3",
"symfony/flex": "<1.20.0 || >=2.0.0,<2.3.0"
}
}
+2
View File
@@ -0,0 +1,2 @@
// register any custom, 3rd party controllers here
// app.register('some_controller_name', SomeImportedController);
@@ -0,0 +1,4 @@
{
"controllers": [],
"entrypoints": []
}
@@ -0,0 +1,16 @@
import { Controller } from '@hotwired/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';
}
}
+46
View File
@@ -0,0 +1,46 @@
{
"bundles": {
"Symfony\\UX\\StimulusBundle\\StimulusBundle": ["all"]
},
"copy-from-recipe": {
"assets/": "assets/"
},
"aliases": ["stimulus", "stimulus-bundle"],
"conflict": {
"symfony/webpack-encore-bundle": "<2.0",
"symfony/flex": "<1.20.0 || >=2.0.0,<2.3.0"
},
"add-lines": [
{
"file": "webpack.config.js",
"content": "\n // enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)\n .enableStimulusBridge('./assets/controllers.json')",
"position": "after_target",
"target": ".splitEntryChunks()"
},
{
"file": "assets/app.js",
"content": "import './bootstrap.js';",
"position": "top",
"warn_if_missing": true
},
{
"file": "assets/bootstrap.js",
"content": "import { startStimulusApp } from '@symfony/stimulus-bridge';\n\n// Registers Stimulus controllers from controllers.json and in the controllers/ directory\nexport const app = startStimulusApp(require.context(\n '@symfony/stimulus-bridge/lazy-controller-loader!./controllers',\n true,\n /\\.[jt]sx?$/\n));",
"position": "top",
"requires": "symfony/webpack-encore-bundle"
},
{
"file": "assets/bootstrap.js",
"content": "import { startStimulusApp } from '@symfony/stimulus-bundle';\n\nconst app = startStimulusApp();",
"position": "top",
"requires": "symfony/asset-mapper"
},
{
"file": "templates/base.html.twig",
"content": " {{ ux_controller_link_tags() }}",
"position": "after_target",
"target": "{% block stylesheets %}",
"warn_if_missing": true
}
]
}
@@ -0,0 +1,6 @@
twig:
default_path: '%kernel.project_dir%/templates'
when@test:
twig:
strict_variables: true
+12
View File
@@ -0,0 +1,12 @@
{
"bundles": {
"Symfony\\Bundle\\TwigBundle\\TwigBundle": ["all"]
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"templates/": "templates/"
},
"conflict": {
"symfony/framework-bundle": "<5.3"
}
}
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text></svg>">
{% block stylesheets %}
{% endblock %}
{% block javascripts %}
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
@@ -0,0 +1,13 @@
import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';
/*
* This file is part of the Symfony UX Translator package.
*
* If folder "../var/translations" does not exist, or some translations are missing,
* you must warmup your Symfony cache to refresh JavaScript translations.
*
* If you use TypeScript, you can rename this file to "translator.ts" to take advantage of types checking.
*/
setLocaleFallbacks(localeFallbacks);
export { trans };
@@ -0,0 +1,3 @@
ux_translator:
# The directory where the JavaScript translations are dumped
dump_directory: '%kernel.project_dir%/var/translations'
+39
View File
@@ -0,0 +1,39 @@
{
"bundles": {
"Symfony\\UX\\Translator\\UxTranslatorBundle": ["all"]
},
"conflict": {
"symfony/flex": "<1.20.0 || >=2.0.0,<2.3.0"
},
"copy-from-recipe": {
"assets/": "assets/",
"config/": "%CONFIG_DIR%/",
"var/": "var/"
},
"add-lines": [
{
"file": "assets/translator.js",
"content": "import { localeFallbacks } from '../var/translations/configuration';",
"position": "top",
"requires": "symfony/webpack-encore-bundle"
},
{
"file": "assets/translator.js",
"content": "import { localeFallbacks } from '@app/translations/configuration';",
"position": "top",
"requires": "symfony/asset-mapper"
},
{
"file": "assets/translator.js",
"content": "export * from '../var/translations';",
"position": "bottom",
"requires": "symfony/webpack-encore-bundle"
},
{
"file": "assets/translator.js",
"content": "export * from '@app/translations';",
"position": "bottom",
"requires": "symfony/asset-mapper"
}
]
}
@@ -0,0 +1,3 @@
export const localeFallbacks = {};
console.log('Run bin/console cache:warmup to generate the translation files.');
@@ -0,0 +1 @@
console.log('Run bin/console cache:warmup to generate the translation files.');
@@ -8,7 +8,7 @@
"package.json": "package.json",
"webpack.config.js": "webpack.config.js"
},
"aliases": ["ux", "encore", "webpack", "webpack-encore"],
"aliases": ["encore", "webpack", "webpack-encore"],
"gitignore": [
"/node_modules/",
"/%PUBLIC_DIR%/build/",
@@ -0,0 +1,9 @@
/*
* 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';
@@ -0,0 +1,3 @@
body {
background-color: lightgray;
}
@@ -0,0 +1,45 @@
webpack_encore:
# 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
# Set attributes that will be rendered on all script and link tags
script_attributes:
defer: true
# Uncomment (also under link_attributes) if using Turbo Drive
# https://turbo.hotwired.dev/handbook/drive#reloading-when-assets-change
# 'data-turbo-track': reload
# link_attributes:
# Uncomment if using Turbo Drive
# 'data-turbo-track': reload
# If using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
# crossorigin: 'anonymous'
# 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:
# frontend: '%kernel.project_dir%/public/frontend/build'
# pass the build name as the 3rd argument to the Twig functions
# {{ encore_entry_script_tags('entry1', null, 'frontend') }}
framework:
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
#when@prod:
# webpack_encore:
# # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# # Available in version 1.2
# cache: true
#when@test:
# webpack_encore:
# strict_mode: false
@@ -0,0 +1,38 @@
{
"bundles": {
"Symfony\\WebpackEncoreBundle\\WebpackEncoreBundle": ["all"]
},
"copy-from-recipe": {
"assets/": "assets/",
"config/": "%CONFIG_DIR%/",
"package.json": "package.json",
"webpack.config.js": "webpack.config.js"
},
"aliases": ["ux", "encore", "webpack", "webpack-encore"],
"gitignore": [
"/node_modules/",
"/%PUBLIC_DIR%/build/",
"npm-debug.log",
"yarn-error.log"
],
"conflict": {
"symfony/framework-bundle": "<5.4",
"symfony/flex": "<1.20.0 || >=2.0.0,<2.3.0"
},
"add-lines": [
{
"file": "templates/base.html.twig",
"content": " {{ encore_entry_script_tags('app') }}",
"position": "after_target",
"target": "{% block javascripts %}",
"warn_if_missing": true
},
{
"file": "templates/base.html.twig",
"content": " {{ encore_entry_link_tags('app') }}",
"position": "after_target",
"target": "{% block stylesheets %}",
"warn_if_missing": true
}
]
}
@@ -0,0 +1,20 @@
{
"devDependencies": {
"@babel/core": "^7.17.0",
"@babel/preset-env": "^7.16.0",
"@symfony/webpack-encore": "^4.0.0",
"core-js": "^3.23.0",
"regenerator-runtime": "^0.13.9",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-notifier": "^1.15.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
}
}
@@ -0,0 +1,3 @@
* Install Yarn and run <fg=green>yarn install</>
* Start the development server: <fg=green>yarn encore dev-server</>
@@ -0,0 +1,73 @@
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.
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 subdirectory deploy
//.setManifestKeyPrefix('build/')
/*
* ENTRY CONFIG
*
* 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/app.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())
// configure Babel
// .configureBabel((config) => {
// config.plugins.push('@babel/a-babel-plugin');
// })
// enables and configure @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = '3.23';
})
// enables Sass/SCSS support
//.enableSassLoader()
// 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()
;
module.exports = Encore.getWebpackConfig();