Skip to content

Commit

Permalink
Extract shared logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adambrgmn authored and RobinMalfait committed Jun 10, 2022
1 parent b365767 commit 4f2fe1c
Showing 1 changed file with 41 additions and 72 deletions.
113 changes: 41 additions & 72 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,45 @@ async function build() {
return [beforePlugins, afterPlugins, config.options]
}

function loadBuiltinPostcssPlugins() {
let postcss = loadPostcss()
let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
return [
[
(root) => {
root.walkAtRules('import', (rule) => {
if (rule.params.slice(1).startsWith('tailwindcss/')) {
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
rule.remove()
}
})
},
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
(root) => {
root.walkComments((rule) => {
if (rule.text.startsWith(IMPORT_COMMENT)) {
rule.after(
postcss.atRule({
name: 'import',
params: rule.text.replace(IMPORT_COMMENT, ''),
})
)
rule.remove()
}
})
},
],
[],
{},
]
}

function resolveConfig() {
let config = configPath ? require(configPath) : {}

Expand Down Expand Up @@ -568,44 +607,9 @@ async function build() {

tailwindPlugin.postcss = true

let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '

let [beforePlugins, afterPlugins, postcssOptions] = includePostCss
? await loadPostCssPlugins()
: [
[
(root) => {
root.walkAtRules('import', (rule) => {
if (rule.params.slice(1).startsWith('tailwindcss/')) {
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
rule.remove()
}
})
},
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
(root) => {
root.walkComments((rule) => {
if (rule.text.startsWith(IMPORT_COMMENT)) {
rule.after(
postcss.atRule({
name: 'import',
params: rule.text.replace(IMPORT_COMMENT, ''),
})
)
rule.remove()
}
})
},
],
[],
{},
]
: loadBuiltinPostcssPlugins()

let plugins = [
...beforePlugins,
Expand Down Expand Up @@ -705,44 +709,9 @@ async function build() {
return resolveConfig()
}

let postcss = loadPostcss()
let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: '
let [beforePlugins, afterPlugins] = includePostCss
? await loadPostCssPlugins()
: [
[
(root) => {
root.walkAtRules('import', (rule) => {
if (rule.params.slice(1).startsWith('tailwindcss/')) {
rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params }))
rule.remove()
}
})
},
(() => {
try {
return require('postcss-import')
} catch {}

return lazyPostcssImport()
})(),
(root) => {
root.walkComments((rule) => {
if (rule.text.startsWith(IMPORT_COMMENT)) {
rule.after(
postcss.atRule({
name: 'import',
params: rule.text.replace(IMPORT_COMMENT, ''),
})
)
rule.remove()
}
})
},
],
[],
{},
]
: loadBuiltinPostcssPlugins()

let plugins = [
...beforePlugins,
Expand Down

0 comments on commit 4f2fe1c

Please sign in to comment.