From 5c54e928cce9179ba26fd77ea42a5558fff2867b Mon Sep 17 00:00:00 2001 From: Clayton Watts Date: Thu, 5 Oct 2017 10:16:42 -0600 Subject: [PATCH] feat: Plugin context option Overrides webpack context, overridden by pattern context Fixes #148 --- README.md | 3 ++- src/index.js | 13 +++++++++++- tests/index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f866b1a4..d72a24d0 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Or, in the simple case of just a `from` with the default destination, you can us | `from` | Y | | _examples:_
'relative/file.txt'
'/absolute/file.txt'
'relative/dir'
'/absolute/dir'
'\*\*/\*'
{glob:'\*\*/\*', dot: true}

Globs accept [minimatch options](https://github.com/isaacs/minimatch) | | `to` | N | output root if `from` is file or dir

resolved glob path if `from` is glob | _examples:_
'relative/file.txt'
'/absolute/file.txt'
'relative/dir'
'/absolute/dir'
'relative/[name].[ext]'
'/absolute/[name].[ext]'

Templates are [file-loader patterns](https://github.com/webpack/file-loader) | | `toType` | N | **'file'** if `to` has extension or `from` is file

**'dir'** if `from` is directory, `to` has no extension or ends in '/'

**'template'** if `to` contains [a template pattern](https://github.com/webpack/file-loader) | | -| `context` | N | compiler.options.context | A path that determines how to interpret the `from` path | +| `context` | N | options.context \|\| compiler.options.context | A path that determines how to interpret the `from` path | | `flatten` | N | false | Removes all directory references and only copies file names

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) {
  return content;
} | Function that modifies file contents before writing to webpack | @@ -47,6 +47,7 @@ Or, in the simple case of just a `from` with the default destination, you can us | Name | Default | Details | | ---- | ------- | ------- | +| `context` | compiler.options.context | A path that determines how to interpret the `from` path, shared for all patterns | | `ignore` | [] | Array of globs to ignore (applied to `from`) | | `copyUnmodified` | false | Copies files, regardless of modification when using watch or webpack-dev-server. All files are copied on first build, regardless of this option. | | `debug` | **'warning'** | _options:_
**'warning'** - only warnings
**'info'** or true - file location and read info
**'debug'** - very detailed debugging info diff --git a/src/index.js b/src/index.js index 097543f5..46d4159f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import Promise from 'bluebird'; +import path from 'path'; import _ from 'lodash'; import preProcessPattern from './preProcessPattern'; import processPattern from './processPattern'; @@ -46,6 +47,16 @@ function CopyWebpackPlugin(patterns = [], options = {}) { let contextDependencies; const written = {}; + let context; + + if (!options.context) { + context = compiler.options.context; + } else if (!path.isAbsolute(options.context)) { + context = path.join(compiler.options.context, options.context); + } else { + context = options.context; + } + compiler.plugin('emit', (compilation, cb) => { debug('starting emit'); const callback = () => { @@ -64,7 +75,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) { written, fileDependencies, contextDependencies, - context: compiler.options.context, + context, output: compiler.options.output.path, ignore: options.ignore || [], copyUnmodified: options.copyUnmodified, diff --git a/tests/index.js b/tests/index.js index cb66a021..ccd53ae0 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1227,5 +1227,59 @@ describe('apply function', () => { .catch(done); }); }); + + describe('context', () => { + it('overrides webpack config context with absolute path', (done) => { + runEmit({ + expectedAssetKeys: [ + 'newdirectory/nestedfile.txt' + ], + options: { + context: path.resolve(HELPER_DIR, 'directory') + }, + patterns: [{ + from: 'nested', + to: 'newdirectory' + }] + }) + .then(done) + .catch(done); + }); + + it('overrides webpack config context with relative path', (done) => { + runEmit({ + expectedAssetKeys: [ + 'newdirectory/nestedfile.txt' + ], + options: { + context: 'directory' + }, + patterns: [{ + from: 'nested', + to: 'newdirectory' + }] + }) + .then(done) + .catch(done); + }); + + it('is overridden by pattern context', (done) => { + runEmit({ + expectedAssetKeys: [ + 'newdirectory/nestedfile.txt' + ], + options: { + context: 'directory' + }, + patterns: [{ + context: 'nested', + from: '.', + to: 'newdirectory' + }] + }) + .then(done) + .catch(done); + }); + }); }); });