Skip to content

Commit cf1c2a9

Browse files
committed
WebPack Loader Script Modules Support
Since the `ScriptModule` has been integrated, we can register WP modules, but only when we create the Assets programmatically. There's no support for modules within the Autoloader. These changes aim to support modules by establishing a module filename convention as `.mjs` might not work correctly if it isn't possible to guarantee the server is serving the mjs resources with the correct MIME type. Hence, a `.module.js` seems to be a good tradeoff and the number of changes necessary to make it work ar limited, so in the future when (and if) the `mjs` are well-supported, we can slightly improve the solution. The changes involve two key points - The `AbstractWebpackLoader` so that the class can recognize the new `ScriptModule` class as an instantiable class for those files ending with `.module.js`. - The `WebpackManifestLoader` to correctly use the original `handle` rather than the file name. WordPress Modules start with `@` though, not required it is an established convention and would make sense to keep the handle provided by the manifest rather than guessing it by the file name. Note: The Manifest file contains a key value pair, and it could be possible to extract the handle from the key, which might be a file path in the classic case the webpack plugin is configured with `basePath: './'` and `publicPath: './'`. I would not suggest changing the approach here and with these changes as it would be comparable to a breaking change, while the current ones can be seen as a minor change because, the file name ending with `.module.js` would trigger the registration using the `ScriptModule`.
1 parent b328f2f commit cf1c2a9

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/Loader/AbstractWebpackLoader.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inpsyde\Assets\Exception\InvalidResourceException;
1212
use Inpsyde\Assets\Script;
1313
use Inpsyde\Assets\Style;
14+
use Inpsyde\Assets\ScriptModule;
1415

1516
abstract class AbstractWebpackLoader implements LoaderInterface
1617
{
@@ -110,20 +111,26 @@ protected function buildAsset(string $handle, string $fileUrl, string $filePath)
110111
$extensionsToClass = [
111112
'css' => Style::class,
112113
'js' => Script::class,
114+
'module.js' => ScriptModule::class,
113115
];
114116

115117
/** @var array{filename?:string, extension?:string} $pathInfo */
116118
$pathInfo = pathinfo($filePath);
119+
$baseName = $pathInfo['basename'] ?? '';
117120
$filename = $pathInfo['filename'] ?? '';
118121
$extension = $pathInfo['extension'] ?? '';
119122

123+
if (self::isModule($baseName)) {
124+
$extension = 'module.js';
125+
}
126+
120127
if (!in_array($extension, array_keys($extensionsToClass), true)) {
121128
return null;
122129
}
123130

124131
$class = $extensionsToClass[$extension];
125132

126-
/** @var Style|Script $asset */
133+
/** @var Style|Script|ScriptModule $asset */
127134
$asset = new $class($handle, $fileUrl, $this->resolveLocation($filename));
128135
$asset->withFilePath($filePath);
129136
$asset->canEnqueue(true);
@@ -137,6 +144,11 @@ protected function buildAsset(string $handle, string $fileUrl, string $filePath)
137144
return $asset;
138145
}
139146

147+
protected static function isModule(string $fileName): bool
148+
{
149+
return str_ends_with($fileName, '.module.js');
150+
}
151+
140152
/**
141153
* The "file"-value can contain:
142154
* - URL

src/Loader/WebpackManifestLoader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ protected function parseData(array $data, string $resource): array
1818
$directory = trailingslashit(dirname($resource));
1919
$assets = [];
2020
foreach ($data as $handle => $file) {
21-
// It can be possible, that the "handle"-key is a filepath.
22-
$handle = pathinfo($handle, PATHINFO_FILENAME);
21+
if (!self::isModule($file)) {
22+
// It can be possible that the "handle"-key is a filepath.
23+
$handle = pathinfo($handle, PATHINFO_FILENAME);
24+
}
2325

2426
$sanitizedFile = $this->sanitizeFileName($file);
2527

0 commit comments

Comments
 (0)