Skip to content

Commit bb4d0d5

Browse files
refactor: rename NS to MODULE_TYPE and use a static value
1 parent 6323e17 commit bb4d0d5

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

Diff for: src/index.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
util: { createHash },
1111
} = webpack;
1212

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

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

@@ -44,7 +44,7 @@ class CssDependencyTemplate {
4444

4545
class CssModule extends webpack.Module {
4646
constructor(dependency) {
47-
super(NS, dependency.context);
47+
super(MODULE_TYPE, dependency.context);
4848
this._identifier = dependency.identifier;
4949
this._identifierIndex = dependency.identifierIndex;
5050
this.content = dependency.content;
@@ -141,7 +141,7 @@ class MiniCssExtractPlugin {
141141
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
142142
const loaderContext = lc;
143143
const module = m;
144-
loaderContext[NS] = (content) => {
144+
loaderContext[MODULE_TYPE] = (content) => {
145145
if (!Array.isArray(content) && content != null) {
146146
throw new Error(
147147
`Exported value was not extracted as an array: ${JSON.stringify(
@@ -169,7 +169,7 @@ class MiniCssExtractPlugin {
169169
pluginName,
170170
(result, { chunk }) => {
171171
const renderedModules = Array.from(chunk.modulesIterable).filter(
172-
(module) => module.type === NS
172+
(module) => module.type === MODULE_TYPE
173173
);
174174
if (renderedModules.length > 0) {
175175
result.push({
@@ -183,10 +183,10 @@ class MiniCssExtractPlugin {
183183
filenameTemplate: this.options.filename,
184184
pathOptions: {
185185
chunk,
186-
contentHashType: NS,
186+
contentHashType: MODULE_TYPE,
187187
},
188188
identifier: `${pluginName}.${chunk.id}`,
189-
hash: chunk.contentHash[NS],
189+
hash: chunk.contentHash[MODULE_TYPE],
190190
});
191191
}
192192
}
@@ -195,7 +195,7 @@ class MiniCssExtractPlugin {
195195
pluginName,
196196
(result, { chunk }) => {
197197
const renderedModules = Array.from(chunk.modulesIterable).filter(
198-
(module) => module.type === NS
198+
(module) => module.type === MODULE_TYPE
199199
);
200200
if (renderedModules.length > 0) {
201201
result.push({
@@ -209,10 +209,10 @@ class MiniCssExtractPlugin {
209209
filenameTemplate: this.options.chunkFilename,
210210
pathOptions: {
211211
chunk,
212-
contentHashType: NS,
212+
contentHashType: MODULE_TYPE,
213213
},
214214
identifier: `${pluginName}.${chunk.id}`,
215-
hash: chunk.contentHash[NS],
215+
hash: chunk.contentHash[MODULE_TYPE],
216216
});
217217
}
218218
}
@@ -226,7 +226,7 @@ class MiniCssExtractPlugin {
226226
}
227227
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
228228
hash.update(
229-
JSON.stringify(chunk.getChunkMaps(true).contentHash[NS] || {})
229+
JSON.stringify(chunk.getChunkMaps(true).contentHash[MODULE_TYPE] || {})
230230
);
231231
}
232232
if (REGEXP_NAME.test(chunkFilename)) {
@@ -239,12 +239,12 @@ class MiniCssExtractPlugin {
239239
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
240240
const hash = createHash(hashFunction);
241241
for (const m of chunk.modulesIterable) {
242-
if (m.type === NS) {
242+
if (m.type === MODULE_TYPE) {
243243
m.updateHash(hash);
244244
}
245245
}
246246
const { contentHash } = chunk;
247-
contentHash[NS] = hash
247+
contentHash[MODULE_TYPE] = hash
248248
.digest(hashDigest)
249249
.substring(0, hashDigestLength);
250250
});
@@ -294,14 +294,14 @@ class MiniCssExtractPlugin {
294294
)}[chunkId] + "`;
295295
},
296296
contentHash: {
297-
[NS]: `" + ${JSON.stringify(
298-
chunkMaps.contentHash[NS]
297+
[MODULE_TYPE]: `" + ${JSON.stringify(
298+
chunkMaps.contentHash[MODULE_TYPE]
299299
)}[chunkId] + "`,
300300
},
301301
contentHashWithLength: {
302-
[NS]: (length) => {
302+
[MODULE_TYPE]: (length) => {
303303
const shortContentHashMap = {};
304-
const contentHash = chunkMaps.contentHash[NS];
304+
const contentHash = chunkMaps.contentHash[MODULE_TYPE];
305305
for (const chunkId of Object.keys(contentHash)) {
306306
if (typeof contentHash[chunkId] === 'string') {
307307
shortContentHashMap[chunkId] = contentHash[
@@ -318,7 +318,7 @@ class MiniCssExtractPlugin {
318318
chunkMaps.name
319319
)}[chunkId]||chunkId) + "`,
320320
},
321-
contentHashType: NS,
321+
contentHashType: MODULE_TYPE,
322322
}
323323
);
324324
return Template.asString([
@@ -382,7 +382,7 @@ class MiniCssExtractPlugin {
382382
const obj = {};
383383
for (const chunk of mainChunk.getAllAsyncChunks()) {
384384
for (const module of chunk.modulesIterable) {
385-
if (module.type === NS) {
385+
if (module.type === MODULE_TYPE) {
386386
obj[chunk.id] = 1;
387387
break;
388388
}

Diff for: src/loader.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
99
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
1010
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
1111

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

1515
const exec = (loaderContext, code, filename) => {
@@ -53,15 +53,15 @@ export function pitch(request) {
5353
childCompiler
5454
);
5555
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
56-
// We set loaderContext[NS] = false to indicate we already in
56+
// We set loaderContext[MODULE_TYPE] = false to indicate we already in
5757
// a child compiler so we don't spawn another child compilers from there.
5858
childCompiler.hooks.thisCompilation.tap(
5959
`${pluginName} loader`,
6060
(compilation) => {
6161
compilation.hooks.normalModuleLoader.tap(
6262
`${pluginName} loader`,
6363
(loaderContext, module) => {
64-
loaderContext[NS] = false; // eslint-disable-line no-param-reassign
64+
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
6565
if (module.request === request) {
6666
// eslint-disable-next-line no-param-reassign
6767
module.loaders = loaders.map((loader) => {
@@ -125,7 +125,7 @@ export function pitch(request) {
125125
};
126126
});
127127
}
128-
this[NS](text);
128+
this[MODULE_TYPE](text);
129129
} catch (e) {
130130
return callback(e);
131131
}

0 commit comments

Comments
 (0)