Skip to content

Commit

Permalink
Merge pull request #5 from formatlos/add-srcdir-outdir-option
Browse files Browse the repository at this point in the history
add srcDir and  outDir options
  • Loading branch information
ahalimkara authored Jan 16, 2018
2 parents 62bba70 + 51ee252 commit 17be943
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ Default options:
".jpg",
".png",
".svg"
]
],
"srcDir": "",
"outDir": ""
}
```

Expand All @@ -74,6 +76,12 @@ import image from './path/to/icon.png';
const image = '/static/icon-[hash].png';
```

### srcDir / outDir

If you are using a compiler like Typescript and compiling your Typescript sources to a
different location it also needs to get the images from the sort location. Use `srcDir` and `outDir`
to control the path replacement.

## Usage

### via .babelrc
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ const applyTransform = (p, t, state, value, calleeName) => {

if (options.extensions && options.extensions.indexOf(ext) >= 0) {
const dir = dirname(resolve(state.file.opts.filename));
const absPath = resolve(dir, value);
let absPath = resolve(dir, value);

if (options.baseDir) {
options.baseDir = options.baseDir.replace(/[\/\\]+/g, path.sep);
}

if (options.srcDir && options.outDir) {
const root = state.file.opts.sourceRoot || process.cwd();
const srcPath = resolve(root, options.srcDir);
const outPath = resolve(root, options.outDir);
absPath = absPath.replace(outPath, srcPath);
}

transform(p, t, state, options, absPath, calleeName);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getFile(state, absPath, opts) {

if (opts.baseDir) {
file = path.sep + path.join(opts.baseDir, file).replace(/^[\/\\]+/, '')
fs.copySync(absPath, path.join(root, file))
fs.copySync(absPath, path.join(root, opts.outDir || '', file))
}

return '/' + file
Expand Down

0 comments on commit 17be943

Please sign in to comment.