Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit bda6004

Browse files
committed
refactor: Extract compilation to stand alone class
1 parent 49ad21e commit bda6004

File tree

2 files changed

+65
-100
lines changed

2 files changed

+65
-100
lines changed

src/index.js

Lines changed: 18 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
/* eslint-disable */
2-
var fs = require('fs');
3-
var ConcatSource = require("webpack-sources").ConcatSource;
4-
var async = require("async");
5-
var ExtractedModule = require("./ExtractedModule");
6-
var Chunk = require("webpack/lib/Chunk");
7-
var OrderUndefinedError = require("./OrderUndefinedError");
8-
var loaderUtils = require("loader-utils");
9-
var validateOptions = require('schema-utils');
10-
var path = require('path');
2+
import fs from 'fs';
3+
import path from 'path';
4+
import Chunk from 'webpack/lib/Chunk';
5+
import { ConcatSource } from 'webpack-sources';
6+
import async from 'async';
7+
import loaderUtils from 'loader-utils';
8+
import validateOptions from 'schema-utils';
9+
import ExtractTextPluginCompilation from './lib/ExtractTextPluginCompilation';
10+
import OrderUndefinedError from './lib/OrderUndefinedError';
11+
import {
12+
isInitialOrHasNoParents,
13+
isInvalidOrder,
14+
getOrder,
15+
getLoaderObject,
16+
mergeOptions,
17+
isString,
18+
isFunction,
19+
} from './lib/helpers';
1120

1221
var NS = fs.realpathSync(__dirname);
1322

1423
var nextId = 0;
1524

