Skip to content

Commit 077e680

Browse files
committed
Added flattening
1 parent f33cf60 commit 077e680

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ A pattern looks like:
3838
- forces the plugin to overwrite files staged by previous plugins
3939
* `context`
4040
- is optional
41-
- defaults to the context of your webpack config
41+
- defaults to the base context
42+
- is a pattern specific context
43+
* `flatten`
44+
- is optional
45+
- defaults to `false`
46+
- removes all directory references and only copies file names
47+
- if files have the same name, the result is non-deterministic
4248

4349
#### Available options:
4450
* `ignore`

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default (patterns = [], options = {}) => {
9999
absDirSrc: absSrc,
100100
compilation,
101101
copyUnmodified,
102+
flatten: pattern.flatten,
102103
forceWrite,
103104
ignoreList,
104105
lastGlobalUpdate,
@@ -122,6 +123,11 @@ export default (patterns = [], options = {}) => {
122123

123124
relFileDest = pattern.to || '';
124125

126+
// Remove any directory references if flattening
127+
if (pattern.flatten) {
128+
relFileSrc = path.basename(relFileSrc);
129+
}
130+
125131
const relFileDirname = path.dirname(relFileSrc);
126132

127133
fileDependencies.push(absFileSrc);

src/writeDirectoryToAssets.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default (opts) => {
1212
const compilation = opts.compilation;
1313
const absDirSrc = opts.absDirSrc;
1414
const relDirDest = opts.relDirDest;
15+
const flatten = opts.flatten;
1516
const forceWrite = opts.forceWrite;
1617
const ignoreList = opts.ignoreList;
1718
const copyUnmodified = opts.copyUnmodified;
@@ -25,6 +26,11 @@ export default (opts) => {
2526

2627
relFileDest = path.join(relDirDest, relFileSrc);
2728

29+
// Remove any directory reference if flattening
30+
if (flatten) {
31+
relFileDest = path.join(relDirDest, path.basename(relFileDest));
32+
}
33+
2834
// Skip if it matches any of our ignore list
2935
if (shouldIgnore(relFileSrc, ignoreList)) {
3036
return false;

tests/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ describe('apply function', () => {
273273
.catch(done);
274274
});
275275

276+
it('can use a glob to flatten multiple files in a relative context to a non-root directory', (done) => {
277+
runEmit({
278+
expectedAssetKeys: [
279+
'nested/directoryfile.txt',
280+
'nested/nestedfile.txt'
281+
],
282+
patterns: [{
283+
context: 'directory',
284+
from: '**/*.txt',
285+
to: 'nested',
286+
flatten: true
287+
}]
288+
})
289+
.then(done)
290+
.catch(done);
291+
});
292+
276293
it('can use a glob to move multiple files in a different absolute context to a non-root directory', (done) => {
277294
runEmit({
278295
expectedAssetKeys: [
@@ -609,6 +626,37 @@ describe('apply function', () => {
609626
.catch(done);
610627
});
611628

629+
it('can move a directory\'s contents to a new directory using a pattern context', (done) => {
630+
runEmit({
631+
expectedAssetKeys: [
632+
'newdirectory/nestedfile.txt'
633+
],
634+
patterns: [{
635+
context: 'directory',
636+
from: 'nested',
637+
to: 'newdirectory'
638+
}]
639+
})
640+
.then(done)
641+
.catch(done);
642+
});
643+
644+
it('can flatten a directory\'s contents to a new directory', (done) => {
645+
runEmit({
646+
expectedAssetKeys: [
647+
'newdirectory/directoryfile.txt',
648+
'newdirectory/nestedfile.txt'
649+
],
650+
patterns: [{
651+
from: 'directory',
652+
to: 'newdirectory',
653+
flatten: true
654+
}]
655+
})
656+
.then(done)
657+
.catch(done);
658+
});
659+
612660
it('can move a directory\'s contents to a new directory using an absolute to', (done) => {
613661
runEmit({
614662
expectedAssetKeys: [

0 commit comments

Comments
 (0)