Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve postcss error handling (#31420)

David Taylor834ea70b1ce3
diff --git a/app/assets/javascripts/theme-transpiler/build.js b/app/assets/javascripts/theme-transpiler/build.js
index 00a233424a9..5619ad53120 100644
--- a/app/assets/javascripts/theme-transpiler/build.js
+++ b/app/assets/javascripts/theme-transpiler/build.js
@@ -51,6 +51,7 @@ esbuild
alias: {
util: "./node_modules/@zxing/text-encoding",
path: "path-browserify",
+ url: "./url-polyfill",
"source-map-js": "source-map-js",
},
banner: {
diff --git a/app/assets/javascripts/theme-transpiler/package.json b/app/assets/javascripts/theme-transpiler/package.json
index 82f8ad61a16..d418404d184 100644
--- a/app/assets/javascripts/theme-transpiler/package.json
+++ b/app/assets/javascripts/theme-transpiler/package.json
@@ -13,6 +13,7 @@
"autoprefixer": "^10.4.20",
"babel-plugin-ember-template-compilation": "^2.3.0",
"content-tag": "^3.1.1",
+ "core-js": "^3.40.0",
"decorator-transforms": "^2.3.0",
"discourse": "workspace:0.0.0",
"discourse-widget-hbs": "workspace:1.0.0",
diff --git a/app/assets/javascripts/theme-transpiler/postcss-variable-prefixer.js b/app/assets/javascripts/theme-transpiler/postcss-variable-prefixer.js
index e2a66f0ef31..196adf6762b 100644
--- a/app/assets/javascripts/theme-transpiler/postcss-variable-prefixer.js
+++ b/app/assets/javascripts/theme-transpiler/postcss-variable-prefixer.js
@@ -23,8 +23,8 @@ export default function postcssVariablePrefixer() {
return {
postcssPlugin: "postcss-var-prefixer",
- prepare(result) {
- hash = hashString(result.root.source.input.css);
+ Once(root) {
+ hash = hashString(root.source.input.css);
},
Declaration(declaration) {
diff --git a/app/assets/javascripts/theme-transpiler/postcss.js b/app/assets/javascripts/theme-transpiler/postcss.js
index bdd44b18c5d..3c0036dc993 100644
--- a/app/assets/javascripts/theme-transpiler/postcss.js
+++ b/app/assets/javascripts/theme-transpiler/postcss.js
@@ -1,3 +1,4 @@
+import "core-js/actual/url";
import postcssLightDark from "@csstools/postcss-light-dark-function";
import autoprefixer from "autoprefixer";
import postcss from "postcss";
diff --git a/app/assets/javascripts/theme-transpiler/url-polyfill.js b/app/assets/javascripts/theme-transpiler/url-polyfill.js
new file mode 100644
index 00000000000..f155fa9459c
--- /dev/null
+++ b/app/assets/javascripts/theme-transpiler/url-polyfill.js
@@ -0,0 +1,7 @@
+export function pathToFileURL(path) {
+ return new URL(path, "file://").toString();
+}
+
+export function fileURLToPath(url) {
+ return new URL(url).pathname;
+}
diff --git a/lib/stylesheet/manager/builder.rb b/lib/stylesheet/manager/builder.rb
index 005dabb11ae..ecffb7cbb93 100644
--- a/lib/stylesheet/manager/builder.rb
+++ b/lib/stylesheet/manager/builder.rb
@@ -49,7 +49,7 @@ class Stylesheet::Manager::Builder
load_paths: load_paths,
dark: @dark,
)
- rescue SassC::SyntaxError, SassC::NotRenderedError => e
+ rescue SassC::SyntaxError, SassC::NotRenderedError, DiscourseJsProcessor::TranspileError => e
if Stylesheet::Importer::THEME_TARGETS.include?(@target.to_s)
# no special errors for theme, handled in theme editor
["", nil]
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 11591420299..7603dfc39c6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1030,6 +1030,9 @@ importers:
content-tag:
specifier: ^3.1.1
version: 3.1.1(patch_hash=lgdkxhmahesfzwpl4vwprolz5m)
+ core-js:
+ specifier: ^3.40.0
+ version: 3.40.0
decorator-transforms:
specifier: ^2.3.0
version: 2.3.0(@babel/[email protected])
diff --git a/spec/lib/stylesheet/compiler_spec.rb b/spec/lib/stylesheet/compiler_spec.rb
index d9c7d9ffab5..1ddc16c31dc 100644
--- a/spec/lib/stylesheet/compiler_spec.rb
+++ b/spec/lib/stylesheet/compiler_spec.rb
@@ -245,5 +245,19 @@ RSpec.describe Stylesheet::Compiler do
expect(css).to include("csstools-light-dark-toggle")
expect(map.size).to be > 10
end
+
+ it "handles errors gracefully" do
+ bad_css = <<~SCSS
+ $foo: unquote("https://notacolor.example.com");
+ .example {
+ color: $foo;
+ }
+ SCSS
+
+ expect { Stylesheet::Compiler.compile(bad_css, "test.scss") }.to raise_error(
+ DiscourseJsProcessor::TranspileError,
+ /Missed semicolon/,
+ )
+ end
end
end