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 5 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
41 changes: 11 additions & 30 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
57 changes: 38 additions & 19 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
import validateOptions from 'schema-utils';

import CssDependency from './CssDependency';

import schema from './options.json';

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

function hotLoader(content, context) {
Expand All @@ -36,25 +37,25 @@ function hotLoader(content, context) {
`;
}

const exec = (loaderContext, code, filename) => {
function evalModuleCode(loaderContext, code, filename) {
const module = new NativeModule(filename, loaderContext);

module.paths = NativeModule._nodeModulePaths(loaderContext.context); // eslint-disable-line no-underscore-dangle
module.filename = filename;
module._compile(code, filename); // eslint-disable-line no-underscore-dangle

return module.exports;
};
}

const findModuleById = (modules, id) => {
function findModuleById(modules, id) {
for (const module of modules) {
if (module.id === id) {
return module;
}
}

return null;
};
}

export function pitch(request) {
const options = loaderUtils.getOptions(this) || {};
Expand Down Expand Up @@ -91,8 +92,6 @@ export function pitch(request) {
);
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);

// We set loaderContext[MODULE_TYPE] = false to indicate we already in
// a child compiler so we don't spawn another child compilers from there.
childCompiler.hooks.thisCompilation.tap(
`${pluginName} loader`,
(compilation) => {
Expand All @@ -101,7 +100,6 @@ export function pitch(request) {
(loaderContext, module) => {
// eslint-disable-next-line no-param-reassign
loaderContext.emitFile = this.emitFile;
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign

if (module.request === request) {
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -136,6 +134,27 @@ export function pitch(request) {
const callback = this.async();

childCompiler.runAsChild((err, entries, compilation) => {
const addDependencies = (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;

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

if (err) {
return callback(err);
}
Expand All @@ -156,27 +175,27 @@ export function pitch(request) {
return callback(new Error("Didn't get a result from child compiler"));
}

let text;
let locals;

try {
text = exec(this, source, request);
locals = text && text.locals;
if (!Array.isArray(text)) {
text = [[null, text]];
let dependencies;
const exports = evalModuleCode(this, source, request);
locals = exports && exports.locals;
if (!Array.isArray(exports)) {
dependencies = [[null, exports]];
} else {
text = text.map((line) => {
const module = findModuleById(compilation.modules, line[0]);
dependencies = exports.map(([id, content, media, sourceMap]) => {
const module = findModuleById(compilation.modules, id);

return {
identifier: module.identifier(),
content: line[1],
media: line[2],
sourceMap: line[3],
content,
media,
sourceMap,
};
});
}
this[MODULE_TYPE](text);
addDependencies(dependencies);
} catch (e) {
return callback(e);
}
Expand Down