Skip to content

Commit

Permalink
Implicit importer corrections (#22)
Browse files Browse the repository at this point in the history
For compile-string, with `importer=nil`, do not set any importer.

The embedded compiler no longer inserts its current directory.
  • Loading branch information
johnfairh authored Apr 21, 2022
1 parent 711adae commit 2d59bbf
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Distributed under the MIT license, see LICENSE.
![Platforms](https://img.shields.io/badge/platform-macOS%20%7C%20linux-lightgrey.svg)
[![codecov](https://codecov.io/gh/johnfairh/swift-sass/branch/main/graph/badge.svg?token=0NAP6IA9EB)](https://codecov.io/gh/johnfairh/swift-sass)
![Tests](https://github.com/johnfairh/swift-sass/workflows/Tests/badge.svg)
![Sass](https://img.shields.io/badge/sass-1.49.10-purple)
![Sass](https://img.shields.io/badge/sass-1.50.1-purple)

# Swift Sass

Expand Down
18 changes: 8 additions & 10 deletions Sources/DartSass/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ public final class Compiler: @unchecked Sendable {
/// - url: The absolute URL to associate with `string`, from where it was loaded.
/// Default `nil` meaning unknown.
/// - importer: Rule to resolve `@import` etc. from `string` relative to `url`. Default `nil`
/// meaning the current filesystem directory is used.
/// meaning no filesystem importer is configured. Unlike some Sass implementations this means that
/// imports of files from the current directory don't work automatically: add a loadpath to `importers`.
/// - outputStyle: How to format the produced CSS. Default `.expanded`.
/// - sourceMapStyle: Kind of source map to create for the CSS. Default `.separateSources`.
/// - importers: Rules for resolving `@import` etc. for this compilation, used in order after
Expand All @@ -401,9 +402,12 @@ public final class Compiler: @unchecked Sendable {
input: .string(.with { m in
m.source = string
m.syntax = .init(syntax)
url.flatMap { m.url = $0.absoluteString }
m.importer = .init(findImplicitImporter(importer: importer),
id: CompilationRequest.baseImporterID)
url.map {
m.url = $0.absoluteString
}
importer.map {
m.importer = .init($0, id: CompilationRequest.baseImporterID)
}
}),
outputStyle: outputStyle,
sourceMapStyle: sourceMapStyle,
Expand All @@ -413,12 +417,6 @@ public final class Compiler: @unchecked Sendable {
}.get()
}

// Compile from string, no importer given, supposed to try the current directory.
// but the child process's CWD could be different to ours - so we can't let it resolve.
private func findImplicitImporter(importer: ImportResolver?) -> ImportResolver {
importer ?? .loadPath(URL(fileURLWithPath: FileManager.default.currentDirectoryPath))
}

/// Consider the pending work queue. When we change `state` or add to `pendingCompilations`.`
private func kickPendingCompilations() {
eventLoop.preconditionInEventLoop()
Expand Down
Binary file modified Sources/DartSassEmbeddedLinux/sass_embedded/dart-sass-embedded
Binary file not shown.
Binary file not shown.
21 changes: 13 additions & 8 deletions Tests/DartSassTests/TestImporters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,26 @@ class TestImporters: DartSassTestCase {
XCTAssertEqual(secondaryCssBlue, results.css)
}

// implicit loadpath works
// no implicit loadpath - 1.50.1 spec clarification
func testImplicitLoadPath() throws {
let tmpDir1 = try FileManager.default.createTemporaryDirectory()
let tmpDir2 = try FileManager.default.createTemporaryDirectory()
let tmpDir = try FileManager.default.createTemporaryDirectory()
let filename = "imported.scss"
try "a { a: 'dir1'; }".write(to: tmpDir1.appendingPathComponent(filename))
try "a { a: 'dir2'; }".write(to: tmpDir2.appendingPathComponent(filename))
try "a { a: 'hello'; }".write(to: tmpDir.appendingPathComponent(filename))

try tmpDir1.withCurrentDirectory {
try tmpDir.withCurrentDirectory {
let compiler = try newCompiler()
try checkCompilerWorking(compiler) // make sure child process is actually started...
try tmpDir2.withCurrentDirectory {
do {
let results = try compiler.compile(string: "@import 'imported';", outputStyle: .compressed)
XCTAssertEqual(#"a{a:"dir2"}"#, results.css)
XCTFail("Managed to resolve import: \(results)")
} catch {
print(error)
}

let results = try compiler.compile(string: "@import 'imported';",
outputStyle: .compressed,
importers: [.loadPath(tmpDir.absoluteURL)])
XCTAssertEqual(#"a{a:"hello"}"#, results.css)
}
}

Expand Down
2 changes: 1 addition & 1 deletion VERSION_DART_SASS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.49.10
1.50.1

0 comments on commit 2d59bbf

Please sign in to comment.