Skip to content

Commit 45469b7

Browse files
refactor: loader and plugin
1 parent 1aaecdd commit 45469b7

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

lib/loader.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ module.exports = function(content, map, meta) {
4141

4242
const plugins = [
4343
plugin({
44+
loaderContext: this,
4445
url: options.url !== false,
4546
import: options.import !== false,
46-
loaderContext: this,
4747
importLoaders: options.importLoaders
4848
})
4949
];
@@ -98,7 +98,7 @@ module.exports = function(content, map, meta) {
9898
}
9999

100100
// Todo need save backward compatibility with old `style-loader` and exports.locals
101-
let newContent = {
101+
let newContentObj = {
102102
imports: "",
103103
runtime: `module.exports = exports = require(${loaderUtils.stringifyRequest(
104104
this,
@@ -111,31 +111,29 @@ module.exports = function(content, map, meta) {
111111
};
112112

113113
if (result.messages && result.messages.length > 0) {
114-
newContent = result.messages
114+
result.messages
115115
.filter(
116116
message =>
117-
message.type === "modify-runtime-code" ? message : false
117+
message.type === "modify-generated-code" ? message : false
118118
)
119-
.reduce((initialValue, message) => {
119+
.forEach((message) => {
120120
try {
121-
initialValue = message.modifyRuntimeCode(this, initialValue);
121+
newContentObj = message.modifyGeneratedCode(this, newContentObj);
122122
} catch (err) {
123123
this.emitError(err);
124124
}
125-
126-
return initialValue;
127-
}, newContent);
125+
});
128126
}
129127

130-
const { imports, runtime, module, exports } = newContent;
128+
const { imports, runtime, module, exports } = newContentObj;
131129

132130
cb(
133131
null,
134132
[
135133
imports ? `// CSS imports\n${imports}` : "",
136134
runtime ? `// CSS runtime\n${runtime}` : "",
137-
module ? `// CSS module\n${newContent.module}` : "",
138-
exports ? `// CSS exports\n${newContent.exports}` : ""
135+
module ? `// CSS module\n${module}` : "",
136+
exports ? `// CSS exports\n${exports}` : ""
139137
].join("\n")
140138
);
141139

lib/plugin.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ module.exports = postcss.plugin(pluginName, function(options) {
8181
if (!alreadyImported[url]) {
8282
result.messages.push({
8383
pluginName,
84-
type: "modify-runtime-code",
85-
modifyRuntimeCode: (loaderContext, content) => {
86-
content.runtime = `${content.runtime}${runtimeCode}\n`;
84+
type: "modify-generated-code",
85+
modifyGeneratedCode: (loaderContext, contentObj) => {
86+
contentObj.runtime = `${contentObj.runtime}${runtimeCode}\n`;
8787

88-
return content;
88+
return contentObj;
8989
}
9090
});
9191

@@ -148,32 +148,32 @@ module.exports = postcss.plugin(pluginName, function(options) {
148148

149149
result.messages.push({
150150
pluginName,
151-
type: "modify-runtime-code",
152-
modifyRuntimeCode: (loaderContext, content) => {
151+
type: "modify-generated-code",
152+
modifyGeneratedCode: (loaderContext, contentObj) => {
153153
if (!hasURLEscapeRuntimeCode) {
154-
content.imports = `var runtimeEscape = require(${loaderUtils.stringifyRequest(
154+
contentObj.imports = `var runtimeEscape = require(${loaderUtils.stringifyRequest(
155155
loaderContext,
156156
require.resolve("./runtimeEscape.js")
157-
)});\n${content.imports}`;
157+
)});\n${contentObj.imports}`;
158158

159159
hasURLEscapeRuntimeCode = true;
160160
}
161161

162-
content.imports = `${
163-
content.imports
162+
contentObj.imports = `${
163+
contentObj.imports
164164
}var ${placeholder} = require(${loaderUtils.stringifyRequest(
165165
loaderContext,
166166
requestedURL
167167
)});\n`;
168168

169-
content.module = content.module.replace(
169+
contentObj.module = contentObj.module.replace(
170170
placeholder,
171171
`" + runtimeEscape(${placeholder}) + "${
172172
splittedURL[1] ? splittedURL[1] : ""
173173
}${splittedURL[2] ? `#${splittedURL[2]}` : ""}`
174174
);
175175

176-
return content;
176+
return contentObj;
177177
}
178178
});
179179

0 commit comments

Comments
 (0)