Skip to content

Commit 5640760

Browse files
committed
updating stimulus-bridge plugin to work with proposed new loader
1 parent b1af95b commit 5640760

File tree

12 files changed

+60
-36
lines changed

12 files changed

+60
-36
lines changed

fixtures/stimulus/assets/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
import { startStimulusApp } from '@symfony/stimulus-bridge';
2-
import '@symfony/autoimport';
32

43
export const app = startStimulusApp(require.context('./controllers', true, /\.(j|t)sx?$/));

fixtures/stimulus/autoimport/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

fixtures/stimulus/autoimport/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

fixtures/stimulus/controllers/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

fixtures/stimulus/controllers/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/WebpackConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class WebpackConfig {
131131
resolveUrlLoader: true,
132132
resolveUrlLoaderOptions: {}
133133
};
134+
/** @deprecated */
134135
this.stimulusOptions = {
135136
controllerJsonPath: null,
136137
};
@@ -686,6 +687,9 @@ class WebpackConfig {
686687
}
687688

688689
this.stimulusOptions.controllersJsonPath = controllerJsonPath;
690+
this.addAliases({
691+
'@symfony/stimulus-bridge/controllers.json': path.resolve(controllerJsonPath),
692+
});
689693
}
690694

691695
enableBuildCache(buildDependencies, callback = (cache) => {}) {

lib/features.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ const features = {
151151
stimulus: {
152152
method: 'enableStimulusBridge()',
153153
packages: [
154-
{ name: '@symfony/stimulus-bridge' },
154+
{ name: '@symfony/stimulus-bridge', enforce_version: true },
155155
{ name: 'stimulus' }
156156
],
157157
description: 'enable Stimulus bridge'

lib/plugins/stimulus-bridge.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@
1212
const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
1313
const loaderFeatures = require('../features');
1414
const fs = require('fs');
15+
const packageHelper = require('../package-helper');
16+
const semver = require('semver');
1517

1618
/**
19+
* Support for @symfony/stimulus-bridge 1.1 or lower.
20+
*
1721
* @param {Array} plugins
1822
* @param {WebpackConfig} webpackConfig
23+
* @deprecated
1924
* @return {void}
2025
*/
2126
module.exports = function(plugins, webpackConfig) {
2227
if (webpackConfig.useStimulusBridge) {
2328
loaderFeatures.ensurePackagesExistAndAreCorrectVersion('stimulus');
2429

30+
const version = packageHelper.getPackageVersion('@symfony/stimulus-bridge');
31+
if (semver.satisfies(version, '^1.2.0')) {
32+
// package is new and doesn't require this plugin
33+
34+
return;
35+
}
36+
2537
const createPlugin = require('@symfony/stimulus-bridge/webpack-helper'); // eslint-disable-line node/no-unpublished-require
2638

2739
plugins.push({

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@
5858
"@babel/plugin-transform-react-jsx": "^7.0.0",
5959
"@babel/preset-react": "^7.0.0",
6060
"@babel/preset-typescript": "^7.0.0",
61-
"@symfony/autoimport": "file:fixtures/stimulus/autoimport",
62-
"@symfony/controllers": "file:fixtures/stimulus/controllers",
6361
"@symfony/mock-module": "file:fixtures/stimulus/mock-module",
64-
"@symfony/stimulus-bridge": "^1.0.0",
62+
"@symfony/stimulus-bridge": "^1.1.0",
6563
"@vue/babel-helper-vue-jsx-merge-props": "^1.0.0",
6664
"@vue/babel-preset-jsx": "^1.0.0",
6765
"@vue/compiler-sfc": "^3.0.2",

test/functional.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const path = require('path');
1818
const testSetup = require('./helpers/setup');
1919
const fs = require('fs-extra');
2020
const getVueVersion = require('../lib/utils/get-vue-version');
21+
const packageHelper = require('../lib/package-helper');
22+
const semver = require('semver');
2123

2224
function createWebpackConfig(outputDirName = '', command, argv = {}) {
2325
const webpackConfig = testSetup.createWebpackConfig(
@@ -427,15 +429,15 @@ describe('Functional tests using webpack', function() {
427429
]);
428430

429431
webpackAssert.assertOutputFileContains(
430-
'styles.79943add.css',
432+
'styles.[hash:8].css',
431433
'font-size: 50px;'
432434
);
433435
webpackAssert.assertManifestPathDoesNotExist(
434436
'styles.js'
435437
);
436438
webpackAssert.assertManifestPath(
437439
'styles.css',
438-
'/styles.79943add.css'
440+
'/styles.[hash:8].css'
439441
);
440442

441443
done();
@@ -466,7 +468,7 @@ describe('Functional tests using webpack', function() {
466468
);
467469
webpackAssert.assertManifestPath(
468470
'styles.css',
469-
'/styles.css?79943addbc894efe'
471+
'/styles.css?[hash:16]'
470472
);
471473

472474
done();
@@ -535,7 +537,7 @@ describe('Functional tests using webpack', function() {
535537
]);
536538

537539
webpackAssert.assertOutputFileContains(
538-
'bg.2eff0999.css',
540+
'bg.[hash:8].css',
539541
'/build/images/symfony_logo.91beba37.png'
540542
);
541543

@@ -1913,9 +1915,17 @@ module.exports = {
19131915
});
19141916
});
19151917

1916-
it('Symfony - Stimulus standard app is built correctly', (done) => {
1918+
it('Symfony - Stimulus standard app is built correctly', function(done) {
19171919
const appDir = testSetup.createTestAppDir();
19181920

1921+
const version = packageHelper.getPackageVersion('@symfony/stimulus-bridge');
1922+
if (!semver.satisfies(version, '^1.2.0')) {
1923+
// we support the old version, but it's not tested
1924+
this.skip();
1925+
1926+
return;
1927+
}
1928+
19191929
const config = testSetup.createWebpackConfig(appDir, 'www/build', 'dev');
19201930
config.enableSingleRuntimeChunk();
19211931
config.setPublicPath('/build');
@@ -1930,13 +1940,14 @@ module.exports = {
19301940
'main.js',
19311941
'main.css',
19321942
'manifest.json',
1943+
'node_modules_symfony_mock-module_dist_controller_js.js',
19331944
'entrypoints.json',
19341945
'runtime.js',
19351946
]);
19361947

19371948
// test controllers and style are shipped
19381949
webpackAssert.assertOutputFileContains('main.js', 'app-controller');
1939-
webpackAssert.assertOutputFileContains('main.js', 'mock-module-controller');
1950+
webpackAssert.assertOutputFileContains('node_modules_symfony_mock-module_dist_controller_js.js', 'mock-module-controller');
19401951
webpackAssert.assertOutputFileContains('main.css', 'body {}');
19411952

19421953
done();

test/helpers/assert.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ const readOutputFile = function(webpackConfig, filePath) {
3131
return fs.readFileSync(fullPath, 'utf8');
3232
};
3333

34+
const getMatchedFilename = function(targetDirectory, filenameRegex) {
35+
const actualFiles = fs.readdirSync(targetDirectory);
36+
let foundFile = false;
37+
actualFiles.forEach((actualFile) => {
38+
// filter out directories
39+
if (fs.statSync(path.join(targetDirectory, actualFile)).isDirectory()) {
40+
return;
41+
}
42+
43+
if (actualFile.match(filenameRegex)) {
44+
foundFile = actualFile;
45+
}
46+
});
47+
48+
return foundFile;
49+
};
50+
3451
/**
3552
* Returns a regex to use to match this filename
3653
*
@@ -61,12 +78,17 @@ class Assert {
6178
}
6279

6380
assertOutputFileContains(filePath, expectedContents) {
64-
const fullPath = path.join(this.webpackConfig.outputPath, filePath);
81+
const actualFilename = getMatchedFilename(
82+
this.webpackConfig.outputPath,
83+
convertFilenameToMatcher(filePath)
84+
);
6585

66-
if (!fs.existsSync(fullPath)) {
86+
if (false === actualFilename) {
6787
throw new Error(`Output file "${filePath}" does not exist.`);
6888
}
6989

90+
const fullPath = path.join(this.webpackConfig.outputPath, actualFilename);
91+
7092
const actualContents = fs.readFileSync(fullPath, 'utf8');
7193
if (!actualContents.includes(expectedContents)) {
7294
throw new Error(`Expected contents "${expectedContents}" not found in file ${fullPath}`);

yarn.lock

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -970,16 +970,10 @@
970970
resolved "https://registry.yarnpkg.com/@stimulus/webpack-helpers/-/webpack-helpers-2.0.0.tgz#54296d2a2dffd4f962d2e802d99a3fdd84b8845f"
971971
integrity sha512-D6tJWsAC024MwGEIKlUVYU8Ln87mlrmiwHvYAjipg+s8H4eLxUMQ3PZkWyPevfipH+oR3leuHsjYsK1gN5ViQA==
972972

973-
"@symfony/autoimport@file:fixtures/stimulus/autoimport":
974-
version "1.0.0"
975-
976-
"@symfony/controllers@file:fixtures/stimulus/controllers":
977-
version "1.0.0"
978-
979973
"@symfony/mock-module@file:fixtures/stimulus/mock-module":
980974
version "1.0.0"
981975

982-
"@symfony/stimulus-bridge@^1.0.0":
976+
"@symfony/stimulus-bridge@^1.1.0":
983977
version "1.1.0"
984978
resolved "https://registry.yarnpkg.com/@symfony/stimulus-bridge/-/stimulus-bridge-1.1.0.tgz#34548c633fe49bcda84cc812939f63640e698af7"
985979
integrity sha512-Sz8iwKQHBjrgCnxCGwH87S/J/Min0gX3EuXJaHi4vpibY3BjVm+WYuP8+OYPkMIROPyNZKYgTffFfJLBAFLwFQ==

0 commit comments

Comments
 (0)