From 3e291f17e2a65e9e55b67d969b73224066f0713d Mon Sep 17 00:00:00 2001 From: Michael Ciniawsky Date: Fri, 7 Sep 2018 09:38:03 +0200 Subject: [PATCH] refactor: rename `NS` to `MODULE_TYPE` and use a static value --- src/index.js | 41 ++++++++++++++++++++--------------------- src/loader.js | 10 ++++------ 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/index.js b/src/index.js index eafae28a..4f584441 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,3 @@ -import fs from 'fs'; -import path from 'path'; - import webpack from 'webpack'; import sources from 'webpack-sources'; @@ -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'; @@ -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; @@ -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( @@ -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({ @@ -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], }); } } @@ -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({ @@ -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], }); } } @@ -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)) { @@ -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); }); @@ -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[ @@ -318,7 +317,7 @@ class MiniCssExtractPlugin { chunkMaps.name )}[chunkId]||chunkId) + "`, }, - contentHashType: NS, + contentHashType: MODULE_TYPE, } ); return Template.asString([ @@ -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; } diff --git a/src/loader.js b/src/loader.js index c3dc2c4c..cd8fefbc 100644 --- a/src/loader.js +++ b/src/loader.js @@ -1,5 +1,3 @@ -import fs from 'fs'; -import path from 'path'; import NativeModule from 'module'; import loaderUtils from 'loader-utils'; @@ -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) => { @@ -53,7 +51,7 @@ 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`, @@ -61,7 +59,7 @@ export function pitch(request) { 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) => { @@ -125,7 +123,7 @@ export function pitch(request) { }; }); } - this[NS](text); + this[MODULE_TYPE](text); } catch (e) { return callback(e); }