Skip to content

Commit c8114ce

Browse files
committedNov 24, 2017
initial commit
0 parents  commit c8114ce

13 files changed

+88060
-0
lines changed
 

‎.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
trim_trailing_whitespace = true
8+
end_of_line = lf
9+
charset = utf-8
10+
insert_final_newline = true

‎.gitattributes

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain
23+
*.md diss=astextplain

‎.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# lockfiles
2+
package-lock.json
3+
yarn.lock
4+
5+
# temp stuff
6+
tmp/
7+
*.tmp
8+
*.bak
9+
10+
# logs
11+
*.stackdump
12+
*.log
13+
14+
# Build
15+
node_modules/
16+
17+
# Windows crap
18+
Thumbs.db
19+
Desktop.ini
20+
21+
# Mac crap
22+
.DS_Store

‎.stylelintrc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# https://github.com/stylelint/stylelint/blob/master/docs/user-guide/rules.md
2+
# https://github.com/stylelint/stylelint-config-standard/blob/master/index.js
3+
4+
extends: stylelint-config-standard
5+
ignoreFiles: "**/*.min.css"
6+
7+
rules:
8+
at-rule-empty-line-before: null
9+
block-no-empty: null
10+
block-opening-brace-space-before: null
11+
color-hex-case: null
12+
color-named: null
13+
comment-empty-line-before: null
14+
comment-no-empty: null
15+
comment-whitespace-inside: null
16+
declaration-bang-space-before: null
17+
declaration-block-no-duplicate-properties: null
18+
declaration-block-single-line-max-declarations: null
19+
declaration-colon-newline-after: null
20+
font-family-name-quotes: always-where-recommended
21+
font-family-no-duplicate-names: true
22+
function-url-quotes: always
23+
indentation: null
24+
max-empty-lines: 0
25+
number-leading-zero: never
26+
number-max-precision: 3
27+
number-no-trailing-zeros: true
28+
rule-empty-line-before: null
29+
selector-combinator-space-after: null
30+
selector-combinator-space-before: null
31+
selector-list-comma-newline-after: null
32+
selector-pseudo-element-colon-notation: single
33+
selector-type-no-unknown: null
34+
string-quotes: double
35+
value-list-comma-newline-after: null

