Skip to content

Commit

Permalink
refactor(svelte): <style> tags need type="text/m-css"
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Previous all `<style>` tags would be transformed, now only ones that have `type="text/m-css"` on them will be transformed.
  • Loading branch information
tivac committed Feb 25, 2022
1 parent 41b3199 commit 713b107
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 28 deletions.
9 changes: 3 additions & 6 deletions packages/svelte/svelte.js
Expand Up @@ -11,7 +11,7 @@ const Processor = require("@modular-css/processor");

const replacer = require("./replacer.js");

const styleRegex = /<style[\S\s]*?>([\S\s]*?)<\/style>/im;
const styleRegex = /<style[^>]*?type=['"]text\/m-css['"][^>]*?>([\S\s]+?)<\/style>/im;
const scriptRegex = /<script[\S\s]*?>([\S\s]*?)<\/script>/im;
const missedRegex = /css\.\w+/gim;
const linkRegex = /<link\b[^<>]*?\bhref=\s*(?:"([^"]+)"|'([^']+)'|([^>\s]+))[^>]*>/gm;
Expand Down Expand Up @@ -115,6 +115,8 @@ module.exports = (opts = {}) => {
if(style) {
log("extract <style>", file);

source = source.replace(style[0], "");

css = "<style>";

if(processor.has(filename)) {
Expand Down Expand Up @@ -280,11 +282,6 @@ module.exports = (opts = {}) => {
processor,
preprocess : {
markup,

// Style elements are always stripped out completely
style : () => ({
code : "/* replaced by modular-css */",
}),
},
};
};
Expand Down
31 changes: 23 additions & 8 deletions packages/svelte/test/__snapshots__/svelte.test.js.snap
Expand Up @@ -207,7 +207,7 @@ exports[`/svelte.js should extract CSS from a <style> tag 1`] = `
</div>
</div>
<style>/* replaced by modular-css */</style>
<script>
export default {
Expand Down Expand Up @@ -291,7 +291,7 @@ exports[`/svelte.js should handle errors: empty css file - <style> 3`] = `
"<div class=\\"{\\"css.nope\\"}\\">NOPE</div>
<div class=\\"{\\"css.alsonope\\"}\\">STILL NOPE</div>
<style>/* replaced by modular-css */</style>
"
`;
Expand Down Expand Up @@ -345,7 +345,7 @@ Array [
exports[`/svelte.js should handle errors: invalid reference <script> - <style> 3`] = `
"<h2 class=\\"mc_yup\\">Yup</h2>
<style>/* replaced by modular-css */</style>
<script>
console.log(\\"css.nuhuh\\");
Expand Down Expand Up @@ -400,7 +400,7 @@ exports[`/svelte.js should handle errors: invalid reference template - <style> 3
<h2 class=\\"mc_yup\\">Yup</h2>
<h3 class=\\"{\\"css.alsonope\\"}\\">Also Nope</h3>
<style>/* replaced by modular-css */</style>
"
`;
Expand All @@ -418,6 +418,21 @@ exports[`/svelte.js should ignore <links> that reference a URL 2`] = `
"
`;
exports[`/svelte.js should ignore <style> tags without the text/m-css attribute 1`] = `
"<style>
.no { color: red; }
</style>
"
`;
exports[`/svelte.js should ignore <style> tags without the text/m-css attribute 2`] = `
"/* packages/svelte/test/specimens/style-no-attribute.svelte */
.mc_yes { color: blue; }
"
`;
exports[`/svelte.js should ignore files without <style> blocks 1`] = `
"<h1>Hello</h1>
<script>console.log(\\"output\\")</script>"
Expand All @@ -444,7 +459,7 @@ exports[`/svelte.js should invalidate files before reprocessing (<link>) 4`] = `
`;
exports[`/svelte.js should invalidate files before reprocessing (<style>) 1`] = `
"<style>/* replaced by modular-css */</style>
"
<div class=\\"mc_source\\">Source</div>"
`;
Expand All @@ -454,7 +469,7 @@ exports[`/svelte.js should invalidate files before reprocessing (<style>) 2`] =
`;
exports[`/svelte.js should invalidate files before reprocessing (<style>) 3`] = `
"<style>/* replaced by modular-css */</style>
"
<div class=\\"mc_source\\">Source</div>"
`;
Expand Down Expand Up @@ -720,10 +735,10 @@ exports[`/svelte.js should use modular-css's file resolver 2`] = `
exports[`/svelte.js should wait for files to finish 1`] = `
Array [
"<style>/* replaced by modular-css */</style>
"
entry1
",
"<style>/* replaced by modular-css */</style>
"
entry2
",
]
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/both.svelte
Expand Up @@ -7,7 +7,7 @@

<link rel="stylesheet" href="./external.css">

<style>
<style type="text/m-css">
.flex {
display: flex;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/error-style.svelte
@@ -1,4 +1,4 @@
<style>
<style type="text/m-css">
.fooga { color: #F00; }
.wooga { composes: foog; }
</style>
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/inline-chunking/a.svelte
@@ -1,6 +1,6 @@
<div class="{css.a}">A.SVELTE</div>

<style>
<style type="text/m-css">
.a {
composes: shared from "./shared.css";
composes: adep from "./a-dep.css";
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/inline-chunking/b.svelte
@@ -1,6 +1,6 @@
<div class="{css.b}">B.SVELTE</div>

<style>
<style type="text/m-css">
.b {
composes: shared from "./shared.css";
composes: bdep from "./b-dep.css";
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/invalid-inline-empty.svelte
@@ -1,5 +1,5 @@
<div class="{css.nope}">NOPE</div>
<div class="{css.alsonope}">STILL NOPE</div>

<style>
<style type="text/m-css">
</style>
@@ -1,6 +1,6 @@
<h2 class="{css.yup}">Yup</h2>

<style>
<style type="text/m-css">
.yup { color: red; }
</style>

Expand Down
Expand Up @@ -2,6 +2,6 @@
<h2 class="{css.yup}">Yup</h2>
<h3 class="{css.alsonope}">Also Nope</h3>

<style>
<style type="text/m-css">
.yup { color: red; }
</style>
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/link-component.svelte
Expand Up @@ -3,7 +3,7 @@
</div>


<style>
<style type="text/m-css">
.flex {
display: flex;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/overlapping/entry1.svelte
@@ -1,4 +1,4 @@
<style>
<style type="text/m-css">
.one {
composes: one from "./tier1.css";
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/overlapping/entry2.svelte
@@ -1,4 +1,4 @@
<style>
<style type="text/m-css">
.one {
composes: one from "./tier1.css";
}
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte/test/specimens/style-no-attribute.svelte
@@ -0,0 +1,7 @@
<style>
.no { color: red; }
</style>

<style type="text/m-css">
.yes { color: blue; }
</style>
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/style.svelte
Expand Up @@ -5,7 +5,7 @@
</div>
</div>

<style>
<style type="text/m-css">
.flex {
display: flex;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/test/specimens/unquoted.svelte
Expand Up @@ -7,7 +7,7 @@
But I am!
</div>

<style>
<style type="text/m-css">
.a {
color: red;
}
Expand Down
25 changes: 23 additions & 2 deletions packages/svelte/test/svelte.test.js
Expand Up @@ -36,6 +36,27 @@ describe("/svelte.js", () => {
expect(output.css).toMatchSnapshot();
});

it("should ignore <style> tags without the text/m-css attribute", async () => {
const filename = require.resolve("./specimens/style-no-attribute.svelte");
const { processor, preprocess } = plugin({
namer,
});

const processed = await svelte.preprocess(
fs.readFileSync(filename, "utf8"),
{
...preprocess,
filename,
},
);

expect(processed.toString()).toMatchSnapshot();

const output = await processor.output();

expect(output.css).toMatchSnapshot();
});

it.each([
"style",
"link",
Expand Down Expand Up @@ -365,7 +386,7 @@ describe("/svelte.js", () => {
it("should invalidate files before reprocessing (<style>)", async () => {
// V1 of files
fs.writeFileSync(path.resolve(__dirname, "./output/source.svelte"), dedent(`
<style>.source { color: red; }</style>
<style type="text/m-css">.source { color: red; }</style>
<div class="{css.source}">Source</div>
`));

Expand All @@ -390,7 +411,7 @@ describe("/svelte.js", () => {

// V2 of CSS
fs.writeFileSync(path.resolve(__dirname, "./output/source.svelte"), dedent(`
<style>.source { color: blue; }</style>
<style type="text/m-css">.source { color: blue; }</style>
<div class="{css.source}">Source</div>
`));

Expand Down

0 comments on commit 713b107

Please sign in to comment.