Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Jan 12, 2024
1 parent 7ad17cc commit a069813
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 31 deletions.
4 changes: 3 additions & 1 deletion integrations/content-resolution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"version": "0.0.0",
"scripts": {
"build": "NODE_ENV=production postcss ./src/index.css -o ./dist/main.css --verbose",
"test": "jest --runInBand --forceExit"
"test": "jest --runInBand --forceExit",
"prewip": "npm run --prefix=../../ build",
"wip": "npx postcss ./src/index.css -o ./dist/main.css"
},
"jest": {
"testTimeout": 10000,
Expand Down
1 change: 1 addition & 0 deletions integrations/content-resolution/src/escapes/(test)/2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="mr-2"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="mr-3"></div>
1 change: 1 addition & 0 deletions integrations/content-resolution/src/escapes/1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="mr-1"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="mr-5"></div>
1 change: 1 addition & 0 deletions integrations/content-resolution/src/escapes/[test]/4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="mr-4"></div>
27 changes: 27 additions & 0 deletions integrations/content-resolution/tests/content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,30 @@ it('it can resolve symlinks for files when relative to the config', async () =>
}
`)
})

it('handles some special glob characters in paths', async () => {
await writeConfigs({
both: {
content: {
relative: true,
files: [
'./src/escapes/1.html',
'./src/escapes/(test)/2.html',
'./src/escapes/(test)/[test]/3.html',
'./src/escapes/[test]/4.html',
'./src/escapes/[test]/(test)/5.html',
],
},
},
})

let result = await build({ cwd: path.resolve(__dirname, '..') })

expect(result.css).toMatchFormattedCss(css`
.mr-1 { margin-right: 0.25rem; }

Check failure on line 308 in integrations/content-resolution/tests/content.test.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·margin-right:·0.25rem;` with `⏎······margin-right:·0.25rem;⏎···`
.mr-2 { margin-right: 0.5rem; }

Check failure on line 309 in integrations/content-resolution/tests/content.test.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·margin-right:·0.5rem;` with `⏎······margin-right:·0.5rem;⏎···`
.mr-3 { margin-right: 0.75rem; }

Check failure on line 310 in integrations/content-resolution/tests/content.test.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·margin-right:·0.75rem;` with `⏎······margin-right:·0.75rem;⏎···`
.mr-4 { margin-right: 1rem; }

Check failure on line 311 in integrations/content-resolution/tests/content.test.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·margin-right:·1rem;` with `⏎······margin-right:·1rem;⏎···`
.mr-5 { margin-right: 1.25rem; }

Check failure on line 312 in integrations/content-resolution/tests/content.test.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `·margin-right:·1.25rem;` with `⏎······margin-right:·1.25rem;⏎···`
`)
})
37 changes: 24 additions & 13 deletions integrations/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ function debounce(fn, ms) {
}
}

function resolveCommandPath(root, command) {
let paths = [
path.resolve(root, 'node_modules', '.bin', command),
path.resolve(root, '..', '..', 'node_modules', '.bin', command),
]

if (path.sep === '\\') {
paths = [
path.resolve(root, 'node_modules', '.bin', `${command}.cmd`),
path.resolve(root, '..', '..', 'node_modules', '.bin', `${command}.cmd`),
...paths,
]
}

for (let filepath of paths) {
if (fs.existsSync(filepath)) {
return filepath
}
}

return `npx ${command}`
}

module.exports = function $(command, options = {}) {
let abortController = new AbortController()
let root = resolveToolRoot()
Expand All @@ -33,19 +56,7 @@ module.exports = function $(command, options = {}) {
command =

Check failure on line 56 in integrations/execute.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `⏎··········command·===·'node'⏎············?·command⏎···········` with `·command·===·'node'·?·command`
command === 'node'
? command
: (function () {
let local = path.resolve(root, 'node_modules', '.bin', command)
if (fs.existsSync(local)) {
return local
}

let hoisted = path.resolve(root, '..', '..', 'node_modules', '.bin', command)
if (fs.existsSync(hoisted)) {
return hoisted
}

return `npx ${command}`
})()
: resolveCommandPath(root, command)
return [command, args]
})()

Expand Down
5 changes: 2 additions & 3 deletions integrations/resolve-tool-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ let path = require('path')

module.exports = function resolveToolRoot() {
let { testPath } = expect.getState()
let separator = '/' // TODO: Does this resolve correctly on windows, or should we use `path.sep` instead.

return path.resolve(
__dirname,
testPath
.replace(__dirname + separator, '')
.split(separator)
.replace(__dirname + path.sep, '')
.split(path.sep)
.shift()
)
}
41 changes: 27 additions & 14 deletions src/lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function normalizePath(path) {
// `/` and `\\` that is _not_ followed by any of the following characters: ()[]
// This is to ensure that we keep the escaping of brackets and parentheses
let segs = path.split(/[/\\]+(?![\(\)\[\]])/)

return prefix + segs.join('/')
}

Expand Down Expand Up @@ -110,6 +111,8 @@ export function parseCandidateFiles(context, tailwindConfig) {
skip: [context.userConfigPath],
})

console.log({ files })

// Normalize the file globs
files = files.filter((filePath) => typeof filePath === 'string')

Expand All @@ -129,15 +132,23 @@ export function parseCandidateFiles(context, tailwindConfig) {

let paths = [...included, ...excluded]

console.log("parseCandidateFiles 0", { paths })

Check failure on line 135 in src/lib/content.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `"parseCandidateFiles·0"` with `'parseCandidateFiles·0'`

// Resolve paths relative to the config file or cwd
paths = resolveRelativePaths(context, paths)

console.log("parseCandidateFiles 1", { paths })

Check failure on line 140 in src/lib/content.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `"parseCandidateFiles·1"` with `'parseCandidateFiles·1'`

// Resolve symlinks if possible
paths = paths.flatMap(resolvePathSymlinks)

console.log("parseCandidateFiles 2", { paths })

Check failure on line 145 in src/lib/content.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `"parseCandidateFiles·2"` with `'parseCandidateFiles·2'`

// Update cached patterns
paths = paths.map(resolveGlobPattern)

console.log("parseCandidateFiles 3", { paths })

Check failure on line 150 in src/lib/content.js

View workflow job for this annotation

GitHub Actions / build (18)

Replace `"parseCandidateFiles·3"` with `'parseCandidateFiles·3'`

return paths
}

Expand Down Expand Up @@ -186,18 +197,13 @@ function parseFilePath(filePath, ignore) {
* @returns {ContentPath}
*/
function resolveGlobPattern(contentPath) {
// This is required for Windows support to properly pick up Glob paths.
// Afaik, this technically shouldn't be needed but there's probably
// some internal, direct path matching with a normalized path in
// a package which can't handle mixed directory separators
let base = normalizePath(contentPath.base)
contentPath.pattern = contentPath.glob
? `${contentPath.base}/${contentPath.glob}`
: contentPath.base

// If the user's file path contains any special characters (like parens) for instance fast-glob
// is like "OOOH SHINY" and treats them as such. So we have to escape the base path to fix this
base = fastGlob.escapePath(base)

contentPath.pattern = contentPath.glob ? `${base}/${contentPath.glob}` : base
contentPath.pattern = contentPath.ignore ? `!${contentPath.pattern}` : contentPath.pattern
contentPath.pattern = contentPath.ignore
? `!${contentPath.pattern}`
: contentPath.pattern

return contentPath
}
Expand All @@ -215,11 +221,15 @@ function resolveRelativePaths(context, contentPaths) {

// Resolve base paths relative to the config file (when possible) if the experimental flag is enabled
if (context.userConfigPath && context.tailwindConfig.content.relative) {
resolveFrom = [path.dirname(context.userConfigPath)]
resolveFrom = [path.posix.dirname(context.userConfigPath)]
}

return contentPaths.map((contentPath) => {
contentPath.base = path.resolve(...resolveFrom, contentPath.base)
contentPath.base = path.posix.resolve(...resolveFrom, contentPath.base)

if (path.sep === '\\' && contentPath.base.startsWith('/') && !contentPath.base.startsWith('//?/')) {
contentPath.base = `C:${contentPath.base}`
}

return contentPath
})
Expand All @@ -238,7 +248,7 @@ function resolvePathSymlinks(contentPath) {
let paths = [contentPath]

try {
let resolvedPath = fs.realpathSync(contentPath.base)
let resolvedPath = normalizePath(fs.realpathSync(contentPath.base))
if (resolvedPath !== contentPath.base) {
paths.push({
...contentPath,
Expand Down Expand Up @@ -286,6 +296,9 @@ function resolveChangedFiles(candidateFiles, fileModifiedMap) {
let changedFiles = new Set()
env.DEBUG && console.time('Finding changed files')
let files = fastGlob.sync(paths, { absolute: true })

console.log({ paths, files })

for (let file of files) {
let prevModified = fileModifiedMap.get(file) || -Infinity
let modified = fs.statSync(file).mtimeMs
Expand Down

0 comments on commit a069813

Please sign in to comment.