Skip to content

Commit

Permalink
fix(frontend): resolve path to css imported from node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Apr 9, 2024
1 parent 97cfaeb commit b369ced
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/frontend/lib/lightningcss.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import { bundleAsync } from "lightningcss";

const require = createRequire(import.meta.url);

/**
* Resolve path to file in `node_modules`
* @param {string} filePath - File path
* @returns {string} Resolved file path
* @example `@import url("~codemirror/lib/codemirror.css");`
*/
function resolveModuleFilePath(filePath) {
if (filePath.includes("~")) {
const moduleFilePath = filePath.split("~")[1];
return require.resolve(moduleFilePath);
}

return filePath;
}

let { code } = await bundleAsync({
filename: fileURLToPath(new URL("../styles/app.css", import.meta.url)),
minify: true,
resolver: {
read(filePath) {
filePath = resolveModuleFilePath(filePath);
return fs.readFileSync(filePath, "utf8");
},
},
});

export const styles = () => code.toString();
2 changes: 1 addition & 1 deletion packages/frontend/styles/vendor/codemirror.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import url("../../../../node_modules/codemirror/lib/codemirror.css");
@import url("~codemirror/lib/codemirror.css");

.CodeMirror {
--fieldset-flow-space: 0;
Expand Down

0 comments on commit b369ced

Please sign in to comment.