Skip to content

Commit 50434b5

Browse files
goganchicevilebottnawi
authored andcommitted
refactor: loader's code (#448)
1 parent 159ce3b commit 50434b5

File tree

4 files changed

+78
-101
lines changed

4 files changed

+78
-101
lines changed

package-lock.json

+15-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CssDependency.js

+22
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

+2-45
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,30 +129,6 @@ class MiniCssExtractPlugin {
148129

149130
apply(compiler) {
150131
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
151-
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
152-
const loaderContext = lc;
153-
const module = m;
154-
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(new CssDependency(line, m.context, count));
170-
identifierCountMap.set(line.identifier, count + 1);
171-
}
172-
};
173-
});
174-
175132
compilation.dependencyFactories.set(
176133
CssDependency,
177134
new CssModuleFactory()

0 commit comments

Comments
 (0)