Skip to content

Commit

Permalink
refactor: rename NS to MODULE_TYPE and use a static value (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Sep 7, 2018
1 parent 6323e17 commit dd141f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
41 changes: 20 additions & 21 deletions src/index.js
@@ -1,6 +1,3 @@
import fs from 'fs';
import path from 'path';

import webpack from 'webpack';
import sources from 'webpack-sources';

Expand All @@ -10,7 +7,7 @@ const {
util: { createHash },
} = webpack;

const NS = path.dirname(fs.realpathSync(__filename));
const MODULE_TYPE = 'css/mini-extract';

const pluginName = 'mini-css-extract-plugin';

Expand Down Expand Up @@ -44,7 +41,7 @@ class CssDependencyTemplate {

class CssModule extends webpack.Module {
constructor(dependency) {
super(NS, dependency.context);
super(MODULE_TYPE, dependency.context);
this._identifier = dependency.identifier;
this._identifierIndex = dependency.identifierIndex;
this.content = dependency.content;
Expand Down Expand Up @@ -141,7 +138,7 @@ class MiniCssExtractPlugin {
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
const loaderContext = lc;
const module = m;
loaderContext[NS] = (content) => {
loaderContext[MODULE_TYPE] = (content) => {
if (!Array.isArray(content) && content != null) {
throw new Error(
`Exported value was not extracted as an array: ${JSON.stringify(
Expand Down Expand Up @@ -169,7 +166,7 @@ class MiniCssExtractPlugin {
pluginName,
(result, { chunk }) => {
const renderedModules = Array.from(chunk.modulesIterable).filter(
(module) => module.type === NS
(module) => module.type === MODULE_TYPE
);
if (renderedModules.length > 0) {
result.push({
Expand All @@ -183,10 +180,10 @@ class MiniCssExtractPlugin {
filenameTemplate: this.options.filename,
pathOptions: {
chunk,
contentHashType: NS,
contentHashType: MODULE_TYPE,
},
identifier: `${pluginName}.${chunk.id}`,
hash: chunk.contentHash[NS],
hash: chunk.contentHash[MODULE_TYPE],
});
}
}
Expand All @@ -195,7 +192,7 @@ class MiniCssExtractPlugin {
pluginName,
(result, { chunk }) => {
const renderedModules = Array.from(chunk.modulesIterable).filter(
(module) => module.type === NS
(module) => module.type === MODULE_TYPE
);
if (renderedModules.length > 0) {
result.push({
Expand All @@ -209,10 +206,10 @@ class MiniCssExtractPlugin {
filenameTemplate: this.options.chunkFilename,
pathOptions: {
chunk,
contentHashType: NS,
contentHashType: MODULE_TYPE,
},
identifier: `${pluginName}.${chunk.id}`,
hash: chunk.contentHash[NS],
hash: chunk.contentHash[MODULE_TYPE],
});
}
}
Expand All @@ -226,7 +223,9 @@ class MiniCssExtractPlugin {
}
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
hash.update(
JSON.stringify(chunk.getChunkMaps(true).contentHash[NS] || {})
JSON.stringify(
chunk.getChunkMaps(true).contentHash[MODULE_TYPE] || {}
)
);
}
if (REGEXP_NAME.test(chunkFilename)) {
Expand All @@ -239,12 +238,12 @@ class MiniCssExtractPlugin {
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
const hash = createHash(hashFunction);
for (const m of chunk.modulesIterable) {
if (m.type === NS) {
if (m.type === MODULE_TYPE) {
m.updateHash(hash);
}
}
const { contentHash } = chunk;
contentHash[NS] = hash
contentHash[MODULE_TYPE] = hash
.digest(hashDigest)
.substring(0, hashDigestLength);
});
Expand Down Expand Up @@ -294,14 +293,14 @@ class MiniCssExtractPlugin {
)}[chunkId] + "`;
},
contentHash: {
[NS]: `" + ${JSON.stringify(
chunkMaps.contentHash[NS]
[MODULE_TYPE]: `" + ${JSON.stringify(
chunkMaps.contentHash[MODULE_TYPE]
)}[chunkId] + "`,
},
contentHashWithLength: {
[NS]: (length) => {
[MODULE_TYPE]: (length) => {
const shortContentHashMap = {};
const contentHash = chunkMaps.contentHash[NS];
const contentHash = chunkMaps.contentHash[MODULE_TYPE];
for (const chunkId of Object.keys(contentHash)) {
if (typeof contentHash[chunkId] === 'string') {
shortContentHashMap[chunkId] = contentHash[
Expand All @@ -318,7 +317,7 @@ class MiniCssExtractPlugin {
chunkMaps.name
)}[chunkId]||chunkId) + "`,
},
contentHashType: NS,
contentHashType: MODULE_TYPE,
}
);
return Template.asString([
Expand Down Expand Up @@ -382,7 +381,7 @@ class MiniCssExtractPlugin {
const obj = {};
for (const chunk of mainChunk.getAllAsyncChunks()) {
for (const module of chunk.modulesIterable) {
if (module.type === NS) {
if (module.type === MODULE_TYPE) {
obj[chunk.id] = 1;
break;
}
Expand Down
10 changes: 4 additions & 6 deletions src/loader.js
@@ -1,5 +1,3 @@
import fs from 'fs';
import path from 'path';
import NativeModule from 'module';

import loaderUtils from 'loader-utils';
Expand All @@ -9,7 +7,7 @@ import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';

const NS = path.dirname(fs.realpathSync(__filename));
const MODULE_TYPE = 'css/mini-extract';
const pluginName = 'mini-css-extract-plugin';

const exec = (loaderContext, code, filename) => {
Expand Down Expand Up @@ -53,15 +51,15 @@ export function pitch(request) {
childCompiler
);
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
// We set loaderContext[NS] = false to indicate we already in
// We set loaderContext[MODULE_TYPE] = false to indicate we already in
// a child compiler so we don't spawn another child compilers from there.
childCompiler.hooks.thisCompilation.tap(
`${pluginName} loader`,
(compilation) => {
compilation.hooks.normalModuleLoader.tap(
`${pluginName} loader`,
(loaderContext, module) => {
loaderContext[NS] = false; // eslint-disable-line no-param-reassign
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
if (module.request === request) {
// eslint-disable-next-line no-param-reassign
module.loaders = loaders.map((loader) => {
Expand Down Expand Up @@ -125,7 +123,7 @@ export function pitch(request) {
};
});
}
this[NS](text);
this[MODULE_TYPE](text);
} catch (e) {
return callback(e);
}
Expand Down

0 comments on commit dd141f2

Please sign in to comment.