‎README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Stylelint Bundle
2+
3+
This repository modifies Stylelint & creates a bundle:
4+
5+
* To allow bundling of the code.
6+
* To reduce the file size of the standalone version.
7+
* It removes excessive code to make it efficient to use with the [Stylus](https://github.com/openstyles/stylus) browser extension.
8+
* Using `browserify -r stylelint -o stylelint-bundle.js`:
9+
* Bundle size before build: `3,424 kB`.
10+
* Bundle size after build: `2,571 kB` (`867 kB` minified)
11+
12+
## Create the bundle
13+
14+
* Download or clone this repository.
15+
* Run `npm install`
16+
* Run `npm run build`
17+
* The `stylelint-bundle.js` and `stylelint-bundle.min.js` are updated using the installed version of Stylelint.
18+
19+
* Test the result by running `npm test`.
20+
21+
## Limitations
22+
23+
The resulting bundle:
24+
25+
* Exposes the standalone version of Stylelint.
26+
* It does not work from the command line:
27+
* All code that uses the node file system (`fs`) or path (`path`) are bypassed or removed.
28+
* All command-line interface (CLI) code is bypassed or removed.
29+
* Vendor prefixed rules and formatters have been removed. Including the following rules:
30+
* `at-rule-no-vendor-prefix`.
31+
* `media-feature-name-no-vendor-prefix`.
32+
* `property-no-vendor-prefix`.
33+
* `selector-no-vendor-prefix`.
34+
* `value-no-vendor-prefix`.
35+
36+
## Usage
37+
38+
* Within your HTML page, load the bundle.
39+
40+
```html
41+
<script src="stylelint-bundle.min.js"></script>
42+
```
43+
44+
* Use `require` to load the bundled script, then access the `lint` function:
45+
46+
```js
47+
const stylelint = require("stylelint");
48+
stylelint.lint({
49+
code: "body { color: #000; }",
50+
config: {
51+
syntax: 'sugarss',
52+
rules: { /*...*/ },
53+
formatter: "string"
54+
}
55+
}).then(({results}) => {
56+
console.log(results[0]);
57+
});
58+
```
59+
60+
To get more details, including all the options and return promise values, see the [stylelint Node API](https://stylelint.io/user-guide/node-api/) page; but, don't forget the limitations of this bundle!

‎build/files.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
function readFile(name) {
6+
return new Promise((resolve, reject) => {
7+
fs.readFile(name, "utf8", (err, file) => {
8+
if (err) {
9+
return reject(err);
10+
}
11+
resolve(file);
12+
});
13+
});
14+
}
15+
16+
function writeFile(name, obj) {
17+
return new Promise((resolve, reject) => {
18+
fs.writeFile(name, obj, "utf8", err => {
19+
if (err) {
20+
console.log(`Error writing ${name}`, err);
21+
return reject();
22+
}
23+
resolve();
24+
});
25+
});
26+
}
27+
28+
module.exports = {
29+
readFile,
30+
writeFile
31+
};

‎build/index.js

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"use strict";
2+
3+
const del = require("del");
4+
const {readFile, writeFile} = require("./files");
5+
6+
let maybeCompatible = true;
7+
8+
// Location of the module
9+
const fileLoc = "node_modules/stylelint/";
10+
11+
// Files to delete; to ensure browserify doesn't bundle them
12+
const remove = [
13+
"bin",
14+
"docs",
15+
"*.md",
16+
"lib/**/__tests__",
17+
"lib/rules/**/__tests__",
18+
"lib/testUtils",
19+
"lib/utils/isAutoprefixable.js",
20+
"lib/rules/at-rule-no-vendor-prefix",
21+
"lib/rules/media-feature-name-no-vendor-prefix",
22+
"lib/rules/property-no-vendor-prefix",
23+
"lib/rules/selector-no-vendor-prefix",
24+
"lib/rules/value-no-vendor-prefix",
25+
"lib/formatters/needlessDisablesStringFormatter.js",
26+
"lib/formatters/stringFormatter.js",
27+
"lib/formatters/verboseFormatter.js"
28+
];
29+
30+
// Specific file modifications
31+
// Remove use of "fs", "path" and
32+
// "autoprefixer" - which includes prefixes downloaded from caniuse
33+
const modify = {
34+
"lib/formatters/index.js": file => {
35+
return replaceBlocks(file, [
36+
[
37+
// Replace https://github.com/stylelint/stylelint/blob/master/lib/formatters/index.js#L5-L6
38+
// with empty functions
39+
/(string: require\(\"\.\/stringFormatter"\),\s*verbose: require\("\.\/verboseFormatter"\))/,
40+
" string: () => {},\n verbose: () => {}"
41+
]
42+
]);
43+
},
44+
"lib/index.js": file => {
45+
return commentOut(file, [
46+
"const createRuleTester =",
47+
"api.createRuleTester ="
48+
]);
49+
},
50+
"lib/rules/index.js": file => {
51+
return commentOut(file, [
52+
"const atRuleNoVendorPrefix =",
53+
"const mediaFeatureNameNoVendorPrefix =",
54+
"const propertyNoVendorPrefix =",
55+
"const selectorNoVendorPrefix =",
56+
"const valueNoVendorPrefix =",
57+
"\"at-rule-no-vendor-prefix\"",
58+
"\"media-feature-name-no-vendor-prefix\"",
59+
"\"property-no-vendor-prefix\"",
60+
"\"selector-no-vendor-prefix\"",
61+
"\"value-no-vendor-prefix\""
62+
]);
63+
},
64+
"lib/standalone.js": file => {
65+
file = commentOut(file, [
66+
"const fs = require(\"fs\");",
67+
"const path = require(\"path\");",
68+
"ignoreText = fs.",
69+
"if (readError.code !=="
70+
]);
71+
return replaceBlocks(
72+
file,
73+
[
74+
[
75+
// Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L60-L62
76+
// without using path
77+
/(const absoluteIgnoreFilePath = path\.isAbsolute\(ignoreFilePath\)\s*\? ignoreFilePath\s*: path\.resolve\(process.cwd\(\), ignoreFilePath\);)/,
78+
" const absoluteIgnoreFilePath = ignoreFilePath;"
79+
], [
80+
// Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L110-L113
81+
// without using path
82+
/(const absoluteCodeFilename =\s*codeFilename \!== undefined && \!path\.isAbsolute\(codeFilename\)\s*\? path\.join\(process\.cwd\(\), codeFilename\)\s*: codeFilename;)/,
83+
" const absoluteCodeFilename = codeFilename;"
84+
], [
85+
// Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L127-L133
86+
// immediately resolve
87+
/(fs\.stat\(absoluteCodeFilename, err => {\s*if \(err\) {\s*reject\(\);\s*} else {\s*resolve\(\);\s*}\s*}\);)/,
88+
" resolve();"
89+
], [
90+
// Replace https://github.com/stylelint/stylelint/blob/master/lib/standalone.js#L178-L253
91+
// return empty string
92+
/(if \(useCache\) \{\s*const stylelintVersion =[\s\S]+function prepareReturnValue)/,
93+
" return \"\";\n\n function prepareReturnValue"
94+
]
95+
]
96+
);
97+
},
98+
"package.json": file => {
99+
return file
100+
.replace(/"autoprefixer": ".+",/, "")
101+
.replace(/"chalk": ".+",/, "");
102+
}
103+
};
104+
105+
function commentOut(file, lines) {
106+
lines.forEach(line => {
107+
const index = file.indexOf(line);
108+
if (index > -1) {
109+
file = file
110+
.replace(line, `// ${line}`)
111+
// Don't let these accumulate
112+
.replace("// //", "//");
113+
} else {
114+
maybeCompatible = false;
115+
console.log(`*** Error: Could not comment out "${line}"`);
116+
}
117+
});
118+
return file;
119+
}
120+
121+
// Non-destructive replacement. The block is commented out by adding an
122+
// opening /* and closing */. It does not account for internal block comments!
123+
function replaceBlocks(file, blocks) {
124+
blocks.forEach(block => {
125+
const index = file.search(block[0]);
126+
if (index > -1) {
127+
// Don't comment out blocks that have already been processed
128+
if (file.substring(index - 3, index) !== "/* ") {
129+
file = file.replace(block[0], "/* $1 */\n" + block[1]);
130+
}
131+
} else {
132+
maybeCompatible = false;
133+
console.log(`*** Error: RegExp ${block[0].toString()} did not match anything`);
134+
}
135+
});
136+
return file;
137+
}
138+
139+
Promise.all(remove.map(file => del(fileLoc + file)))
140+
.then(
141+
Promise.all(
142+
Object.keys(modify).map(file => {
143+
readFile(fileLoc + file)
144+
.then(data => modify[file](data))
145+
.then(data => writeFile(fileLoc + file, data));
146+
})
147+
)
148+
)

‎build/post-build.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const pkg = require("../package.json");
4+
const {readFile, writeFile} = require("./files");
5+
6+
const name = "stylelint-bundle.js";
7+
const modComment = `/*!= Stylelint v${pkg.version} bundle =*/\n/* See https://github.com/Mottie/stylelint-bundle */\n`;
8+
9+
readFile(name)
10+
.then(data => {
11+
writeFile(name, modComment + data);
12+
});

‎package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "stylelint-bundle",
3+
"version": "8.2.0",
4+
"description": "Create & provide a bundled version of Stylelint",
5+
"keywords": [
6+
"css",
7+
"less",
8+
"scss",
9+
"sugarss",
10+
"lint",
11+
"linter",
12+
"stylelint",
13+
"bundle",
14+
"browserify"
15+
],
16+
"authors": [
17+
"Rob Garrison"
18+
],
19+
"license": "MIT",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/Mottie/stylelint-bundle.git"
23+
},
24+
"main": "stylelint-bundle.min.js",
25+
"devDependencies": {
26+
"browserify": "^14.5.0",
27+
"del": "^3.0.0",
28+
"mocha": "^4.0.1",
29+
"stylelint": "^8.2.0",
30+
"uglify-es": "^3.1.10"
31+
},
32+
"scripts": {
33+
"build": "node build/index.js && npm run bundle && node build/post-build.js && npm run uglify",
34+
"bundle": "browserify -r stylelint -o stylelint-bundle.js",
35+
"test": "mocha",
36+
"uglify": "uglifyjs ./stylelint-bundle.js --comments \"/^!=/\" -o stylelint-bundle.min.js -c -m"
37+
}
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.