Skip to content

Commit 1007648

Browse files
committed
Changing to "fetch" option from webpackMode
1 parent e3b9161 commit 1007648

14 files changed

+112
-37
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
Before: `@symfony/ux-dropzone/dropzone`
1212
After: `symfony--ux-dropzone--dropzone`
1313

14-
* Support for "lazy controllers" was added. By setting the `webpackMode`
15-
in `controllers.json` to `lazy-controller`, your controller will not
14+
* Support for "lazy controllers" was added. By setting the `fetch`
15+
in `controllers.json` to `lazy`, your controller will not
1616
be downloaded until the controller element first appears on the page.
1717

18+
* The `webpackMode` option in `controllers.json` was deprecated. Use
19+
the new `fetch` option instead.
20+
1821
## 1.1.0
1922

2023
* Support for Stimulus 1 dropped and support for Stimulus 2 added - #4.

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ look like this:
7474
"@symfony/ux-dropzone": {
7575
"dropzone": {
7676
"enabled": true,
77-
"webpackMode": "eager",
77+
"fetch": "eager",
7878
"autoimport": {
7979
"@symfony/ux-dropzone/src/style.css": true
8080
}
@@ -90,14 +90,12 @@ registered with a specific name - in this case the controller would
9090
be called `symfony--ux-dropzone--dropzone` (the `/` becomes `--`).
9191

9292
By default, the new controller will always be included in your
93-
JavaScript package. You can control that with the `webpackMode` option,
93+
JavaScript package. You can control that with the `fetch` option,
9494
ordered from least to most lazy:
9595

96-
* `webpackMode: 'eager'`: controller & dependencies are included in the JavaScript
96+
* `fetch: 'eager'`: controller & dependencies are included in the JavaScript
9797
that's downloaded when the page is loaded.
98-
* `webpackMode: 'lazy'`: controller & dependencies are isolated into a separate
99-
JavaScript file, but immediately downloaded asynchronously after the page is loaded.
100-
* `webpackMode: 'lazy-controller'`: controller & dependencies are isolated into a
98+
* `fetch: 'lazy'`: controller & dependencies are isolated into a
10199
separate file and only downloaded asynchronously if (and when) the `data-controller`
102100
HTML appears on the page.
103101

dist/webpack/create-controllers-module.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function createControllersModule(config) {
1414
var controllerContents = 'export default {';
1515
var autoImportContents = '';
1616
var hasLazyControllers = false;
17+
var deprecations = [];
1718

1819
if ('undefined' !== typeof config['placeholder']) {
1920
throw new Error('Your controllers.json file was not found. Be sure to add a Webpack alias from "@symfony/stimulus-bridge/controllers.json" to *your* controllers.json file.');
@@ -43,10 +44,23 @@ module.exports = function createControllersModule(config) {
4344
}
4445

4546
var controllerMain = packageName + '/' + controllerPackageConfig.main;
46-
var webpackMode = controllerUserConfig.webpackMode;
47-
var moduleValueContents = "import(/* webpackMode: \"".concat(webpackMode, "\" */ '").concat(controllerMain, "')");
47+
var fetchMode = 'eager';
4848

49-
if (webpackMode === 'lazy-controller') {
49+
if ('undefined' !== typeof controllerUserConfig.webpackMode) {
50+
deprecations.push('The "webpackMode" config key is deprecated in controllers.json. Use "fetch" instead, set to either "eager" or "lazy".');
51+
}
52+
53+
if ('undefined' !== typeof controllerUserConfig.fetch) {
54+
if (!['eager', 'lazy'].includes(controllerUserConfig.fetch)) {
55+
throw new Error("Invalid \"fetch\" value \"".concat(controllerUserConfig.fetch, "\" in controllers.json. Expected \"eager\" or \"lazy\"."));
56+
}
57+
58+
fetchMode = controllerUserConfig.fetch;
59+
}
60+
61+
var moduleValueContents = "import(/* webpackMode: \"eager\" */ '".concat(controllerMain, "')");
62+
63+
if (fetchMode === 'lazy') {
5064
hasLazyControllers = true;
5165
moduleValueContents = "\nnew Promise((resolve, reject) => resolve({ default:\n".concat(generateLazyController(controllerMain, 6), "\n }))\n ").trim();
5266
}
@@ -65,5 +79,8 @@ module.exports = function createControllersModule(config) {
6579
controllerContents = "import { Controller } from 'stimulus';\n".concat(controllerContents);
6680
}
6781

68-
return "".concat(autoImportContents).concat(controllerContents, "\n};");
82+
return {
83+
finalSource: "".concat(autoImportContents).concat(controllerContents, "\n};"),
84+
deprecations: deprecations
85+
};
6986
};

dist/webpack/loader.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var createControllersModule = require('./create-controllers-module');
2525

2626

2727
module.exports = function (source) {
28+
var _this = this;
29+
2830
var logger = this.getLogger('stimulus-bridge-loader');
2931
/*
3032
* The following code prevents the normal JSON loader from
@@ -44,5 +46,12 @@ module.exports = function (source) {
4446
this._module.parser = factory.getParser(requiredType);
4547
/* End workaround */
4648

47-
return createControllersModule(JSON.parse(source));
49+
var _createControllersMod = createControllersModule(JSON.parse(source)),
50+
finalSource = _createControllersMod.finalSource,
51+
deprecations = _createControllersMod.deprecations;
52+
53+
deprecations.forEach(function (message) {
54+
_this.emitWarning(new Error(message));
55+
});
56+
return finalSource;
4857
};

src/webpack/create-controllers-module.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function createControllersModule(config) {
1515
let controllerContents = 'export default {';
1616
let autoImportContents = '';
1717
let hasLazyControllers = false;
18+
const deprecations = [];
1819

1920
if ('undefined' !== typeof config['placeholder']) {
2021
throw new Error(
@@ -49,10 +50,26 @@ module.exports = function createControllersModule(config) {
4950
}
5051

5152
const controllerMain = packageName + '/' + controllerPackageConfig.main;
52-
const webpackMode = controllerUserConfig.webpackMode;
53+
let fetchMode = 'eager';
5354

54-
let moduleValueContents = `import(/* webpackMode: "${webpackMode}" */ '${controllerMain}')`;
55-
if (webpackMode === 'lazy-controller') {
55+
if ('undefined' !== typeof controllerUserConfig.webpackMode) {
56+
deprecations.push(
57+
'The "webpackMode" config key is deprecated in controllers.json. Use "fetch" instead, set to either "eager" or "lazy".'
58+
);
59+
}
60+
61+
if ('undefined' !== typeof controllerUserConfig.fetch) {
62+
if (!['eager', 'lazy'].includes(controllerUserConfig.fetch)) {
63+
throw new Error(
64+
`Invalid "fetch" value "${controllerUserConfig.fetch}" in controllers.json. Expected "eager" or "lazy".`
65+
);
66+
}
67+
68+
fetchMode = controllerUserConfig.fetch;
69+
}
70+
71+
let moduleValueContents = `import(/* webpackMode: "eager" */ '${controllerMain}')`;
72+
if (fetchMode === 'lazy') {
5673
hasLazyControllers = true;
5774
moduleValueContents = `
5875
new Promise((resolve, reject) => resolve({ default:
@@ -75,5 +92,8 @@ ${generateLazyController(controllerMain, 6)}
7592
controllerContents = `import { Controller } from 'stimulus';\n${controllerContents}`;
7693
}
7794

78-
return `${autoImportContents}${controllerContents}\n};`;
95+
return {
96+
finalSource: `${autoImportContents}${controllerContents}\n};`,
97+
deprecations,
98+
};
7999
};

src/webpack/loader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ module.exports = function (source) {
4040
this._module.parser = factory.getParser(requiredType);
4141
/* End workaround */
4242

43-
return createControllersModule(JSON.parse(source));
43+
const { finalSource, deprecations } = createControllersModule(JSON.parse(source));
44+
45+
deprecations.forEach((message) => {
46+
this.emitWarning(new Error(message));
47+
});
48+
49+
return finalSource;
4450
};

test/controllers.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "eager",
5+
"fetch": "eager",
66
"enabled": true
77
}
88
}

test/fixtures/lazy-controller-no-autoimport.json renamed to test/fixtures/deprecated-webpack-mode.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "lazy-controller",
5+
"webpackMode": "eager",
66
"enabled": true
77
}
88
}

test/fixtures/disabled-autoimport.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "lazy",
5+
"fetch": "eager",
66
"enabled": true,
77
"autoimport": {
88
"@symfony/mock-module/dist/style.css": false

test/fixtures/disabled-controller.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "eager",
5+
"fetch": "eager",
66
"enabled": false,
77
"autoimport": {
88
"@symfony/mock-module/dist/style.css": false

test/fixtures/lazy-autoimport.json renamed to test/fixtures/eager-autoimport.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "lazy",
5+
"fetch": "eager",
66
"enabled": true,
77
"autoimport": {
88
"@symfony/mock-module/dist/style.css": true

test/fixtures/eager-no-autoimport.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"controllers": {
33
"@symfony/mock-module": {
44
"mock": {
5-
"webpackMode": "eager",
5+
"fetch": "eager",
66
"enabled": true
77
}
88
}

test/fixtures/lazy-no-autoimport.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"controllers": {
3+
"@symfony/mock-module": {
4+
"mock": {
5+
"fetch": "lazy",
6+
"enabled": true
7+
}
8+
}
9+
},
10+
"entrypoints": []
11+
}

test/webpack/create-controllers-module.test.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,59 @@ describe('createControllersModule', () => {
1515
describe('empty.json', () => {
1616
it('must return an empty file', () => {
1717
const config = require('../fixtures/empty.json');
18-
expect(createControllersModule(config)).toEqual('export default {\n};');
18+
expect(createControllersModule(config).finalSource).toEqual('export default {\n};');
1919
});
2020
});
2121

2222
describe('disabled-controller.json', () => {
2323
it('must return an empty file', () => {
2424
const config = require('../fixtures/disabled-controller.json');
25-
expect(createControllersModule(config)).toEqual('export default {\n};');
25+
expect(createControllersModule(config).finalSource).toEqual('export default {\n};');
2626
});
2727
});
2828

2929
describe('disabled-autoimport.json', () => {
3030
it('must return file with no autoimport', () => {
3131
const config = require('../fixtures/disabled-autoimport.json');
32-
expect(createControllersModule(config)).toEqual(
33-
"export default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"lazy\" */ '@symfony/mock-module/dist/controller.js'),\n};"
32+
expect(createControllersModule(config).finalSource).toEqual(
33+
"export default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/controller.js'),\n};"
3434
);
3535
});
3636
});
3737

3838
describe('eager-no-autoimport.json', () => {
3939
it('must return file with no autoimport', () => {
4040
const config = require('../fixtures/eager-no-autoimport.json');
41-
expect(createControllersModule(config)).toEqual(
41+
expect(createControllersModule(config).finalSource).toEqual(
42+
"export default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/controller.js'),\n};"
43+
);
44+
});
45+
});
46+
47+
describe('deprecated-webpack-mode.json', () => {
48+
it('must return eager mode with deprecation', () => {
49+
const config = require('../fixtures/deprecated-webpack-mode.json');
50+
const result = createControllersModule(config);
51+
expect(result.finalSource).toEqual(
4252
"export default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/controller.js'),\n};"
4353
);
54+
expect(result.deprecations).toHaveLength(1);
4455
});
4556
});
4657

47-
describe('lazy-autoimport.json', () => {
48-
it('must return a file with the enabled controller', () => {
49-
const config = require('../fixtures/lazy-autoimport.json');
50-
expect(createControllersModule(config)).toEqual(
51-
"import '@symfony/mock-module/dist/style.css';\nexport default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"lazy\" */ '@symfony/mock-module/dist/controller.js'),\n};"
58+
describe('eager-autoimport.json', () => {
59+
it('must return a file with the enabled controller and auto-import', () => {
60+
const config = require('../fixtures/eager-autoimport.json');
61+
expect(createControllersModule(config).finalSource).toEqual(
62+
"import '@symfony/mock-module/dist/style.css';\nexport default {\n 'symfony--mock-module--mock': import(/* webpackMode: \"eager\" */ '@symfony/mock-module/dist/controller.js'),\n};"
5263
);
5364
});
5465
});
5566

5667
describe('lazy-controller-no-autoimport.json', () => {
57-
it('must return a file with the enabled controller', () => {
58-
const config = require('../fixtures/lazy-controller-no-autoimport.json');
59-
expect(createControllersModule(config)).toEqual(
68+
it('must return a file with a lazy controller', () => {
69+
const config = require('../fixtures/lazy-no-autoimport.json');
70+
expect(createControllersModule(config).finalSource).toEqual(
6071
`
6172
import { Controller } from 'stimulus';
6273
export default {

0 commit comments

Comments
 (0)