Skip to content

Commit

Permalink
feat: add hook for customizing injected runtime tags
Browse files Browse the repository at this point in the history
Enable other plugins to tap into the tag injection mechanism in order to
customize the functionality of lazy loaded chunks.

Closes #40.
  • Loading branch information
Patrik Sletmo committed Apr 24, 2020
1 parent 1ffc393 commit 608bc70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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": {
Expand Down
17 changes: 17 additions & 0 deletions src/index.js
Expand Up @@ -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';
Expand All @@ -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() {}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -376,6 +390,9 @@ class MiniCssExtractPlugin {
'}',
])
: '',
MiniCssExtractPlugin.getCompilerHooks(
compiler
).customize.call(''),
'var head = document.getElementsByTagName("head")[0];',
'head.appendChild(linkTag);',
]),
Expand Down

0 comments on commit 608bc70

Please sign in to comment.