Skip to content

Commit 629ef38

Browse files
committed
style: Update code style for prettier / eslint configuration
1 parent f0d00a1 commit 629ef38

File tree

4 files changed

+254
-143
lines changed

4 files changed

+254
-143
lines changed

.eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ module.exports = {
77
'error',
88
{ singleQuote: true, trailingComma: 'es5', arrowParens: 'always' },
99
],
10+
'class-methods-use-this': 'off',
11+
'no-undefined': 'off',
1012
},
1113
};

src/index.js

+125-74
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3+
34
import webpack from 'webpack';
45
import sources from 'webpack-sources';
56

@@ -15,7 +16,11 @@ const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
1516
const REGEXP_NAME = /\[name\]/i;
1617

1718
class CssDependency extends webpack.Dependency {
18-
constructor({ identifier, content, media, sourceMap }, context, identifierIndex) {
19+
constructor(
20+
{ identifier, content, media, sourceMap },
21+
context,
22+
identifierIndex
23+
) {
1924
super();
2025
this.identifier = identifier;
2126
this.identifierIndex = identifierIndex;
@@ -55,7 +60,9 @@ class CssModule extends webpack.Module {
5560
}
5661

5762
readableIdentifier(requestShortener) {
58-
return `css ${requestShortener.shorten(this._identifier)}${this._identifierIndex ? ` (${this._identifierIndex})` : ''}`;
63+
return `css ${requestShortener.shorten(this._identifier)}${
64+
this._identifierIndex ? ` (${this._identifierIndex})` : ''
65+
}`;
5966
}
6067

6168
nameForCondition() {
@@ -97,9 +104,12 @@ class CssModuleFactory {
97104

98105
class MiniCssExtractPlugin {
99106
constructor(options) {
100-
this.options = Object.assign({
101-
filename: '[name].css',
102-
}, options);
107+
this.options = Object.assign(
108+
{
109+
filename: '[name].css',
110+
},
111+
options
112+
);
103113
if (!this.options.chunkFilename) {
104114
const { filename } = this.options;
105115
const hasName = filename.includes('[name]');
@@ -110,7 +120,10 @@ class MiniCssExtractPlugin {
110120
this.options.chunkFilename = filename;
111121
} else {
112122
// Elsewise prefix '[id].' in front of the basename to make it changing
113-
this.options.chunkFilename = filename.replace(/(^|\/)([^/]*(?:\?|$))/, '$1[id].$2');
123+
this.options.chunkFilename = filename.replace(
124+
/(^|\/)([^/]*(?:\?|$))/,
125+
'$1[id].$2'
126+
);
114127
}
115128
}
116129
}
@@ -122,7 +135,11 @@ class MiniCssExtractPlugin {
122135
const module = m;
123136
loaderContext[NS] = (content) => {
124137
if (!Array.isArray(content) && content != null) {
125-
throw new Error(`Exported value was not extracted as an array: ${JSON.stringify(content)}`);
138+
throw new Error(
139+
`Exported value was not extracted as an array: ${JSON.stringify(
140+
content
141+
)}`
142+
);
126143
}
127144
const identifierCountMap = new Map();
128145
for (const line of content) {
@@ -132,38 +149,62 @@ class MiniCssExtractPlugin {
132149
}
133150
};
134151
});
135-
compilation.dependencyFactories.set(CssDependency, new CssModuleFactory());
136-
compilation.dependencyTemplates.set(CssDependency, new CssDependencyTemplate());
137-
compilation.mainTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
138-
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
139-
if (renderedModules.length > 0) {
140-
result.push({
141-
render: () => this.renderContentAsset(renderedModules, compilation.runtimeTemplate.requestShortener),
142-
filenameTemplate: this.options.filename,
143-
pathOptions: {
144-
chunk,
145-
contentHashType: NS,
146-
},
147-
identifier: `mini-css-extract-plugin.${chunk.id}`,
148-
hash: chunk.contentHash[NS],
149-
});
152+
compilation.dependencyFactories.set(
153+
CssDependency,
154+
new CssModuleFactory()
155+
);
156+
compilation.dependencyTemplates.set(
157+
CssDependency,
158+
new CssDependencyTemplate()
159+
);
160+
compilation.mainTemplate.hooks.renderManifest.tap(
161+
pluginName,
162+
(result, { chunk }) => {
163+
const renderedModules = Array.from(chunk.modulesIterable).filter(
164+
(module) => module.type === NS
165+
);
166+
if (renderedModules.length > 0) {
167+
result.push({
168+
render: () =>
169+
this.renderContentAsset(
170+
renderedModules,
171+
compilation.runtimeTemplate.requestShortener
172+
),
173+
filenameTemplate: this.options.filename,
174+
pathOptions: {
175+
chunk,
176+
contentHashType: NS,
177+
},
178+
identifier: `mini-css-extract-plugin.${chunk.id}`,
179+
hash: chunk.contentHash[NS],
180+
});
181+
}
150182
}
151-
});
152-
compilation.chunkTemplate.hooks.renderManifest.tap(pluginName, (result, { chunk }) => {
153-
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
154-
if (renderedModules.length > 0) {
155-
result.push({
156-
render: () => this.renderContentAsset(renderedModules, compilation.runtimeTemplate.requestShortener),
157-
filenameTemplate: this.options.chunkFilename,
158-
pathOptions: {
159-
chunk,
160-
contentHashType: NS,
161-
},
162-
identifier: `mini-css-extract-plugin.${chunk.id}`,
163-
hash: chunk.contentHash[NS],
164-
});
183+
);
184+
compilation.chunkTemplate.hooks.renderManifest.tap(
185+
pluginName,
186+
(result, { chunk }) => {
187+
const renderedModules = Array.from(chunk.modulesIterable).filter(
188+
(module) => module.type === NS
189+
);
190+
if (renderedModules.length > 0) {
191+
result.push({
192+
render: () =>
193+
this.renderContentAsset(
194+
renderedModules,
195+
compilation.runtimeTemplate.requestShortener
196+
),
197+
filenameTemplate: this.options.chunkFilename,
198+
pathOptions: {
199+
chunk,
200+
contentHashType: NS,
201+
},
202+
identifier: `mini-css-extract-plugin.${chunk.id}`,
203+
hash: chunk.contentHash[NS],
204+
});
205+
}
165206
}
166-
});
207+
);
167208
compilation.mainTemplate.hooks.hashForChunk.tap(
168209
pluginName,
169210
(hash, chunk) => {
@@ -173,15 +214,13 @@ class MiniCssExtractPlugin {
173214
}
174215
if (REGEXP_CONTENTHASH.test(chunkFilename)) {
175216
hash.update(
176-
JSON.stringify(
177-
chunk.getChunkMaps(true).contentHash[NS] || {},
178-
),
217+
JSON.stringify(chunk.getChunkMaps(true).contentHash[NS] || {})
179218
);
180219
}
181220
if (REGEXP_NAME.test(chunkFilename)) {
182221
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
183222
}
184-
},
223+
}
185224
);
186225
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
187226
const { outputOptions } = compilation;
@@ -198,25 +237,22 @@ class MiniCssExtractPlugin {
198237
.substring(0, hashDigestLength);
199238
});
200239
const { mainTemplate } = compilation;
201-
mainTemplate.hooks.localVars.tap(
202-
pluginName,
203-
(source, chunk) => {
204-
const chunkMap = this.getCssChunkObject(chunk);
205-
if (Object.keys(chunkMap).length > 0) {
206-
return Template.asString([
207-
source,
208-
'',
209-
'// object to store loaded CSS chunks',
210-
'var installedCssChunks = {',
211-
Template.indent(
212-
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(',\n'),
213-
),
214-
'}',
215-
]);
216-
}
217-
return source;
218-
},
219-
);
240+
mainTemplate.hooks.localVars.tap(pluginName, (source, chunk) => {
241+
const chunkMap = this.getCssChunkObject(chunk);
242+
if (Object.keys(chunkMap).length > 0) {
243+
return Template.asString([
244+
source,
245+
'',
246+
'// object to store loaded CSS chunks',
247+
'var installedCssChunks = {',
248+
Template.indent(
249+
chunk.ids.map((id) => `${JSON.stringify(id)}: 0`).join(',\n')
250+
),
251+
'}',
252+
]);
253+
}
254+
return source;
255+
});
220256
mainTemplate.hooks.requireEnsure.tap(
221257
pluginName,
222258
(source, chunk, hash) => {
@@ -227,7 +263,7 @@ class MiniCssExtractPlugin {
227263
JSON.stringify(this.options.chunkFilename),
228264
{
229265
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
230-
hashWithLength: length =>
266+
hashWithLength: (length) =>
231267
`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
232268
chunk: {
233269
id: '" + chunkId + "',
@@ -236,14 +272,18 @@ class MiniCssExtractPlugin {
236272
const shortChunkHashMap = Object.create(null);
237273
for (const chunkId of Object.keys(chunkMaps.hash)) {
238274
if (typeof chunkMaps.hash[chunkId] === 'string') {
239-
shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substring(0, length);
275+
shortChunkHashMap[chunkId] = chunkMaps.hash[
276+
chunkId
277+
].substring(0, length);
240278
}
241279
}
242-
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
280+
return `" + ${JSON.stringify(
281+
shortChunkHashMap
282+
)}[chunkId] + "`;
243283
},
244284
contentHash: {
245285
[NS]: `" + ${JSON.stringify(
246-
chunkMaps.contentHash[NS],
286+
chunkMaps.contentHash[NS]
247287
)}[chunkId] + "`,
248288
},
249289
contentHashWithLength: {
@@ -258,14 +298,16 @@ class MiniCssExtractPlugin {
258298
}
259299
}
260300
return `" + ${JSON.stringify(
261-
shortContentHashMap,
301+
shortContentHashMap
262302
)}[chunkId] + "`;
263303
},
264304
},
265-
name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`,
305+
name: `" + (${JSON.stringify(
306+
chunkMaps.name
307+
)}[chunkId]||chunkId) + "`,
266308
},
267309
contentHashType: NS,
268-
},
310+
}
269311
);
270312
return Template.asString([
271313
source,
@@ -277,7 +319,6 @@ class MiniCssExtractPlugin {
277319
Template.indent([
278320
'promises.push(installedCssChunks[chunkId] = new Promise(function(resolve, reject) {',
279321
Template.indent([
280-
281322
`var href = ${linkHrefPath};`,
282323
`var fullhref = ${mainTemplate.requireFn}.p + href;`,
283324
'var existingLinkTags = document.getElementsByTagName("link");',
@@ -313,16 +354,15 @@ class MiniCssExtractPlugin {
313354
'head.appendChild(linkTag);',
314355
]),
315356
'}).then(function() {',
316-
Template.indent([
317-
'installedCssChunks[chunkId] = 0;',
318-
]),
357+
Template.indent(['installedCssChunks[chunkId] = 0;']),
319358
'}));',
320359
]),
321360
'}',
322361
]);
323362
}
324363
return source;
325-
});
364+
}
365+
);
326366
});
327367
}
328368

@@ -361,9 +401,20 @@ class MiniCssExtractPlugin {
361401
source.add(`@media ${m.media} {\n`);
362402
}
363403
if (m.sourceMap) {
364-
source.add(new SourceMapSource(m.content, m.readableIdentifier(requestShortener), m.sourceMap));
404+
source.add(
405+
new SourceMapSource(
406+
m.content,
407+
m.readableIdentifier(requestShortener),
408+
m.sourceMap
409+
)
410+
);
365411
} else {
366-
source.add(new OriginalSource(m.content, m.readableIdentifier(requestShortener)));
412+
source.add(
413+
new OriginalSource(
414+
m.content,
415+
m.readableIdentifier(requestShortener)
416+
)
417+
);
367418
}
368419
source.add('\n');
369420
if (m.media) {

0 commit comments

Comments
 (0)