Skip to content

Commit f33cf60

Browse files
committed
Added pattern-level context
1 parent 6a05f2e commit f33cf60

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ A pattern looks like:
3636
- is optional
3737
- defaults to `false`
3838
- forces the plugin to overwrite files staged by previous plugins
39+
* `context`
40+
- is optional
41+
- defaults to the context of your webpack config
3942

4043
#### Available options:
4144
* `ignore`
@@ -84,6 +87,13 @@ module.exports = {
8487
},
8588
to: '/absolute/path'
8689
},
90+
91+
// Copy glob results, relative to context
92+
{
93+
context: 'from/directory',
94+
from: '**/*',
95+
to: '/absolute/path'
96+
},
8797

8898
// {output}/file/without/extension
8999
{

src/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default (patterns = [], options = {}) => {
3939
}
4040

4141
const apply = (compiler) => {
42-
const baseDir = compiler.options.context;
42+
const webpackContext = compiler.options.context;
4343
const outputPath = getOutputDir(compiler);
4444
const fileDependencies = [];
4545
const contextDependencies = [];
@@ -56,9 +56,16 @@ export default (patterns = [], options = {}) => {
5656
Promise.each(patterns, (pattern) => {
5757
let relDest;
5858
let globOpts;
59+
let context;
60+
61+
if (pattern.context && !path.isAbsolute(pattern.context)) {
62+
pattern.context = path.resolve(webpackContext, pattern.context);
63+
}
64+
65+
context = pattern.context || webpackContext;
5966

6067
globOpts = {
61-
cwd: baseDir
68+
cwd: context
6269
};
6370

6471
// From can be an object
@@ -68,7 +75,7 @@ export default (patterns = [], options = {}) => {
6875
}
6976

7077
const relSrc = pattern.from;
71-
const absSrc = path.resolve(baseDir, relSrc);
78+
const absSrc = path.resolve(context, relSrc);
7279

7380
relDest = pattern.to || '';
7481

@@ -111,7 +118,7 @@ export default (patterns = [], options = {}) => {
111118
return false;
112119
}
113120

114-
const absFileSrc = path.resolve(baseDir, relFileSrc);
121+
const absFileSrc = path.resolve(context, relFileSrc);
115122

116123
relFileDest = pattern.to || '';
117124

@@ -124,7 +131,7 @@ export default (patterns = [], options = {}) => {
124131
// If the source is absolute
125132
if (path.isAbsolute(relFileSrc)) {
126133
// Make the destination relative
127-
relFileDest = path.join(path.relative(baseDir, relFileDirname), path.basename(relFileSrc));
134+
relFileDest = path.join(path.relative(context, relFileDirname), path.basename(relFileSrc));
128135

129136
// If the source is relative
130137
} else {

tests/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,38 @@ describe('apply function', () => {
257257
.catch(done);
258258
});
259259

260+
it('can use a glob to move multiple files in a different relative context to a non-root directory', (done) => {
261+
runEmit({
262+
expectedAssetKeys: [
263+
'nested/directoryfile.txt',
264+
'nested/nested/nestedfile.txt'
265+
],
266+
patterns: [{
267+
context: 'directory',
268+
from: '**/*.txt',
269+
to: 'nested'
270+
}]
271+
})
272+
.then(done)
273+
.catch(done);
274+
});
275+
276+
it('can use a glob to move multiple files in a different absolute context to a non-root directory', (done) => {
277+
runEmit({
278+
expectedAssetKeys: [
279+
'nested/directoryfile.txt',
280+
'nested/nested/nestedfile.txt'
281+
],
282+
patterns: [{
283+
context: path.join(HELPER_DIR, 'directory'),
284+
from: '**/*.txt',
285+
to: 'nested'
286+
}]
287+
})
288+
.then(done)
289+
.catch(done);
290+
});
291+
260292
it('can use a glob with a full path to move a file to the root directory', (done) => {
261293
runEmit({
262294
expectedAssetKeys: [

0 commit comments

Comments
 (0)