Skip to content

Commit

Permalink
Remove explicit postcss require (#456)
Browse files Browse the repository at this point in the history
Fixes #455
  • Loading branch information
oscarotero committed May 6, 2021
1 parent fab1f9a commit de7b657
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
44 changes: 23 additions & 21 deletions index.js
Expand Up @@ -32,7 +32,7 @@ function AtImport(options) {

return {
postcssPlugin: "postcss-import",
Once(styles, { result, atRule }) {
Once(styles, { result, atRule, postcss }) {
const state = {
importedFiles: {},
hashFiles: {},
Expand Down Expand Up @@ -265,28 +265,30 @@ function AtImport(options) {
if (state.hashFiles[content] && state.hashFiles[content][media])
return

return processContent(result, content, filename, options).then(
importedResult => {
const styles = importedResult.root
result.messages = result.messages.concat(
importedResult.messages
)

if (options.skipDuplicates) {
const hasImport = styles.some(child => {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) state.hashFiles[content] = {}
state.hashFiles[content][media] = true
}
return processContent(
result,
content,
filename,
options,
postcss
).then(importedResult => {
const styles = importedResult.root
result.messages = result.messages.concat(importedResult.messages)

if (options.skipDuplicates) {
const hasImport = styles.some(child => {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) state.hashFiles[content] = {}
state.hashFiles[content][media] = true
}

// recursion: import @import from imported file
return parseStyles(result, styles, options, state, media)
}
)

// recursion: import @import from imported file
return parseStyles(result, styles, options, state, media)
})
}
)
}
Expand Down
20 changes: 12 additions & 8 deletions lib/process-content.js
Expand Up @@ -3,13 +3,16 @@
// builtin tooling
const path = require("path")

// external tooling
const postcss = require("postcss")

// placeholder tooling
let sugarss

module.exports = function processContent(result, content, filename, options) {
module.exports = function processContent(
result,
content,
filename,
options,
postcss
) {
const { plugins } = options
const ext = path.extname(filename)

Expand All @@ -22,7 +25,8 @@ module.exports = function processContent(result, content, filename, options) {
sugarss = require("sugarss")
} catch {} // Ignore
}
if (sugarss) return runPostcss(content, filename, plugins, [sugarss])
if (sugarss)
return runPostcss(postcss, content, filename, plugins, [sugarss])
}

// Syntax support:
Expand All @@ -35,10 +39,10 @@ module.exports = function processContent(result, content, filename, options) {
// Try the default as a last resort:
parserList.push(null)

return runPostcss(content, filename, plugins, parserList)
return runPostcss(postcss, content, filename, plugins, parserList)
}

function runPostcss(content, filename, plugins, parsers, index) {
function runPostcss(postcss, content, filename, plugins, parsers, index) {
if (!index) index = 0
return postcss(plugins)
.process(content, {
Expand All @@ -50,6 +54,6 @@ function runPostcss(content, filename, plugins, parsers, index) {
index++
// If there are no parsers left, throw it
if (index === parsers.length) throw err
return runPostcss(content, filename, plugins, parsers, index)
return runPostcss(postcss, content, filename, plugins, parsers, index)
})
}

0 comments on commit de7b657

Please sign in to comment.