16-
function ExtractTextPluginCompilation() {
17-
this.modulesByIdentifier = {};
18-
}
19-
20-
function isInitialOrHasNoParents(chunk) {
21-
return chunk.isInitial() || chunk.parents.length === 0;
22-
}
23-
2425
ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, checkedChunks) {
2526
if(!intoChunk) {
2627
checkedChunks = [];
@@ -41,36 +42,6 @@ ExtractTextPlugin.prototype.mergeNonInitialChunks = function(chunk, intoChunk, c
4142
}
4243
};
4344

44-
ExtractTextPluginCompilation.prototype.addModule = function(identifier, originalModule, source, additionalInformation, sourceMap, prevModules) {
45-
var m;
46-
if(!this.modulesByIdentifier[identifier]) {
47-
m = this.modulesByIdentifier[identifier] = new ExtractedModule(identifier, originalModule, source, sourceMap, additionalInformation, prevModules);
48-
} else {
49-
m = this.modulesByIdentifier[identifier];
50-
m.addPrevModules(prevModules);
51-
if(originalModule.index2 < m.getOriginalModule().index2) {
52-
m.setOriginalModule(originalModule);
53-
}
54-
}
55-
return m;
56-
};
57-
58-
ExtractTextPluginCompilation.prototype.addResultToChunk = function(identifier, result, originalModule, extractedChunk) {
59-
if(!Array.isArray(result)) {
60-
result = [[identifier, result]];
61-
}
62-
var counterMap = {};
63-
var prevModules = [];
64-
result.forEach(function(item) {
65-
var c = counterMap[item[0]];
66-
var module = this.addModule.call(this, item[0] + (c || ""), originalModule, item[1], item[2], item[3], prevModules.slice());
67-
extractedChunk.addModule(module);
68-
module.addChunk(extractedChunk);
69-
counterMap[item[0]] = (c || 0) + 1;
70-
prevModules.push(module);
71-
}, this);
72-
};
73-
7445
ExtractTextPlugin.prototype.renderExtractedChunk = function(chunk) {
7546
var source = new ConcatSource();
7647
chunk.modules.forEach(function(module) {
@@ -80,32 +51,6 @@ ExtractTextPlugin.prototype.renderExtractedChunk = function(chunk) {
8051
return source;
8152
};
8253

83-
function isInvalidOrder(a, b) {
84-
var bBeforeA = a.getPrevModules().indexOf(b) >= 0;
85-
var aBeforeB = b.getPrevModules().indexOf(a) >= 0;
86-
return aBeforeB && bBeforeA;
87-
}
88-
89-
function getOrder(a, b) {
90-
var aOrder = a.getOrder();
91-
var bOrder = b.getOrder();
92-
if(aOrder < bOrder) return -1;
93-
if(aOrder > bOrder) return 1;
94-
var aIndex = a.getOriginalModule().index2;
95-
var bIndex = b.getOriginalModule().index2;
96-
if(aIndex < bIndex) return -1;
97-
if(aIndex > bIndex) return 1;
98-
var bBeforeA = a.getPrevModules().indexOf(b) >= 0;
99-
var aBeforeB = b.getPrevModules().indexOf(a) >= 0;
100-
if(aBeforeB && !bBeforeA) return -1;
101-
if(!aBeforeB && bBeforeA) return 1;
102-
var ai = a.identifier();
103-
var bi = b.identifier();
104-
if(ai < bi) return -1;
105-
if(ai > bi) return 1;
106-
return 0;
107-
}
108-
10954
function ExtractTextPlugin(options) {
11055
if(arguments.length > 1) {
11156
throw new Error("Breaking change: ExtractTextPlugin now only takes a single argument. Either an options " +
@@ -134,33 +79,6 @@ function ExtractTextPlugin(options) {
13479
}
13580
module.exports = ExtractTextPlugin;
13681

137-
function getLoaderObject(loader) {
138-
if (isString(loader)) {
139-
return {loader: loader};
140-
}
141-
return loader;
142-
}
143-
144-
function mergeOptions(a, b) {
145-
if(!b) return a;
146-
Object.keys(b).forEach(function(key) {
147-
a[key] = b[key];
148-
});
149-
return a;
150-
}
151-
152-
function isString(a) {
153-
return typeof a === "string";
154-
}
155-
156-
function isFunction(a) {
157-
return isType('Function', a);
158-
}
159-
160-
function isType(type, obj) {
161-
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
162-
}
163-
16482
ExtractTextPlugin.loader = function(options) {
16583
return { loader: require.resolve("./loader"), options: options };
16684
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-disable no-multi-assign */
2+
import ExtractedModule from './ExtractedModule';
3+
4+
class ExtractTextPluginCompilation {
5+
constructor() {
6+
this.modulesByIdentifier = {};
7+
}
8+
9+
addModule(
10+
identifier,
11+
originalModule,
12+
source,
13+
additionalInformation,
14+
sourceMap,
15+
prevModules) {
16+
let m;
17+
if (!this.modulesByIdentifier[identifier]) {
18+
m = this.modulesByIdentifier[identifier] =
19+
new ExtractedModule(identifier, originalModule, source, sourceMap, additionalInformation, prevModules);
20+
} else {
21+
m = this.modulesByIdentifier[identifier];
22+
m.addPrevModules(prevModules);
23+
if (originalModule.index2 < m.getOriginalModule().index2) {
24+
m.setOriginalModule(originalModule);
25+
}
26+
}
27+
return m;
28+
}
29+
30+
addResultToChunk(identifier, result, originalModule, extractedChunk) {
31+
if (!Array.isArray(result)) {
32+
result = [[identifier, result]];
33+
}
34+
const counterMap = {};
35+
const prevModules = [];
36+
result.forEach((item) => {
37+
const c = counterMap[item[0]];
38+
const module = this.addModule.call(this, item[0] + (c || ''), originalModule, item[1], item[2], item[3], prevModules.slice());
39+
extractedChunk.addModule(module);
40+
module.addChunk(extractedChunk);
41+
counterMap[item[0]] = (c || 0) + 1;
42+
prevModules.push(module);
43+
}, this);
44+
}
45+
}
46+
47+
export default ExtractTextPluginCompilation;

0 commit comments

Comments
 (0)