Skip to content

Commit 6b1ab20

Browse files
committed
feature #111 Add support for loader-utils@^3.0.0 and schema-utils@^4.0.0, close #102 (Kocal)
This PR was squashed before being merged into the main branch. Discussion ---------- Add support for `loader-utils@^3.0.0` and `schema-utils@^4.0.0`, close #102 Commits ------- 08fb52c Add support for `loader-utils@^3.0.0` and `schema-utils@^4.0.0`, close #102
2 parents fe9ca0b + 08fb52c commit 6b1ab20

File tree

8 files changed

+122
-8
lines changed

8 files changed

+122
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
* Thus having no effects on the `dist/` files, TypeScript `module` is now set to `ESNext`, `moduleResolution` to `NodeNext`, and `target` to `ES2021` by @Kocal in https://github.com/symfony/stimulus-bridge/pull/99
1313
* Upgrade minimum supported `acorn` version to 8.2.0 by @Kocal in https://github.com/symfony/stimulus-bridge/pull/110
14+
* Add support for `loader-utils@^3.0.0` and `schema-utils@^4.0.0` by @Kocal in https://github.com/symfony/stimulus-bridge/pull/111
1415

1516
### Internal
1617

biome.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
33
"files": {
44
"include": ["*.js", "src/**/*.[jt]s", "test/**/*.[jt]s"],
5-
"ignore": ["**/package.json", "**/node_modules", "dist/**", "test/dist/**", "test/fixtures/**/dist/**"]
5+
"ignore": [
6+
"**/package.json",
7+
"**/node_modules",
8+
"dist/**",
9+
"test/dist/**",
10+
"test/fixtures/**/dist/**",
11+
"test/parseQuery.ts"
12+
]
613
},
714
"formatter": {
815
"lineWidth": 120,

dist/webpack/lazy-controller-loader.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5297,12 +5297,17 @@ const schema = {
52975297
export: { type: "string" }
52985298
}
52995299
};
5300+
function getLoaderOptions(loaderContext) {
5301+
if (Object.prototype.hasOwnProperty.call(loaderContext, "getOptions")) return loaderContext.getOptions();
5302+
if (Object.prototype.hasOwnProperty.call(loader_utils, "getOptions")) return loader_utils.getOptions(loaderContext);
5303+
throw new Error("Unable to get loader options, please upgrade to \"webpack@^5\" or downgrade to \"loader-utils@^2\".");
5304+
}
53005305
function lazy_controller_loader_default(source, sourceMap) {
53015306
const { options, errors } = get_stimulus_comment_options_default(source);
53025307
for (const error of errors) this.emitError(new Error(`Invalid comment found:\n\n "/* ${error.comment.value.trim()} */".\n\nCheck your syntax.`));
53035308
const stimulusFetch = typeof options.stimulusFetch !== "undefined" ? options.stimulusFetch : "eager";
53045309
if (!["eager", "lazy"].includes(stimulusFetch)) this.emitError(new Error(`Invalid value "${stimulusFetch}" found for "stimulusFetch". Allowed values are "lazy" or "eager"`));
5305-
const loaderOptions = loader_utils.getOptions(this);
5310+
const loaderOptions = getLoaderOptions(this);
53065311
schema_utils.validate(schema, loaderOptions, {
53075312
name: "@symfony/stimulus-bridge/lazy-controller-loader",
53085313
baseDataPath: "options"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"@hotwired/stimulus-webpack-helpers": "^1.0.1",
2424
"@types/webpack-env": "^1.16.4",
2525
"acorn": "^8.2.0",
26-
"loader-utils": "^2.0.0",
27-
"schema-utils": "^3.0.0"
26+
"loader-utils": "^2.0.0 || ^3.0.0",
27+
"schema-utils": "^3.0.0 || ^4.0.0"
2828
},
2929
"devDependencies": {
3030
"@babel/core": "^7.22.0",

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/webpack/lazy-controller-loader.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ const schema = {
2424
},
2525
};
2626

27+
function getLoaderOptions(loaderContext: any) {
28+
// Webpack 5, when using loader-utils ^3.0.0
29+
if (Object.prototype.hasOwnProperty.call(loaderContext, 'getOptions')) {
30+
return loaderContext.getOptions();
31+
}
32+
33+
// loader-utils ^2.0.0
34+
if (Object.prototype.hasOwnProperty.call(loaderUtils, 'getOptions')) {
35+
return loaderUtils.getOptions(loaderContext);
36+
}
37+
38+
throw new Error('Unable to get loader options, please upgrade to "webpack@^5" or downgrade to "loader-utils@^2".');
39+
}
40+
2741
/**
2842
* Loader that can make a Stimulus controller lazy.
2943
*
@@ -56,7 +70,7 @@ export default function (source: string, sourceMap: string) {
5670
);
5771
}
5872

59-
const loaderOptions = loaderUtils.getOptions(this);
73+
const loaderOptions = getLoaderOptions(this);
6074

6175
schemaUtils.validate(schema, loaderOptions, {
6276
name: '@symfony/stimulus-bridge/lazy-controller-loader',

test/parseQuery.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// biome-ignore file: d
2+
// This file is copied from https://github.com/symfony/stimulus-bridge/blob/main/src/webpack/lazy-controller-loader.ts
3+
// to keep "emulated" call to loader working when using loader-utils@^3.0.0.
4+
5+
import JSON5 from 'json5';
6+
7+
const specialValues = {
8+
null: null,
9+
true: true,
10+
false: false,
11+
};
12+
13+
export function parseQuery(query) {
14+
if (query.substr(0, 1) !== '?') {
15+
throw new Error("A valid query string passed to parseQuery should begin with '?'");
16+
}
17+
18+
query = query.substr(1);
19+
20+
if (!query) {
21+
return {};
22+
}
23+
24+
if (query.substr(0, 1) === '{' && query.substr(-1) === '}') {
25+
return JSON5.parse(query);
26+
}
27+
28+
const queryArgs = query.split(/[,&]/g);
29+
const result = {};
30+
31+
queryArgs.forEach((arg) => {
32+
const idx = arg.indexOf('=');
33+
34+
if (idx >= 0) {
35+
let name = arg.substr(0, idx);
36+
let value = decodeURIComponent(arg.substr(idx + 1));
37+
38+
// eslint-disable-next-line no-prototype-builtins
39+
if (specialValues.hasOwnProperty(value)) {
40+
value = specialValues[value];
41+
}
42+
43+
if (name.substr(-2) === '[]') {
44+
name = decodeURIComponent(name.substr(0, name.length - 2));
45+
46+
if (!Array.isArray(result[name])) {
47+
result[name] = [];
48+
}
49+
50+
result[name].push(value);
51+
} else {
52+
name = decodeURIComponent(name);
53+
result[name] = value;
54+
}
55+
} else {
56+
if (arg.substr(0, 1) === '-') {
57+
result[decodeURIComponent(arg.substr(1))] = false;
58+
} else if (arg.substr(0, 1) === '+') {
59+
result[decodeURIComponent(arg.substr(1))] = true;
60+
} else {
61+
result[decodeURIComponent(arg)] = true;
62+
}
63+
}
64+
});
65+
66+
return result;
67+
}

test/webpack/lazy-controller-loader.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import * as loaderUtils from 'loader-utils';
1011
import lazyControllerLoader from '../../src/webpack/lazy-controller-loader';
12+
import { parseQuery } from '../parseQuery';
13+
14+
const isLoaderUtilsV3 = !Object.prototype.hasOwnProperty.call(loaderUtils, 'getOptions');
1115

1216
function callLoader(src: string, startingSourceMap = '', query = '') {
1317
const loaderThis = {
@@ -24,6 +28,22 @@ function callLoader(src: string, startingSourceMap = '', query = '') {
2428
},
2529
};
2630

31+
// Re-used from https://github.com/webpack/loader-utils/blob/09128d7a9ae08da110180c31fbec11e195a75220/lib/getOptions.js
32+
if (isLoaderUtilsV3) {
33+
loaderThis.getOptions = () => {
34+
if (typeof query === 'string' && query !== '') {
35+
return parseQuery(query);
36+
}
37+
38+
if (!query || typeof query !== 'object') {
39+
// Not object-like queries are not supported.
40+
return {};
41+
}
42+
43+
return query;
44+
};
45+
}
46+
2747
lazyControllerLoader.call(loaderThis, src, startingSourceMap);
2848

2949
return {

0 commit comments

Comments
 (0)