Skip to content

Prevent RequireJS from adding .min.js suffix to external files #25869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions lib/internal/Magento/Framework/RequireJs/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,14 @@ public function getConfigFileRelativePath()
*/
public function getMixinsFileRelativePath()
{
$map = $this->getRepositoryFilesMap(Config::MIXINS_FILE_NAME, [
'area' => $this->staticContext->getAreaCode(),
'theme' => $this->staticContext->getThemePath(),
'locale' => $this->staticContext->getLocale(),
]);
$map = $this->getRepositoryFilesMap(
Config::MIXINS_FILE_NAME,
[
'area' => $this->staticContext->getAreaCode(),
'theme' => $this->staticContext->getThemePath(),
'locale' => $this->staticContext->getLocale(),
]
);
if ($map) {
$relativePath = implode('/', $map) . '/' . Config::MIXINS_FILE_NAME;
} else {
Expand Down Expand Up @@ -254,11 +257,14 @@ public function getMinResolverRelativePath()
*/
public function getUrlResolverFileRelativePath()
{
$map = $this->getRepositoryFilesMap(Config::URL_RESOLVER_FILE_NAME, [
'area' => $this->staticContext->getAreaCode(),
'theme' => $this->staticContext->getThemePath(),
'locale' => $this->staticContext->getLocale(),
]);
$map = $this->getRepositoryFilesMap(
Config::URL_RESOLVER_FILE_NAME,
[
'area' => $this->staticContext->getAreaCode(),
'theme' => $this->staticContext->getThemePath(),
'locale' => $this->staticContext->getLocale(),
]
);
if ($map) {
$relativePath = implode('/', $map) . '/' . Config::URL_RESOLVER_FILE_NAME;
} else {
Expand All @@ -278,6 +284,8 @@ public function getMapFileRelativePath()
}

/**
* Get path to configuration file
*
* @return string
*/
protected function getConfigFileName()
Expand All @@ -286,19 +294,22 @@ protected function getConfigFileName()
}

/**
* Get resolver code which RequireJS fetch minified files instead
*
* @return string
*/
public function getMinResolverCode()
{
$excludes = [];
$excludes = ['url.indexOf(baseUrl) === 0'];
foreach ($this->minification->getExcludes('js') as $expression) {
$excludes[] = '!url.match(/' . str_replace('/', '\/', $expression) . '/)';
}
$excludesCode = empty($excludes) ? 'true' : implode('&&', $excludes);

$result = <<<code
var ctx = require.s.contexts._,
origNameToUrl = ctx.nameToUrl;
origNameToUrl = ctx.nameToUrl,
baseUrl = ctx.config.baseUrl;

ctx.nameToUrl = function() {
var url = origNameToUrl.apply(ctx, arguments);
Expand All @@ -317,6 +328,8 @@ public function getMinResolverCode()
}

/**
* Get map for given file.
*
* @param string $fileId
* @param array $params
* @return array
Expand Down
15 changes: 10 additions & 5 deletions lib/internal/Magento/Framework/RequireJs/Test/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ public function testGetConfig()
{
$this->fileReader->expects($this->any())
->method('readAll')
->will($this->returnCallback(function ($file) {
return $file . ' content';
}));
->will(
$this->returnCallback(
function ($file) {
return $file . ' content';
}
)
);
$fileOne = $this->createMock(\Magento\Framework\View\File::class);
$fileOne->expects($this->once())
->method('getFilename')
Expand Down Expand Up @@ -180,11 +184,12 @@ public function testGetMinResolverCode()

$expected = <<<code
var ctx = require.s.contexts._,
origNameToUrl = ctx.nameToUrl;
origNameToUrl = ctx.nameToUrl,
baseUrl = ctx.config.baseUrl;

ctx.nameToUrl = function() {
var url = origNameToUrl.apply(ctx, arguments);
if (!url.match(/\.min\./)) {
if (url.indexOf(baseUrl) === 0&&!url.match(/\.min\./)) {
url = url.replace(/(\.min)?\.js$/, '.min.js');
}
return url;
Expand Down