Skip to content

Commit dd141f2

Browse files
refactor: rename NS to MODULE_TYPE and use a static value (#265)
1 parent 6323e17 commit dd141f2

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

Diff for: src/index.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
41
import webpack from 'webpack';
52
import sources from 'webpack-sources';
63

@@ -10,7 +7,7 @@ const {
107
util: { createHash },
118
} = webpack;
129

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

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

@@ -44,7 +41,7 @@ class CssDependencyTemplate {
4441

4542
class CssModule extends webpack.Module {
4643
constructor(dependency) {
47-
super(NS, dependency.context);
44+
super(MODULE_TYPE, dependency.context);
4845
this._identifier = dependency.identifier;
4946
this._identifierIndex = dependency.identifierIndex;
5047
this.content = dependency.content;
@@ -141,7 +138,7 @@ class MiniCssExtractPlugin {
141138
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
142139
const loaderContext = lc;
143140
const module = m;
144-
loaderContext[NS] = (content) => {
141+
loaderContext[MODULE_TYPE] = (content) => {
145142
if (!Array.isArray(content) && content != null) {
146143
throw new Error(
147144
`Exported value was not extracted as an array: ${JSON.stringify(
@@ -169,7 +166,7 @@ class MiniCssExtractPlugin {
169166
pluginName,
170167
(result, { chunk }) => {
171168
const renderedModules = Array.from(chunk.modulesIterable).filter(
172-
(module) => module.type === NS
169+
(module) => module.type === MODULE_TYPE
173170
);
174171
if (renderedModules.length > 0) {
175172
result.push({
@@ -183,10 +180,10 @@ class MiniCssExtractPlugin {
183180
filenameTemplate: this.options.filename,
184181
pathOptions: {
185182
chunk,
186-
contentHashType: NS,
183+
contentHashType: MODULE_TYPE,
187184
},
188185
identifier: `${pluginName}.${chunk.id}`,
189-
hash: chunk.contentHash[NS],
186+
hash: chunk.contentHash[MODULE_TYPE],
190187
});
191188
}
192189
}
@@ -195,7 +192,7 @@ class MiniCssExtractPlugin {
195192
pluginName,
196193
(result, { chunk }) => {
197194
const renderedModules = Array.from(chunk.modulesIterable).filter(
198-
(module) => module.type === NS
195+
(module) => module.type === MODULE_TYPE
199196
);
200197
if (renderedModules.length > 0) {
201198
result.push({
@@ -209,10 +206,10 @@ class MiniCssExtractPlugin {
209206
filenameTemplate: this.options.chunkFilename,
210207
pathOptions: {
211208
chunk,
212-
contentHashType: NS,
209+
contentHashType: MODULE_TYPE,
213210
},
214211
identifier: `${pluginName}.${chunk.id}`,
215-
hash: chunk.contentHash[NS],
212+
hash: chunk.contentHash[MODULE_TYPE],
216213
});
217214
}
218215
}
@@ -226,7 +223,9 @@ class MiniCssExtractPlugin {
226223
}
227224
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
228225
hash.update(
229-
JSON.stringify(chunk.getChunkMaps(true).contentHash[NS] || {})
226+
JSON.stringify(
227+
chunk.getChunkMaps(true).contentHash[MODULE_TYPE] || {}
228+
)
230229
);
231230
}
232231
if (REGEXP_NAME.test(chunkFilename)) {
@@ -239,12 +238,12 @@ class MiniCssExtractPlugin {
239238
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
240239
const hash = createHash(hashFunction);
241240
for (const m of chunk.modulesIterable) {
242-
if (m.type === NS) {
241+
if (m.type === MODULE_TYPE) {
243242
m.updateHash(hash);
244243
}
245244
}
246245
const { contentHash } = chunk;
247-
contentHash[NS] = hash
246+
contentHash[MODULE_TYPE] = hash
248247
.digest(hashDigest)
249248
.substring(0, hashDigestLength);
250249
});
@@ -294,14 +293,14 @@ class MiniCssExtractPlugin {
294293
)}[chunkId] + "`;
295294
},
296295
contentHash: {
297-
[NS]: `" + ${JSON.stringify(
298-
chunkMaps.contentHash[NS]
296+
[MODULE_TYPE]: `" + ${JSON.stringify(
297+
chunkMaps.contentHash[MODULE_TYPE]
299298
)}[chunkId] + "`,
300299
},
301300
contentHashWithLength: {
302-
[NS]: (length) => {
301+
[MODULE_TYPE]: (length) => {
303302
const shortContentHashMap = {};
304-
const contentHash = chunkMaps.contentHash[NS];
303+
const contentHash = chunkMaps.contentHash[MODULE_TYPE];
305304
for (const chunkId of Object.keys(contentHash)) {
306305
if (typeof contentHash[chunkId] === 'string') {
307306
shortContentHashMap[chunkId] = contentHash[
@@ -318,7 +317,7 @@ class MiniCssExtractPlugin {
318317
chunkMaps.name
319318
)}[chunkId]||chunkId) + "`,
320319
},
321-
contentHashType: NS,
320+
contentHashType: MODULE_TYPE,
322321
}
323322
);
324323
return Template.asString([
@@ -382,7 +381,7 @@ class MiniCssExtractPlugin {
382381
const obj = {};
383382
for (const chunk of mainChunk.getAllAsyncChunks()) {
384383
for (const module of chunk.modulesIterable) {
385-
if (module.type === NS) {
384+
if (module.type === MODULE_TYPE) {
386385
obj[chunk.id] = 1;
387386
break;
388387
}

Diff for: src/loader.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fs from 'fs';
2-
import path from 'path';
31
import NativeModule from 'module';
42

53
import loaderUtils from 'loader-utils';
@@ -9,7 +7,7 @@ import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
97
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
108
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
119

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

1513
const exec = (loaderContext, code, filename) => {
@@ -53,15 +51,15 @@ export function pitch(request) {
5351
childCompiler
5452
);
5553
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);
56-
// We set loaderContext[NS] = false to indicate we already in
54+
// We set loaderContext[MODULE_TYPE] = false to indicate we already in
5755
// a child compiler so we don't spawn another child compilers from there.
5856
childCompiler.hooks.thisCompilation.tap(
5957
`${pluginName} loader`,
6058
(compilation) => {
6159
compilation.hooks.normalModuleLoader.tap(
6260
`${pluginName} loader`,
6361
(loaderContext, module) => {
64-
loaderContext[NS] = false; // eslint-disable-line no-param-reassign
62+
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign
6563
if (module.request === request) {
6664
// eslint-disable-next-line no-param-reassign
6765
module.loaders = loaders.map((loader) => {
@@ -125,7 +123,7 @@ export function pitch(request) {
125123
};
126124
});
127125
}
128-
this[NS](text);
126+
this[MODULE_TYPE](text);
129127
} catch (e) {
130128
return callback(e);
131129
}

0 commit comments

Comments
 (0)