Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS Modules #11

Merged
merged 22 commits into from
May 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rebase compat with constructable stylesheets
thescientist13 committed May 24, 2024
commit 829ab146b3160bfa3d331819b051ebfd9886aefe
35 changes: 28 additions & 7 deletions patches/@greenwood+cli+0.30.0-alpha.2.patch
Original file line number Diff line number Diff line change
@@ -71,10 +71,13 @@ index 6201ea8..855c3fd 100644
}

diff --git a/node_modules/@greenwood/cli/src/loader.js b/node_modules/@greenwood/cli/src/loader.js
index 5d347bc..5cefb92 100644
index 5d347bc..99e3cff 100644
--- a/node_modules/@greenwood/cli/src/loader.js
+++ b/node_modules/@greenwood/cli/src/loader.js
@@ -3,7 +3,8 @@ import { readAndMergeConfig as initConfig } from './lifecycles/config.js';
@@ -1,9 +1,11 @@
import { readAndMergeConfig as initConfig } from './lifecycles/config.js';
+import { mergeResponse } from './lib/resource-utils.js';

const config = await initConfig();
const resourcePlugins = config.plugins.filter(plugin => plugin.type === 'resource').map(plugin => plugin.provider({
context: {
@@ -84,7 +87,7 @@ index 5d347bc..5cefb92 100644
},
config: {
devServer: {}
@@ -11,22 +12,37 @@ const resourcePlugins = config.plugins.filter(plugin => plugin.type === 'resourc
@@ -11,22 +13,37 @@ const resourcePlugins = config.plugins.filter(plugin => plugin.type === 'resourc
graph: []
}));

@@ -104,13 +107,13 @@ index 5d347bc..5cefb92 100644

+
+ for (const plugin of resourcePlugins) {
+ if (initUrl.protocol === 'file:' && plugin.shouldResolve && await plugin.shouldResolve(url, request)) {
+ if (initUrl.protocol === 'file:' && plugin.shouldResolve && await plugin.shouldResolve(initUrl, request)) {
+ shouldHandle = true;
+
+ if (!checkOnly) {
+ request = await plugin.resolve(url, request);
+ // request = await plugin.resolve(url, request);
+ // console.log('checking....', request.url);
+ // url = request.url;
+ url = new URL((await plugin.resolve(url, request)).url);
+ }
+ }
+ }
@@ -122,7 +125,25 @@ index 5d347bc..5cefb92 100644

if (!checkOnly) {
- response = await plugin.serve(url, request);
+ response = await plugin.serve(initUrl, request);
+ response = mergeResponse(response, await plugin.serve(initUrl, request));
}
}
}
@@ -36,7 +53,7 @@ async function getCustomLoaderResponse(url, checkOnly = false) {
shouldHandle = true;

if (!checkOnly) {
- response = await plugin.preIntercept(url, request, response.clone());
+ response = mergeResponse(response, await plugin.preIntercept(url, request, response.clone()));
}
}

@@ -44,7 +61,7 @@ async function getCustomLoaderResponse(url, checkOnly = false) {
shouldHandle = true;

if (!checkOnly) {
- response = await plugin.intercept(url, request, response.clone());
+ response = mergeResponse(response, await plugin.intercept(url, request, response.clone()));
}
}
}
5 changes: 3 additions & 2 deletions plugin-css-modules.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import { ResourceInterface } from "@greenwood/cli/src/lib/resource-interface.js"
import * as acornWalk from "acorn-walk";
import * as acorn from "acorn";
import { hashString } from "@greenwood/cli/src/lib/hashing-utils.js";
import { importAttributes } from 'acorn-import-attributes'; // comes from Greenwood

function getCssModulesMap(compilation) {
const locationUrl = new URL("./__css-modules-map.json", compilation.context.scratchDir);
@@ -26,7 +27,7 @@ function walkAllImportsForCssModules(scriptUrl, sheets, compilation) {
const scriptContents = fs.readFileSync(scriptUrl, "utf-8");

acornWalk.simple(
acorn.parse(scriptContents, {
acorn.Parser.extend(importAttributes).parse(scriptContents, {
ecmaVersion: "2020",
sourceType: "module",
}),
@@ -236,7 +237,7 @@ class CssModulesResource extends ResourceInterface {
let contents = await response.clone().text();

acornWalk.simple(
acorn.parse(contents, {
acorn.Parser.extend(importAttributes).parse(contents, {
ecmaVersion: "2020",
sourceType: "module",
}),
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ function transformConstructableStylesheetsPlugin() {
name: "transform-css-module-scripts",
enforce: "pre",
resolveId: (id, importer) => {
if (importer?.indexOf("/src/components/") >= 0 && id.endsWith(".css")) {
if (importer?.indexOf("/src/components/") >= 0 && id.endsWith(".css") && !id.endsWith(".module.css")) {
// add .type so CSS modules are not precessed by the default pipeline
return path.join(path.dirname(importer), `${id}.type`);
}