@@ -1,35 +1,41 @@
-'use strict';
+"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
+exports.default = void 0;
-var _webpack = require('webpack');
+var _webpack = _interopRequireDefault(require("webpack"));
-var _webpack2 = _interopRequireDefault(_webpack);
-
-var _webpackSources = require('webpack-sources');
-
-var _webpackSources2 = _interopRequireDefault(_webpackSources);
+var _webpackSources = _interopRequireDefault(require("webpack-sources"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-const { ConcatSource, SourceMapSource, OriginalSource } = _webpackSources2.default;
+/* eslint-disable class-methods-use-this */
+const {
+ ConcatSource,
+ SourceMapSource,
+ OriginalSource
+} = _webpackSources.default;
const {
Template,
- util: { createHash }
-} = _webpack2.default;
-
+ util: {
+ createHash
+ }
+} = _webpack.default;
const MODULE_TYPE = 'css/mini-extract';
-
const pluginName = 'mini-css-extract-plugin';
-
const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
const REGEXP_NAME = /\[name\]/i;
-class CssDependency extends _webpack2.default.Dependency {
- constructor({ identifier, content, media, sourceMap }, context, identifierIndex) {
+class CssDependency extends _webpack.default.Dependency {
+ constructor({
+ identifier,
+ content,
+ media,
+ sourceMap
+ }, context, identifierIndex) {
super();
this.identifier = identifier;
this.identifierIndex = identifierIndex;
@@ -46,9 +53,10 @@
class CssDependencyTemplate {
apply() {}
+
}
-class CssModule extends _webpack2.default.Module {
+class CssModule extends _webpack.default.Module {
constructor(dependency) {
super(MODULE_TYPE, dependency.context);
this.id = '';
@@ -57,9 +65,8 @@
this.content = dependency.content;
this.media = dependency.media;
this.sourceMap = dependency.sourceMap;
- }
+ } // no source() so webpack doesn't do add stuff to the bundle
- // no source() so webpack doesn't do add stuff to the bundle
size() {
return this.content.length;
@@ -75,8 +82,13 @@
nameForCondition() {
const resource = this._identifier.split('!').pop();
+
const idx = resource.indexOf('?');
- if (idx >= 0) return resource.substring(0, idx);
+
+ if (idx >= 0) {
+ return resource.substring(0, idx);
+ }
+
return resource;
}
@@ -117,12 +131,15 @@
this.options = Object.assign({
filename: '[name].css'
}, options);
+
if (!this.options.chunkFilename) {
- const { filename } = this.options;
+ const {
+ filename
+ } = this.options;
const hasName = filename.includes('[name]');
const hasId = filename.includes('[id]');
- const hasChunkHash = filename.includes('[chunkhash]');
- // Anything changing depending on chunk is fine
+ const hasChunkHash = filename.includes('[chunkhash]'); // Anything changing depending on chunk is fine
+
if (hasChunkHash || hasName || hasId) {
this.options.chunkFilename = filename;
} else {
@@ -152,8 +171,11 @@
});
compilation.dependencyFactories.set(CssDependency, new CssModuleFactory());
compilation.dependencyTemplates.set(CssDependency, new CssDependencyTemplate());
- compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
+ compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, {
+ chunk
+ }) => {
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === MODULE_TYPE);
+
if (renderedModules.length > 0) {
result.push({
render: () => this.renderContentAsset(compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
@@ -167,8 +189,11 @@
});
}
});
- compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
+ compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, {
+ chunk
+ }) => {
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === MODULE_TYPE);
+
if (renderedModules.length > 0) {
result.push({
render: () => this.renderContentAsset(compilation, chunk, renderedModules, compilation.runtimeTemplate.requestShortener),
@@ -183,7 +208,10 @@
}
});
compilation.mainTemplate.hooks.hashForChunk.tap(pluginName, (hash, chunk) => {
- const { chunkFilename } = this.options;
+ const {
+ chunkFilename
+ } = this.options;
+
if (REGEXP_CHUNKHASH.test(chunkFilename)) {
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
}
@@ -190,25 +219,39 @@
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
hash.update(JSON.stringify(chunk.getChunkMaps(true).contentHash[MODULE_TYPE] || {}));
}
+
if (REGEXP_NAME.test(chunkFilename)) {
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
}
});
compilation.hooks.contentHash.tap(pluginName, chunk => {
- const { outputOptions } = compilation;
- const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
+ const {
+ outputOptions
+ } = compilation;
+ const {
+ hashFunction,
+ hashDigest,
+ hashDigestLength
+ } = outputOptions;
const hash = createHash(hashFunction);
+
for (const m of chunk.modulesIterable) {
if (m.type === MODULE_TYPE) {
m.updateHash(hash);
}
}
- const { contentHash } = chunk;
+
+ const {
+ contentHash
+ } = chunk;
contentHash[MODULE_TYPE] = hash.digest(hashDigest).substring(0, hashDigestLength);
});
- const { mainTemplate } = compilation;
+ const {
+ mainTemplate
+ } = compilation;
mainTemplate.hooks.localVars.tap(pluginName, (source, chunk) => {
const chunkMap = this.getCssChunkObject(chunk);
+
if (Object.keys(chunkMap).length > 0) {
return Template.asString([source, '', '// object to store loaded CSS chunks', 'var installedCssChunks = {', Template.indent(chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n')), '}']);
}
@@ -216,8 +260,12 @@
});
mainTemplate.hooks.requireEnsure.tap(pluginName, (source, chunk, hash) => {
const chunkMap = this.getCssChunkObject(chunk);
+
if (Object.keys(chunkMap).length > 0) {
const chunkMaps = chunk.getChunkMaps();
+ const {
+ crossOriginLoading
+ } = mainTemplate.outputOptions;
const linkHrefPath = mainTemplate.getAssetPath(JSON.stringify(this.options.chunkFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: length => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
@@ -252,8 +306,9 @@
},
contentHashType: MODULE_TYPE
});
- return Template.asString([source, '', `// ${pluginName} CSS loading`, `var cssChunks = ${JSON.stringify(chunkMap)};`, 'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', 'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', Template.indent(['promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {', Template.indent([`var href = ${linkHrefPath};`, `var fullhref = ${mainTemplate.requireFn}.p + href;`, 'var existingLinkTags = document.getElementsByTagName("link");', 'for(var i = 0; i < existingLinkTags.length; i++) {', Template.indent(['var tag = existingLinkTags[i];', 'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");', 'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();']), '}', 'var existingStyleTags = document.getElementsByTagName("style");', 'for(var i = 0; i < existingStyleTags.length; i++) {', Template.indent(['var tag = existingStyleTags[i];', 'var dataHref = tag.getAttribute("data-href");', 'if(dataHref === href || dataHref === fullhref) return resolve();']), '}', 'var linkTag = document.createElement("link");', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', 'linkTag.onload = resolve;', 'linkTag.onerror = function(event) {', Template.indent(['var request = event && event.target && event.target.src || fullhref;', 'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + request + ")");', 'err.request = request;', 'delete installedCssChunks[chunkId]', 'linkTag.parentNode.removeChild(linkTag)', 'reject(err);']), '};', 'linkTag.href = fullhref;', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);']), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), '}));']), '}']);
+ return Template.asString([source, '', `// ${pluginName} CSS loading`, `var cssChunks = ${JSON.stringify(chunkMap)};`, 'if(installedCssChunks[chunkId]) promises.push(installedCssChunks[chunkId]);', 'else if(installedCssChunks[chunkId] !== 0 && cssChunks[chunkId]) {', Template.indent(['promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {', Template.indent([`var href = ${linkHrefPath};`, `var fullhref = ${mainTemplate.requireFn}.p + href;`, 'var existingLinkTags = document.getElementsByTagName("link");', 'for(var i = 0; i < existingLinkTags.length; i++) {', Template.indent(['var tag = existingLinkTags[i];', 'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");', 'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();']), '}', 'var existingStyleTags = document.getElementsByTagName("style");', 'for(var i = 0; i < existingStyleTags.length; i++) {', Template.indent(['var tag = existingStyleTags[i];', 'var dataHref = tag.getAttribute("data-href");', 'if(dataHref === href || dataHref === fullhref) return resolve();']), '}', 'var linkTag = document.createElement("link");', 'linkTag.rel = "stylesheet";', 'linkTag.type = "text/css";', 'linkTag.onload = resolve;', 'linkTag.onerror = function(event) {', Template.indent(['var request = event && event.target && event.target.src || fullhref;', 'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + request + ")");', 'err.code = "CSS_CHUNK_LOAD_FAILED";', 'err.request = request;', 'delete installedCssChunks[chunkId]', 'linkTag.parentNode.removeChild(linkTag)', 'reject(err);']), '};', 'linkTag.href = fullhref;', crossOriginLoading ? Template.asString([`if (linkTag.href.indexOf(window.location.origin + '/') !== 0) {`, Template.indent(`linkTag.crossOrigin = ${JSON.stringify(crossOriginLoading)};`), '}']) : '', 'var head = document.getElementsByTagName("head")[0];', 'head.appendChild(linkTag);']), '}).then(function() {', Template.indent(['installedCssChunks[chunkId] = 0;']), '}));']), '}']);
}
+
return source;
});
});
@@ -274,33 +331,34 @@
renderContentAsset(compilation, chunk, modules, requestShortener) {
let usedModules;
-
const [chunkGroup] = chunk.groupsIterable;
+
if (typeof chunkGroup.getModuleIndex2 === 'function') {
// Store dependencies for modules
- const moduleDependencies = new Map(modules.map(m => [m, new Set()]));
-
- // Get ordered list of modules per chunk group
+ const moduleDependencies = new Map(modules.map(m => [m, new Set()])); // Get ordered list of modules per chunk group
// This loop also gathers dependencies from the ordered lists
// Lists are in reverse order to allow to use Array.pop()
+
const modulesByChunkGroup = Array.from(chunk.groupsIterable, cg => {
const sortedModules = modules.map(m => {
return {
module: m,
index: cg.getModuleIndex2(m)
};
- }).filter(item => item.index !== undefined).sort((a, b) => b.index - a.index).map(item => item.module);
+ }) // eslint-disable-next-line no-undefined
+ .filter(item => item.index !== undefined).sort((a, b) => b.index - a.index).map(item => item.module);
+
for (let i = 0; i < sortedModules.length; i++) {
const set = moduleDependencies.get(sortedModules[i]);
+
for (let j = i + 1; j < sortedModules.length; j++) {
set.add(sortedModules[j]);
}
}
return sortedModules;
- });
+ }); // set with already included modules in correct order
- // set with already included modules in correct order
usedModules = new Set();
const unusedModulesFilter = m => !usedModules.has(m);
@@ -308,20 +366,21 @@
while (usedModules.size < modules.length) {
let success = false;
let bestMatch;
- let bestMatchDeps;
- // get first module where dependencies are fulfilled
+ let bestMatchDeps; // get first module where dependencies are fulfilled
+
for (const list of modulesByChunkGroup) {
// skip and remove already added modules
- while (list.length > 0 && usedModules.has(list[list.length - 1])) list.pop();
+ while (list.length > 0 && usedModules.has(list[list.length - 1])) {
+ list.pop();
+ } // skip empty lists
+
- // skip empty lists
if (list.length !== 0) {
const module = list[list.length - 1];
- const deps = moduleDependencies.get(module);
- // determine dependencies that are not yet included
- const failedDeps = Array.from(deps).filter(unusedModulesFilter);
+ const deps = moduleDependencies.get(module); // determine dependencies that are not yet included
+
+ const failedDeps = Array.from(deps).filter(unusedModulesFilter); // store best match for fallback behavior
- // store best match for fallback behavior
if (!bestMatchDeps || bestMatchDeps.length > failedDeps.length) {
bestMatch = list;
bestMatchDeps = failedDeps;
@@ -340,7 +400,7 @@
// use list with fewest failed deps
// and emit a warning
const fallbackModule = bestMatch.pop();
- compilation.warnings.push(new Error(`chunk ${chunk.name || chunk.id} [mini-css-extract-plugin]\n` + 'Conflicting order between:\n' + ` * ${fallbackModule.readableIdentifier(requestShortener)}\n` + `${bestMatchDeps.map(m => ` * ${m.readableIdentifier(requestShortener)}`).join('\n')}`));
+ compilation.warnings.push(new Error(`chunk ${chunk.name || chunk.id} [${pluginName}]\n` + 'Conflicting order between:\n' + ` * ${fallbackModule.readableIdentifier(requestShortener)}\n` + `${bestMatchDeps.map(m => ` * ${m.readableIdentifier(requestShortener)}`).join('\n')}`));
usedModules.add(fallbackModule);
}
}
@@ -352,13 +412,18 @@
modules.sort((a, b) => a.index2 - b.index2);
usedModules = modules;
}
+
const source = new ConcatSource();
const externalsSource = new ConcatSource();
+
for (const m of usedModules) {
if (/^@import url/.test(m.content)) {
// HACK for IE
// http://stackoverflow.com/a/14676665/1458162
- let { content } = m;
+ let {
+ content
+ } = m;
+
if (m.media) {
// insert media into the @import
// this is rar
@@ -382,10 +451,12 @@
}
}
}
+
return new ConcatSource(externalsSource, source);
}
+
}
MiniCssExtractPlugin.loader = require.resolve('./loader');
-
-exports.default = MiniCssExtractPlugin;
\ No newline at end of file
+var _default = MiniCssExtractPlugin;
+exports.default = _default;
\ No newline at end of file