haoxins / gulp-file-include

MAINTAINER WANTED ~ [gulp-file-include] a gulp plugin for file include
MIT License
677 stars 95 forks source link

Proposal for `include_once` directive as a means for skipping files already included #171

Open DanStevens opened 5 years ago

DanStevens commented 5 years ago

I would like a way to skip including a file that has already been included like that of include_once in PHP.

Proposal

I would propose a new directive include_once that can be used in addition to include . When it is used, gulp-file-include records that the file was included. If the file was not previously included by another include_once directive, it includes the contents of file as normal, otherwise it skips the directive.

Q&A

The following are questions that came to my mind while I was writing up this proposal.

Q: What do you mean by 'skip' exactly? Is the include_once directive left unchanged or replaced empty string?

I would suggest a option to allow the caller to specify the behaviour.

Q: How would include_once determine whether a file is included or not in ambiguous cases such as different path referring to the same file?

By default, I would suggest following the same means by which PHP handles this and have an option to change to allow the caller to control the behaviour. Options would include literal, absolute and possibly content.

literal uses the path as given when determining if a file has been include (like PHP). So, for example:

@@include_once('./myfile.txt')
@@include_once('./myfile.txt')

the contents of 'myfile.txt' would be included once since path of the second include_once directive has exactly the same path, while the following would include the file twice:

@@include_once('./myfile.txt')
@@include_once('myfile.txt')

absolute converts the given path to an absolute path from the file system root and uses that to determine if a file has already been included, so that the same files are skipped. So with the above example, 'myfile.txt' would only be included once since the paths refer to the same file.

content hashes the content of the included file and uses the hash when determining if a file has already been included. So, for example, assuming myfile - copy.txt is an exact copy of myfile.txt, the myfile.txt. doesn't get included:

@@include_once('./myfile - copy.txt')
@@include_once('./myfile.txt')

Q: Would the tracking of files already included be on a per-file or per-stream basis?

My preference would be per-file by default, with an option to choose per-stream.

Q: Will include directive record which files are included or just include_once? If I do @@include('myfile.txt') @@include_once('myfile.txt'), will 'myfile.txt' be included once or twice?

Twice, unless anyone thinks of a valid use case for the otherwise. My thinking for this is that it would be best if no new behaviour is introduced to the existing include directive.