Skip to content

Commit

Permalink
Add resourcePath to fix source map bug (#17)
Browse files Browse the repository at this point in the history
Without specifying a resourcePath for the CSS that’s
extracted from the component template we are seeing
build errors from the resource path starting with encoded <.

This also renames the file parameter in the virtual loader
path to css, which is more clear.
  • Loading branch information
backspace authored Jun 6, 2023
1 parent 75af638 commit f52c8cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion glimmer-scoped-css/src/ast-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const scopedCSSTransform: ASTPluginBuilder<Env> = (env) => {
let encodedCss = encodeURIComponent(btoa(textContent(node)));

jsutils.importForSideEffect(
`style-loader!css-loader!glimmer-scoped-css/virtual-loader?file=${encodedCss}&selector=${dataAttribute}!`
`style-loader!css-loader!glimmer-scoped-css/virtual-loader?path=${env.filename}&css=${encodedCss}&selector=${dataAttribute}!`
);
return null;
} else {
Expand Down
26 changes: 18 additions & 8 deletions glimmer-scoped-css/src/virtual-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@ import postcss from 'postcss';
import scopedStylesPlugin from './postcss-plugin';

export default function virtualLoader(this: LoaderContext<unknown>) {
let encodedCssAndSelectorString = this.loaders[this.loaderIndex]?.options;
let optionsString = this.loaders[this.loaderIndex]?.options;

if (typeof encodedCssAndSelectorString !== 'string') {
if (typeof optionsString !== 'string') {
throw new Error(
`glimmer-scoped-css/src/virtual-loader received unexpected request: ${encodedCssAndSelectorString}`
`glimmer-scoped-css/src/virtual-loader received unexpected request: ${optionsString}`
);
}

let encodedCssAndSelector = new URLSearchParams(encodedCssAndSelectorString);
let options = new URLSearchParams(optionsString);

let encodedCss = encodedCssAndSelector.get('file');
let encodedCss = options.get('css');

if (!encodedCss) {
throw new Error(
`glimmer-scoped-css/src/virtual-loader missing file parameter: ${encodedCssAndSelectorString}`
`glimmer-scoped-css/src/virtual-loader missing file parameter: ${optionsString}`
);
}

let cssSelector = encodedCssAndSelector.get('selector');
let cssSelector = options.get('selector');

if (!cssSelector) {
throw new Error(
`glimmer-scoped-css/src/virtual-loader missing selector parameter: ${encodedCssAndSelectorString}`
`glimmer-scoped-css/src/virtual-loader missing selector parameter: ${optionsString}`
);
}

let path = options.get('path');

if (!path) {
throw new Error(
`glimmer-scoped-css/src/virtual-loader missing path parameter: ${optionsString}`
);
}

let cssSource = atob(decodeURIComponent(encodedCss));
let result = postcss([scopedStylesPlugin(cssSelector)]).process(cssSource);

this.resourcePath = path;

return result.css;
}

0 comments on commit f52c8cc

Please sign in to comment.