injectDirective
Inject directive statements into JavaScript output files with deduplication and normalization support.
Overview
injectDirective is a utility for injecting JavaScript directive statements into compiled output files.
It scans files based on the provided glob pattern(s) and inserts one or more directive statements (e.g. "use strict", "use client") at the top of each file when applicable.
Directives are injected in a safe and controlled way, ensuring they are not duplicated and do not interfere with existing file structure.
Key capabilities
-
Directive Injection
Adds one or multiple directive statements to matched files. -
Targeted File Processing
- Processes only JavaScript output files.
- Skips files that are empty, contain no executable code, or only directives.
-
Smart Placement
- Injected after shebang (
#!) and banner comments. - Maintains correct execution order.
- Injected after shebang (
-
Content Preservation
Keeps the original file structure intact without modifying existing code. -
Idempotent Operation
Running multiple times will not duplicate directives.
Why use this?
- Ensures required directives (e.g.
"use strict","use client") are consistently applied. - Prevents duplicate directive declarations.
- Automatically adapts to file type (e.g. CommonJS vs ESM).
- Helps maintain correct runtime behavior.
- Simplifies directive management in build pipelines.
Importing
Syntax
Parameters
Prop
Type
pattern
Glob pattern or list of patterns used to target files for directive injection.
Accepted values:
string➔ single glob pattern.string[]➔ multiple glob patterns.Set<string>➔ unique collection of patterns.
Supported glob features
Patterns support common glob syntax:
*➔ match any characters except/.**➔ match nested directories.{}➔ brace expansion (multiple patterns/extensions).!➔ exclude patterns (can be combined withoptions.patternOptions.ignore).
Info
Pattern resolution behavior can be customized via
options.patternOptions
(e.g. hidden files, case sensitivity, concurrency, and ignore rules).
File processing behavior
All files matched by the provided pattern(s) are scanned.
Only files recognized as JavaScript output files are processed.
This includes common formats such as:
.js..mjs..cjs..esm.js..module.js.
Files outside these formats are automatically skipped.
Additional filtering
A matched file will be skipped if it:
- Is empty or contains only whitespace.
- Contains no executable JavaScript code.
- Contains only directive statements.
Important notes
- Directive injection only applies to valid JavaScript output files.
- Existing directives are automatically detected and not duplicated.
- File structure (shebang, banners, and code order) is preserved.
Examples
Summary
- Uses glob patterns to select target files.
- Processes only valid JavaScript output files.
- Skips non-executable or directive-only files.
directive
Directive string or collection of strings to inject into matched files.
Accepted values:
string➔ single directive.string[]➔ multiple directives.Set<string>➔ unique collection of directives.
Each entry represents a directive that will be injected as a standalone statement.
Injection behavior
- Directives are inserted at the top of each file.
- Existing shebang (
#!) and banner comments are preserved. - Existing
"use *"directives are automatically detected. - Duplicate directives are not injected.
Each directive is normalized into:
Automatic handling
"use strict"is automatically handled for CommonJS files.- Duplicate
"use strict"will not be inserted. - Custom directives are trimmed and normalized before injection.
Normalization
Each directive is normalized before injection:
- Wrapped in quotes if needed.
- Ensured to end with a semicolon.
- Consistent line formatting is applied.
The operation is idempotent — running multiple times will not duplicate directives.
Important notes
- Only top-level
"use *"directives are considered. - Directives are inserted before executable code.
- Existing file structure (shebang, banners, code order) is preserved.
Examples
Single directive:
Multiple directives:
Using Set:
Summary
directivedefines which directive statements are injected.- Supports single or multiple directives.
- Automatically avoids duplicates.
- Normalized into valid
"use *"statements.
options
Optional configuration object that defines how the utility function should behave in various scenarios, allowing customization and greater control at runtime.
logLevel
-
Description:
Controls how detailed the logger output should be during execution.Supported values
silent➔ no output.error➔ errors only.info➔ standard logs (default).debug➔ verbose logs.
Notes
- Invalid values are automatically reset to default.
- All levels include error output except
silent.
-
Required:
-
Type:
"error"|"silent"|"info"|"debug" -
Default:
"info" -
Example:
TypeScript/JavaScript
patternOptions
-
Description:
Controls how input file patterns are matched.Used to filter or adjust which files are included during processing.
Useful for ignoring files, handling hidden files, or tweaking matching behavior.Behavior
- Affects which files are selected.
- Can exclude unwanted files (e.g. test files).
- Falls back to default configuration when invalid type or not provided.
-
Required:
-
Type:
object -
Default:
-
Example:
TypeScript/JavaScript
Returns
Returns a Promise<void>.
The function performs asynchronous file processing and does not return any value.
Version information
- Since:
v0.0.7. - Category:
inject. - Stability: ✅
Stable(Production-ready).
