Skip to content

Commit

Permalink
refactor: remove the global context option (#453)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the global `context` option was removed in favor the `patten.context` option
  • Loading branch information
evilebottnawi committed Apr 28, 2020
1 parent f6da280 commit 64b2e1a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 155 deletions.
24 changes: 3 additions & 21 deletions README.md
Expand Up @@ -547,10 +547,9 @@ module.exports = {

### Options

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

#### `ignore`

Expand All @@ -569,23 +568,6 @@ module.exports = {
};
```

#### `context`

A path that determines how to interpret the `from` path, shared for all patterns.

**webpack.config.js**

```js
module.exports = {
plugins: [
new CopyPlugin({
patterns: [...patterns],
options: { context: '/app' },
}),
],
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
22 changes: 3 additions & 19 deletions src/index.js
@@ -1,5 +1,3 @@
import path from 'path';

import validateOptions from 'schema-utils';

import schema from './options.json';
Expand All @@ -21,40 +19,26 @@ class CopyPlugin {
apply(compiler) {
const fileDependencies = new Set();
const contextDependencies = new Set();

let context;

if (!this.options.context) {
({ context } = compiler.options);
} else if (!path.isAbsolute(this.options.context)) {
context = path.join(compiler.options.context, this.options.context);
} else {
({ context } = this.options);
}

const logger = compiler.getInfrastructureLogger('copy-webpack-plugin');

const plugin = { name: 'CopyPlugin' };
const logger = compiler.getInfrastructureLogger('copy-webpack-plugin');

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

const globalRef = {
context: compiler.options.context,
logger,
compilation,
fileDependencies,
contextDependencies,
context,
inputFileSystem: compiler.inputFileSystem,
output: compiler.options.output.path,
ignore: this.options.ignore || [],
concurrency: this.options.concurrency,
};

const { patterns } = this;

Promise.all(
patterns.map((pattern) =>
this.patterns.map((pattern) =>
Promise.resolve()
.then(() => preProcessPattern(globalRef, pattern))
// Every source (from) is assumed to exist here
Expand Down
3 changes: 0 additions & 3 deletions src/options.json
Expand Up @@ -90,9 +90,6 @@
"type": "object",
"additionalProperties": false,
"properties": {
"context": {
"type": "string"
},
"ignore": {
"type": "array"
}
Expand Down
2 changes: 1 addition & 1 deletion src/preProcessPattern.js
Expand Up @@ -11,8 +11,8 @@ import { stat } from './utils/promisify';

export default function preProcessPattern(globalRef, pattern) {
const {
logger,
context,
logger,
inputFileSystem,
fileDependencies,
contextDependencies,
Expand Down
13 changes: 8 additions & 5 deletions test/CopyPlugin.test.js
Expand Up @@ -387,9 +387,11 @@ describe('apply function', () => {
patterns: [
{
from: 'tempfile1.txt',
context: 'watch',
},
{
from: 'tempfile2.txt',
context: 'watch',
},
],
})
Expand All @@ -414,7 +416,7 @@ describe('apply function', () => {
),
patterns: [
{
from: 'directory',
from: 'watch/directory',
},
],
})
Expand All @@ -439,7 +441,7 @@ describe('apply function', () => {
),
patterns: [
{
context: 'directory',
context: 'watch/directory',
from: '**/*.txt',
to: 'dest1',
},
Expand Down Expand Up @@ -471,12 +473,12 @@ describe('apply function', () => {
),
patterns: [
{
context: 'directory',
context: 'watch/directory',
from: '**/*.txt',
to: 'dest1',
},
{
context: 'directory',
context: 'watch/directory',
from: '**/*.txt',
to: 'dest2',
},
Expand All @@ -502,11 +504,12 @@ describe('apply function', () => {
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
patterns: [
{
context: 'directory',
context: 'watch/directory',
from: '**/*.txt',
to: 'dest1',
},
{
context: 'watch',
from: '**/*.txt',
to: 'dest2',
},
Expand Down
11 changes: 6 additions & 5 deletions test/__snapshots__/validate-options.test.js.snap
@@ -1,16 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validate options should throw an error on the "options" option with "{"context":true}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.options.context should be a string."
`;

exports[`validate options should throw an error on the "options" option with "{"ignore":true}" value 1`] = `
"Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options.options.ignore should be an array:
[any, ...]"
`;

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

0 comments on commit 64b2e1a

Please sign in to comment.