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

Commit

Permalink
feat: added the sourceFilename property to asset info with original…
Browse files Browse the repository at this point in the history
… filename (#393)
  • Loading branch information
evilebottnawi authored Oct 27, 2020
1 parent 381d8bd commit 654e0d6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getOptions, interpolateName } from 'loader-utils';
import { validate } from 'schema-utils';

import schema from './options.json';
import { normalizePath } from './utils';

export default function loader(content) {
const options = getOptions(this);
Expand Down Expand Up @@ -73,6 +74,10 @@ export default function loader(content) {
}
}

assetInfo.sourceFilename = normalizePath(
path.relative(this.rootContext, this.resourcePath)
);

this.emitFile(outputPath, content, null, assetInfo);
}

Expand Down
38 changes: 38 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function normalizePath(path, stripTrailing) {
if (path === '\\' || path === '/') {
return '/';
}

const len = path.length;

if (len <= 1) {
return path;
}

// ensure that win32 namespaces has two leading slashes, so that the path is
// handled properly by the win32 version of path.parse() after being normalized
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
let prefix = '';

if (len > 4 && path[3] === '\\') {
// eslint-disable-next-line prefer-destructuring
const ch = path[2];

if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
// eslint-disable-next-line no-param-reassign
path = path.slice(2);
prefix = '//';
}
}

const segs = path.split(/[/\\]+/);

if (stripTrailing !== false && segs[segs.length - 1] === '') {
segs.pop();
}

return prefix + segs.join('/');
}

// eslint-disable-next-line import/prefer-default-export
export { normalizePath };
43 changes: 43 additions & 0 deletions test/name-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,47 @@ describe('"name" option', () => {
}
}
});

it('should work and add "sourceFilename" to asset info', async () => {
expect.assertions(1);

const compiler = getCompiler('simple.js');
const stats = await compile(compiler);

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.endsWith('.png')) {
expect(info.sourceFilename).toBe('file.png');
}
}
});

it('should work and add "sourceFilename" to asset info #2', async () => {
expect.assertions(1);

const compiler = getCompiler('simple.js', {
name: '[name].asset.[ext]?foo=[contenthash]',
});
const stats = await compile(compiler);

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.startsWith('file.asset.png')) {
expect(info.sourceFilename).toBe('file.png');
}
}
});

it('should work and add "sourceFilename" to asset info #3', async () => {
expect.assertions(1);

const compiler = getCompiler('cdn.js', {
name: '[name].asset.[ext]',
});
const stats = await compile(compiler);

for (const [name, info] of stats.compilation.assetsInfo) {
if (name.startsWith('file.asset.png')) {
expect(info.sourceFilename).toBe('nested/file.png');
}
}
});
});

0 comments on commit 654e0d6

Please sign in to comment.