Skip to content

Commit 1330534

Browse files
refactor: stuff
1 parent ca846b4 commit 1330534

File tree

7 files changed

+67
-57
lines changed

7 files changed

+67
-57
lines changed

src/SyntaxError.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ export default class SyntaxError extends Error {
55
const { reason, line, column } = error;
66

77
this.name = 'SyntaxError';
8-
this.message = reason;
98

10-
if (line && column) {
11-
this.message += ` (${line}:${column})`;
9+
// Based on https://github.com/postcss/postcss/blob/master/lib/css-syntax-error.es6#L132
10+
// We don't need `plugin` and `file` properties.
11+
this.message = `${this.name}\n\n`;
12+
13+
if (typeof line !== 'undefined') {
14+
this.message += `(${line}:${column}) `;
1215
}
1316

17+
this.message += `${reason}`;
18+
1419
const code = error.showSourceCode();
1520

1621
if (code) {
1722
this.message += `\n\n${code}\n`;
1823
}
1924

20-
// We don't need stack https://github.com/postcss/postcss/blob/ebaa53640657fb028803d624395ea76a8df11cbe/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
25+
// We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
2126
this.stack = false;
2227
}
2328
}

src/Warning.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ export default class Warning extends Error {
22
constructor(warning) {
33
super(warning);
44

5+
const { text, line, column } = warning;
6+
57
this.name = 'Warning';
6-
this.message = warning.toString();
8+
// Based on https://github.com/postcss/postcss/blob/master/lib/warning.es6#L74
9+
// We don't need `plugin` properties.
10+
this.message = `${this.name}\n\n`;
11+
12+
if (typeof line !== 'undefined') {
13+
this.message += `(${line}:${column}) `;
14+
}
15+
16+
this.message += `${text}`;
717

8-
// We don't need stack https://github.com/postcss/postcss/blob/ebaa53640657fb028803d624395ea76a8df11cbe/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
18+
// We don't need stack https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
919
this.stack = false;
1020
}
1121
}

src/index.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,14 @@ export default function loader(content, map, meta) {
3636
options
3737
);
3838

39-
let prevMap = map;
40-
41-
if (sourceMap && prevMap) {
42-
if (typeof prevMap === 'string') {
43-
prevMap = JSON.parse(prevMap);
44-
}
45-
46-
if (prevMap.sources) {
47-
prevMap.sources = prevMap.sources.map((source) =>
48-
source.replace(/\\/g, '/')
49-
);
50-
prevMap.sourceRoot = '';
51-
}
52-
} else {
53-
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
54-
prevMap = null;
55-
}
39+
const plugins = [
40+
plugin({
41+
loaderContext: this,
42+
url,
43+
import: importOpt,
44+
importLoaders,
45+
}),
46+
];
5647

5748
let contentOrAst = content;
5849

@@ -65,14 +56,20 @@ export default function loader(content, map, meta) {
6556
}
6657
}
6758

68-
const plugins = [
69-
plugin({
70-
loaderContext: this,
71-
url,
72-
import: importOpt,
73-
importLoaders,
74-
}),
75-
];
59+
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
60+
let prevMap = sourceMap && map ? map : null;
61+
62+
// Some loader emit source map as `{String}`
63+
if (sourceMap && typeof map === 'string') {
64+
prevMap = JSON.parse(map);
65+
}
66+
67+
if (sourceMap && prevMap) {
68+
prevMap.sources = prevMap.sources.map((source) =>
69+
source.replace(/\\/g, '/')
70+
);
71+
prevMap.sourceRoot = '';
72+
}
7673

