Skip to content

Commit

Permalink
fix: persist assets between rebuilds
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `copyUnmodified` was removed without replacements
  • Loading branch information
evilebottnawi committed Apr 27, 2020
1 parent 5a0daeb commit 57f3e61
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 151 deletions.
23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,10 @@ 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 |
| [`copyUnmodified`](#copyunmodified) | `{Boolean}` | `false` | Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option |
| 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 |

#### `ignore`

Expand All @@ -529,20 +528,6 @@ module.exports = {
};
```

#### `copyUnmodified`

Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option.

> ℹ️ By default, we only copy **modified** files during a `webpack --watch` or `webpack-dev-server` build. Setting this option to `true` will copy all files.
**webpack.config.js**

```js
module.exports = {
plugins: [new CopyPlugin([...patterns], { copyUnmodified: true })],
};
```

## Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.
Expand Down
3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class CopyPlugin {
apply(compiler) {
const fileDependencies = new Set();
const contextDependencies = new Set();
const written = {};

let context;

Expand All @@ -44,14 +43,12 @@ class CopyPlugin {
const globalRef = {
logger,
compilation,
written,
fileDependencies,
contextDependencies,
context,
inputFileSystem: compiler.inputFileSystem,
output: compiler.options.output.path,
ignore: this.options.ignore || [],
copyUnmodified: this.options.copyUnmodified,
concurrency: this.options.concurrency,
};

Expand Down
6 changes: 0 additions & 6 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,6 @@
},
"ignore": {
"type": "array"
},
"logLevel": {
"type": "string"
},
"copyUnmodified": {
"type": "boolean"
}
}
}
Expand Down
32 changes: 1 addition & 31 deletions src/postProcessPattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ import { stat, readFile } from './utils/promisify';
/* eslint-disable no-param-reassign */

export default function postProcessPattern(globalRef, pattern, file) {
const {
logger,
compilation,
fileDependencies,
written,
inputFileSystem,
copyUnmodified,
} = globalRef;
const { logger, compilation, fileDependencies, inputFileSystem } = globalRef;

logger.debug(`getting stats for '${file.absoluteFrom}' to write to assets`);

Expand Down Expand Up @@ -148,30 +141,7 @@ export default function postProcessPattern(globalRef, pattern, file) {
return content;
})
.then((content) => {
const hash = loaderUtils.getHashDigest(content);
const targetPath = normalizePath(file.webpackTo);
const targetAbsolutePath = normalizePath(file.absoluteFrom);

if (
!copyUnmodified &&
written[targetPath] &&
written[targetPath][targetAbsolutePath] &&
written[targetPath][targetAbsolutePath] === hash
) {
logger.log(
`skipping '${file.webpackTo}', because content hasn't changed`
);

return;
}

logger.debug(`adding '${file.webpackTo}' for tracking content changes`);

if (!written[targetPath]) {
written[targetPath] = {};
}

written[targetPath][targetAbsolutePath] = hash;

if (compilation.assets[targetPath] && !file.force) {
logger.log(`skipping '${file.webpackTo}', because it already exists`);
Expand Down
107 changes: 15 additions & 92 deletions test/CopyPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ describe('apply function', () => {
.catch(done);
});

it('only include files that have changed', (done) => {
it('should include files that have changed when `from` is a file', (done) => {
runChange({
expectedAssetKeys: ['tempfile1.txt'],
expectedAssetKeys: ['tempfile1.txt', 'tempfile2.txt'],
newFileLoc1: path.join(FIXTURES_DIR, 'watch', 'tempfile1.txt'),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
patterns: [
Expand All @@ -403,9 +403,9 @@ describe('apply function', () => {
.catch(done);
});

it('only include files that have changed', (done) => {
it('should include all files when `from` is a directory', (done) => {
runChange({
expectedAssetKeys: ['tempfile1.txt'],
expectedAssetKeys: ['.gitkeep', 'tempfile1.txt', 'tempfile2.txt'],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
Expand All @@ -428,37 +428,9 @@ describe('apply function', () => {
.catch(done);
});

it('include all files if copyUnmodified is true', (done) => {
it('should include all files when `from` is a glob', (done) => {
runChange({
expectedAssetKeys: ['tempfile1.txt', 'tempfile2.txt', '.gitkeep'],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(
FIXTURES_DIR,
'watch',
'directory',
'tempfile2.txt'
),
options: {
copyUnmodified: true,
},
patterns: [
{
from: 'directory',
},
],
})
.then(done)
.catch(done);
});

it('copy only changed files', (done) => {
runChange({
expectedAssetKeys: ['dest1/tempfile1.txt'],
expectedAssetKeys: ['dest1/tempfile1.txt', 'dest1/tempfile2.txt'],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
Expand All @@ -483,9 +455,14 @@ describe('apply function', () => {
.catch(done);
});

it('copy only changed files (multiple patterns)', (done) => {
it('should include all files when multiple patterns used', (done) => {
runChange({
expectedAssetKeys: ['dest1/tempfile1.txt', 'dest2/tempfile1.txt'],
expectedAssetKeys: [
'dest1/tempfile1.txt',
'dest1/tempfile2.txt',
'dest2/tempfile1.txt',
'dest2/tempfile2.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
Expand Down Expand Up @@ -515,11 +492,12 @@ describe('apply function', () => {
.catch(done);
});

it('copy only changed files (multiple patterns with difference context)', (done) => {
it('should include all files when multiple patterns with difference contexts', (done) => {
runChange({
expectedAssetKeys: [
'dest1/tempfile1.txt',
'dest2/directory/tempfile1.txt',
'dest2/tempfile2.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
Expand All @@ -543,60 +521,5 @@ describe('apply function', () => {
.then(done)
.catch(done);
});

it('copy only changed files (multiple patterns with difference context 1)', (done) => {
runChange({
expectedAssetKeys: [
'dest1/directory/tempfile1.txt',
'dest2/tempfile1.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
patterns: [
{
from: '**/*.txt',
to: 'dest1',
},
{
context: 'directory',
from: '**/*.txt',
to: 'dest2',
},
],
})
.then(done)
.catch(done);
});

it('copy only changed files (multiple patterns with difference context 2)', (done) => {
runChange({
expectedAssetKeys: ['dest1/tempfile1.txt'],
newFileLoc1: path.join(FIXTURES_DIR, 'watch', 'tempfile1.txt'),
newFileLoc2: path.join(
FIXTURES_DIR,
'watch',
'directory',
'tempfile2.txt'
),
patterns: [
{
from: '**/*.txt',
to: 'dest1',
},
{
context: 'directory',
from: '**/*.txt',
to: 'dest2',
},
],
})
.then(done)
.catch(done);
});
});
});

0 comments on commit 57f3e61

Please sign in to comment.