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

Commit d7df331

Browse files
committed
refactor: Modernize loader syntax
- Convert to ES6+ Class - Convert to ES6+ default export - Convert to const / let - Uses import syntax - Convert errors to templates
1 parent ed1f68e commit d7df331

File tree

1 file changed

+106
-114
lines changed

1 file changed

+106
-114
lines changed

src/loader.js

Lines changed: 106 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,114 @@
1-
/* eslint-disable */
2-
var fs = require("fs");
3-
var loaderUtils = require("loader-utils");
4-
var NodeTemplatePlugin = require("webpack/lib/node/NodeTemplatePlugin");
5-
var NodeTargetPlugin = require("webpack/lib/node/NodeTargetPlugin");
6-
var LibraryTemplatePlugin = require("webpack/lib/LibraryTemplatePlugin");
7-
var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
8-
var LimitChunkCountPlugin = require("webpack/lib/optimize/LimitChunkCountPlugin");
1+
import fs from 'fs';
2+
import loaderUtils from 'loader-utils';
3+
import NodeTemplatePlugin from 'webpack/lib/node/NodeTemplatePlugin';
4+
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
5+
import LibraryTemplatePlugin from 'webpack/lib/LibraryTemplatePlugin';
6+
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
7+
import LimitChunkCountPlugin from 'webpack/lib/optimize/LimitChunkCountPlugin';
98

10-
var NS = fs.realpathSync(__dirname);
9+
const NS = fs.realpathSync(__dirname);
1110

12-
module.exports = function(source) {
13-
return source;
14-
};
11+
export default source => source;
1512