7774
const postcssOptions = {
7875
// We need a prefix to avoid path rewriting of PostCSS
@@ -108,17 +105,13 @@ export default function loader(content, map, meta) {
108105

109106
if (sourceMap && newMap) {
110107
newMap = newMap.toJSON();
111-
112-
if (newMap.sources) {
113-
newMap.sources = newMap.sources.map((source) =>
114-
source
115-
.split('!')
116-
.pop()
117-
.replace(/\\/g, '/')
118-
);
119-
newMap.sourceRoot = '';
120-
}
121-
108+
newMap.sources = newMap.sources.map((source) =>
109+
source
110+
.split('!')
111+
.pop()
112+
.replace(/\\/g, '/')
113+
);
114+
newMap.sourceRoot = '';
122115
newMap.file = newMap.file
123116
.split('!')
124117
.pop()
@@ -140,12 +133,10 @@ export default function loader(content, map, meta) {
140133

141134
if (result.messages && result.messages.length > 0) {
142135
result.messages
143-
.filter(
144-
(message) => (message.type === 'modify-module' ? message : false)
145-
)
136+
.filter((message) => (message.type === 'module' ? message : false))
146137
.forEach((message) => {
147138
try {
148-
moduleObj = message.modifyModule(moduleObj, this);
139+
moduleObj = message.modify(moduleObj, this);
149140
} catch (err) {
150141
this.emitError(err);
151142
}

src/plugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export default postcss.plugin(
8585
if (!alreadyImported[url]) {
8686
result.messages.push({
8787
pluginName,
88-
type: 'modify-module',
89-
modifyModule(moduleObj) {
88+
type: 'module',
89+
modify(moduleObj) {
9090
// eslint-disable-next-line no-param-reassign
9191
moduleObj.runtime = `${moduleObj.runtime}${runtimeCode}\n`;
9292

@@ -155,8 +155,8 @@ export default postcss.plugin(
155155

156156
result.messages.push({
157157
pluginName,
158-
type: 'modify-module',
159-
modifyModule(moduleObj, loaderContext) {
158+
type: 'module',
159+
modify(moduleObj, loaderContext) {
160160
if (!hasURLEscapeRuntimeCode) {
161161
// eslint-disable-next-line no-param-reassign
162162
moduleObj.imports = `var runtimeEscape = require(${stringifyRequest(

src/runtime.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module.exports = function(useSourceMap) {
55
const list = [];
66

7-
// return the list of modules as css string
7+
// Return the list of modules as css string
88
list.toString = function toString() {
99
return this.map(function(item) {
1010
const content = cssWithMappingToString(item, useSourceMap);
@@ -17,7 +17,7 @@ module.exports = function(useSourceMap) {
1717
}).join('');
1818
};
1919

20-
// import a list of modules into the list
20+
// Import a list of modules into the list
2121
list.i = function(modules, mediaQuery) {
2222
if (typeof modules === 'string') {
2323
modules = [[null, modules, '']];
@@ -36,10 +36,10 @@ module.exports = function(useSourceMap) {
3636
for (let i = 0; i < modules.length; i++) {
3737
const item = modules[i];
3838

39-
// skip already imported module
40-
// this implementation is not 100% perfect for weird media query combinations
39+
// Skip already imported module.
40+
// This implementation is not 100% perfect for weird media query combinations
4141
// when a module is imported multiple times with different media queries.
42-
// I hope this will never occur (Hey this way we have smaller bundles)
42+
// I hope this will never occur (Hey this way we have smaller bundles).
4343
if (typeof item[0] !== 'number' || !isImported[item[0]]) {
4444
if (mediaQuery && !item[2]) {
4545
item[2] = mediaQuery;

test/__snapshots__/SyntaxError.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
exports[`SyntaxError basic: errors 1`] = `
44
Array [
55
[ModuleBuildError: Module build failed (from \`replaced original path\`):
6-
Unknown word (2:3)
6+
SyntaxError
7+
8+
(2:3) Unknown word
79
810
 1 | .some {
911
> 2 |  invalid css;

test/__snapshots__/import-option.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ exports[`import true from modules: warnings 1`] = `Array []`;
225225
exports[`import true invalid: errors 1`] = `
226226
Array [
227227
[ModuleBuildError: Module build failed (from \`replaced original path\`):
228-
Unexpected format (\`replaced original path\`)
228+
SyntaxError
229+
230+
(1:1) Unexpected format
229231
230232
> 1 | @import;
231233
 | ^

0 commit comments

Comments
 (0)