Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(js): improve inconsistencies with assets in tsc and swc from prev…
…ious implementation (#9470)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Mar 25, 2022
1 parent f5e40ed commit 8fbbe3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 22 additions & 0 deletions packages/js/src/utils/copy-assets-handler.spec.ts
Expand Up @@ -42,13 +42,16 @@ describe('AssetInputOutputHandler', () => {
output: 'docs',
ignore: ['ignore.md', '**/nested-ignore.md'],
},
'LICENSE',
],
});
});

test('watchAndProcessOnAssetChange', async () => {
const dispose = await sut.watchAndProcessOnAssetChange();

fse.writeFileSync(path.join(rootDir, 'LICENSE'), 'license');
await wait(100);
fse.writeFileSync(path.join(projectDir, 'README.md'), 'readme');
await wait(100); // give watch time to react
fse.writeFileSync(path.join(projectDir, 'docs/test1.md'), 'test');
Expand All @@ -72,6 +75,15 @@ describe('AssetInputOutputHandler', () => {
await wait(100);

expect(callback.mock.calls).toEqual([
[
[
{
type: 'create',
src: path.join(rootDir, 'LICENSE'),
dest: path.join(rootDir, 'dist/mylib/LICENSE'),
},
],
],
[
[
{
Expand Down Expand Up @@ -130,6 +142,7 @@ describe('AssetInputOutputHandler', () => {
});

test('processAllAssetsOnce', async () => {
fse.writeFileSync(path.join(rootDir, 'LICENSE'), 'license');
fse.writeFileSync(path.join(projectDir, 'README.md'), 'readme');
fse.writeFileSync(path.join(projectDir, 'docs/test1.md'), 'test');
fse.writeFileSync(path.join(projectDir, 'docs/test2.md'), 'test');
Expand All @@ -144,6 +157,15 @@ describe('AssetInputOutputHandler', () => {
await sut.processAllAssetsOnce();

expect(callback.mock.calls).toEqual([
[
[
{
type: 'create',
src: path.join(rootDir, 'LICENSE'),
dest: path.join(rootDir, 'dist/mylib/LICENSE'),
},
],
],
[
[
{
Expand Down
18 changes: 7 additions & 11 deletions packages/js/src/utils/copy-assets-handler.ts
Expand Up @@ -120,14 +120,12 @@ export class CopyAssetsHandler {
!ag.ignore?.some((ig) => minimatch(src, ig)) &&
!this.ignore.ignores(src)
) {
const relPath = path.relative(ag.input, src);
const dest = relPath.startsWith('..') ? src : relPath;
acc.push({
type: 'create',
src: path.join(this.rootDir, src),
dest: path.join(
this.rootDir,
ag.output,
path.relative(ag.input, src)
),
dest: path.join(this.rootDir, ag.output, dest),
});
}
return acc;
Expand All @@ -140,7 +138,7 @@ export class CopyAssetsHandler {
async watchAndProcessOnAssetChange(): Promise<() => Promise<void>> {
const watcher = await import('@parcel/watcher');
const subscription = await watcher.subscribe(
this.projectDir,
this.rootDir,
(err, events) => {
if (err) {
logger.error(`Watch error: ${err?.message ?? 'Unknown'}`);
Expand All @@ -163,14 +161,12 @@ export class CopyAssetsHandler {
!ag.ignore?.some((ig) => minimatch(pathFromRoot, ig)) &&
!this.ignore.ignores(pathFromRoot)
) {
const relPath = path.relative(ag.input, pathFromRoot);
const destPath = relPath.startsWith('..') ? pathFromRoot : relPath;
fileEvents.push({
type: event.type,
src: path.join(this.rootDir, pathFromRoot),
dest: path.join(
this.rootDir,
ag.output,
path.relative(ag.input, pathFromRoot)
),
dest: path.join(this.rootDir, ag.output, destPath),
});
// Match first entry and skip the rest for this file.
break;
Expand Down

0 comments on commit 8fbbe3b

Please sign in to comment.