Skip to content

Fix file watching with file replacements #11509

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

Merged
merged 10 commits into from
Jul 12, 2018
Merged

Conversation

clydin
Copy link
Member

@clydin clydin commented Jul 10, 2018

Fixes #11339


export function normalizeFileReplacements(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: file name does not match export name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -0,0 +1,31 @@
import { getGlobalVariable } from '../../utils/env';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of replacing this with a new spec large in replacements_spec_large?

This is the one I had on my branch:

it('file replacements work with watch mode', (done) => {
    const overrides = {
      fileReplacements: [
        {
          replace: normalize('/src/meaning.ts'),
          with: normalize('/src/meaning-too.ts'),
        },
      ],
      watch: true,
    };

    let buildNumber = 0;

    runTargetSpec(host, browserTargetSpec, overrides, 45000, createConsoleLogger()).pipe(
      debounceTime(1000),
      tap((buildEvent) => expect(buildEvent.success).toBe(true)),
      tap(() => {
        const fileName = join(outputPath, 'main.js');
        buildNumber += 1;

        switch (buildNumber) {
          case 1:
            expect(virtualFs.fileBufferToString(host.scopedSync().read(fileName)))
              .toMatch(/meaning\s*=\s*42/);
            expect(virtualFs.fileBufferToString(host.scopedSync().read(fileName)))
              .not.toMatch(/meaning\s*=\s*10/);
            host.writeMultipleFiles({
              'src/meaning-too.ts': 'export var meaning = 84;',
            });
            break;

          case 2:
            expect(virtualFs.fileBufferToString(host.scopedSync().read(fileName)))
              .toMatch(/meaning\s*=\s*84/);
            expect(virtualFs.fileBufferToString(host.scopedSync().read(fileName)))
              .not.toMatch(/meaning\s*=\s*42/);
            break;
        }
      }),
      take(2),
    ).toPromise().then(done, done.fail);
  });

It should be faster than the e2e and also test the content is correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@filipesilva
Copy link
Contributor

Also I think this fixes #7305

@clydin clydin force-pushed the fix-file-watching branch from 971fdeb to 838b2f4 Compare July 11, 2018 16:39
@filipesilva
Copy link
Contributor

LGTM, great work!

@hansl
Copy link
Contributor

hansl commented Jul 12, 2018

I fear this doesn't cover the NativeScript case with pattern matching (matching all .html.tns files to .tns). They'd have to list all those files, and even then it doesn't work when creating new files...

@clydin
Copy link
Member Author

clydin commented Jul 12, 2018

Based on the custom host found here, code similar to the following should work:

            new AngularCompilerPlugin({
                entryModule: resolve(appPath, "app.module#AppModule"),
                tsConfigPath: join(__dirname, "tsconfig.esm.json"),
                skipCodeGeneration: !aot,
                hostReplacementPaths: path => {
                    const { dir, name, ext } = parse(path);

                    for (const platform of extensions) {
                        const platformFileName = `${name}.${platform}${ext}`;
                        const platformPath = toSystemPath(join(dir, platformFileName));
            
                        try {
                            const stat = statSync(platformPath);
                            if (stat && stat.isFile()) {
                                return platformPath;
                            }
                        } catch(_e) {
                            // continue checking the other platforms
                        }
                    }
            
                    return path;
                },
            }),

Another option with this PR is to try to use the PlatformFSPlugin which appears to be used within the non-Angular demos found in the repository.

@hansl hansl merged commit 839c1f8 into angular:master Jul 12, 2018
@clydin clydin deleted the fix-file-watching branch July 13, 2018 00:40
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Watching doesn't work for file paths remapped by the provided FS host
4 participants