A Webpack 5 plugin that enhances the new URL() constructor handling, supporting both static and dynamic URL resolution
✅ Static URL resolution: Resolves static URL strings at build time ✅ Dynamic URL support: Handles dynamic URL construction with import.meta.url ✅ Detailed logging: Configurable logging output for debugging
pnpm add webpack-plugin-dynamic-new-url -D
Add the plugin to your webpack.config.js:
const WebpackDynamicURLPlugin = require('webpack-plugin-dynamic-new-url');
module.exports = {
// ... other configurations
plugins: [
new WebpackDynamicURLPlugin({
enabled: true, // Whether to enable the plugin, default true
enableStaticUrl: true, // Whether to enable static URL processing, default false
enableLogging: true, // Whether to enable logging, default false
logLevel: 'info' // Log level: 'debug', 'info', 'warn', 'error'
})
]
};
// Will be resolved to full file path URL at build time
const assetUrl = new URL('./assets/logo.png', import.meta.url);
console.log(assetUrl.href);
// Supports dynamic paths, resolved at runtime
const fileName = 'config.json';
const configUrl = new URL(`./data/${fileName}`, import.meta.url);
dynamic-new-url-plugin/
├── src/ # Source code
│ ├── index.js # Main plugin file (WebpackDynamicURLPlugin)
│ └── url-context-dependency.js # URL context dependency module
├── dist/ # Build output
│ ├── index.js # CommonJS output
│ └── index.mjs # ES Module output
└── package.json # Project configuration
- Development environment: Node.js 14+
- Webpack 5
- Build command:
pnpm run build
- Release process:
pnpm build
pnpm release-patch # Patch version
pnpm release-minor # Minor version
pnpm release-major # Major version
- Webpack: 5.x
- Node.js: 14+
- Browsers: Modern browsers supporting ES2020+
- Module systems: ES Module, CommonJS
- Static processing: enableStaticUrl is false by default, needs manual enabling (recommended to use official implementation)
- Path resolution: Uses import.meta.url as base URL for relative path resolution
- Performance: Enabling logging may impact build performance, recommended to disable in production
- Dependency management: Plugin automatically handles URL-related dependencies, no manual management needed