Skip to content

Ability to add attributes to link tag and intercept onload event #413

@Rulexec

Description

@Rulexec

Feature Proposal

Add ability to add custom code to requireEnsure hook code to be able do something custom with the linkTag.

For me there is only two cases:

  • Add custom code right after var linkTag = document.createElement("link"); (to be able add some attributes)
  • Replace onload function to do some work before resolve() call

Feature Use Case

I'm using css-vars-ponyfill to use CSS Variables in IE11. And I want to call it after <link> tag was added by mini-css-extract-plugin, but before chunk will be loaded. Also, I need to add custom attribute to <link> tag to handle only own CSS.

Currently I'm solving it in a very dirty way:

compilation.mainTemplate.hooks.requireEnsure.tap({
	name: 'SkinsPlugin',
	stage: 9999999,
}, source => {
	const miniCssPluginPresent = source.indexOf('if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);') >= 0;
	if (!miniCssPluginPresent) {
		throw new Error('SkinsPlugin:noMiniCssExtractPlugin');
	}

	let replaced = false;

	const regexp =/^(\s*)linkTag.onload = resolve;$/m;

	source = source.replace(regexp, (full, indent) => {
		replaced = true;

		let code = ``;

		code += indent + `linkTag.setAttribute('data-include-css-ponyfill', '');`;

		code += indent + `linkTag.onload = function(e){\n`;
		code += indent + `\tif (typeof SomeGlobalObject !== 'undefined' && SomeGlobalObject && SomeGlobalObject._someFun) {\n`;
		code += indent + '\t\tSomeGlobalObject._someFun();\n';
		code += indent + '\t\tresolve(e);\n';
		code += indent + '\t}\n';
		code += indent + '};\n';

		return code;
	});

	if (!replaced) {
		throw new Error('SkinsPlugin:noMiniCssExtractPlugin');
	}

	return source;
});

But I think, that better is to collect some other use cases which need interception and implement some options to do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions