Skip to content

Commit

Permalink
Merge pull request #12 from cloverinteractive/hotfix/baseDir
Browse files Browse the repository at this point in the history
baseDir should override file dirname
  • Loading branch information
yeojz committed Feb 27, 2018
2 parents 8b1c644 + 832e6c9 commit 2ed6eca
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 187 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {dirname, extname, resolve} from'path';
import transform from './transform';

export const defaultOptions = {
flatten: false,
extensions: [
'.gif',
'.jpeg',
Expand Down
8 changes: 5 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getHash(str) {
.slice(0, 8);
}

function getFile(absPath, baseDir, uri) {
function getFile(absPath, baseDir, uri, flatten) {
const file = absPath
.split(baseDir || path.sep)
.pop();
Expand All @@ -19,7 +19,9 @@ function getFile(absPath, baseDir, uri) {
return (uri) ? '/' + file : file;
}

return path.join(baseDir, file)
const fileName = flatten ? path.basename(file) : file;

return path.join(baseDir, fileName)
.replace(/\\/g, '/')
.replace(/\/\/g/, '/');
}
Expand All @@ -35,7 +37,7 @@ const getVariableName = (p) => {
}

export default (p, t, opts, absPath, calleeName) => {
const file = getFile(absPath, opts.baseDir, opts.baseUri);
const file = getFile(absPath, opts.baseDir, opts.baseUri, opts.flatten);
let hash = '';

if (opts.hash === 1) {
Expand Down
8 changes: 8 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('index', function () {
expect(result).to.equal('const test = \'http://cdn.address/assets/path/to/icon.svg\';');
});

it('should let you flatten the file path', function () {
const config = Object.assign({}, baseConfig, {
flatten: true
});
const result = transformCode(getFixtures('import-image.js'), config).code;
expect(result).to.equal('const test = \'http://cdn.address/assets/icon.svg\';');
});

it('should replace import statements with uri and hash of content', function () {
const config = Object.assign({}, baseConfig, {
hash: 1,
Expand Down
Loading

0 comments on commit 2ed6eca

Please sign in to comment.