Skip to content
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

fix(js): improve inconsistencies with assets in tsc and swc compared to node:package #9470

Merged
merged 1 commit into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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