1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Package\Processor\PostProcessor;
use Magento\Deploy\Package\PackageFile;
use Magento\Deploy\Service\DeployStaticFile;
use Magento\Deploy\Package\Package;
use Magento\Deploy\Package\PackageFileFactory;
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
use Magento\Deploy\Package\Processor\ProcessorInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Asset\Minification;
use Magento\Framework\View\Asset\RepositoryMap;
/**
* Map post-processor is for generating map files for JavaScript and server-side URL resolvers
*
* Map files needed only for compact deployment, and yet can be used for tracking changes of deployed static files
* in development mode, when using symlinks is not possible
*/
class Map implements ProcessorInterface
{
/**
* Service class is used for deploying files to public directory
*
* The service does simple write, copy or go through publication process (apply fallback rules, pre-processing etc)
*
* @var DeployStaticFile
*/
private $deployStaticFile;
/**
* PHP code formatter
*
* Formatter generates code for PHP file that returns data array
*
* @var PhpFormatter
*/
private $formatter;
/**
* Factory class for deployment package object
*
* @see PackageFile
* @var PackageFileFactory
*/
private $packageFileFactory;
/**
* Helper class for static files minification related processes
*
* @var Minification
*/
private $minification;
/**
* Deployment procedure options
*
* @var array
*/
private $options = [];
/**
* Map constructor
*
* @param DeployStaticFile $deployStaticFile
* @param PhpFormatter $formatter
* @param PackageFileFactory $packageFileFactory
* @param Minification $minification
*/
public function __construct(
DeployStaticFile $deployStaticFile,
PhpFormatter $formatter,
PackageFileFactory $packageFileFactory,
Minification $minification
) {
$this->deployStaticFile = $deployStaticFile;
$this->packageFileFactory = $packageFileFactory;
$this->formatter = $formatter;
$this->minification = $minification;
}
/**
* @inheritdoc
*/
public function process(Package $package, array $options)
{
if ($package->isVirtual()) {
return true;
}
// delete existing map files
$this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::MAP_NAME);
$this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::REQUIRE_JS_MAP_NAME);
$this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::RESULT_MAP_NAME);
if (!$package->getParam('build_map')) {
return true;
}
$this->options = $options;
$packageMap = $package->getParentMap();
if ($packageMap) {
$this->deployStaticFile->writeFile(
RepositoryMap::MAP_NAME,
$package->getPath(),
json_encode($packageMap)
);
$this->deployStaticFile->writeFile(
$this->minification->addMinifiedSign(RepositoryMap::REQUIRE_JS_MAP_NAME),
$package->getPath(),
$this->getRequireJsMap($packageMap)
);
}
$resultMap = $package->getResultMap();
if ($resultMap) {
$this->deployStaticFile->writeFile(
RepositoryMap::RESULT_MAP_NAME,
$package->getPath(),
json_encode($resultMap)
);
}
return true;
}
/**
* Retrieve require js map
*
* @param array $map
* @return string
*/
private function getRequireJsMap(array $map)
{
$jsonMap = [];
foreach ($map as $fileId => $fileInfo) {
if (!in_array(pathinfo($fileId, PATHINFO_EXTENSION), ['js', 'html'])) {
continue;
}
$fileId = '/' . $fileId; // add leading slash to match exclude patterns
$filePath = $this->minification->addMinifiedSign(str_replace(Repository::FILE_ID_SEPARATOR, '/', $fileId));
$filePath = substr($filePath, 1); // and remove
$jsonMap[$filePath] = '../../../../'
. $fileInfo['area'] . '/' . $fileInfo['theme'] . '/' . $fileInfo['locale'] . '/';
}
$jsonMap = json_encode($jsonMap);
return "require.config({\"config\": {\"baseUrlInterceptor\":{$jsonMap}}});";
}
}