Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for target path transforms (options.transformPath) #115

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ A pattern looks like:
| `flatten` | N | false | Removes all directory references and only copies file names<br><br>If files have the same name, the result is non-deterministic |
| `ignore` | N | [] | Additional globs to ignore for this pattern |
| `transform` | N | function(content, path) {<br>&nbsp;&nbsp;return content;<br>} | Function that modifies file contents before writing to webpack |
| `transformPath` | N | function(targetPath, sourcePath) {<br>&nbsp;&nbsp;return targetPath;<br>} | Function that modifies target file path before writing to webpack |
| `force` | N | false | Overwrites files already in compilation.assets (usually added by other plugins) |

#### Available options:
Expand Down
4 changes: 4 additions & 0 deletions src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default function writeFile(globalRef, pattern, file) {
}
}

if (typeof pattern.transformPath === 'function') {
file.webpackTo = pattern.transformPath(file.webpackTo, file.absoluteFrom);
}

if (!copyUnmodified &&
written[file.absoluteFrom] && written[file.absoluteFrom][hash]) {
info(`skipping '${file.webpackTo}', because it hasn't changed`);
Expand Down
79 changes: 79 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,49 @@ describe('apply function', () => {
.then(done)
.catch(done);
});

it('can transform target path of every file in glob', (done) => {
runEmit({
expectedAssetKeys: [
'/some/path/binextension.bin.tst',
'/some/path/file.txt.tst',
'/some/path/directoryfile.txt.tst',
'/some/path/nestedfile.txt.tst',
'/some/path/noextension.tst'
],
patterns: [{
from: '**/*',
transformPath: function(targetPath, absoluteFrom) {
expect(absoluteFrom).to.have.string(HELPER_DIR);
return '/some/path/' + path.basename(targetPath) + '.tst';
}
}]
})
.then(done)
.catch(done);
});

it('can transform target path of every file in glob after applying template', (done) => {
runEmit({
expectedAssetKeys: [
'transformed/binextension-d41d8c.bin',
'transformed/file-22af64.txt',
'transformed/directory/directoryfile-22af64.txt',
'transformed/directory/nested/nestedfile-d41d8c.txt',
'transformed/noextension-d41d8c'
],
patterns: [{
from: '**/*',
to: 'nested/[path][name]-[hash:6].[ext]',
transformPath: function(targetPath, absoluteFrom) {
expect(absoluteFrom).to.have.string(HELPER_DIR);
return targetPath.replace('nested/', 'transformed/');
}
}]
})
.then(done)
.catch(done);
});
});

describe('with file in from', () => {
Expand Down Expand Up @@ -405,6 +448,23 @@ describe('apply function', () => {
.catch(done);
});

it('can transform target path', (done) => {
runEmit({
expectedAssetKeys: [
'subdir/test.txt'
],
patterns: [{
from: 'file.txt',
transformPath: function(targetPath, absoluteFrom) {
expect(absoluteFrom).to.equal(path.join(HELPER_DIR, 'file.txt'));
return targetPath.replace('file.txt', 'subdir/test.txt');
}
}]
})
.then(done)
.catch(done);
});

it('warns when file not found', (done) => {
runEmit({
expectedAssetKeys: [],
Expand Down Expand Up @@ -817,6 +877,25 @@ describe('apply function', () => {
.catch(done);
});

it('can transform target path of every file in directory', (done) => {
runEmit({
expectedAssetKeys: [
'/some/path/.dottedfile',
'/some/path/directoryfile.txt',
'/some/path/nestedfile.txt'
],
patterns: [{
from: 'directory',
transformPath: function(targetPath, absoluteFrom) {
expect(absoluteFrom).to.have.string(path.join(HELPER_DIR, 'directory'));
return '/some/path/' + path.basename(targetPath);
}
}]
})
.then(done)
.catch(done);
});

it('warns when directory not found', (done) => {
runEmit({
expectedAssetKeys: [],
Expand Down