Skip to content

Commit e92e817

Browse files
author
Michael Vurchio
committed
Set initial preprocessor
1 parent 15cab74 commit e92e817

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 micantoine
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

index.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const path = require('path')
2+
const { interpolateName } = require('loader-utils')
3+
4+
const pluginOptions = {
5+
includePaths: [],
6+
localIdentName: '[local]-[hash:base64:6]'
7+
}
8+
9+
const regex = {
10+
module: /\$style\.(:?[\w\d-]*)/gm,
11+
style: /<style(\s[^]*?)?>([^]*?)<\/style>/gi
12+
};
13+
14+
function generateName(resourcePath, styles, className) {
15+
const filePath = resourcePath
16+
const fileName = path.basename(filePath)
17+
const localName = pluginOptions.localIdentName.length
18+
? pluginOptions.localIdentName.replace(/\[local\]/gi, () => className)
19+
: className
20+
21+
const content = `${styles}-${filePath}-${fileName}-${className}`
22+
23+
let interpolatedName = interpolateName({ resourcePath }, localName, { content })
24+
25+
// prevent class error when the generated classname starts from a non word charater
26+
if (/^(?![a-zA-Z_])/.test(interpolatedName)) {
27+
interpolatedName = `_${interpolatedName}`
28+
}
29+
30+
// prevent svelte "Unused CSS selector" warning when the generated classname ends by `-`
31+
if (interpolatedName.slice(-1) === '-') {
32+
interpolatedName = interpolatedName.slice(0, -1)
33+
}
34+
35+
return interpolatedName
36+
}
37+
38+
const markup = async ({ content, filename }) => {
39+
const code = content;
40+
41+
if (pluginOptions.includePaths.length) {
42+
for (const includePath of pluginOptions.includePaths) {
43+
if (filename.indexOf(path.resolve(__dirname, includePath)) === -1) {
44+
return { code };
45+
}
46+
}
47+
}
48+
49+
if (!regex.module.test(content)) {
50+
return { code };
51+
}
52+
53+
const styles = content.match(regex.style);
54+
let parsedStyles = null;
55+
56+
let parsedSource = content.replace(regex.module, (match, className) => {
57+
let replacement = '';
58+
59+
if (styles.length) {
60+
const classRegex = new RegExp(`\\.(${className})\\b(?![-_])`, 'gm');
61+
const toBeParsed = parsedStyles ? parsedStyles : styles[0];
62+
63+
if (classRegex.test(toBeParsed)) {
64+
const interpolatedName = generateName(
65+
filename,
66+
styles[0],
67+
className
68+
);
69+
parsedStyles = toBeParsed.replace(
70+
classRegex,
71+
() => `:global(.${interpolatedName})`
72+
);
73+
replacement = interpolatedName;
74+
}
75+
}
76+
return replacement;
77+
});
78+
79+
if (parsedStyles) {
80+
parsedSource = parsedSource.replace(regex.style, parsedStyles);
81+
}
82+
83+
return {
84+
code: parsedSource
85+
}
86+
};
87+
88+
export default (options) => {
89+
for (const option in options) {
90+
pluginOptions[option] = options[option];
91+
}
92+
return {
93+
markup,
94+
}
95+
};

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "svelte-preprocess-cssmodules",
3+
"version": "0.1.0",
4+
"description": "Svelte preprocess to generate CSS Modules classname on Svelte components",
5+
"keywords": [
6+
"svelte",
7+
"svelte-preprocess",
8+
"css-modules"
9+
],
10+
"homepage": "https://github.com/micantoine/svelte-preprocess-cssmodules",
11+
"bugs": {
12+
"url": "https://github.com/micantoine/svelte-preprocess-cssmodules/issues"
13+
},
14+
"author": {
15+
"name": "micantoine"
16+
},
17+
"license": "MIT",
18+
"main": "index.js",
19+
"directories": {
20+
"example": "example"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "https://github.com/micantoine/svelte-preprocess-cssmodules.git"
25+
},
26+
"dependencies": {
27+
"loader-utils": "^1.1.0"
28+
},
29+
"peerDependencies": {
30+
"svelte": ">1.44.0"
31+
},
32+
"files": [
33+
"index.js"
34+
]
35+
}

0 commit comments

Comments
 (0)