Skip to content

Commit

Permalink
I think I figured it out
Browse files Browse the repository at this point in the history
I think the generated files being modified by `lit-localize build` was
causing Parcel to rebuild in an infinite loop. There seems to be no
real way to exclude files from Parcel's watcher in `serve` mode, so
I'm doing so in a very hacky way: putting the generated strings files
in Parcel's cache directory.
  • Loading branch information
oyamauchi committed Nov 16, 2023
1 parent 0cb4554 commit 8503067
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ yarn-error.log*
# cypress
cypress/videos/*.mp4
cypress/screenshots/*

# lit-localize generated
generated
4 changes: 2 additions & 2 deletions lit-localize.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"tsConfig": "./tsconfig.json",
"output": {
"mode": "runtime",
"localeCodesModule": "generated/locales.ts",
"outputDir": "generated/strings"
"localeCodesModule": ".parcel-cache/locales.ts",
"outputDir": ".parcel-cache/strings"
},
"interchange": {
"format": "xliff",
Expand Down
21 changes: 17 additions & 4 deletions scripts/parcel-resolver-locales.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ async function allXlfFiles(projectRoot) {
* Resolves import specifiers starting with `locales:` by running lit-localize
* to generate strings files, and pointing Parcel at the generated files.
*
* The generated files are in Parcel's cache directory (configured in
* lit-localize.json) so that they don't trigger Parcel's watcher, which could
* result in an infinite loop of rebuilding. Yes this is silly, but Parcel does
* not have an official way to make the watcher ignore some files.
*
* NB: this is not a Typescript file! (Parcel doesn't support plugins written
* in TS.) No type checking!
*/
export default new Resolver({
async resolve({ specifier, options: { projectRoot } }) {
if (specifier.startsWith('locales:')) {
await promisify(exec)('npx lit-localize build');

const locale = specifier.substring('locales:'.length);

const litConfig = JSON.parse(
await promisify(fs.readFile)(
path.join(projectRoot, 'lit-localize.json'),
'utf-8',
),
);

const filePath =
locale === 'config'
? path.join(projectRoot, 'generated/locales.ts')
: path.join(projectRoot, `generated/strings/${locale}.ts`);
? path.join(projectRoot, litConfig.output.localeCodesModule)
: path.join(projectRoot, litConfig.output.outputDir, `${locale}.ts`);

await promisify(exec)('npx lit-localize build\n');

return {
// Rebuild if an XLIFF file changes, or the lit-localize config.
Expand Down

0 comments on commit 8503067

Please sign in to comment.