-
Notifications
You must be signed in to change notification settings - Fork 11
fix(tasks): update styles watcher to support chokidar v4 #141
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
base: main
Are you sure you want to change the base?
Changes from all commits
b6f9e27
2feb61f
f96d85d
efaf48f
70b9028
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,61 @@ | ||
| const path = require('path'); | ||
| const glob = require('fast-glob'); | ||
| const { log } = require('../utils/log'); | ||
| const generateEntries = require('../utils/generateEntries'); | ||
| const renderStyles = require('../utils/renderStyles'); | ||
|
|
||
| // extend log to proper say what file is running | ||
| module.exports = (config) => { | ||
| return new Promise((resolve) => { | ||
| if (config && config.general && config.general.watch) { | ||
| try { | ||
| log(__filename, 'Watcher Sass / autoprefixer running...', '', 'info'); | ||
|
|
||
| const chokidar = require('chokidar') | ||
| const sassPattern = path.join(config.general.sourcesPath, `**/*.${config.general.sourceKey}.scss`); | ||
| module.exports = async (config) => { | ||
|
Hugoer marked this conversation as resolved.
|
||
| if (config && config.general && config.general.watch) { | ||
| try { | ||
| log(__filename, 'Watcher Sass / autoprefixer running...', '', 'info'); | ||
|
|
||
| const chokidar = require('chokidar'); | ||
| const pattern = `**/*.${config.general.sourceKey}.scss`; | ||
|
|
||
| // Note: fast-glob resolves files once at startup, so newly created SCSS files | ||
| // won't be picked up by the watcher without a restart. This is an accepted | ||
| // tradeoff to avoid EMFILE errors with recursive directory watching. | ||
| const files = await glob(pattern, { | ||
|
Hugoer marked this conversation as resolved.
|
||
| cwd: config.general.sourcesPath, | ||
| absolute: true | ||
| }); | ||
|
Hugoer marked this conversation as resolved.
|
||
|
|
||
| const watcher = chokidar.watch(sassPattern, { | ||
| ignoreInitial: true | ||
| }) | ||
| if (!files.length) { | ||
| log(__filename, 'No SCSS sources found to watch', '', 'warning'); | ||
| return; | ||
| } | ||
|
|
||
| watcher.on('all', (event, file) => { | ||
| const relativePath = path.relative( | ||
| config.general.sourcesPath, | ||
| path.dirname(file) | ||
| ) | ||
| config.stylelint.failOnError = false; | ||
|
|
||
| const fileName = path | ||
| .basename(file) | ||
| .replace(config.general.sourceKey, config.general.bundleKey) | ||
| const watcher = chokidar.watch(files, { | ||
|
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. Instead of creating a huge static list of paths with glob why not use something like this which translates to the previous behavior? 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 saw the issue description after I left my comment. The why of the solution I think should be clearly stated also in the PR description in addition to the link to the issue. The current proposal could seem to be the only alternative with chokidar. |
||
| ignoreInitial: true | ||
| }); | ||
|
|
||
| const destFile = path.join(relativePath, fileName) | ||
| watcher.on('all', (event, file) => { | ||
| const relativePath = path.relative( | ||
| config.general.sourcesPath, | ||
| path.dirname(file) | ||
| ); | ||
|
|
||
| config.stylelint.failOnError = false | ||
| const fileName = path | ||
| .basename(file) | ||
| .replace(config.general.sourceKey, config.general.bundleKey); | ||
|
|
||
| renderStyles(file, destFile, config) | ||
| }); | ||
| const destFile = path.join(relativePath, fileName); | ||
|
Hugoer marked this conversation as resolved.
|
||
|
|
||
| } catch (e) { | ||
| log(__filename, 'Something is missing, you need install dev dependencies for this.', e.message, 'error'); | ||
| } | ||
| } else { | ||
| log(__filename, 'Sass / autoprefixer running...', '', 'info'); | ||
|
|
||
| // checking all entries at this configuration | ||
| const entries = generateEntries(config, 'scss'); | ||
| const promises = Object.keys(entries).map(file => renderStyles(entries[file], file, config)); | ||
| Promise.allSettled(promises).then((results) => { | ||
| log(__filename, 'Styles done', '', 'info'); | ||
| resolve(); | ||
| renderStyles(file, destFile, config); | ||
| }); | ||
|
|
||
| } catch (e) { | ||
|
Hugoer marked this conversation as resolved.
|
||
| log(__filename, 'Failed to start SCSS watcher', e.message, 'error'); | ||
| } | ||
| }); | ||
| } else { | ||
| log(__filename, 'Sass / autoprefixer running...', '', 'info'); | ||
|
|
||
| const entries = generateEntries(config, 'scss'); | ||
| const promises = Object.keys(entries).map(file => renderStyles(entries[file], file, config)); | ||
| await Promise.allSettled(promises); | ||
| log(__filename, 'Styles done', '', 'info'); | ||
| } | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.