-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #90 by adding postProcess hook to scoped-css-preprocessor to break cache for associated template files #95
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,15 @@ import Filter from 'broccoli-persistent-filter'; | |
import path from 'path'; | ||
|
||
import fsExists from './fsExists.js'; | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import { packageScopedPathToModulePath } from './generateAbsolutePathHash.js'; | ||
import getClassesTagsFromCss from './getClassesTagsFromCss.js'; | ||
import generateHash from './generateRelativePathHash.js'; | ||
import rewriteCss from './rewriteCss.js'; | ||
import rewriteHbs from './rewriteHbs.js'; | ||
|
||
const COMPONENT_EXTENSIONS = ['hbs', 'js', 'ts', 'gjs', 'gts']; | ||
const TEMPLATE_EXTENSIONS = ['hbs', 'gjs', 'gts']; | ||
|
||
class ScopedFilter extends Filter { | ||
constructor(componentsNode, options = {}) { | ||
|
@@ -67,12 +71,57 @@ class ScopedFilter extends Filter { | |
return ''; | ||
} | ||
} | ||
async postProcess(results, relativePath) { | ||
if (relativePath.endsWith('.module.css')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably be a config option, but I think since usage is low, we can skip it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a config option to opt in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misunderstood, will look at doing that in a later PR since However, I do think I'd like to check for CI to skip the whole thing since CI doesn't do a second pass and we'd be doing extra work for nothing. if (process.env.CI || relativePath.endsWith('.module.css')) {
return results;
} |
||
return results; | ||
} | ||
|
||
// need to add gjs/gts support | ||
// check existence of gjs and gts files | ||
// extract template tag | ||
|
||
const templatePath = relativePath.replace(/\.css/, '.hbs'); | ||
|
||
for (let inputPath of this.inputPaths) { | ||
const templateFilePath = path.join(inputPath, templatePath); | ||
const fileExists = await fsExists(templateFilePath); | ||
|
||
if (fileExists) { | ||
const cssFilePath = path.join(inputPath, relativePath); | ||
const cssContents = await readFile(cssFilePath, 'utf-8'); | ||
const { classes, tags } = getClassesTagsFromCss(cssContents); | ||
|
||
const previousClasses = this.options.previousClasses.get(relativePath); | ||
|
||
// if we have previous classes, and they are different, build templates to compare | ||
if (previousClasses && classes.toString() !== previousClasses.toString()) { | ||
const localPackagerStylePath = packageScopedPathToModulePath(relativePath); | ||
const postfix = generateHash(localPackagerStylePath); | ||
const templateContents = await readFile(templateFilePath, 'utf-8'); | ||
const templateOriginal = rewriteHbs(templateContents, previousClasses, tags, postfix); | ||
const templateRewrite = rewriteHbs(templateContents, classes, tags, postfix); | ||
// if the rewrite doesn't match the original output, we want to rebuild the template | ||
if (templateOriginal !== templateRewrite) { | ||
// this is an awful hack because we don't know how to invalidate broccoli-persistent-filter cache | ||
// ideally we'd invalidate the broccoli-peristent-filter cache here | ||
await writeFile(templateFilePath, templateContents + ' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel sad that I had to revert to using a hack like this, but the cache invalidation was not working as advertised (or I was just doing it wrong). Would love to fix this to actually invalidate the cache correctly, but this will work for now. |
||
} | ||
} | ||
// assign for next run comparison | ||
this.options.previousClasses.set(relativePath, classes); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} | ||
|
||
export default class ScopedCssPreprocessor { | ||
constructor(options) { | ||
this.owner = options.owner; | ||
this.appName = options.owner.parent.pkg.name; | ||
this.name = 'scoped-css-preprocessor'; | ||
this.previousClasses = new Map(); | ||
} | ||
|
||
/** | ||
|
@@ -98,7 +147,9 @@ export default class ScopedCssPreprocessor { | |
|
||
componentsNode = new ScopedFilter(componentsNode, { | ||
inputPath, | ||
getUserOptions: () => this.userOptions, | ||
getUserOptions: () => this.userOptions, | ||
owner: this.owner, | ||
previousClasses: this.previousClasses, | ||
}); | ||
|
||
const componentStyles = new Funnel(componentsNode, { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be using these for gjs/gts support later