Skip to content

Commit f356652

Browse files
ENGCOM-6382: Prevent RequireJS from adding .min.js suffix to external files #25869
2 parents c299bfb + 879cda2 commit f356652

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

lib/internal/Magento/Framework/RequireJs/Config.php

+25-12
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,14 @@ public function getConfigFileRelativePath()
196196
*/
197197
public function getMixinsFileRelativePath()
198198
{
199-
$map = $this->getRepositoryFilesMap(Config::MIXINS_FILE_NAME, [
200-
'area' => $this->staticContext->getAreaCode(),
201-
'theme' => $this->staticContext->getThemePath(),
202-
'locale' => $this->staticContext->getLocale(),
203-
]);
199+
$map = $this->getRepositoryFilesMap(
200+
Config::MIXINS_FILE_NAME,
201+
[
202+
'area' => $this->staticContext->getAreaCode(),
203+
'theme' => $this->staticContext->getThemePath(),
204+
'locale' => $this->staticContext->getLocale(),
205+
]
206+
);
204207
if ($map) {
205208
$relativePath = implode('/', $map) . '/' . Config::MIXINS_FILE_NAME;
206209
} else {
@@ -254,11 +257,14 @@ public function getMinResolverRelativePath()
254257
*/
255258
public function getUrlResolverFileRelativePath()
256259
{
257-
$map = $this->getRepositoryFilesMap(Config::URL_RESOLVER_FILE_NAME, [
258-
'area' => $this->staticContext->getAreaCode(),
259-
'theme' => $this->staticContext->getThemePath(),
260-
'locale' => $this->staticContext->getLocale(),
261-
]);
260+
$map = $this->getRepositoryFilesMap(
261+
Config::URL_RESOLVER_FILE_NAME,
262+
[
263+
'area' => $this->staticContext->getAreaCode(),
264+
'theme' => $this->staticContext->getThemePath(),
265+
'locale' => $this->staticContext->getLocale(),
266+
]
267+
);
262268
if ($map) {
263269
$relativePath = implode('/', $map) . '/' . Config::URL_RESOLVER_FILE_NAME;
264270
} else {
@@ -278,6 +284,8 @@ public function getMapFileRelativePath()
278284
}
279285

280286
/**
287+
* Get path to configuration file
288+
*
281289
* @return string
282290
*/
283291
protected function getConfigFileName()
@@ -286,19 +294,22 @@ protected function getConfigFileName()
286294
}
287295

288296
/**
297+
* Get resolver code which RequireJS fetch minified files instead
298+
*
289299
* @return string
290300
*/
291301
public function getMinResolverCode()
292302
{
293-
$excludes = [];
303+
$excludes = ['url.indexOf(baseUrl) === 0'];
294304
foreach ($this->minification->getExcludes('js') as $expression) {
295305
$excludes[] = '!url.match(/' . str_replace('/', '\/', $expression) . '/)';
296306
}
297307
$excludesCode = empty($excludes) ? 'true' : implode('&&', $excludes);
298308

299309
$result = <<<code
300310
var ctx = require.s.contexts._,
301-
origNameToUrl = ctx.nameToUrl;
311+
origNameToUrl = ctx.nameToUrl,
312+
baseUrl = ctx.config.baseUrl;
302313
303314
ctx.nameToUrl = function() {
304315
var url = origNameToUrl.apply(ctx, arguments);
@@ -317,6 +328,8 @@ public function getMinResolverCode()
317328
}
318329

319330
/**
331+
* Get map for given file.
332+
*
320333
* @param string $fileId
321334
* @param array $params
322335
* @return array

lib/internal/Magento/Framework/RequireJs/Test/Unit/ConfigTest.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,13 @@ public function testGetConfig()
101101
{
102102
$this->fileReader->expects($this->any())
103103
->method('readAll')
104-
->will($this->returnCallback(function ($file) {
105-
return $file . ' content';
106-
}));
104+
->will(
105+
$this->returnCallback(
106+
function ($file) {
107+
return $file . ' content';
108+
}
109+
)
110+
);
107111
$fileOne = $this->createMock(\Magento\Framework\View\File::class);
108112
$fileOne->expects($this->once())
109113
->method('getFilename')
@@ -180,11 +184,12 @@ public function testGetMinResolverCode()
180184

181185
$expected = <<<code
182186
var ctx = require.s.contexts._,
183-
origNameToUrl = ctx.nameToUrl;
187+
origNameToUrl = ctx.nameToUrl,
188+
baseUrl = ctx.config.baseUrl;
184189
185190
ctx.nameToUrl = function() {
186191
var url = origNameToUrl.apply(ctx, arguments);
187-
if (!url.match(/\.min\./)) {
192+
if (url.indexOf(baseUrl) === 0&&!url.match(/\.min\./)) {
188193
url = url.replace(/(\.min)?\.js$/, '.min.js');
189194
}
190195
return url;

0 commit comments

Comments
 (0)