Skip to content

Commit

Permalink
refactor: loader's code (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
goganchic authored and evilebottnawi committed Sep 16, 2019
1 parent 159ce3b commit 50434b5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 101 deletions.
51 changes: 15 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/CssDependency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import webpack from 'webpack';

export default class CssDependency extends webpack.Dependency {
constructor(
{ identifier, content, media, sourceMap },
context,
identifierIndex
) {
super();

this.identifier = identifier;
this.identifierIndex = identifierIndex;
this.content = content;
this.media = media;
this.sourceMap = sourceMap;
this.context = context;
}

getResourceIdentifier() {
return `css-module-${this.identifier}-${this.identifierIndex}`;
}
}
47 changes: 2 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import webpack from 'webpack';
import sources from 'webpack-sources';

import CssDependency from './CssDependency';

const { ConcatSource, SourceMapSource, OriginalSource } = sources;
const {
Template,
Expand All @@ -19,27 +21,6 @@ const REGEXP_NAME = /\[name\]/i;
const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
const DEFAULT_FILENAME = '[name].css';

class CssDependency extends webpack.Dependency {
constructor(
{ identifier, content, media, sourceMap },
context,
identifierIndex
) {
super();

this.identifier = identifier;
this.identifierIndex = identifierIndex;
this.content = content;
this.media = media;
this.sourceMap = sourceMap;
this.context = context;
}

getResourceIdentifier() {
return `css-module-${this.identifier}-${this.identifierIndex}`;
}
}

class CssDependencyTemplate {
apply() {}
}
Expand Down Expand Up @@ -148,30 +129,6 @@ class MiniCssExtractPlugin {

apply(compiler) {
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.normalModuleLoader.tap(pluginName, (lc, m) => {
const loaderContext = lc;
const module = m;

loaderContext[MODULE_TYPE] = (content) => {
if (!Array.isArray(content) && content != null) {
throw new Error(
`Exported value was not extracted as an array: ${JSON.stringify(
content
)}`
);
}

const identifierCountMap = new Map();

for (const line of content) {
const count = identifierCountMap.get(line.identifier) || 0;

module.addDependency(new CssDependency(line, m.context, count));
identifierCountMap.set(line.identifier, count + 1);
}
};
});

compilation.dependencyFactories.set(
CssDependency,
new CssModuleFactory()
Expand Down
Loading

0 comments on commit 50434b5

Please sign in to comment.