postcss-syntax
Advanced tools
Comparing version 0.28.0 to 0.30.0
58
index.js
"use strict"; | ||
const stringify = require("./stringify"); | ||
const parse = require("./parse"); | ||
const defaultConfig = { | ||
rules: [ | ||
{ | ||
test: /\.less$/i, | ||
lang: "less", | ||
}, | ||
{ | ||
test: /\.sass$/i, | ||
lang: "sass", | ||
}, | ||
{ | ||
test: /\.scss$/i, | ||
lang: "scss", | ||
}, | ||
{ | ||
test: /\.s(?:ugar)?ss$/i, | ||
lang: "sugarss", | ||
}, | ||
{ | ||
test: /\.styl(?:us)?$/i, | ||
lang: "stylus", | ||
}, | ||
{ | ||
// WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html | ||
// acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss | ||
// `*.pcss`, `*.postcss` | ||
test: /\.(?:wx|\w*c)ss$/i, | ||
lang: "css", | ||
}, | ||
{ | ||
// *.xslt? https://msdn.microsoft.com/en-us/library/ms764661(v=vs.85).aspx | ||
// *.vue https://vue-loader.vuejs.org/spec.html | ||
// *.ux https://doc.quickapp.cn/framework/source-file.html | ||
// `*.xml` Just for fault tolerance, XML is not supported except XSLT | ||
// `*.htm`, `*.*htm` | ||
// `*.html`, `*.*html` | ||
test: /\.(?:\w*html?|x(?:ht|ml|slt?)|markdown|md|php|vue|ux)$/i, | ||
extract: "html", | ||
}, | ||
{ | ||
test: /\.(?:markdown|md)$/i, | ||
extract: "markdown", | ||
}, | ||
{ | ||
test: /\.(?:m?[jt]sx?|es\d*|pac)$/i, | ||
extract: "jsx", | ||
}, | ||
], | ||
postcss: "css", | ||
stylus: "css", | ||
babel: "jsx", | ||
xml: "html", | ||
xsl: "html", | ||
}; | ||
try { | ||
require.resolve("postcss-jsx"); | ||
} catch (ex) { | ||
defaultConfig.jsx = "styled"; | ||
} | ||
function initSyntax (syntax) { | ||
@@ -58,0 +20,0 @@ syntax.stringify = stringify.bind(syntax); |
{ | ||
"name": "postcss-syntax", | ||
"version": "0.28.0", | ||
"version": "0.30.0", | ||
"description": "Automatically switch PostCSS syntax by file extensions", | ||
@@ -42,10 +42,11 @@ "repository": { | ||
"postcss": "^6.0.22", | ||
"postcss-html": ">=0.28.0", | ||
"postcss-jsx": ">=0.28.0", | ||
"postcss-html": ">=0.30.0", | ||
"postcss-jsx": ">=0.30.0", | ||
"postcss-less": "^2.0.0", | ||
"postcss-markdown": ">=0.28.0", | ||
"postcss-markdown": ">=0.30.0", | ||
"postcss-safe-parser": "^3.0.1", | ||
"postcss-scss": "^1.0.5", | ||
"proxyquire": "^2.0.1", | ||
"sugarss": "^1.0.1" | ||
} | ||
} |
19
parse.js
@@ -5,2 +5,3 @@ "use strict"; | ||
const processor = require("./processor"); | ||
const getLang = require("./get-lang"); | ||
@@ -14,11 +15,17 @@ function parse (source, opts) { | ||
} | ||
let rules = opts.syntax.config.rules; | ||
const file = opts.from ? opts.from.replace(/^(\w+:\/\/.*?\.\w+)(?:[?#].*)?$/, "$1") : ""; | ||
rules = rules && rules.filter( | ||
rule => rule.test.test(file) | ||
); | ||
source = source.toString(); | ||
return processor(source, rules, opts) || parser(source, rules, opts); | ||
const syntax = getLang(opts, source); | ||
const syntaxOpts = Object.assign({}, opts, syntax.opts); | ||
let root; | ||
if (syntax.extract) { | ||
root = processor(source, syntax.extract, syntaxOpts); | ||
root.source.lang = syntax.extract; | ||
} else { | ||
root = parser(source, syntax.lang, syntaxOpts); | ||
root.source.lang = syntax.lang; | ||
} | ||
return root; | ||
} | ||
module.exports = parse; |
@@ -6,32 +6,4 @@ "use strict"; | ||
const extToLang = { | ||
"sugarss": /^s(?:ugar)?ss$/i, | ||
"stylus": /^styl(?:us)?$/i, | ||
// WXSS(WeiXin Style Sheets) See: https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html | ||
// acss(AntFinancial Style Sheet) See: https://docs.alipay.com/mini/framework/acss | ||
// `*.pcss`, `*.postcss` | ||
"css": /^(?:wx|\w*c)ss$/i, | ||
"html": /^(?:[sx]?html?|[sx]ht|vue|ux|php)$/i, | ||
"markdown": /^(?:markdown|md)$/i, | ||
"jsx": /^(?:m?[jt]sx?|es\d*|pac)$/i, | ||
}; | ||
function parser (source, rules, opts) { | ||
function parser (source, lang, opts) { | ||
patch(); | ||
let lang = rules.find(rule => rule.lang); | ||
if (lang) { | ||
lang = lang.lang; | ||
} else if (opts.from && /\.(\w+)$/.test(opts.from)) { | ||
lang = RegExp.$1.toLowerCase(); | ||
if (!/^(?:css|less|sass|scss)$/i.test(lang)) { | ||
for (const langName in extToLang) { | ||
if (extToLang[langName].test(lang)) { | ||
lang = langName; | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
lang = "css"; | ||
} | ||
@@ -38,0 +10,0 @@ const syntax = getSyntax(lang, opts); |
@@ -5,21 +5,23 @@ "use strict"; | ||
function processor (source, rules, opts) { | ||
rules = rules && rules.filter(rule => rule.extract).map(rule => { | ||
if (typeof rule.extract === "string") { | ||
rule.extract = rule.extract.toLowerCase().replace(/^(postcss-)?/i, "postcss-"); | ||
rule.extract = require(rule.extract + "/extract"); | ||
} | ||
return rule; | ||
}); | ||
function getSyntax (config, syntax) { | ||
if (typeof syntax !== "string") { | ||
return syntax; | ||
} | ||
let syntaxConfig = config[syntax]; | ||
if (!rules || !rules.length) { | ||
return; | ||
if (syntaxConfig) { | ||
syntaxConfig = getSyntax(config, syntaxConfig); | ||
} else { | ||
syntaxConfig = { | ||
extract: require(syntax.toLowerCase().replace(/^(postcss-)?/i, "postcss-") + "/extract"), | ||
}; | ||
config[syntax] = syntaxConfig; | ||
} | ||
const styles = rules.reduce( | ||
(styles, rule) => ( | ||
rule.extract(source, Object.assign({}, opts, rule.opts), styles) || styles | ||
), | ||
[] | ||
); | ||
return syntaxConfig; | ||
} | ||
function processor (source, lang, opts) { | ||
const syntax = getSyntax(opts.syntax.config, lang); | ||
const styles = (syntax.extract || syntax)(source, opts) || []; | ||
return parseStyle(source, opts, styles); | ||
@@ -26,0 +28,0 @@ } |
@@ -14,3 +14,3 @@ PostCSS Syntax | ||
postcss-syntax can automatically switch the required [PostCSS](https://github.com/postcss/postcss) syntax by file extensions | ||
postcss-syntax can automatically switch the required [PostCSS](https://github.com/postcss/postcss) syntax by file extension/source | ||
@@ -17,0 +17,0 @@ ## Getting Started |
@@ -5,6 +5,9 @@ "use strict"; | ||
module.exports = (extract, defaultConfig) => { | ||
defaultConfig = defaultConfig || { | ||
module.exports = (extract, lang) => { | ||
const defaultConfig = { | ||
postcss: "css", | ||
stylus: "css", | ||
babel: "jsx", | ||
xml: "html", | ||
xsl: "html", | ||
}; | ||
@@ -19,3 +22,5 @@ function parse (source, opts) { | ||
} | ||
return parseStyle(source, opts, extract(source, opts)); | ||
const document = parseStyle(source, opts, extract(source, opts)); | ||
document.source.lang = lang; | ||
return document; | ||
} | ||
@@ -22,0 +27,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
19778
15
545
13