postcss-import
Advanced tools
Comparing version 13.0.0 to 14.0.0
@@ -0,1 +1,9 @@ | ||
# 14.0.0 / 2020-12-14 | ||
This release should not have breaking changes for the vast majority of users; only those with `@charset` statements in their CSS _may_ be affected. | ||
- **BREAKING:** Error if multiple incompatible `@charset` statements ([#447](https://github.com/postcss/postcss-import/pull/447)) | ||
- **BREAKING:** Warn if `@charset` statements are not at the top of files ([#447](https://github.com/postcss/postcss-import/pull/447)) | ||
- Fix handing of `@charset` ([#436](https://github.com/postcss/postcss-import/issues/436), [#447](https://github.com/postcss/postcss-import/pull/447)) | ||
# 13.0.0 / 2020-10-20 | ||
@@ -2,0 +10,0 @@ |
61
index.js
@@ -13,14 +13,12 @@ "use strict" | ||
function AtImport(options) { | ||
options = Object.assign( | ||
{ | ||
root: process.cwd(), | ||
path: [], | ||
skipDuplicates: true, | ||
resolve: resolveId, | ||
load: loadContent, | ||
plugins: [], | ||
addModulesDirectories: [], | ||
}, | ||
options | ||
) | ||
options = { | ||
root: process.cwd(), | ||
path: [], | ||
skipDuplicates: true, | ||
resolve: resolveId, | ||
load: loadContent, | ||
plugins: [], | ||
addModulesDirectories: [], | ||
...options, | ||
} | ||
@@ -63,3 +61,3 @@ options.root = path.resolve(options.root) | ||
if (stmt.parent) { | ||
const before = stmt.parent.node.raws.before | ||
const { before } = stmt.parent.node.raws | ||
if (stmt.type === "nodes") stmt.nodes[0].raws.before = before | ||
@@ -81,4 +79,4 @@ else stmt.node.raws.before = before | ||
else { | ||
const nodes = stmt.nodes | ||
const parent = nodes[0].parent | ||
const { nodes } = stmt | ||
const { parent } = nodes[0] | ||
const mediaNode = atRule({ | ||
@@ -115,8 +113,5 @@ name: "media", | ||
bundle.forEach(stmt => { | ||
if (stmt.type === "import") { | ||
if (["charset", "import", "media"].includes(stmt.type)) { | ||
stmt.node.parent = undefined | ||
styles.append(stmt.node) | ||
} else if (stmt.type === "media") { | ||
stmt.node.parent = undefined | ||
styles.append(stmt.node) | ||
} else if (stmt.type === "nodes") { | ||
@@ -159,11 +154,29 @@ stmt.nodes.forEach(node => { | ||
.then(() => { | ||
let charset | ||
const imports = [] | ||
const bundle = [] | ||
function handleCharset(stmt) { | ||
if (!charset) charset = stmt | ||
// charsets aren't case-sensitive, so convert to lower case to compare | ||
else if ( | ||
stmt.node.params.toLowerCase() !== | ||
charset.node.params.toLowerCase() | ||
) { | ||
throw new Error( | ||
`Incompatable @charset statements: | ||
${stmt.node.params} specified in ${stmt.node.source.input.file} | ||
${charset.node.params} specified in ${charset.node.source.input.file}` | ||
) | ||
} | ||
} | ||
// squash statements and their children | ||
statements.forEach(stmt => { | ||
if (stmt.type === "import") { | ||
if (stmt.type === "charset") handleCharset(stmt) | ||
else if (stmt.type === "import") { | ||
if (stmt.children) { | ||
stmt.children.forEach((child, index) => { | ||
if (child.type === "import") imports.push(child) | ||
else if (child.type === "charset") handleCharset(child) | ||
else bundle.push(child) | ||
@@ -179,3 +192,5 @@ // For better output | ||
return imports.concat(bundle) | ||
return charset | ||
? [charset, ...imports.concat(bundle)] | ||
: imports.concat(bundle) | ||
}) | ||
@@ -212,3 +227,3 @@ } | ||
plugin: "postcss-import", | ||
file: file, | ||
file, | ||
parent: sourceFile, | ||
@@ -234,3 +249,3 @@ }) | ||
const atRule = stmt.node | ||
const media = stmt.media | ||
const { media } = stmt | ||
if (options.skipDuplicates) { | ||
@@ -237,0 +252,0 @@ // skip files already imported at the same scope |
@@ -7,3 +7,3 @@ "use strict" | ||
// extended tooling | ||
const stringify = valueParser.stringify | ||
const { stringify } = valueParser | ||
@@ -33,2 +33,3 @@ function split(params, start) { | ||
else if (node.name === "media") stmt = parseMedia(result, node) | ||
else if (node.name === "charset") stmt = parseCharset(result, node) | ||
} | ||
@@ -40,3 +41,3 @@ | ||
type: "nodes", | ||
nodes: nodes, | ||
nodes, | ||
media: [], | ||
@@ -53,3 +54,3 @@ }) | ||
type: "nodes", | ||
nodes: nodes, | ||
nodes, | ||
media: [], | ||
@@ -71,9 +72,23 @@ }) | ||
function parseCharset(result, atRule) { | ||
if (atRule.prev()) { | ||
return result.warn("@charset must precede all other statements", { | ||
node: atRule, | ||
}) | ||
} | ||
return { | ||
type: "charset", | ||
node: atRule, | ||
media: [], | ||
} | ||
} | ||
function parseImport(result, atRule) { | ||
let prev = getPrev(atRule) | ||
let prev = atRule.prev() | ||
if (prev) { | ||
do { | ||
if ( | ||
prev.type !== "atrule" || | ||
(prev.name !== "import" && prev.name !== "charset") | ||
prev.type !== "comment" && | ||
(prev.type !== "atrule" || | ||
(prev.name !== "import" && prev.name !== "charset")) | ||
) { | ||
@@ -84,3 +99,4 @@ return result.warn( | ||
) | ||
} else prev = getPrev(prev) | ||
} | ||
prev = prev.prev() | ||
} while (prev) | ||
@@ -136,9 +152,1 @@ } | ||
} | ||
function getPrev(item) { | ||
let prev = item.prev() | ||
while (prev && prev.type === "comment") { | ||
prev = prev.prev() | ||
} | ||
return prev | ||
} |
@@ -13,3 +13,3 @@ "use strict" | ||
module.exports = function processContent(result, content, filename, options) { | ||
const plugins = options.plugins | ||
const { plugins } = options | ||
const ext = path.extname(filename) | ||
@@ -24,5 +24,3 @@ | ||
sugarss = require("sugarss") | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} catch {} // Ignore | ||
} | ||
@@ -29,0 +27,0 @@ if (sugarss) return runPostcss(content, filename, plugins, [sugarss]) |
@@ -20,3 +20,3 @@ "use strict" | ||
moduleDirectory: moduleDirectories.concat(options.addModulesDirectories), | ||
paths: paths, | ||
paths, | ||
extensions: [".css"], | ||
@@ -23,0 +23,0 @@ packageFilter: function processPackage(pkg) { |
{ | ||
"name": "postcss-import", | ||
"version": "13.0.0", | ||
"version": "14.0.0", | ||
"description": "PostCSS plugin to import CSS files", | ||
@@ -29,10 +29,9 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "^1.0.1", | ||
"eslint": "^5.0.0", | ||
"eslint-config-i-am-meticulous": "^11.0.0", | ||
"eslint-plugin-import": "^2.17.1", | ||
"ava": "^3.0.0", | ||
"eslint": "^7.0.0", | ||
"eslint-config-problems": "^5.0.0", | ||
"eslint-plugin-prettier": "^3.0.0", | ||
"postcss": "^8.0.0", | ||
"postcss-scss": "^3.0.0", | ||
"prettier": "~2.1.0", | ||
"prettier": "~2.2.0", | ||
"sugarss": "^3.0.0" | ||
@@ -50,3 +49,6 @@ }, | ||
"eslintConfig": { | ||
"extends": "eslint-config-i-am-meticulous", | ||
"extends": "eslint-config-problems", | ||
"env": { | ||
"node": true | ||
}, | ||
"plugins": [ | ||
@@ -53,0 +55,0 @@ "prettier" |
37437
8
483