Skip to content

Commit 93724d0

Browse files
committed
refactor: move dependency adding function into loader
1 parent 49199bf commit 93724d0

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

src/CssDependency.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import webpack from 'webpack';
2+
3+
export default class CssDependency extends webpack.Dependency {
4+
constructor(
5+
{ identifier, content, media, sourceMap },
6+
context,
7+
identifierIndex
8+
) {
9+
super();
10+
11+
this.identifier = identifier;
12+
this.identifierIndex = identifierIndex;
13+
this.content = content;
14+
this.media = media;
15+
this.sourceMap = sourceMap;
16+
this.context = context;
17+
}
18+
19+
getResourceIdentifier() {
20+
return `css-module-${this.identifier}-${this.identifierIndex}`;
21+
}
22+
}

src/index.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import webpack from 'webpack';
44
import sources from 'webpack-sources';
55

6+
import CssDependency from './CssDependency';
7+
68
const { ConcatSource, SourceMapSource, OriginalSource } = sources;
79
const {
810
Template,
@@ -19,27 +21,6 @@ const REGEXP_NAME = /\[name\]/i;
1921
const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
2022
const DEFAULT_FILENAME = '[name].css';
2123

22-
class CssDependency extends webpack.Dependency {
23-
constructor(
24-
{ identifier, content, media, sourceMap },
25-
context,
26-
identifierIndex
27-
) {
28-
super();
29-
30-
this.identifier = identifier;
31-
this.identifierIndex = identifierIndex;
32-
this.content = content;
33-
this.media = media;
34-
this.sourceMap = sourceMap;
35-
this.context = context;
36-
}
37-
38-
getResourceIdentifier() {
39-
return `css-module-${this.identifier}-${this.identifierIndex}`;
40-
}
41-
}
42-
4324
class CssDependencyTemplate {
4425
apply() {}
4526
}
@@ -148,33 +129,6 @@ class MiniCssExtractPlugin {
148129

149130
apply(compiler) {
150131
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
151-
compilation.hooks.normalModuleLoader.tap(
152-
pluginName,
153-
(loaderContext, module) => {
154-
// eslint-disable-next-line no-param-reassign
155-
loaderContext[MODULE_TYPE] = (content) => {
156-
if (!Array.isArray(content) && content != null) {
157-
throw new Error(
158-
`Exported value was not extracted as an array: ${JSON.stringify(
159-
content
160-
)}`
161-
);
162-
}
163-
164-
const identifierCountMap = new Map();
165-
166-
for (const line of content) {
167-
const count = identifierCountMap.get(line.identifier) || 0;
168-
169-
module.addDependency(
170-
new CssDependency(line, module.context, count)
171-
);
172-
identifierCountMap.set(line.identifier, count + 1);
173-
}
174-
};
175-
}
176-
);
177-
178132
compilation.dependencyFactories.set(
179133
CssDependency,
180134
new CssModuleFactory()

src/loader.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
1010
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
1111
import validateOptions from 'schema-utils';
1212

13+
import CssDependency from './CssDependency';
14+
1315
import schema from './options.json';
1416

15-
const MODULE_TYPE = 'css/mini-extract';
1617
const pluginName = 'mini-css-extract-plugin';
1718

1819
function hotLoader(content, context) {
@@ -133,6 +134,27 @@ export function pitch(request) {
133134
const callback = this.async();
134135

135136
childCompiler.runAsChild((err, entries, compilation) => {
137+
const addDependencies = (content) => {
138+
if (!Array.isArray(content) && content != null) {
139+
throw new Error(
140+
`Exported value was not extracted as an array: ${JSON.stringify(
141+
content
142+
)}`
143+
);
144+
}
145+
146+
const identifierCountMap = new Map();
147+
148+
for (const line of content) {
149+
const count = identifierCountMap.get(line.identifier) || 0;
150+
151+
this._module.addDependency(
152+
new CssDependency(line, module.context, count)
153+
);
154+
identifierCountMap.set(line.identifier, count + 1);
155+
}
156+
};
157+
136158
if (err) {
137159
return callback(err);
138160
}
@@ -173,7 +195,7 @@ export function pitch(request) {
173195
};
174196
});
175197
}
176-
this[MODULE_TYPE](text);
198+
addDependencies(text);
177199
} catch (e) {
178200
return callback(e);
179201
}

0 commit comments

Comments
 (0)