|
226 | 226 | end
|
227 | 227 | end
|
228 | 228 |
|
229 |
| - it 'prefers a relative importer load to an importer' do |
230 |
| - result = described_class.compile_string( |
231 |
| - '@import "other";', |
232 |
| - importers: [{ |
233 |
| - canonicalize: lambda { |*| |
234 |
| - raise 'canonicalize() should not be called' |
235 |
| - }, |
236 |
| - load: lambda { |*| |
237 |
| - raise 'load() should not be called' |
238 |
| - } |
239 |
| - }], |
240 |
| - url: 'o:style.scss', |
241 |
| - importer: { |
242 |
| - canonicalize: ->(url, **) { url }, |
243 |
| - load: lambda { |*| |
244 |
| - return { |
245 |
| - contents: 'a {from: relative}', |
246 |
| - syntax: 'scss' |
247 |
| - } |
248 |
| - } |
249 |
| - } |
250 |
| - ) |
251 |
| - expect(result.css).to eq("a {\n from: relative;\n}") |
252 |
| - end |
253 |
| - |
254 | 229 | it 'prefers an importer to a load path' do
|
255 | 230 | sandbox do |dir|
|
256 | 231 | dir.write({
|
|
337 | 312 | end
|
338 | 313 | end
|
339 | 314 |
|
| 315 | + describe "compile_string()'s importer option" do |
| 316 | + it 'loads relative imports from the entrypoint' do |
| 317 | + result = described_class.compile_string( |
| 318 | + '@import "orange";', |
| 319 | + importer: { |
| 320 | + canonicalize: lambda { |url, **| |
| 321 | + expect(url).to eq('u:orange') |
| 322 | + url |
| 323 | + }, |
| 324 | + load: lambda { |url| |
| 325 | + color = url.split(':')[1] |
| 326 | + return { |
| 327 | + contents: ".#{color} {color: #{color}}", |
| 328 | + syntax: 'scss' |
| 329 | + } |
| 330 | + } |
| 331 | + }, |
| 332 | + url: 'u:entrypoint' |
| 333 | + ) |
| 334 | + |
| 335 | + expect(result.css).to eq(".orange {\n color: orange;\n}") |
| 336 | + end |
| 337 | + |
| 338 | + it 'takes precedence over the importer list for relative URLs' do |
| 339 | + result = described_class.compile_string( |
| 340 | + '@import "other";', |
| 341 | + importer: { |
| 342 | + canonicalize: lambda { |url, **| |
| 343 | + url |
| 344 | + }, |
| 345 | + load: lambda { |_url| |
| 346 | + return { |
| 347 | + contents: 'a {from: relative}', |
| 348 | + syntax: 'scss' |
| 349 | + } |
| 350 | + } |
| 351 | + }, |
| 352 | + importers: [{ |
| 353 | + canonicalize: lambda { |*| |
| 354 | + raise 'canonicalize() should not be called' |
| 355 | + }, |
| 356 | + load: lambda { |*| |
| 357 | + raise 'load() should not be called' |
| 358 | + } |
| 359 | + }], |
| 360 | + url: 'o:style.scss' |
| 361 | + ) |
| 362 | + |
| 363 | + expect(result.css).to eq("a {\n from: relative;\n}") |
| 364 | + end |
| 365 | + |
| 366 | + it "doesn't load absolute imports" do |
| 367 | + result = described_class.compile_string( |
| 368 | + '@import "u:orange";', |
| 369 | + importer: { |
| 370 | + canonicalize: lambda { |*| |
| 371 | + raise 'canonicalize() should not be called' |
| 372 | + }, |
| 373 | + load: lambda { |*| |
| 374 | + raise 'load() should not be called' |
| 375 | + } |
| 376 | + }, |
| 377 | + importers: [{ |
| 378 | + canonicalize: lambda { |url, **| |
| 379 | + expect(url).to eq('u:orange') |
| 380 | + url |
| 381 | + }, |
| 382 | + load: lambda { |url| |
| 383 | + color = url.split(':')[1] |
| 384 | + return { |
| 385 | + contents: ".#{color} {color: #{color}}", |
| 386 | + syntax: 'scss' |
| 387 | + } |
| 388 | + } |
| 389 | + }], |
| 390 | + url: 'x:entrypoint' |
| 391 | + ) |
| 392 | + |
| 393 | + expect(result.css).to eq(".orange {\n color: orange;\n}") |
| 394 | + end |
| 395 | + |
| 396 | + it "doesn't load from other importers" do |
| 397 | + result = described_class.compile_string( |
| 398 | + '@import "u:midstream";', |
| 399 | + importer: { |
| 400 | + canonicalize: lambda { |*| |
| 401 | + raise 'canonicalize() should not be called' |
| 402 | + }, |
| 403 | + load: lambda { |*| |
| 404 | + raise 'load() should not be called' |
| 405 | + } |
| 406 | + }, |
| 407 | + importers: [{ |
| 408 | + canonicalize: lambda { |url, **| |
| 409 | + url |
| 410 | + }, |
| 411 | + load: lambda { |url| |
| 412 | + pathname = url.split(':')[1] |
| 413 | + if pathname == 'midstream' |
| 414 | + return { |
| 415 | + contents: "@import 'orange';", |
| 416 | + syntax: 'scss' |
| 417 | + } |
| 418 | + else |
| 419 | + color = pathname |
| 420 | + return { |
| 421 | + contents: ".#{color} {color: #{color}}", |
| 422 | + syntax: 'scss' |
| 423 | + } |
| 424 | + end |
| 425 | + } |
| 426 | + }], |
| 427 | + url: 'x:entrypoint' |
| 428 | + ) |
| 429 | + |
| 430 | + expect(result.css).to eq(".orange {\n color: orange;\n}") |
| 431 | + end |
| 432 | + |
| 433 | + it 'importer order is preserved for absolute imports' do |
| 434 | + # The second importer should only be invoked once, because when the |
| 435 | + # "first:other" import is resolved it should be passed to the first |
| 436 | + # importer first despite being in the second importer's file. |
| 437 | + second_called = false |
| 438 | + result = described_class.compile_string( |
| 439 | + '@import "second:other";', |
| 440 | + importers: [{ |
| 441 | + canonicalize: lambda { |url, **| |
| 442 | + url if url.start_with?('first:') |
| 443 | + }, |
| 444 | + load: lambda { |*| |
| 445 | + return { |
| 446 | + contents: 'a {from: first}', |
| 447 | + syntax: 'scss' |
| 448 | + } |
| 449 | + } |
| 450 | + }, { |
| 451 | + canonicalize: lambda { |url, **| |
| 452 | + raise 'Second importer should only be called once.' if second_called |
| 453 | + |
| 454 | + second_called = true |
| 455 | + url if url.start_with?('second:') |
| 456 | + }, |
| 457 | + load: lambda { |*| |
| 458 | + return { |
| 459 | + contents: '@import "first:other";', |
| 460 | + syntax: 'scss' |
| 461 | + } |
| 462 | + } |
| 463 | + }] |
| 464 | + ) |
| 465 | + |
| 466 | + expect(result.css).to eq("a {\n from: first;\n}") |
| 467 | + end |
| 468 | + end |
| 469 | + |
340 | 470 | describe 'from_import is' do
|
341 | 471 | def expect_from_import(canonicalize, expected)
|
342 | 472 | allow(canonicalize).to receive(:call) { |url, from_import:|
|
|
0 commit comments