Skip to content

Commit 25a16a0

Browse files
refactor: source map code
BREAKING CHANGE: `sources` in source maps are absolute
1 parent 677c2fe commit 25a16a0

27 files changed

+1988
-595
lines changed

package-lock.json

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"dependencies": {
4545
"cosmiconfig": "^7.0.0",
4646
"loader-utils": "^2.0.0",
47-
"normalize-path": "^3.0.0",
4847
"postcss": "^7.0.0",
4948
"schema-utils": "^2.7.0"
5049
},
@@ -80,7 +79,7 @@
8079
"postcss-short": "^5.0.0",
8180
"prettier": "^2.0.5",
8281
"sass": "^1.26.10",
83-
"sass-loader": "^9.0.3",
82+
"sass-loader": "^10.0.1",
8483
"standard": "^14.3.4",
8584
"standard-version": "^8.0.2",
8685
"strip-ansi": "^6.0.0",

src/index.js

+23-36
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import {
1212
loadConfig,
1313
getPostcssOptions,
1414
exec,
15-
getSourceMapAbsolutePath,
16-
getSourceMapRelativePath,
1715
normalizeSourceMap,
16+
normalizeSourceMapAfterPostcss,
1817
} from './utils';
1918

2019
/**
@@ -38,43 +37,40 @@ export default async function loader(content, sourceMap, meta = {}) {
3837
baseDataPath: 'options',
3938
});
4039

41-
const file = this.resourcePath;
42-
const configOptions =
40+
const callback = this.async();
41+
const configOption =
4342
typeof options.postcssOptions === 'undefined' ||
4443
typeof options.postcssOptions.config === 'undefined'
4544
? true
4645
: options.postcssOptions.config;
47-
4846
let loadedConfig = {};
4947

50-
const callback = this.async();
51-
52-
if (configOptions) {
48+
if (configOption) {
5349
const dataForLoadConfig = {
54-
path: path.dirname(file),
50+
path: path.dirname(this.resourcePath),
5551
ctx: {
5652
file: {
57-
extname: path.extname(file),
58-
dirname: path.dirname(file),
59-
basename: path.basename(file),
53+
extname: path.extname(this.resourcePath),
54+
dirname: path.dirname(this.resourcePath),
55+
basename: path.basename(this.resourcePath),
6056
},
6157
options: {},
6258
},
6359
};
6460

65-
if (typeof configOptions.path !== 'undefined') {
66-
dataForLoadConfig.path = path.resolve(configOptions.path);
61+
if (typeof configOption.path !== 'undefined') {
62+
dataForLoadConfig.path = path.resolve(configOption.path);
6763
}
6864

69-
if (typeof configOptions.ctx !== 'undefined') {
70-
dataForLoadConfig.ctx.options = configOptions.ctx;
65+
if (typeof configOption.ctx !== 'undefined') {
66+
dataForLoadConfig.ctx.options = configOption.ctx;
7167
}
7268

7369
dataForLoadConfig.ctx.webpack = this;
7470

7571
try {
7672
loadedConfig = await loadConfig(
77-
configOptions,
73+
configOption,
7874
dataForLoadConfig.ctx,
7975
dataForLoadConfig.path,
8076
this
@@ -104,18 +100,12 @@ export default async function loader(content, sourceMap, meta = {}) {
104100

105101
if (useSourceMap) {
106102
processOptions.map = { inline: false, annotation: false };
107-
// options.sourceMap === 'inline'
108-
// ? { inline: true, annotation: false }
109-
// : { inline: false, annotation: false };
110103

111104
if (sourceMap) {
112-
const sourceMapNormalized = normalizeSourceMap(sourceMap);
113-
114-
sourceMapNormalized.sources = sourceMapNormalized.sources.map((src) =>
115-
getSourceMapRelativePath(src, path.dirname(file))
105+
processOptions.map.prev = normalizeSourceMap(
106+
sourceMap,
107+
this.resourcePath
116108
);
117-
118-
processOptions.map.prev = sourceMapNormalized;
119109
}
120110
}
121111

@@ -137,11 +127,11 @@ export default async function loader(content, sourceMap, meta = {}) {
137127
return;
138128
}
139129

140-
result.warnings().forEach((warning) => {
130+
for (const warning of result.warnings()) {
141131
this.emitWarning(new Warning(warning));
142-
});
132+
}
143133

144-
result.messages.forEach((message) => {
134+
for (const message of result.messages) {
145135
if (message.type === 'dependency') {
146136
this.addDependency(message.file);
147137
}
@@ -154,16 +144,13 @@ export default async function loader(content, sourceMap, meta = {}) {
154144
message.info
155145
);
156146
}
157-
});
147+
}
158148

159-
const map = result.map ? result.map.toJSON() : null;
149+
// eslint-disable-next-line no-undefined
150+
let map = result.map ? result.map.toJSON() : undefined;
160151

161152
if (map && useSourceMap) {
162-
if (typeof map.file !== 'undefined') {
163-
delete map.file;
164-
}
165-
166-
map.sources = map.sources.map((src) => getSourceMapAbsolutePath(src, file));
153+
map = normalizeSourceMapAfterPostcss(map, this.resourcePath);
167154
}
168155

169156
const ast = {

src/options.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@
7575
]
7676
},
7777
"plugins": {
78-
"description": "Set PostCSS Plugins (https://github.com/postcss/postcss-loader#plugins)",
79-
"anyOf": [
80-
{ "type": "array" },
81-
{ "type": "object" },
82-
{ "instanceof": "Function" }
83-
]
78+
"description": "Sets PostCSS Plugins (https://github.com/postcss/postcss-loader#plugins)",
79+
"anyOf": [{ "type": "array" }, { "type": "object" }]
8480
}
8581
}
8682
},
@@ -90,13 +86,13 @@
9086
]
9187
},
9288
"exec": {
93-
"description": "Enable PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)",
89+
"description": "Enables/Disables PostCSS Parser support in 'CSS-in-JS' (https://github.com/postcss/postcss-loader#exec)",
9490
"type": "boolean"
9591
},
9692
"sourceMap": {
9793
"description": "Enables/Disables generation of source maps (https://github.com/postcss/postcss-loader#sourcemap)",
9894
"type": "boolean"
9995
}
10096
},
101-
"additionalProperties": true
97+
"additionalProperties": false
10298
}

0 commit comments

Comments
 (0)