Skip to content

General upgrades #97

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 4 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
'no-restricted-syntax': 0,
'prefer-arrow-callback': 0,
'prefer-destructuring': 0,
'prefer-template': 0
'prefer-template': 0,
'class-methods-use-this': 0
}
};
59 changes: 0 additions & 59 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extract-css-chunks-webpack-plugin",
"version": "0.0.0-placeholder",
"version": "3.1.0-beta.1",
"author": "James Gillmore <james@faceyspacey.com>",
"contributors": [
"Zack Jackson <zackary.l.jackson@gmail.com> (https://github.com/ScriptedAlchemy)"
Expand Down
23 changes: 8 additions & 15 deletions src/hotLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@ const loaderUtils = require('loader-utils');
const defaultOptions = {
fileMap: '{fileName}',
};

function hotReload(content) {
module.exports = function (content) {
this.cacheable();
const options = Object.assign(
{},
defaultOptions,
loaderUtils.getOptions(this),
);
{},
defaultOptions,
loaderUtils.getOptions(this),
);

return `${content}
return content + `
if(module.hot) {
// ${Date.now()}
var cssReload = require(${loaderUtils.stringifyRequest(
this,
`!${path.join(__dirname, 'hotModuleReplacement.js')}`,
)})(module.id, ${JSON.stringify(options)});
var cssReload = require(${loaderUtils.stringifyRequest(this, '!' + path.join(__dirname, 'hotModuleReplacement.js'))})(module.id, ${JSON.stringify(options)});
module.hot.dispose(cssReload);
module.hot.accept(undefined, cssReload);
}
`;
}

module.exports = hotReload;
};
49 changes: 24 additions & 25 deletions src/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const getCurrentScriptUrl = function (moduleId) {
}
return fileMap.split(',').map(function (mapRule) {
const reg = new RegExp(filename + '\\.js$', 'g');
return normalizeUrl(
src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'),
{ stripWWW: false },
);
return normalizeUrl(src.replace(reg, mapRule.replace(/{fileName}/g, filename) + '.css'), { stripWWW: false });
});
};
};
Expand All @@ -46,8 +43,8 @@ function updateCss(el, url) {
url = el.href.split('?')[0];
}
if (el.isLoaded === false) {
// We seem to be about to replace a css link that hasn't loaded yet.
// We're probably changing the same file more than once.
// We seem to be about to replace a css link that hasn't loaded yet.
// We're probably changing the same file more than once.
return;
}
if (!url || !(url.indexOf('.css') > -1)) return;
Expand All @@ -58,29 +55,18 @@ function updateCss(el, url) {
newEl.isLoaded = false;
newEl.addEventListener('load', function () {
newEl.isLoaded = true;
el.parentNode.removeChild(el);
el.remove();
});
newEl.addEventListener('error', function () {
newEl.isLoaded = true;
el.parentNode.removeChild(el);
el.remove();
});

newEl.href = url + '?' + Date.now();
el.parentNode.appendChild(newEl);
}

function getReloadUrl(href, src) {
href = normalizeUrl(href, { stripWWW: false });
let ret;
src.some(function (url) { // eslint-disable-line array-callback-return
if (href.indexOf(src) > -1) {
ret = url;
}
});
return ret;
}

function reloadStyle(src) { // eslint-disable-line no-unused-vars
function reloadStyle(src) {
const elements = document.querySelectorAll('link');
let loaded = false;

Expand All @@ -97,6 +83,17 @@ function reloadStyle(src) { // eslint-disable-line no-unused-vars
return loaded;
}

function getReloadUrl(href, src) {
href = normalizeUrl(href, { stripWWW: false });
let ret;
src.some(function (url) {
if (href.indexOf(src) > -1) {
ret = url;
}
});
return ret;
}

function reloadAll() {
const elements = document.querySelectorAll('link');
forEach.call(elements, function (el) {
Expand All @@ -106,19 +103,21 @@ function reloadAll() {
}

module.exports = function (moduleId, options) {
let getScriptSrc;

if (noDocument) {
return noop;
}

const getScriptSrc = getCurrentScriptUrl(moduleId);
getScriptSrc = getCurrentScriptUrl(moduleId);

function update() {
const src = getScriptSrc(options.fileMap);
const reloaded = false; // hack of all hacks...for now
if (reloaded) {
console.log('[HMR] css reload %s', src.join(' ')); // eslint-disable-line no-console
const reloaded = reloadStyle(src);
if (reloaded && !options.reloadAll) {
console.log('[HMR] css reload %s', src.join(' '));
} else {
console.log('[HMR] Reload all css'); // eslint-disable-line no-console
console.log('[HMR] Reload all css');
reloadAll();
}
}
Expand Down
Loading