diff --git a/package-lock.json b/package-lock.json index 35117107..5c2b0037 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13033,8 +13033,7 @@ "tapable": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/tapable/-/tapable-1.1.3.tgz", - "integrity": "sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==", - "dev": true + "integrity": "sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==" }, "term-size": { "version": "1.2.0", diff --git a/package.json b/package.json index 01a2cd4e..44c49016 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "loader-utils": "^1.1.0", "normalize-url": "1.9.1", "schema-utils": "^1.0.0", + "tapable": "^1.1.3", "webpack-sources": "^1.1.0" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index d9f6bf2d..8ce637b9 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ import webpack from 'webpack'; import sources from 'webpack-sources'; import validateOptions from 'schema-utils'; +import { SyncWaterfallHook } from 'tapable'; import CssDependency from './CssDependency'; import schema from './plugin-options.json'; @@ -24,6 +25,8 @@ const REGEXP_NAME = /\[name\]/i; const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g; const DEFAULT_FILENAME = '[name].css'; +const compilerHookMap = new WeakMap(); + class CssDependencyTemplate { apply() {} } @@ -127,6 +130,17 @@ class MiniCssExtractPlugin { } } + static getCompilerHooks(compiler) { + let hooks = compilerHookMap.get(compiler); + if (!hooks) { + hooks = { + customize: new SyncWaterfallHook(['source']), + }; + compilerHookMap.set(compiler, hooks); + } + return hooks; + } + apply(compiler) { compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { compilation.dependencyFactories.set( @@ -376,6 +390,9 @@ class MiniCssExtractPlugin { '}', ]) : '', + MiniCssExtractPlugin.getCompilerHooks( + compiler + ).customize.call(''), 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);', ]),