Skip to content

Commit e59691a

Browse files
committed
feature #887 Adding Asset Mapper support + new StimulusBundle (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Adding Asset Mapper support + new StimulusBundle | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | None | License | MIT Adds: * A) Integration with new `symfony.importmap` key introduced to Flex in symfony/flex#975 * B) An "asset mapper" path in each bundle so that you can refer to the controller/files in that bundle. * C) Introduces new StimulusBundle (thanks to `@jmsche` for helping port from WebpackEncoreBundle). This includes the Twig `stimulus_*` functions + a revamped "helper" system for generating those attributes. * D) The new StimulusBundle also contains integration for AssetMapper to load custom controllers and `controllers.json`. Cheers! Commits ------- b8acf1a Adding Asset Mapper support + new StimulusBundle
2 parents ce1bcfb + b8acf1a commit e59691a

File tree

160 files changed

+3718
-882
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+3718
-882
lines changed

.github/build-packages.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
<?php
22

3-
$dir = __DIR__.'/../src/LiveComponent';
4-
$flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
3+
/**
4+
* Updates the composer.json files to use the local version of the Symfony UX packages.
5+
*/
56

6-
$json = ltrim(file_get_contents($dir.'/composer.json'));
7-
if (null === $package = json_decode($json)) {
8-
passthru("composer validate $dir/composer.json");
9-
exit(1);
10-
}
7+
require __DIR__.'/../vendor/autoload.php';
8+
9+
use Symfony\Component\Finder\Finder;
10+
11+
$finder = (new Finder())
12+
->in(__DIR__.'/../src/*/')
13+
->depth(0)
14+
->name('composer.json')
15+
;
1116

12-
$package->repositories[] = [
13-
'type' => 'path',
14-
'url' => '../TwigComponent',
15-
];
17+
foreach ($finder as $composerFile) {
18+
$json = file_get_contents($composerFile->getPathname());
19+
if (null === $packageData = json_decode($json, true)) {
20+
passthru(sprintf('composer validate %s', $composerFile->getPathname()));
21+
exit(1);
22+
}
1623

17-
$json = preg_replace('/\n "repositories": \[\n.*?\n \],/s', '', $json);
18-
$json = rtrim(json_encode(['repositories' => $package->repositories], $flags), "\n}").','.substr($json, 1);
19-
$json = preg_replace('/"symfony\/ux-twig-component": "(\^[\d]+\.[\d]+)"/s', '"symfony/ux-twig-component": "@dev"', $json);
20-
file_put_contents($dir.'/composer.json', $json);
24+
$repositories = [];
2125

26+
if (isset($packageData['require']['symfony/ux-twig-component'])
27+
|| isset($packageData['require-dev']['symfony/ux-twig-component'])
28+
) {
29+
$repositories[] = [
30+
'type' => 'path',
31+
'url' => '../TwigComponent',
32+
];
33+
$key = isset($packageData['require']['symfony/ux-twig-component']) ? 'require' : 'require-dev';
34+
$packageData[$key]['symfony/ux-twig-component'] = '@dev';
35+
}
36+
37+
if (isset($packageData['require']['symfony/stimulus-bundle'])
38+
|| isset($packageData['require-dev']['symfony/stimulus-bundle'])
39+
) {
40+
$repositories[] = [
41+
'type' => 'path',
42+
'url' => '../StimulusBundle',
43+
];
44+
$key = isset($packageData['require']['symfony/stimulus-bundle']) ? 'require' : 'require-dev';
45+
$packageData[$key]['symfony/stimulus-bundle'] = '@dev';
46+
}
47+
if ($repositories) {
48+
$packageData['repositories'] = $repositories;
49+
}
50+
51+
$json = json_encode($packageData, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
52+
file_put_contents($composerFile->getPathname(), $json."\n");
53+
}

.github/workflows/test-turbo.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Setup PHP
1515
uses: shivammathur/setup-php@v2
1616
with:
17-
php-version: '8.0'
17+
php-version: '8.1'
1818
extensions: zip
1919

2020
- uses: ramsey/composer-install@v2
@@ -33,7 +33,7 @@ jobs:
3333
runs-on: ubuntu-latest
3434
strategy:
3535
matrix:
36-
php-versions: ['7.3', '7.4', '8.0']
36+
php-versions: ['8.1']
3737
fail-fast: false
3838
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
3939

@@ -96,7 +96,7 @@ jobs:
9696

9797
tests-php-low-deps:
9898
runs-on: ubuntu-latest
99-
name: PHP 8.0 (lowest) Test on ubuntu-latest
99+
name: PHP 8.1 (lowest) Test on ubuntu-latest
100100

101101
services:
102102
mercure:
@@ -118,7 +118,7 @@ jobs:
118118
- name: Setup PHP
119119
uses: shivammathur/setup-php@v2
120120
with:
121-
php-version: '8.0'
121+
php-version: '8.1'
122122
extensions: zip, pdo_sqlite
123123

124124
- uses: ramsey/composer-install@v2

.github/workflows/test.yaml

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ jobs:
7575
with:
7676
php-version: '7.2'
7777

78-
- name: Chartjs Dependencies
79-
uses: ramsey/composer-install@v2
80-
with:
81-
working-directory: src/Chartjs
82-
dependency-versions: lowest
83-
- name: Chartjs Tests
84-
run: php vendor/bin/simple-phpunit
85-
working-directory: src/Chartjs
86-
8778
- name: Cropperjs Dependencies
8879
uses: ramsey/composer-install@v2
8980
with:
@@ -111,24 +102,6 @@ jobs:
111102
run: php vendor/bin/simple-phpunit
112103
working-directory: src/LazyImage
113104

114-
- name: React Dependencies
115-
uses: ramsey/composer-install@v2
116-
with:
117-
working-directory: src/React
118-
dependency-versions: lowest
119-
- name: React Tests
120-
run: php vendor/bin/simple-phpunit
121-
working-directory: src/React
122-
123-
- name: Svelte Dependencies
124-
uses: ramsey/composer-install@v2
125-
with:
126-
working-directory: src/Svelte
127-
dependency-versions: lowest
128-
- name: Svelte Tests
129-
run: php vendor/bin/simple-phpunit
130-
working-directory: src/Svelte
131-
132105
tests-php8-low-deps:
133106
runs-on: ubuntu-latest
134107
steps:
@@ -142,6 +115,8 @@ jobs:
142115
with:
143116
working-directory: src/TwigComponent
144117
dependency-versions: lowest
118+
# needed for php 8.0 to skip symfony/stimulus-bundle
119+
composer-options: "--ignore-platform-reqs"
145120
- name: TwigComponent Tests
146121
run: php vendor/bin/simple-phpunit
147122
working-directory: src/TwigComponent
@@ -173,22 +148,28 @@ jobs:
173148
run: php vendor/bin/simple-phpunit
174149
working-directory: src/Translator
175150

151+
- name: StimulusBundle Dependencies
152+
uses: ramsey/composer-install@v2
153+
with:
154+
working-directory: src/StimulusBundle
155+
dependency-versions: lowest
156+
- name: StimulusBundle Tests
157+
working-directory: src/StimulusBundle
158+
run: php vendor/bin/simple-phpunit
159+
176160
tests-php-high-deps:
177161
runs-on: ubuntu-latest
178162
steps:
179163
- uses: actions/checkout@master
164+
180165
- uses: shivammathur/setup-php@v2
181166
with:
182167
php-version: '8.0'
183-
- run: php .github/build-packages.php
184168

185-
- name: Chartjs Dependencies
169+
- name: Install root Dependencies
186170
uses: ramsey/composer-install@v2
187-
with:
188-
working-directory: src/Chartjs
189-
- name: Chartjs Tests
190-
run: php vendor/bin/simple-phpunit
191-
working-directory: src/Chartjs
171+
172+
- run: php .github/build-packages.php
192173

193174
- name: Cropperjs Dependencies
194175
uses: ramsey/composer-install@v2
@@ -214,14 +195,6 @@ jobs:
214195
run: php vendor/bin/simple-phpunit
215196
working-directory: src/LazyImage
216197

217-
- name: TwigComponent Dependencies
218-
uses: ramsey/composer-install@v2
219-
with:
220-
working-directory: src/TwigComponent
221-
- name: TwigComponent Tests
222-
run: php vendor/bin/simple-phpunit
223-
working-directory: src/TwigComponent
224-
225198
- name: LiveComponent Dependencies
226199
uses: ramsey/composer-install@v2
227200
with:
@@ -230,14 +203,6 @@ jobs:
230203
working-directory: src/LiveComponent
231204
run: php vendor/bin/simple-phpunit
232205

233-
- name: React Dependencies
234-
uses: ramsey/composer-install@v2
235-
with:
236-
working-directory: src/React
237-
- name: React Tests
238-
working-directory: src/React
239-
run: php vendor/bin/simple-phpunit
240-
241206
- name: Autocomplete Dependencies
242207
uses: ramsey/composer-install@v2
243208
with:
@@ -246,23 +211,28 @@ jobs:
246211
working-directory: src/Autocomplete
247212
run: php vendor/bin/simple-phpunit
248213

249-
- name: Svelte Dependencies
250-
uses: ramsey/composer-install@v2
251-
with:
252-
working-directory: src/Svelte
253-
- name: Svelte Tests
254-
working-directory: src/Svelte
255-
run: php vendor/bin/simple-phpunit
256-
257214
tests-php81-high-deps:
258215
runs-on: ubuntu-latest
259216
steps:
260217
- uses: actions/checkout@master
218+
261219
- uses: shivammathur/setup-php@v2
262220
with:
263221
php-version: '8.1'
222+
223+
- name: Install root Dependencies
224+
uses: ramsey/composer-install@v2
225+
264226
- run: php .github/build-packages.php
265227

228+
- name: Chartjs Dependencies
229+
uses: ramsey/composer-install@v2
230+
with:
231+
working-directory: src/Chartjs
232+
- name: Chartjs Tests
233+
working-directory: src/Chartjs
234+
run: php vendor/bin/simple-phpunit
235+
266236
- name: Notify Dependencies
267237
uses: ramsey/composer-install@v2
268238
with:
@@ -271,6 +241,32 @@ jobs:
271241
working-directory: src/Notify
272242
run: php vendor/bin/simple-phpunit
273243

244+
- name: React Dependencies
245+
uses: ramsey/composer-install@v2
246+
with:
247+
working-directory: src/React
248+
dependency-versions: lowest
249+
- name: React Tests
250+
run: php vendor/bin/simple-phpunit
251+
working-directory: src/React
252+
253+
- name: StimulusBundle Dependencies
254+
uses: ramsey/composer-install@v2
255+
with:
256+
working-directory: src/StimulusBundle
257+
- name: StimulusBundle Tests
258+
working-directory: src/StimulusBundle
259+
run: php vendor/bin/simple-phpunit
260+
261+
- name: Svelte Dependencies
262+
uses: ramsey/composer-install@v2
263+
with:
264+
working-directory: src/Svelte
265+
dependency-versions: lowest
266+
- name: Svelte Tests
267+
run: php vendor/bin/simple-phpunit
268+
working-directory: src/Svelte
269+
274270
- name: Translator Dependencies
275271
uses: ramsey/composer-install@v2
276272
with:
@@ -279,6 +275,23 @@ jobs:
279275
working-directory: src/Translator
280276
run: php vendor/bin/simple-phpunit
281277

278+
- name: TwigComponent Dependencies
279+
uses: ramsey/composer-install@v2
280+
with:
281+
working-directory: src/TwigComponent
282+
- name: TwigComponent Tests
283+
run: php vendor/bin/simple-phpunit
284+
working-directory: src/TwigComponent
285+
286+
- name: Vue Dependencies
287+
uses: ramsey/composer-install@v2
288+
with:
289+
working-directory: src/Vue
290+
dependency-versions: lowest
291+
- name: Vue Tests
292+
run: php vendor/bin/simple-phpunit
293+
working-directory: src/Vue
294+
282295
tests-js:
283296
runs-on: ubuntu-latest
284297
steps:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ to build the chart in PHP. The JavaScript is handled for you automatically.
1515
**That's Symfony UX.**
1616

1717
Symfony UX leverages [Stimulus](https://stimulus.hotwired.dev/) for JavaScript
18-
and the [Stimulus Bridge](https://github.com/symfony/stimulus-bridge) for
19-
integrating it into [Webpack Encore](https://github.com/symfony/webpack-encore).
18+
and can integrate with [Webpack Encore](https://github.com/symfony/webpack-encore)
19+
(with the help of [Stimulus Bridge](https://github.com/symfony/stimulus-bridge))
20+
or with [AssetMapper](https://symfony.com/doc/current/frontend/asset-mapper.html)
2021

2122
## Resources
2223

build.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* This file is used to compile the TypeScript files in the assets/src directory
3+
* of each package.
4+
*
5+
* It allows each package to spawn its own rollup process, which is necessary
6+
* to keep memory usage down.
7+
*/
8+
const { spawnSync } = require('child_process');
9+
const glob = require('glob');
10+
11+
const files = [
12+
// custom handling for StimulusBundle
13+
'src/StimulusBundle/assets/src/loader.ts',
14+
'src/StimulusBundle/assets/src/controllers.ts',
15+
...glob.sync('src/*/assets/src/*controller.ts'),
16+
];
17+
18+
files.forEach((file) => {
19+
const result = spawnSync('node', [
20+
'node_modules/.bin/rollup',
21+
'-c',
22+
'--environment',
23+
`INPUT_FILE:${file}`,
24+
], {
25+
stdio: 'inherit',
26+
shell: true
27+
});
28+
29+
if (result.error) {
30+
console.error(`Error compiling ${file}:`, result.error);
31+
}
32+
});

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"license": "MIT",
44
"require-dev": {
55
"symfony/filesystem": "^5.2|^6.0",
6+
"symfony/finder": "^5.4|^6.0",
67
"php-cs-fixer/shim": "^3.13"
78
}
89
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"src/*/assets"
55
],
66
"scripts": {
7-
"build": "yarn rollup -c",
7+
"build": "node build.js",
88
"test": "yarn workspaces run jest",
99
"lint": "yarn workspaces run eslint src test",
1010
"format": "prettier src/*/assets/src/*.ts src/*/assets/test/*.js {,src/*/}*.{json,md} --write",

0 commit comments

Comments
 (0)