Skip to content

Commit 6c4f498

Browse files
weaverryannicolas-grekas
authored andcommitted
[AssetMapper] Fixing incorrect exception & adding allowing more realistic error mode
1 parent 6228da5 commit 6c4f498

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed

DependencyInjection/Configuration.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,10 @@ private function addAssetMapperSection(ArrayNodeDefinition $rootNode, callable $
870870
->info('The public path where the assets will be written to (and served from when "server" is true)')
871871
->defaultValue('/assets/')
872872
->end()
873-
->booleanNode('strict_mode')
874-
->info('If true, an exception will be thrown if an asset cannot be found when imported from JavaScript or CSS files - e.g. "import \'./non-existent.js\'"')
875-
->defaultValue(true)
873+
->enumNode('missing_import_mode')
874+
->values(['strict', 'warn', 'ignore'])
875+
->info('Behavior if an asset cannot be found when imported from JavaScript or CSS files - e.g. "import \'./non-existent.js\'". "strict" means an exception is thrown, "warn" means a warning is logged, "ignore" means the import is left as-is.')
876+
->defaultValue('warn')
876877
->end()
877878
->arrayNode('extensions')
878879
->info('Key-value pair of file extensions set to their mime type.')

DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,10 @@ private function registerAssetMapperConfiguration(array $config, ContainerBuilde
13051305
}
13061306

13071307
$container->getDefinition('asset_mapper.compiler.css_asset_url_compiler')
1308-
->setArgument(0, $config['strict_mode']);
1308+
->setArgument(0, $config['missing_import_mode']);
13091309

13101310
$container->getDefinition('asset_mapper.compiler.javascript_import_path_compiler')
1311-
->setArgument(0, $config['strict_mode']);
1311+
->setArgument(0, $config['missing_import_mode']);
13121312

13131313
$container
13141314
->getDefinition('asset_mapper.importmap.manager')

Resources/config/asset_mapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@
114114

115115
->set('asset_mapper.compiler.css_asset_url_compiler', CssAssetUrlCompiler::class)
116116
->args([
117-
abstract_arg('strict mode'),
117+
abstract_arg('missing import mode'),
118+
service('logger'),
118119
])
119120
->tag('asset_mapper.compiler')
120121

@@ -123,7 +124,8 @@
123124

124125
->set('asset_mapper.compiler.javascript_import_path_compiler', JavaScriptImportPathCompiler::class)
125126
->args([
126-
abstract_arg('strict mode'),
127+
abstract_arg('missing import mode'),
128+
service('logger'),
127129
])
128130
->tag('asset_mapper.compiler')
129131

Resources/config/schema/symfony-1.0.xsd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
<xsd:attribute name="enabled" type="xsd:boolean" />
198198
<xsd:attribute name="server" type="xsd:boolean" />
199199
<xsd:attribute name="public-prefix" type="xsd:string" />
200-
<xsd:attribute name="strict-mode" type="xsd:boolean" />
200+
<xsd:attribute name="missing-import-mode" type="missing-import-mode" />
201201
<xsd:attribute name="importmap-path" type="xsd:string" />
202202
<xsd:attribute name="importmap-polyfill" type="xsd:string" />
203203
<xsd:attribute name="vendor-dir" type="xsd:string" />
@@ -216,6 +216,14 @@
216216
<xsd:attribute name="key" type="xsd:string" use="required" />
217217
</xsd:complexType>
218218

219+
<xsd:simpleType name="missing-import-mode">
220+
<xsd:restriction base="xsd:string">
221+
<xsd:enumeration value="strict" />
222+
<xsd:enumeration value="warn" />
223+
<xsd:enumeration value="ignore" />
224+
</xsd:restriction>
225+
</xsd:simpleType>
226+
219227
<xsd:complexType name="translator">
220228
<xsd:sequence>
221229
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testAssetMapperCanBeEnabled()
107107
'excluded_patterns' => [],
108108
'server' => true,
109109
'public_prefix' => '/assets/',
110-
'strict_mode' => true,
110+
'missing_import_mode' => 'warn',
111111
'extensions' => [],
112112
'importmap_path' => '%kernel.project_dir%/importmap.php',
113113
'importmap_polyfill' => null,
@@ -619,7 +619,7 @@ protected static function getBundleDefaultConfig()
619619
'excluded_patterns' => [],
620620
'server' => true,
621621
'public_prefix' => '/assets/',
622-
'strict_mode' => true,
622+
'missing_import_mode' => 'warn',
623623
'extensions' => [],
624624
'importmap_path' => '%kernel.project_dir%/importmap.php',
625625
'importmap_polyfill' => null,

Tests/DependencyInjection/Fixtures/xml/asset_mapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
enabled="true"
1212
server="true"
1313
public-prefix="/assets_path/"
14-
strict-mode="true"
14+
missing-import-mode="strict"
1515
importmap-path="%kernel.project_dir%/importmap.php"
1616
importmap-polyfill="https://cdn.example.com/polyfill.js"
1717
vendor-dir="%kernel.project_dir%/assets/vendor"

Tests/DependencyInjection/XmlFrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,8 @@ public function testAssetMapper()
8989

9090
$definition = $container->getDefinition('asset_mapper.repository');
9191
$this->assertSame(['assets/' => '', 'assets2/' => 'my_namespace'], $definition->getArgument(0));
92+
93+
$definition = $container->getDefinition('asset_mapper.compiler.css_asset_url_compiler');
94+
$this->assertSame('strict', $definition->getArgument(0));
9295
}
9396
}

0 commit comments

Comments
 (0)