use oxide

- Setup the codebase to be able to use the Rust parts based on an
  environment variable: `OXIDE=1`.
- Setup some tests that run both the non-Rust and Rust version in the
  same test.
- Sort the candidates in a consistent way, to guarantee the order for
  now (especially in tests).
- Reflect sorting related changes in tests.
- Ensure tests run in both the Rust and non-Rust version. (Some tests
  are explicitly skipped when using the Rust version since we haven't
  implemented those features yet. These include: custom prefix,
  transformers and extractors).
  - `jest`
  -`OXIDE=1 jest`
This commit is contained in:
Robin Malfait 2022-11-30 00:10:15 +01:00
parent cc92ef53a6
commit b643d399db
No known key found for this signature in database
GPG key ID: 92F53D68B9041AFE
37 changed files with 3340 additions and 4691 deletions

View file

@ -184,10 +184,17 @@ let state = {
let files = fastGlob.sync(this.contentPatterns.all)
for (let file of files) {
content.push({
content: fs.readFileSync(path.resolve(file), 'utf8'),
extension: path.extname(file).slice(1),
})
if (env.OXIDE) {
content.push({
file,
extension: path.extname(file).slice(1),
})
} else {
content.push({
content: fs.readFileSync(path.resolve(file), 'utf8'),
extension: path.extname(file).slice(1),
})
}
}
// Resolve raw content in the tailwind config
@ -234,11 +241,9 @@ let state = {
env.DEBUG && console.timeEnd('Watch new files')
}
env.DEBUG && console.time('Reading content files')
for (let file of this.readContentPaths()) {
this.context.changedContent.push(file)
}
env.DEBUG && console.timeEnd('Reading content files')
return this.context
},

View file

@ -2,4 +2,5 @@
export const env = {
DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
OXIDE: process.env.OXIDE,
}

View file

@ -174,9 +174,8 @@ export function resolvedChangedContent(context, candidateFiles, fileModifiedMap)
let [changedFiles, mTimesToCommit] = resolveChangedFiles(candidateFiles, fileModifiedMap)
for (let changedFile of changedFiles) {
let content = fs.readFileSync(changedFile, 'utf8')
let extension = path.extname(changedFile).slice(1)
changedContent.push({ content, extension })
changedContent.push({ file: changedFile, extension })
}
return [changedContent, mTimesToCommit]

View file

@ -1,3 +1,4 @@
import fs from 'fs'
import LRU from 'quick-lru'
import * as sharedState from './sharedState'
import { generateRules } from './generateRules'
@ -5,6 +6,8 @@ import log from '../util/log'
import cloneNodes from '../util/cloneNodes'
import { defaultExtractor } from './defaultExtractor'
import oxide from 'tailwind-oxide'
let env = sharedState.env
const builtInExtractors = {
@ -124,15 +127,32 @@ export default function expandTailwindAtRules(context) {
// ---
// Find potential rules in changed files
let candidates = new Set([sharedState.NOT_ON_DEMAND])
let candidates = new Set([...(context.candidates ?? []), sharedState.NOT_ON_DEMAND])
let seen = new Set()
env.DEBUG && console.time('Reading changed files')
for (let { content, extension } of context.changedContent) {
let transformer = getTransformer(context.tailwindConfig, extension)
let extractor = getExtractor(context, extension)
getClassCandidates(transformer(content), extractor, candidates, seen)
if (env.OXIDE) {
// TODO: Pass through or implement `extractor`
for (let candidate of oxide.parseCandidateStringsFromFiles(
context.changedContent
// Object.assign({}, builtInTransformers, context.tailwindConfig.content.transform)
)) {
candidates.add(candidate)
}
// for (let { file, content, extension } of context.changedContent) {
// let transformer = getTransformer(context.tailwindConfig, extension)
// let extractor = getExtractor(context, extension)
// getClassCandidatesOxide(file, transformer(content), extractor, candidates, seen)
// }
} else {
for (let { file, content, extension } of context.changedContent) {
let transformer = getTransformer(context.tailwindConfig, extension)
let extractor = getExtractor(context, extension)
content = file ? fs.readFileSync(file, 'utf8') : content
getClassCandidates(transformer(content), extractor, candidates, seen)
}
}
env.DEBUG && console.timeEnd('Reading changed files')
@ -143,7 +163,21 @@ export default function expandTailwindAtRules(context) {
let classCacheCount = context.classCache.size
env.DEBUG && console.time('Generate rules')
generateRules(candidates, context)
// TODO: Sorting is _probably_ slow, but right now it can guarantee the same order. Eventually
// we will be able to get rid of this.
env.DEBUG && console.time('Sorting candidates')
let sortedCandidates =
typeof process !== 'undefined' && process.env.JEST_WORKER_ID
? new Set(
[...candidates].sort((a, z) => {
if (a === z) return 0
if (a < z) return -1
return 1
})
)
: candidates
env.DEBUG && console.timeEnd('Sorting candidates')
generateRules(sortedCandidates, context)
env.DEBUG && console.timeEnd('Generate rules')
// We only ever add to the classCache, so if it didn't grow, there is nothing new.

View file

@ -1,6 +1,7 @@
export const env = {
NODE_ENV: process.env.NODE_ENV,
DEBUG: resolveDebug(process.env.DEBUG),
OXIDE: process.env.OXIDE,
}
export const contextMap = new Map()
export const configContextMap = new Map()

View file

@ -1,4 +1,4 @@
export default function (value) {
export default function negateValue(value) {
value = `${value}`
if (value === '0') {