Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor loader's code #448

Merged
merged 8 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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