Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 654e0d6

Browse files
authoredOct 27, 2020
feat: added the sourceFilename property to asset info with original filename (#393)
1 parent 381d8bd commit 654e0d6

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
 

‎src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getOptions, interpolateName } from 'loader-utils';
44
import { validate } from 'schema-utils';
55

66
import schema from './options.json';
7+
import { normalizePath } from './utils';
78

89
export default function loader(content) {
910
const options = getOptions(this);
@@ -73,6 +74,10 @@ export default function loader(content) {
7374
}
7475
}
7576

77+
assetInfo.sourceFilename = normalizePath(
78+
path.relative(this.rootContext, this.resourcePath)
79+
);
80+
7681
this.emitFile(outputPath, content, null, assetInfo);
7782
}
7883

‎src/utils.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function normalizePath(path, stripTrailing) {
2+
if (path === '\\' || path === '/') {
3+
return '/';
4+
}
5+
6+
const len = path.length;
7+
8+
if (len <= 1) {
9+
return path;
10+
}
11+
12+
// ensure that win32 namespaces has two leading slashes, so that the path is
13+
// handled properly by the win32 version of path.parse() after being normalized
14+
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
15+
let prefix = '';
16+
17+
if (len > 4 && path[3] === '\\') {
18+
// eslint-disable-next-line prefer-destructuring
19+
const ch = path[2];
20+
21+
if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
22+
// eslint-disable-next-line no-param-reassign
23+
path = path.slice(2);
24+
prefix = '//';
25+
}
26+
}
27+
28+
const segs = path.split(/[/\\]+/);
29+
30+
if (stripTrailing !== false && segs[segs.length - 1] === '') {
31+
segs.pop();
32+
}
33+
34+
return prefix + segs.join('/');
35+
}
36+
37+
// eslint-disable-next-line import/prefer-default-export
38+
export { normalizePath };

‎test/name-option.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,47 @@ describe('"name" option', () => {
157157
}
158158
}
159159
});
160+
161+
it('should work and add "sourceFilename" to asset info', async () => {
162+
expect.assertions(1);
163+
164+
const compiler = getCompiler('simple.js');
165+
const stats = await compile(compiler);
166+
167+
for (const [name, info] of stats.compilation.assetsInfo) {
168+
if (name.endsWith('.png')) {
169+
expect(info.sourceFilename).toBe('file.png');
170+
}
171+
}
172+
});
173+
174+
it('should work and add "sourceFilename" to asset info #2', async () => {
175+
expect.assertions(1);
176+
177+
const compiler = getCompiler('simple.js', {
178+
name: '[name].asset.[ext]?foo=[contenthash]',
179+
});
180+
const stats = await compile(compiler);
181+
182+
for (const [name, info] of stats.compilation.assetsInfo) {
183+
if (name.startsWith('file.asset.png')) {
184+
expect(info.sourceFilename).toBe('file.png');
185+
}
186+
}
187+
});
188+
189+
it('should work and add "sourceFilename" to asset info #3', async () => {
190+
expect.assertions(1);
191+
192+
const compiler = getCompiler('cdn.js', {
193+
name: '[name].asset.[ext]',
194+
});
195+
const stats = await compile(compiler);
196+
197+
for (const [name, info] of stats.compilation.assetsInfo) {
198+
if (name.startsWith('file.asset.png')) {
199+
expect(info.sourceFilename).toBe('nested/file.png');
200+
}
201+
}
202+
});
160203
});

0 commit comments

Comments
 (0)
This repository has been archived.