Skip to content

Commit d111093

Browse files
authored
support more extensions and js source type (#30)
- exports extensions - add support for more extensions - add support for source type
1 parent 695e0e8 commit d111093

File tree

8 files changed

+183
-117
lines changed

8 files changed

+183
-117
lines changed

get-lang.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
"use strict";
22

3-
// https://github.com/Microsoft/vscode/blob/master/extensions/markdown-basics/package.json
4-
// https://github.com/Microsoft/vscode/blob/master/extensions/html/package.json
5-
// https://github.com/Microsoft/vscode/blob/master/extensions/javascript/package.json
6-
// https://github.com/Microsoft/vscode/blob/master/extensions/typescript-basics/package.json
7-
// https://github.com/Microsoft/vscode/blob/master/extensions/xml/package.json
8-
// https://github.com/Microsoft/vscode/blob/master/extensions/css/package.json
9-
// https://github.com/Microsoft/vscode/blob/master/extensions/less/package.json
10-
// https://github.com/vuejs/vetur/blob/master/package.json
11-
123
const languages = {
134
sass: /^sass$/i,
5+
// https://github.com/Microsoft/vscode/blob/master/extensions/scss/package.json
146
scss: /^scss$/i,
7+
// https://github.com/Microsoft/vscode/blob/master/extensions/less/package.json
158
less: /^less$/i,
9+
// https://github.com/MhMadHamster/vscode-postcss-language/blob/master/package.json
1610
sugarss: /^s(?:ugar)?ss$/i,
11+
// https://github.com/d4rkr00t/language-stylus/blob/master/package.json
1712
stylus: /^styl(?:us)?$/i,
18-
// WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html
19-
// acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss
13+
// WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html
14+
// acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss
2015
// `*.pcss`, `*.postcss`
16+
// https://github.com/Microsoft/vscode/blob/master/extensions/css/package.json
17+
// https://github.com/rcsole/postcss-syntax/blob/master/package.json
2118
css: /^(?:wx|\w*c)ss$/i,
2219
};
2320

2421
const extracts = {
25-
jsx: /^(?:m?[jt]sx?|es\d*|pac)$/i,
22+
// https://github.com/Microsoft/vscode/blob/master/extensions/javascript/package.json
23+
// https://github.com/Microsoft/vscode/blob/master/extensions/typescript-basics/package.json
24+
// https://github.com/michaelgmcd/vscode-language-babel/blob/master/package.json
25+
jsx: /^(?:m?[jt]sx?|es\d*|pac|babel|flow)$/i,
26+
// *.*html? HTML https://github.com/Microsoft/vscode/blob/master/extensions/html/package.json
2627
// *.xslt? XSLT https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx
2728
// *.vue VUE https://vue-loader.vuejs.org/spec.html
28-
// *.ux quickapp https://doc.quickapp.cn/framework/source-file.html
2929
// *.wpy WePY https://github.com/Tencent/wepy/blob/master/docs/md/doc.md#wpy文件说明
30-
html: /^(?:\w*html?|xht|mdoc|jsp|aspx?|volt|ejs|php|vue|ux|wpy)$/i,
30+
// *.ux quickapp https://doc.quickapp.cn/framework/source-file.html
31+
// *.php* PHP https://github.com/Microsoft/vscode/blob/master/extensions/php/package.json
32+
// *.twig Twig https://github.com/mblode/vscode-twig-language/blob/master/package.json
33+
// *.liquid Liquid https://github.com/GingerBear/vscode-liquid/blob/master/package.json
34+
html: /^(?:\w*html?|xht|xslt?|mdoc|jsp|aspx?|volt|ejs|vue|wpy|ux|php\d*|ctp|twig|liquid)$/i,
35+
// https://github.com/Microsoft/vscode/blob/master/extensions/markdown-basics/package.json
3136
markdown: /^(?:m(?:ark)?d(?:ow)?n|mk?d)$/i,
32-
xsl: /^xslt?$/i,
37+
// https://github.com/Microsoft/vscode/blob/master/extensions/xml/package.json
3338
xml: /^(?:xml|xsd|ascx|atom|axml|bpmn|config|cpt|csl|csproj|csproj|user|dita|ditamap|dtd|dtml|fsproj|fxml|iml|isml|jmx|launch|menu|mxml|nuspec|opml|owl|proj|props|pt|publishsettings|pubxml|pubxml|user|rdf|rng|rss|shproj|storyboard|svg|targets|tld|tmx|vbproj|vbproj|user|vcxproj|vcxproj|filters|wsdl|wxi|wxl|wxs|xaml|xbl|xib|xlf|xliff|xpdl|xul|xoml)$/i,
3439
};
3540

@@ -39,21 +44,28 @@ function sourceType (source) {
3944
return;
4045
}
4146
let extract;
42-
if ((/^#!([^\r\n]+)/.test(source) && /(?:^|\s+)(?:ts-)?node(?:\.\w+)?(?:\s+|$)$/.test(RegExp.$1)) || /^("|')use strict\1;*\s*(\r?\n|$)/.test(source) || /^import(?:\s+[^;]+\s+from)?\s+("|')[^'"]+?\1;*\s*(\r?\n|$)/.test(source) || /^(?:(?:var|let|const)\s+[^;]+\s*=)?\s*(?:require|import)\(.+\)/.test(source)) {
47+
if (
48+
// start with strict mode
49+
// start with import code
50+
// start with require code
51+
/^(?:(?:\/\/[^\r\n]*\r?\n|\/\*.*?\*\/)\s*)*(?:(?:("|')use strict\1|import(?:\s+[^;]+\s+from)?\s+("|')[^'"]+?\2)\s*(;|\r?\n|$)|(?:(?:var|let|const)\s+[^;]+\s*=\s*)?(?:require|import)\(.+\))/.test(source) ||
4352
// https://en.wikipedia.org/wiki/Shebang_(Unix)
44-
// or start with strict mode
45-
// or start with import code
53+
(/^#!([^\r\n]+)/.test(source) && /(?:^|\s+|\/)(?:ts-)?node(?:\.\w+)?(?:\s+|$)$/.test(RegExp.$1))
54+
) {
4655
extract = "jsx";
47-
} else if (/^<(?:!DOCTYPE\s+)?html(\s+[^<>]*)?>/i.test(source)) {
56+
} else if (
57+
/^(?:<\?.*?\?>\s*)*<(?:!DOCTYPE\s+)?html(\s+[^<>]*)?>/i.test(source) ||
58+
/^<\?php(?:\s+[\s\S]*)?(?:\?>|$)/.test(source)
59+
) {
4860
extract = "html";
4961
} else if (/^<\?xml(\s+[^<>]*)?\?>/i.test(source)) {
5062
// https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx
5163
if (/<xsl:\w+\b[^<>]*>/.test(source) || /<\/xsl:\w+>/i.test(source)) {
52-
extract = "xsl";
64+
extract = "html";
5365
} else {
5466
extract = "xml";
5567
}
56-
} else if (/^#+\s+\S+/.test(source) || /^\S+[^\r\n]*\r?\n=+(\r?\n|$)/.test(source)) {
68+
} else if (/^(?:#+\s+\S+|\S+[^\r\n]*\r?\n=+(\r?\n|$))/.test(source)) {
5769
extract = "markdown";
5870
} else if (/<(\w+)(?:\s+[^<>]*)?>[\s\S]*?<\/\1>/.test(source)) {
5971
extract = "html";
@@ -91,14 +103,8 @@ function fileType (file) {
91103
}
92104
}
93105

94-
function getLang (opts, source) {
95-
const file = opts.from;
96-
const rules = opts.syntax.config.rules;
97-
return (rules && rules.find(
98-
rule => rule.test.test ? rule.test.test(file) : rule.test(file, source)
99-
)) || fileType(file) || sourceType(source) || {
100-
lang: "css",
101-
};
106+
function getLang (file, source) {
107+
return fileType(file) || sourceType(source);
102108
}
103109

104110
module.exports = getLang;

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const defaultConfig = {
77
stylus: "css",
88
babel: "jsx",
99
xml: "html",
10-
xsl: "html",
1110
};
1211

1312
try {

normal-opts.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
function normalOpts (opts, syntax) {
4+
if (!opts) {
5+
opts = {};
6+
}
7+
opts.syntax = syntax;
8+
return opts;
9+
}
10+
11+
module.exports = normalOpts;

package.json

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-syntax",
3-
"version": "0.30.0",
3+
"version": "0.31.0",
44
"description": "Automatically switch PostCSS syntax by file extensions",
55
"repository": {
66
"type": "git",
@@ -30,6 +30,54 @@
3030
"scripts": {
3131
"test": "nyc mocha --require ./test/fixtures/link --no-timeouts"
3232
},
33+
"extensions": [
34+
".css",
35+
".pcss",
36+
".postcss",
37+
".acss",
38+
".wxss",
39+
".sass",
40+
".scss",
41+
".less",
42+
".sss",
43+
".js",
44+
".es6",
45+
".mjs",
46+
".pac",
47+
".jsx",
48+
".ts",
49+
".tsx",
50+
".babel",
51+
".flow",
52+
".html",
53+
".htm",
54+
".shtml",
55+
".xhtml",
56+
".mdoc",
57+
".jsp",
58+
".asp",
59+
".aspx",
60+
".jshtm",
61+
".volt",
62+
".ejs",
63+
".rhtml",
64+
".xsl",
65+
".xslt",
66+
".vue",
67+
".wpy",
68+
".ux",
69+
".php",
70+
".php4",
71+
".php5",
72+
".phtml",
73+
".ctp",
74+
".twig",
75+
".liquid",
76+
".md",
77+
".mdown",
78+
".markdown",
79+
".markdn"
80+
],
3381
"peerDependencies": {
3482
"postcss": ">=5.0.0"
3583
},
@@ -38,13 +86,13 @@
3886
"codecov": "^3.0.2",
3987
"mocha": "^5.2.0",
4088
"nyc": "^12.0.2",
41-
"postcss": "^6.0.22",
42-
"postcss-html": ">=0.30.0",
43-
"postcss-jsx": ">=0.30.0",
89+
"postcss": "^6.0.23",
90+
"postcss-html": ">=0.31.0",
91+
"postcss-jsx": ">=0.31.0",
4492
"postcss-less": "^2.0.0",
45-
"postcss-markdown": ">=0.30.0",
93+
"postcss-markdown": ">=0.31.0",
4694
"postcss-safe-parser": "^3.0.1",
47-
"postcss-scss": "^1.0.5",
95+
"postcss-scss": "^1.0.6",
4896
"proxyquire": "^2.0.1",
4997
"sugarss": "^1.0.1"
5098
}

parse.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
const parser = require("./parser");
44
const processor = require("./processor");
55
const getLang = require("./get-lang");
6+
const normalOpts = require("./normal-opts");
67

7-
function parse (source, opts) {
8-
if (!opts) {
9-
opts = {};
10-
}
11-
if (!opts.syntax) {
12-
opts.syntax = this;
13-
}
8+
function getSyntax (opts, source) {
9+
const rules = opts.syntax && opts.syntax.config && opts.syntax.config.rules;
10+
const file = opts.from || "";
11+
return (rules && rules.find(
12+
rule => rule.test.test ? rule.test.test(file) : rule.test(file, source)
13+
)) || getLang(file, source) || {
14+
lang: "css",
15+
};
16+
}
1417

18+
function parse (source, opts) {
1519
source = source.toString();
16-
const syntax = getLang(opts, source);
20+
opts = normalOpts(opts, this);
21+
const syntax = getSyntax(opts, source);
1722
const syntaxOpts = Object.assign({}, opts, syntax.opts);
1823
let root;
1924
if (syntax.extract) {

syntax.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
"use strict";
22
const stringify = require("./stringify");
33
const parseStyle = require("./parse-style");
4+
const normalOpts = require("./normal-opts");
45

56
module.exports = (extract, lang) => {
67
const defaultConfig = {
78
postcss: "css",
89
stylus: "css",
910
babel: "jsx",
1011
xml: "html",
11-
xsl: "html",
1212
};
1313
function parse (source, opts) {
1414
source = source.toString();
15-
if (!opts) {
16-
opts = {};
17-
}
18-
if (!opts.syntax) {
19-
opts.syntax = this;
20-
}
15+
opts = normalOpts(opts, this);
2116
const document = parseStyle(source, opts, extract(source, opts));
2217
document.source.lang = lang;
2318
return document;

test/custom.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"use strict";
22
const expect = require("chai").expect;
33
const Module = require("module");
4-
const _findPath = Module._findPath;
4+
let _findPath;
55
let syntax;
66

77
describe("custom language", () => {
88
before(() => {
9+
_findPath = Module._findPath;
10+
911
Module._findPath = (request, paths, isMain) => {
1012
if (request === "postcss-jsx") {
1113
return null;
@@ -16,9 +18,11 @@ describe("custom language", () => {
1618
delete require.cache[require.resolve("../")];
1719
syntax = require("../");
1820
});
21+
1922
after(() => {
20-
Module._findPath = Module._findPath;
23+
Module._findPath = _findPath;
2124
});
25+
2226
it("custom.postcss", () => {
2327
const code = "a { display: block; }";
2428
const document = syntax({

0 commit comments

Comments
 (0)