stencil icon indicating copy to clipboard operation
stencil copied to clipboard

feat: stencil compiler should be configurable to watch arbitrary files for change and recompile

Open petercmuc opened this issue 3 years ago • 3 comments

Prerequisites

Describe the Feature Request

stencil compiler should be configurable to watch arbitrary files for change and recompile.

Describe the Use Case

When devolping a multi-npm-package stencil application it would be helpful if a change of a node module would trigger re-compilation of the app. In our case we develop an app and have a npm package that includes certain re-usable webcomponents, also built with stencil. These need to be integrated in the app to check their functionality. This is achieved using npm link which creates a symlink in the node_modules directory of the app that uses them. This setup requires the component package to be built (this can be simplified using the watch compiler option) and the app to be re-started with every change of the components.

Describe Preferred Solution

Make included default watch files completely configurable (default seems to be ".src/")

Describe Alternatives

Since there is already an option to exclude certain files from watch using "watchIgnoredRegex" it would be convenient to have a "watchAdditionalRegex" with contains an array of RegEx which matches additional files to be watched.

Related Code

No response

Additional Information

No response

petercmuc avatar Nov 22 '21 13:11 petercmuc

+1

michaelplazek avatar Nov 24 '21 16:11 michaelplazek

If anyone would like a temporary workaround, it's possible to add watch files via a custom output target.

watchAdditionalRegex.ts

import {
    CompilerCtx,
    Config,
    OutputTargetCustom,
} from '@stencil/core/internal';
import { stream, Entry } from 'fast-glob';

export const watchAdditionalRegex = (
    globs: string | string[],
): OutputTargetCustom => ({
    type: 'custom',
    name: 'watchAdditionalRegex',
    async generator(stencilConfig: Config, compilerCtx: CompilerCtx) {
        if (!stencilConfig.watch) return;

        for await (const stats of stream(globs, {
            objectMode: true,
            unique: true,
            absolute: true,
        })) {
            const { path, dirent } = stats as unknown as Entry;
            if (dirent.isFile()) {
                compilerCtx.addWatchFile(path);
            } else if (dirent.isDirectory()) {
                compilerCtx.addWatchDir(path, false);
            }
        }
    },
});

stencil.config.ts

import { Config } from '@stencil/core';
import { watchAdditionalRegex } from './watchAdditionalRegex';

export const config: Config = {
   // ...
    outputTargets: [
        {
            type: 'www',
        },
        watchAdditionalRegex('../watchThis/**/*'),
    ],
  // ...
};

George-Payne avatar Nov 26 '21 10:11 George-Payne

@George-Payne does workaround still working, it's not watching even it is registered https://codesandbox.io/p/devbox/stencil-web-component-forked-dmnvcc?file=%2Fsrc%2Findex.html%3A16%2C1 Screenshot 2024-05-03 at 17 17 32

venkatesh-nagineni avatar May 03 '24 15:05 venkatesh-nagineni