16-
module.exports.pitch = function(request) {
17-
var query = loaderUtils.getOptions(this) || {};
18-
var loaders = this.loaders.slice(this.loaderIndex + 1);
19-
this.addDependency(this.resourcePath);
20-
// We already in child compiler, return empty bundle
21-
if(this[NS] === undefined) {
22-
throw new Error(
23-
'"extract-text-webpack-plugin" loader is used without the corresponding plugin, ' +
24-
'refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example'
25-
);
26-
} else if(this[NS] === false) {
27-
return "";
28-
} else if(this[NS](null, query)) {
29-
if(query.omit) {
30-
this.loaderIndex += +query.omit + 1;
31-
request = request.split("!").slice(+query.omit).join("!");
32-
loaders = loaders.slice(+query.omit);
33-
}
34-
var resultSource;
35-
if(query.remove) {
36-
resultSource = "// removed by extract-text-webpack-plugin";
37-
} else {
38-
resultSource = undefined;
39-
}
13+
export function pitch(request) {
14+
const query = loaderUtils.getOptions(this) || {};
15+
let loaders = this.loaders.slice(this.loaderIndex + 1);
16+
this.addDependency(this.resourcePath);
17+
// We already in child compiler, return empty bundle
18+
if (this[NS] === undefined) { // eslint-disable-line no-undefined
19+
throw new Error(
20+
'"extract-text-webpack-plugin" loader is used without the corresponding plugin, ' +
21+
'refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example',
22+
);
23+
} else if (this[NS] === false) {
24+
return '';
25+
} else if (this[NS](null, query)) {
26+
if (query.omit) {
27+
this.loaderIndex += +query.omit + 1;
28+
request = request.split('!').slice(+query.omit).join('!');
29+
loaders = loaders.slice(+query.omit);
30+
}
31+
let resultSource;
32+
if (query.remove) {
33+
resultSource = '// removed by extract-text-webpack-plugin';
34+
} else {
35+
resultSource = undefined; // eslint-disable-line no-undefined
36+
}
4037

41-
var childFilename = "extract-text-webpack-plugin-output-filename"; // eslint-disable-line no-path-concat
42-
var publicPath = typeof query.publicPath === "string" ? query.publicPath : this._compilation.outputOptions.publicPath;
43-
var outputOptions = {
44-
filename: childFilename,
45-
publicPath: publicPath
46-
};
47-
var childCompiler = this._compilation.createChildCompiler("extract-text-webpack-plugin " + NS + " " + request, outputOptions);
48-
childCompiler.apply(new NodeTemplatePlugin(outputOptions));
49-
childCompiler.apply(new LibraryTemplatePlugin(null, "commonjs2"));
50-
childCompiler.apply(new NodeTargetPlugin());
51-
childCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request));
52-
childCompiler.apply(new LimitChunkCountPlugin({ maxChunks: 1 }));
53-
// We set loaderContext[NS] = false to indicate we already in
54-
// a child compiler so we don't spawn another child compilers from there.
55-
childCompiler.plugin("this-compilation", function(compilation) {
56-
compilation.plugin("normal-module-loader", function(loaderContext, module) {
57-
loaderContext[NS] = false;
58-
if (module.request === request) {
59-
module.loaders = loaders.map(function(loader) {
60-
return {
61-
loader: loader.path,
62-
options: loader.options
63-
};
64-
});
65-
}
66-
});
67-
});
38+
const childFilename = 'extract-text-webpack-plugin-output-filename'; // eslint-disable-line no-path-concat
39+
const publicPath = typeof query.publicPath === 'string' ? query.publicPath : this._compilation.outputOptions.publicPath;
40+
const outputOptions = {
41+
filename: childFilename,
42+
publicPath,
43+
};
44+
const childCompiler = this._compilation.createChildCompiler(`extract-text-webpack-plugin ${NS} ${request}`, outputOptions);
45+
childCompiler.apply(new NodeTemplatePlugin(outputOptions));
46+
childCompiler.apply(new LibraryTemplatePlugin(null, 'commonjs2'));
47+
childCompiler.apply(new NodeTargetPlugin());
48+
childCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`));
49+
childCompiler.apply(new LimitChunkCountPlugin({ maxChunks: 1 }));
50+
// We set loaderContext[NS] = false to indicate we already in
51+
// a child compiler so we don't spawn another child compilers from there.
52+
childCompiler.plugin('this-compilation', (compilation) => {
53+
compilation.plugin('normal-module-loader', (loaderContext, module) => {
54+
loaderContext[NS] = false;
55+
if (module.request === request) {
56+
module.loaders = loaders.map((loader) => {
57+
return ({
58+
loader: loader.path,
59+
options: loader.options,
60+
});
61+
});
62+
}
63+
});
64+
});
6865

69-
var source;
70-
childCompiler.plugin("after-compile", function(compilation, callback) {
71-
source = compilation.assets[childFilename] && compilation.assets[childFilename].source();
66+
let source;
67+
childCompiler.plugin('after-compile', (compilation, callback) => {
68+
source = compilation.assets[childFilename] && compilation.assets[childFilename].source();
7269

73-
// Remove all chunk assets
74-
compilation.chunks.forEach(function(chunk) {
75-
chunk.files.forEach(function(file) {
76-
delete compilation.assets[file];
77-
});
78-
});
70+
// Remove all chunk assets
71+
compilation.chunks.forEach((chunk) => {
72+
chunk.files.forEach((file) => {
73+
delete compilation.assets[file];
74+
});
75+
});
7976

80-
callback();
81-
});
82-
var callback = this.async();
83-
childCompiler.runAsChild(function(err, entries, compilation) {
84-
if(err) return callback(err);
77+
callback();
78+
});
79+
const callback = this.async();
80+
childCompiler.runAsChild((err, entries, compilation) => {
81+
if (err) return callback(err);
8582

86-
if(compilation.errors.length > 0) {
87-
return callback(compilation.errors[0]);
88-
}
89-
compilation.fileDependencies.forEach(function(dep) {
90-
this.addDependency(dep);
91-
}, this);
92-
compilation.contextDependencies.forEach(function(dep) {
93-
this.addContextDependency(dep);
94-
}, this);
95-
if(!source) {
96-
return callback(new Error("Didn't get a result from child compiler"));
97-
}
98-
try {
99-
var text = this.exec(source, request);
100-
if(typeof text === "string")
101-
text = [[0, text]];
102-
text.forEach(function(item) {
103-
var id = item[0];
104-
compilation.modules.forEach(function(module) {
105-
if(module.id === id)
106-
item[0] = module.identifier();
107-
});
108-
});
109-
this[NS](text, query);
110-
if(text.locals && typeof resultSource !== "undefined") {
111-
resultSource += "\nmodule.exports = " + JSON.stringify(text.locals) + ";";
112-
}
113-
} catch(e) {
114-
return callback(e);
115-
}
116-
if(resultSource)
117-
callback(null, resultSource);
118-
else
119-
callback();
120-
}.bind(this));
121-
}
122-
};
83+
if (compilation.errors.length > 0) {
84+
return callback(compilation.errors[0]);
85+
}
86+
compilation.fileDependencies.forEach((dep) => {
87+
this.addDependency(dep);
88+
}, this);
89+
compilation.contextDependencies.forEach((dep) => {
90+
this.addContextDependency(dep);
91+
}, this);
92+
if (!source) {
93+
return callback(new Error("Didn't get a result from child compiler"));
94+
}
95+
try {
96+
let text = this.exec(source, request);
97+
if (typeof text === 'string') { text = [[0, text]]; }
98+
text.forEach((item) => {
99+
const id = item[0];
100+
compilation.modules.forEach((module) => {
101+
if (module.id === id) { item[0] = module.identifier(); }
102+
});
103+
});
104+
this[NS](text, query);
105+
if (text.locals && typeof resultSource !== 'undefined') {
106+
resultSource += `\nmodule.exports = ${JSON.stringify(text.locals)};`;
107+
}
108+
} catch (e) {
109+
return callback(e);
110+
}
111+
if (resultSource) { callback(null, resultSource); } else { callback(); }
112+
});
113+
}
114+
}

0 commit comments

Comments
 (0)