Skip to content

feat(src/index.js): Check for hot reloading before adding hot reload … #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import webpack from 'webpack';
import sources from 'webpack-sources';

const thing = path.resolve(__dirname, './hotLoader.js');
const hotLoader = path.resolve(__dirname, './hotLoader.js');

const { ConcatSource, SourceMapSource, OriginalSource } = sources;
const { Template, util: { createHash } } = webpack;
Expand All @@ -17,6 +17,20 @@ const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
const REGEXP_NAME = /\[name\]/i;

const isHMR = (compiler) => {
if (compiler && compiler.options) {
if (compiler.options.devServer && compiler.options.devServer.hot) {
return true;
}

if (compiler.options.entry) {
const entryString = JSON.stringify(compiler.options.entry);
return entryString.includes('hot') || entryString.includes('hmr');
}
}
return false;
};

class CssDependency extends webpack.Dependency {
constructor(
{ identifier, content, media, sourceMap },
Expand Down Expand Up @@ -132,24 +146,31 @@ class ExtractCssChunks {
}

apply(compiler) {
console.log('DevServerHot:', compiler.options.devServer.hot)
// console.log(compiler.options)
const updatedRules = compiler.options.module.rules.reduce((rules, rule) => {
if (rule.use && Array.isArray(rule.use)) {
const isMiniCss = rule.use.some((l) => {
const needle = l.loader || l;
return needle.includes(pluginName);
});
if (isMiniCss) {
rule.use.unshift(thing);
}
}
rules.push(rule);
try {
const isHOT = isHMR(compiler);

return rules;
}, []);
if (isHOT && compiler.options.module && compiler.options.module.rules) {
const updatedRules = compiler.options.module.rules.reduce((rules, rule) => {
if (rule.use && Array.isArray(rule.use)) {
const isMiniCss = rule.use.some((l) => {
const needle = l.loader || l;
return needle.includes(pluginName);
});
if (isMiniCss) {
rule.use.unshift(hotLoader);
}
}
rules.push(rule);

return rules;
}, []);

compiler.options.module.rules = updatedRules;
}
} catch (e) {
console.error('Something went wrong: contact the author', e);
}

compiler.options.module.rules = updatedRules;

compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
Expand Down