12
12
namespace Symfony \Component \AssetMapper \Tests \Compiler ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \AssetMapper \AssetDependency ;
15
16
use Symfony \Component \AssetMapper \AssetMapper ;
16
17
use Symfony \Component \AssetMapper \Compiler \JavaScriptImportPathCompiler ;
17
18
use Symfony \Component \AssetMapper \MappedAsset ;
@@ -43,20 +44,23 @@ public function testCompile(string $sourceLogicalName, string $input, string $ex
43
44
44
45
$ compiler = new JavaScriptImportPathCompiler ();
45
46
$ this ->assertSame ($ expectedOutput , $ compiler ->compile ($ input , $ asset , $ assetMapper ));
46
- $ assetDependencyLogicalPaths = array_map (fn (MappedAsset $ asset ) => $ asset ->getLogicalPath (), $ asset ->getDependencies ());
47
- $ this ->assertEquals ($ expectedDependencies , $ assetDependencyLogicalPaths );
47
+ $ actualDependencies = [];
48
+ foreach ($ asset ->getDependencies () as $ dependency ) {
49
+ $ actualDependencies [$ dependency ->asset ->getLogicalPath ()] = $ dependency ->isLazy ;
50
+ }
51
+ $ this ->assertEquals ($ expectedDependencies , $ actualDependencies );
48
52
}
49
53
50
54
public function provideCompileTests (): iterable
51
55
{
52
- yield 'simple_double_quotes ' => [
56
+ yield 'dynamic_simple_double_quotes ' => [
53
57
'sourceLogicalName ' => 'app.js ' ,
54
58
'input ' => 'import("./other.js"); ' ,
55
59
'expectedOutput ' => 'import("./other.js"); ' ,
56
- 'expectedDependencies ' => ['other.js ' ]
60
+ 'expectedDependencies ' => ['other.js ' => true ]
57
61
];
58
62
59
- yield 'simple_multiline ' => [
63
+ yield 'dynamic_simple_multiline ' => [
60
64
'sourceLogicalName ' => 'app.js ' ,
61
65
'input ' => <<<EOF
62
66
const fun;
@@ -68,63 +72,133 @@ public function provideCompileTests(): iterable
68
72
import("./other.js");
69
73
EOF
70
74
,
71
- 'expectedDependencies ' => ['other.js ' ]
75
+ 'expectedDependencies ' => ['other.js ' => true ]
72
76
];
73
77
74
- yield 'simple_single_quotes ' => [
78
+ yield 'dynamic_simple_single_quotes ' => [
75
79
'sourceLogicalName ' => 'app.js ' ,
76
80
'input ' => 'import( \'./other.js \'); ' ,
77
81
'expectedOutput ' => 'import( \'./other.js \'); ' ,
78
- 'expectedDependencies ' => ['other.js ' ]
82
+ 'expectedDependencies ' => ['other.js ' => true ]
79
83
];
80
84
81
- yield 'resolves_without_extension ' => [
85
+ yield 'dynamic_simple_tick_quotes ' => [
86
+ 'sourceLogicalName ' => 'app.js ' ,
87
+ 'input ' => 'import(`./other`); ' ,
88
+ 'expectedOutput ' => 'import(`./other.js`); ' ,
89
+ 'expectedDependencies ' => ['other.js ' => true ]
90
+ ];
91
+
92
+ yield 'dynamic_resolves_without_extension ' => [
82
93
'sourceLogicalName ' => 'app.js ' ,
83
94
'input ' => 'import("./other"); ' ,
84
95
'expectedOutput ' => 'import("./other.js"); ' ,
85
- 'expectedDependencies ' => ['other.js ' ],
96
+ 'expectedDependencies ' => ['other.js ' => true ],
86
97
];
87
98
88
- yield 'resolves_multiple ' => [
99
+ yield 'dynamic_resolves_multiple ' => [
89
100
'sourceLogicalName ' => 'app.js ' ,
90
101
'input ' => 'import("./other.js"); import("./subdir/foo.js"); ' ,
91
102
'expectedOutput ' => 'import("./other.js"); import("./subdir/foo.js"); ' ,
92
- 'expectedDependencies ' => ['other.js ' , 'subdir/foo.js ' ],
103
+ 'expectedDependencies ' => ['other.js ' => true , 'subdir/foo.js ' => true ],
93
104
];
94
105
95
- yield 'avoid_resolving_non_relative_imports ' => [
106
+ yield 'dynamic_avoid_resolving_non_relative_imports ' => [
96
107
'sourceLogicalName ' => 'app.js ' ,
97
108
'input ' => 'import("other.js"); ' ,
98
109
'expectedOutput ' => 'import("other.js"); ' ,
99
110
'expectedDependencies ' => [],
100
111
];
101
112
102
- yield 'resolves_dynamic_imports_later_in_file ' => [
113
+ yield 'dynamic_resolves_dynamic_imports_later_in_file ' => [
103
114
'sourceLogicalName ' => 'app.js ' ,
104
115
'input ' => 'console.log("Hello test!") import("./subdir/foo.js").then(() => console.log("inside promise!")); ' ,
105
116
'expectedOutput ' => 'console.log("Hello test!") import("./subdir/foo.js").then(() => console.log("inside promise!")); ' ,
106
- 'expectedDependencies ' => ['subdir/foo.js ' ],
117
+ 'expectedDependencies ' => ['subdir/foo.js ' => true ],
107
118
];
108
119
109
- yield 'correctly_moves_to_higher_directories ' => [
120
+ yield 'dynamic_correctly_moves_to_higher_directories ' => [
110
121
'sourceLogicalName ' => 'subdir/app.js ' ,
111
122
'input ' => 'import("../other.js"); ' ,
112
123
'expectedOutput ' => 'import("../other.js"); ' ,
113
- 'expectedDependencies ' => ['other.js ' ],
124
+ 'expectedDependencies ' => ['other.js ' => true ],
114
125
];
115
126
116
- yield 'correctly_moves_to_higher_directories_and_adds_extension ' => [
127
+ yield 'dynamic_correctly_moves_to_higher_directories_and_adds_extension ' => [
117
128
'sourceLogicalName ' => 'subdir/app.js ' ,
118
129
'input ' => 'import("../other"); ' ,
119
130
'expectedOutput ' => 'import("../other.js"); ' ,
120
- 'expectedDependencies ' => ['other.js ' ],
131
+ 'expectedDependencies ' => ['other.js ' => true ],
121
132
];
122
133
123
- yield 'resolves_the_index_file_in_directory ' => [
134
+ yield 'dynamic_resolves_the_index_file_in_directory ' => [
124
135
'sourceLogicalName ' => 'app.js ' ,
125
136
'input ' => 'import("./dir_with_index"); ' ,
126
137
'expectedOutput ' => 'import("./dir_with_index/index.js"); ' ,
127
- 'expectedDependencies ' => ['dir_with_index/index.js ' ],
138
+ 'expectedDependencies ' => ['dir_with_index/index.js ' => true ],
139
+ ];
140
+
141
+ yield 'static_named_import_double_quotes ' => [
142
+ 'sourceLogicalName ' => 'app.js ' ,
143
+ 'input ' => 'import { myFunction } from "./other"; ' ,
144
+ 'expectedOutput ' => 'import { myFunction } from "./other.js"; ' ,
145
+ 'expectedDependencies ' => ['other.js ' => false ],
146
+ ];
147
+
148
+ yield 'static_named_import_single_quotes ' => [
149
+ 'sourceLogicalName ' => 'app.js ' ,
150
+ 'input ' => 'import { myFunction } from \'./other \'; ' ,
151
+ 'expectedOutput ' => 'import { myFunction } from \'./other.js \'; ' ,
152
+ 'expectedDependencies ' => ['other.js ' => false ],
153
+ ];
154
+
155
+ yield 'static_default_import ' => [
156
+ 'sourceLogicalName ' => 'app.js ' ,
157
+ 'input ' => 'import myFunction from "./other"; ' ,
158
+ 'expectedOutput ' => 'import myFunction from "./other.js"; ' ,
159
+ 'expectedDependencies ' => ['other.js ' => false ],
160
+ ];
161
+
162
+ yield 'static_default_and_named_import ' => [
163
+ 'sourceLogicalName ' => 'app.js ' ,
164
+ 'input ' => 'import myFunction, { helperFunction } from "./other"; ' ,
165
+ 'expectedOutput ' => 'import myFunction, { helperFunction } from "./other.js"; ' ,
166
+ 'expectedDependencies ' => ['other.js ' => false ],
167
+ ];
168
+
169
+ yield 'static_import_everything ' => [
170
+ 'sourceLogicalName ' => 'app.js ' ,
171
+ 'input ' => 'import * as myModule from "./other"; ' ,
172
+ 'expectedOutput ' => 'import * as myModule from "./other.js"; ' ,
173
+ 'expectedDependencies ' => ['other.js ' => false ],
174
+ ];
175
+
176
+ yield 'static_import_just_for_side_effects ' => [
177
+ 'sourceLogicalName ' => 'app.js ' ,
178
+ 'input ' => 'import "./other"; ' ,
179
+ 'expectedOutput ' => 'import "./other.js"; ' ,
180
+ 'expectedDependencies ' => ['other.js ' => false ],
181
+ ];
182
+
183
+ yield 'mix_of_static_and_dynamic_imports ' => [
184
+ 'sourceLogicalName ' => 'app.js ' ,
185
+ 'input ' => 'import "./other"; import("./subdir/foo.js"); ' ,
186
+ 'expectedOutput ' => 'import "./other.js"; import("./subdir/foo.js"); ' ,
187
+ 'expectedDependencies ' => ['other.js ' => false , 'subdir/foo.js ' => true ],
188
+ ];
189
+
190
+ yield 'extra_import_word_does_not_cause_issues ' => [
191
+ 'sourceLogicalName ' => 'app.js ' ,
192
+ 'input ' => "// about to do an import \nimport('./other'); " ,
193
+ 'expectedOutput ' => "// about to do an import \nimport('./other.js'); " ,
194
+ 'expectedDependencies ' => ['other.js ' => true ],
195
+ ];
196
+
197
+ yield 'import_on_one_line_then_module_name_on_next_is_ok ' => [
198
+ 'sourceLogicalName ' => 'app.js ' ,
199
+ 'input ' => "import \n './other'; " ,
200
+ 'expectedOutput ' => "import \n './other.js'; " ,
201
+ 'expectedDependencies ' => ['other.js ' => false ],
128
202
];
129
203
}
130
204
}
0 commit comments