Enhances html-webpack-plugin functionality with async and defer attributes for script elements
—
Add arbitrary custom attributes to script elements for specialized use cases like CORS, CSP, or custom loading behavior.
Add custom attributes to script elements based on configurable patterns and values.
/**
* Array of custom attribute configurations
*/
custom?: CustomAttributeConfig[];
interface CustomAttributeConfig {
test: string | RegExp | Array<string | RegExp>;
attribute: string;
value?: any;
}Usage Examples:
// Single custom attribute
new ScriptExtHtmlWebpackPlugin({
custom: {
test: /\.js$/,
attribute: 'crossorigin',
value: 'anonymous'
}
})
// Multiple custom attributes
new ScriptExtHtmlWebpackPlugin({
custom: [
{
test: /\.js$/,
attribute: 'crossorigin',
value: 'anonymous'
},
{
test: 'analytics.js',
attribute: 'data-category',
value: 'tracking'
}
]
})Custom attributes support various value types for different use cases.
/**
* Custom attribute value types
*/
interface CustomAttributeConfig {
test: string | RegExp | Array<string | RegExp>;
attribute: string;
value?: string | number | boolean | null | undefined;
}Value Examples:
new ScriptExtHtmlWebpackPlugin({
custom: [
// String value
{
test: /\.js$/,
attribute: 'crossorigin',
value: 'anonymous'
},
// Boolean value (true)
{
test: 'critical.js',
attribute: 'async',
value: true
},
// Boolean attribute (no value)
{
test: 'module.js',
attribute: 'nomodule'
// value defaults to true for boolean attributes
},
// Numeric value
{
test: 'versioned.js',
attribute: 'data-version',
value: 1.5
}
]
})Add Cross-Origin Resource Sharing attributes for CDN scripts.
new ScriptExtHtmlWebpackPlugin({
custom: {
test: /\.js$/,
attribute: 'crossorigin',
value: 'anonymous'
}
})Generated HTML:
<script src="app.js" crossorigin="anonymous"></script>Add nonce attributes for Content Security Policy compliance.
new ScriptExtHtmlWebpackPlugin({
custom: {
test: /\.js$/,
attribute: 'nonce',
value: 'abc123'
}
})Generated HTML:
<script src="app.js" nonce="abc123"></script>Add custom data attributes for analytics or application-specific metadata.
new ScriptExtHtmlWebpackPlugin({
custom: [
{
test: 'analytics.js',
attribute: 'data-category',
value: 'tracking'
},
{
test: /chunk.*\.js$/,
attribute: 'data-chunk-type',
value: 'lazy'
}
]
})Generated HTML:
<script src="analytics.js" data-category="tracking"></script>
<script src="chunk1.js" data-chunk-type="lazy"></script>Add attributes for progressive enhancement and feature detection.
new ScriptExtHtmlWebpackPlugin({
custom: [
{
test: 'modern.js',
attribute: 'type',
value: 'module'
},
{
test: 'legacy.js',
attribute: 'nomodule',
value: true
}
]
})Generated HTML:
<script src="modern.js" type="module"></script>
<script src="legacy.js" nomodule></script>Custom attributes support the same flexible pattern matching as other plugin features.
/**
* Pattern matching options for custom attributes
*/
type TestPattern =
| string // Exact filename match
| RegExp // Regular expression match
| Array<string | RegExp>; // Array of patternsPattern Examples:
new ScriptExtHtmlWebpackPlugin({
custom: [
// Exact match
{
test: 'specific-script.js',
attribute: 'id',
value: 'main-script'
},
// RegExp pattern
{
test: /^vendor-.*\.js$/,
attribute: 'data-source',
value: 'third-party'
},
// Multiple patterns
{
test: ['app.js', 'main.js', /^core/],
attribute: 'data-priority',
value: 'high'
}
]
})Custom attributes work alongside all other plugin features.
// Complete configuration example
new ScriptExtHtmlWebpackPlugin({
// Script attributes
async: /chunk.*\.js$/,
defer: 'analytics.js',
// Resource hints
preload: 'critical.js',
prefetch: {
test: /lazy.*\.js$/,
chunks: 'async'
},
// Custom attributes
custom: [
// CORS for all scripts
{
test: /\.js$/,
attribute: 'crossorigin',
value: 'anonymous'
},
// Special handling for analytics
{
test: 'analytics.js',
attribute: 'data-consent-required',
value: 'true'
},
// Version tracking for app scripts
{
test: /^(app|main)/,
attribute: 'data-version',
value: '2.1.5'
}
]
})Custom attributes are applied after script attribute processing to ensure they don't interfere with core functionality:
The plugin validates custom attribute configurations and provides helpful error messages.
/**
* Validates custom attribute configuration
* @param config - Custom attribute configuration
* @returns Whether configuration is valid
*/
function validateCustomAttribute(config: CustomAttributeConfig): boolean;Common validation errors:
test patternattribute nameExample error:
ScriptExtHtmlWebpackPlugin: invalid configuration - custom attribute missing 'test' propertyInstall with Tessl CLI
npx tessl i tessl/npm-script-ext-html-webpack-plugin