Skip to content

Commit

Permalink
fix: Add test & fix #982
Browse files Browse the repository at this point in the history
Make sure that `external` for each value stays legit instead of just splatting the entire value over the top.
  • Loading branch information
tivac committed Oct 26, 2023
1 parent da0e3c3 commit 4a54a80
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
19 changes: 19 additions & 0 deletions packages/css-to-js/test/__snapshots__/api.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ export {
};
`;
exports[`@modular-css/css-to-js API should represent external @values aliased to local @values: code 1`] = `
import { $values as $aValues } from "<ROOT-DIR>/a.css";
const v = "#00F";
const $values = {
"values" : $aValues,
v,
};
const b = "mc71966a67_b";
export default {
$values,
b
};
export {
$values,
b
};
`;
exports[`@modular-css/css-to-js API should represent external @values namespaces: code 1`] = `
import { $values as $aValues } from "<ROOT-DIR>/a.css";
const v = "#0F0";
Expand Down
18 changes: 18 additions & 0 deletions packages/css-to-js/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ describe("@modular-css/css-to-js API", () => {
expect(code).toMatchSnapshot("code");
expect(namedExports).toEqual([ "$values", "b" ]);
});

it("should represent external @values aliased to local @values", async () => {
const processor = new Processor({ resolvers });

await processor.string("./a.css", `@value v1: #00F; @value v2: #F00; `);
await processor.string("./b.css", dedent(`
@value * as values from "./a.css";
@value v: values.v1;
.b {
border-color: v;
}
`));

const { code } = transform("./b.css", processor);

expect(code).toMatchSnapshot("code");
});


it("should generate javascript from composes", async () => {
Expand Down
16 changes: 11 additions & 5 deletions packages/processor/plugins/before/values-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ module.exports = () => ({
return;
}

// Simple references to existing values are handled as object references,
// so they're always kept up-to-date
// Simple reference to an existing value
if(values[details.value]) {
values[details.name] = values[details.value];
values[details.name] = {
...values[details.value],
source : rule.source,
external : false,
};

// console.log("values-local after", values[details.name]);
} else {
// Otherwise need to walk @value body and check for any replacments to make
const parsed = value(details.value);
Expand All @@ -47,8 +52,9 @@ module.exports = () => ({
});

values[details.name] = {
value : parsed.toString(),
source : rule.source,
value : parsed.toString(),
source : rule.source,
external : false,
};
}

Expand Down
7 changes: 5 additions & 2 deletions packages/processor/plugins/values-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,12 @@ module.exports = () => ({
// Update any references that might've been affected by imports
for(const name of Object.keys(values)) {
const { value } = values[name];

if(value in values) {
values[name] = values[value];
values[name] = {
...values[value],
external : values[name].external,
};
}
}
},
Expand Down

0 comments on commit 4a54a80

Please sign in to comment.