Skip to content

Commit 64b2e1a

Browse files
authoredApr 28, 2020
refactor: remove the global context option (#453)
BREAKING CHANGE: the global `context` option was removed in favor the `patten.context` option
1 parent f6da280 commit 64b2e1a

9 files changed

+67
-155
lines changed
 

‎README.md

+3-21
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,9 @@ module.exports = {
547547

548548
### Options
549549

550-
| Name | Type | Default | Description |
551-
| :-------------------: | :--------: | :------------------------: | :------------------------------------------------------------------------------- |
552-
| [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |
553-
| [`context`](#context) | `{String}` | `compiler.options.context` | A path that determines how to interpret the `from` path, shared for all patterns |
550+
| Name | Type | Default | Description |
551+
| :-----------------: | :-------: | :-----: | :------------------------------------------- |
552+
| [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |
554553

555554
#### `ignore`
556555

@@ -569,23 +568,6 @@ module.exports = {
569568
};
570569
```
571570

572-
#### `context`
573-
574-
A path that determines how to interpret the `from` path, shared for all patterns.
575-
576-
**webpack.config.js**
577-
578-
```js
579-
module.exports = {
580-
plugins: [
581-
new CopyPlugin({
582-
patterns: [...patterns],
583-
options: { context: '/app' },
584-
}),
585-
],
586-
};
587-
```
588-
589571
## Contributing
590572

591573
Please take a moment to read our contributing guidelines if you haven't yet done so.

‎src/index.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import path from 'path';
2-
31
import validateOptions from 'schema-utils';
42

53
import schema from './options.json';
@@ -21,40 +19,26 @@ class CopyPlugin {
2119
apply(compiler) {
2220
const fileDependencies = new Set();
2321
const contextDependencies = new Set();
24-
25-
let context;
26-
27-
if (!this.options.context) {
28-
({ context } = compiler.options);
29-
} else if (!path.isAbsolute(this.options.context)) {
30-
context = path.join(compiler.options.context, this.options.context);
31-
} else {
32-
({ context } = this.options);
33-
}
34-
35-
const logger = compiler.getInfrastructureLogger('copy-webpack-plugin');
36-
3722
const plugin = { name: 'CopyPlugin' };
23+
const logger = compiler.getInfrastructureLogger('copy-webpack-plugin');
3824

3925
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
4026
logger.debug('starting emit');
4127

4228
const globalRef = {
29+
context: compiler.options.context,
4330
logger,
4431
compilation,
4532
fileDependencies,
4633
contextDependencies,
47-
context,
4834
inputFileSystem: compiler.inputFileSystem,
4935
output: compiler.options.output.path,
5036
ignore: this.options.ignore || [],
5137
concurrency: this.options.concurrency,
5238
};
5339

54-
const { patterns } = this;
55-
5640
Promise.all(
57-
patterns.map((pattern) =>
41+
this.patterns.map((pattern) =>
5842
Promise.resolve()
5943
.then(() => preProcessPattern(globalRef, pattern))
6044
// Every source (from) is assumed to exist here

‎src/options.json

-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@
9090
"type": "object",
9191
"additionalProperties": false,
9292
"properties": {
93-
"context": {
94-
"type": "string"
95-
},
9693
"ignore": {
9794
"type": "array"
9895
}

‎src/preProcessPattern.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { stat } from './utils/promisify';
1111

1212
export default function preProcessPattern(globalRef, pattern) {
1313
const {
14-
logger,
1514
context,
15+
logger,
1616
inputFileSystem,
1717
fileDependencies,
1818
contextDependencies,

‎test/CopyPlugin.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,11 @@ describe('apply function', () => {
387387
patterns: [
388388
{
389389
from: 'tempfile1.txt',
390+
context: 'watch',
390391
},
391392
{
392393
from: 'tempfile2.txt',
394+
context: 'watch',
393395
},
394396
],
395397
})
@@ -414,7 +416,7 @@ describe('apply function', () => {
414416
),
415417
patterns: [
416418
{
417-
from: 'directory',
419+
from: 'watch/directory',
418420
},
419421
],
420422
})
@@ -439,7 +441,7 @@ describe('apply function', () => {
439441
),
440442
patterns: [
441443
{
442-
context: 'directory',
444+
context: 'watch/directory',
443445
from: '**/*.txt',
444446
to: 'dest1',
445447
},
@@ -471,12 +473,12 @@ describe('apply function', () => {
471473
),
472474
patterns: [
473475
{
474-
context: 'directory',
476+
context: 'watch/directory',
475477
from: '**/*.txt',
476478
to: 'dest1',
477479
},
478480
{
479-
context: 'directory',
481+
context: 'watch/directory',
480482
from: '**/*.txt',
481483
to: 'dest2',
482484
},
@@ -502,11 +504,12 @@ describe('apply function', () => {
502504
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
503505
patterns: [
504506
{
505-
context: 'directory',
507+
context: 'watch/directory',
506508
from: '**/*.txt',
507509
to: 'dest1',
508510
},
509511
{
512+
context: 'watch',
510513
from: '**/*.txt',
511514
to: 'dest2',
512515
},

‎test/__snapshots__/validate-options.test.js.snap

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`validate options should throw an error on the "options" option with "{"context":true}" value 1`] = `
4-
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
5-
- options.options.context should be a string."
6-
`;
7-
83
exports[`validate options should throw an error on the "options" option with "{"ignore":true}" value 1`] = `
94
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
105
- options.options.ignore should be an array:
116
[any, ...]"
127
`;
138

9+
exports[`validate options should throw an error on the "options" option with "{"unknown":true}" value 1`] = `
10+
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
11+
- options.options has an unknown property 'unknown'. These properties are valid:
12+
object { ignore? }"
13+
`;
14+
1415
exports[`validate options should throw an error on the "patterns" option with "" value 1`] = `
1516
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
1617
- options.patterns should be an array:

‎test/context-option.test.js

+43-98
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { runEmit } from './helpers/run';
55
const FIXTURES_DIR = path.join(__dirname, 'fixtures');
66

77
describe('context option', () => {
8-
it('should work when "from" is a file', (done) => {
8+
it('should work when "from" is a file and "context" is a relative path', (done) => {
99
runEmit({
1010
expectedAssetKeys: ['directoryfile.txt'],
1111
patterns: [
@@ -19,26 +19,29 @@ describe('context option', () => {
1919
.catch(done);
2020
});
2121

22-
it('should work when "from" is a file and "context" with special characters', (done) => {
22+
it('should work when "from" is a directory and "context" is a relative path', (done) => {
2323
runEmit({
24-
expectedAssetKeys: ['directoryfile.txt'],
24+
expectedAssetKeys: ['deep-nested/deepnested.txt', 'nestedfile.txt'],
2525
patterns: [
2626
{
27-
from: 'directoryfile.txt',
28-
context: '[special?directory]',
27+
from: 'nested',
28+
context: 'directory',
2929
},
3030
],
3131
})
3232
.then(done)
3333
.catch(done);
3434
});
3535

36-
it('should work when "from" is a directory', (done) => {
36+
it('should work when "from" is a glob and "context" is a relative path', (done) => {
3737
runEmit({
38-
expectedAssetKeys: ['deep-nested/deepnested.txt', 'nestedfile.txt'],
38+
expectedAssetKeys: [
39+
'nested/deep-nested/deepnested.txt',
40+
'nested/nestedfile.txt',
41+
],
3942
patterns: [
4043
{
41-
from: 'nested',
44+
from: 'nested/**/*',
4245
context: 'directory',
4346
},
4447
],
@@ -47,44 +50,35 @@ describe('context option', () => {
4750
.catch(done);
4851
});
4952

50-
it('should work when "from" is a directory and "to" is a new directory', (done) => {
53+
it('should work when "from" is a file and "context" is an absolute path', (done) => {
5154
runEmit({
52-
expectedAssetKeys: [
53-
'newdirectory/deep-nested/deepnested.txt',
54-
'newdirectory/nestedfile.txt',
55-
],
55+
expectedAssetKeys: ['directoryfile.txt'],
5656
patterns: [
5757
{
58-
context: 'directory',
59-
from: 'nested',
60-
to: 'newdirectory',
58+
from: 'directoryfile.txt',
59+
context: path.join(FIXTURES_DIR, 'directory'),
6160
},
6261
],
6362
})
6463
.then(done)
6564
.catch(done);
6665
});
6766

68-
it('should work when "from" is a directory and "context" with special characters', (done) => {
67+
it('should work when "from" is a directory and "context" is an absolute path', (done) => {
6968
runEmit({
70-
expectedAssetKeys: [
71-
'directoryfile.txt',
72-
'(special-*file).txt',
73-
'nested/nestedfile.txt',
74-
],
69+
expectedAssetKeys: ['deep-nested/deepnested.txt', 'nestedfile.txt'],
7570
patterns: [
7671
{
77-
// Todo strange behavour when you use `FIXTURES_DIR`, need investigate for next major release
78-
from: '.',
79-
context: '[special?directory]',
72+
from: 'nested',
73+
context: path.join(FIXTURES_DIR, 'directory'),
8074
},
8175
],
8276
})
8377
.then(done)
8478
.catch(done);
8579
});
8680

87-
it('should work when "from" is a glob', (done) => {
81+
it('should work when "from" is a glob and "context" is an absolute path', (done) => {
8882
runEmit({
8983
expectedAssetKeys: [
9084
'nested/deep-nested/deepnested.txt',
@@ -93,45 +87,40 @@ describe('context option', () => {
9387
patterns: [
9488
{
9589
from: 'nested/**/*',
96-
context: 'directory',
90+
context: path.join(FIXTURES_DIR, 'directory'),
9791
},
9892
],
9993
})
10094
.then(done)
10195
.catch(done);
10296
});
10397

104-
it('should work when "from" is a glob and "to" is a directory', (done) => {
98+
it('should work when "from" is a file and "context" with special characters', (done) => {
10599
runEmit({
106-
expectedAssetKeys: [
107-
'nested/directoryfile.txt',
108-
'nested/nested/deep-nested/deepnested.txt',
109-
'nested/nested/nestedfile.txt',
110-
],
100+
expectedAssetKeys: ['directoryfile.txt'],
111101
patterns: [
112102
{
113-
context: 'directory',
114-
from: '**/*',
115-
to: 'nested',
103+
from: 'directoryfile.txt',
104+
context: '[special?directory]',
116105
},
117106
],
118107
})
119108
.then(done)
120109
.catch(done);
121110
});
122111

123-
it('should work when "from" is a glob and "to" is a directory and "content" is an absolute path', (done) => {
112+
it('should work when "from" is a directory and "context" with special characters', (done) => {
124113
runEmit({
125114
expectedAssetKeys: [
126-
'nested/directoryfile.txt',
127-
'nested/nested/deep-nested/deepnested.txt',
128-
'nested/nested/nestedfile.txt',
115+
'directoryfile.txt',
116+
'(special-*file).txt',
117+
'nested/nestedfile.txt',
129118
],
130119
patterns: [
131120
{
132-
context: path.join(FIXTURES_DIR, 'directory'),
133-
from: '**/*',
134-
to: 'nested',
121+
// Todo strange behavour when you use `FIXTURES_DIR`, need investigate for next major release
122+
from: '.',
123+
context: '[special?directory]',
135124
},
136125
],
137126
})
@@ -171,32 +160,13 @@ describe('context option', () => {
171160
.catch(done);
172161
});
173162

174-
it('should work when "from" is a file and "context" is an absolute path', (done) => {
163+
it('should work when "from" is a file and "to" is a directory', (done) => {
175164
runEmit({
176-
expectedAssetKeys: ['directoryfile.txt'],
165+
expectedAssetKeys: ['newdirectory/directoryfile.txt'],
177166
patterns: [
178167
{
168+
context: 'directory',
179169
from: 'directoryfile.txt',
180-
context: path.join(FIXTURES_DIR, 'directory'),
181-
},
182-
],
183-
})
184-
.then(done)
185-
.catch(done);
186-
});
187-
188-
it('should override webpack config context with an absolute path', (done) => {
189-
runEmit({
190-
expectedAssetKeys: [
191-
'newdirectory/deep-nested/deepnested.txt',
192-
'newdirectory/nestedfile.txt',
193-
],
194-
options: {
195-
context: path.join(FIXTURES_DIR, 'directory'),
196-
},
197-
patterns: [
198-
{
199-
from: 'nested',
200170
to: 'newdirectory',
201171
},
202172
],
@@ -205,17 +175,15 @@ describe('context option', () => {
205175
.catch(done);
206176
});
207177

208-
it('should override webpack config context with a relative path', (done) => {
178+
it('should work when "from" is a directory and "to" is a directory', (done) => {
209179
runEmit({
210180
expectedAssetKeys: [
211181
'newdirectory/deep-nested/deepnested.txt',
212182
'newdirectory/nestedfile.txt',
213183
],
214-
options: {
215-
context: 'directory',
216-
},
217184
patterns: [
218185
{
186+
context: 'directory',
219187
from: 'nested',
220188
to: 'newdirectory',
221189
},
@@ -225,41 +193,18 @@ describe('context option', () => {
225193
.catch(done);
226194
});
227195

228-
it('should override global context on pattern context with a relative path', (done) => {
229-
runEmit({
230-
expectedAssetKeys: [
231-
'newdirectory/deep-nested/deepnested.txt',
232-
'newdirectory/nestedfile.txt',
233-
],
234-
options: {
235-
context: 'directory',
236-
},
237-
patterns: [
238-
{
239-
context: 'nested',
240-
from: '.',
241-
to: 'newdirectory',
242-
},
243-
],
244-
})
245-
.then(done)
246-
.catch(done);
247-
});
248-
249-
it('overrides webpack config context with an absolute path', (done) => {
196+
it('should work when "from" is a glob and "to" is a directory', (done) => {
250197
runEmit({
251198
expectedAssetKeys: [
252-
'newdirectory/file.txt',
253-
'newdirectory/nesteddir/deepnesteddir/deepnesteddir.txt',
254-
'newdirectory/nesteddir/nestedfile.txt',
199+
'nested/directoryfile.txt',
200+
'nested/nested/deep-nested/deepnested.txt',
201+
'nested/nested/nestedfile.txt',
255202
],
256-
options: {
257-
context: path.join(FIXTURES_DIR, 'dir (86)'),
258-
},
259203
patterns: [
260204
{
205+
context: 'directory',
261206
from: '**/*',
262-
to: 'newdirectory',
207+
to: 'nested',
263208
},
264209
],
265210
})

‎test/helpers/run.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function runChange(opts) {
173173

174174
return run({
175175
compiler,
176-
options: Object.assign({}, opts.options, { context: 'watch' }),
176+
options: Object.assign({}, opts.options),
177177
patterns: opts.patterns,
178178
})
179179
.then(() => {

‎test/validate-options.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ describe('validate options', () => {
235235
],
236236
},
237237
options: {
238-
success: [{ context: 'context' }, { ignore: ['test'] }],
239-
failure: [{ context: true }, { ignore: true }],
238+
success: [{ ignore: ['test'] }],
239+
failure: [{ unknown: true }, { ignore: true }],
240240
},
241241
unknown: {
242242
success: [],

0 commit comments

Comments
 (0)
Please sign in to comment.