Make import paths work right

This commit is contained in:
Reid 'arrdem' McKenzie 2023-05-27 18:55:38 -06:00
parent 2dfb2ca574
commit f1ea2cb645

View file

@ -33,12 +33,12 @@ https://github.com/dart-lang/rules_dart).
SassInfo = provider( SassInfo = provider(
doc = "Collects files from sass_library for use in downstream sass_binary", doc = "Collects files from sass_library for use in downstream sass_binary",
fields = { fields = {
"transitive_includes": "Directories to be treated as @use/@import search paths",
"transitive_sources": "Sass sources for this target and its dependencies", "transitive_sources": "Sass sources for this target and its dependencies",
}, },
) )
def _collect_transitive_sources(srcs, deps): def _collect_transitive_sources(srcs, deps):
"Sass compilation requires all transitive .sass source files"
return depset( return depset(
srcs, srcs,
transitive = [dep[SassInfo].transitive_sources for dep in deps], transitive = [dep[SassInfo].transitive_sources for dep in deps],
@ -46,6 +46,13 @@ def _collect_transitive_sources(srcs, deps):
order = "postorder", order = "postorder",
) )
def _collect_transitive_imports(imports, deps):
return depset(
imports,
transitive = [dep[SassInfo].transitive_includes for dep in deps],
order = "postorder",
)
def _sass_library_impl(ctx): def _sass_library_impl(ctx):
"""sass_library collects all transitive sources for given srcs and deps. """sass_library collects all transitive sources for given srcs and deps.
@ -61,8 +68,16 @@ def _sass_library_impl(ctx):
ctx.files.srcs, ctx.files.srcs,
ctx.attr.deps, ctx.attr.deps,
) )
transitive_includes = _collect_transitive_imports(
[ctx.label.package] + ctx.attr.imports,
ctx.attr.deps,
)
return [ return [
SassInfo(transitive_sources = transitive_sources), SassInfo(
transitive_sources = transitive_sources,
transitive_includes = transitive_includes,
),
DefaultInfo( DefaultInfo(
files = transitive_sources, files = transitive_sources,
runfiles = ctx.runfiles(transitive_files = transitive_sources), runfiles = ctx.runfiles(transitive_files = transitive_sources),
@ -84,11 +99,8 @@ def _run_sass(ctx, input, css_output, map_output = None):
if ctx.attr.sourcemap_embed_sources: if ctx.attr.sourcemap_embed_sources:
args.add("--sourcemap-contents") args.add("--sourcemap-contents")
# Sources for compilation may exist in the source tree, in bazel-bin, or bazel-genfiles. for include_path in _collect_transitive_imports([ctx.label.package], ctx.attr.deps).to_list():
for prefix in [".", ctx.var["BINDIR"], ctx.var["GENDIR"]]: args.add("--include-path=%s/" % include_path)
args.add("--include-path=%s/" % prefix)
for include_path in ctx.attr.include_paths:
args.add("--include-path=%s/%s" % (prefix, include_path))
# Last arguments are input and output paths # Last arguments are input and output paths
# Note that the sourcemap is implicitly written to a path the same as the # Note that the sourcemap is implicitly written to a path the same as the
@ -163,6 +175,9 @@ _sass_library_attrs = {
allow_empty = False, allow_empty = False,
mandatory = True, mandatory = True,
), ),
"imports": attr.string_list(
doc = "Import prefixes",
),
"deps": sass_deps_attr, "deps": sass_deps_attr,
} }
@ -179,6 +194,9 @@ _sass_binary_attrs = {
mandatory = True, mandatory = True,
allow_single_file = _ALLOWED_SRC_FILE_EXTENSIONS, allow_single_file = _ALLOWED_SRC_FILE_EXTENSIONS,
), ),
"imports": attr.string_list(
doc = "Import prefixes",
),
"sourcemap": attr.bool( "sourcemap": attr.bool(
default = True, default = True,
doc = "Whether source maps should be emitted.", doc = "Whether source maps should be emitted.",