Skip to content

Commit 82052dd

Browse files
committed
Merge pull request #49 from kevlened/remove-filesystem-write
Don't write to filesystem directly
2 parents d5e59e0 + a753c16 commit 82052dd

File tree

7 files changed

+49
-89
lines changed

7 files changed

+49
-89
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 3.0.0 (May 14, 2016)
2+
3+
BREAKING CHANGE
4+
5+
* No longer writing to filesystem when webpack-dev-server is running
6+
Use the [write-file-webpack-plugin](https://www.npmjs.com/package/write-file-webpack-plugin) to force writing files to the filesystem
7+
18
## 2.1.6 (May 14, 2016)
29

310
* Readded Node v6.0.0 compatibility after finding root cause

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ var path = require('path');
6767
module.exports = {
6868
context: path.join(__dirname, 'app'),
6969
devServer: {
70-
// This is required for webpack-dev-server. The path should
71-
// be an absolute path to your build destination.
70+
// This is required for webpack-dev-server if using a version <3.0.0.
71+
// The path should be an absolute path to your build destination.
7272
outputPath: path.join(__dirname, 'build')
7373
},
7474
plugins: [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "copy-webpack-plugin",
3-
"version": "2.1.6",
3+
"version": "3.0.0",
44
"description": "Copy files and directories in webpack",
55
"main": "dist/index.js",
66
"repository": {

src/index.js

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,21 @@ const globAsync = Promise.promisify(require('glob'));
1111
const fs = Promise.promisifyAll(require('fs-extra'));
1212
/* eslint-enable */
1313

14-
const union = (set1, set2) => {
15-
return new Set([...set1, ...set2]);
16-
};
17-
18-
const isDevServer = (compiler) => {
19-
return compiler.outputFileSystem.constructor.name === 'MemoryFileSystem';
20-
};
21-
22-
const getOutputDir = (compiler) => {
23-
if (compiler.options.output.path && compiler.options.output.path !== '/') {
24-
return compiler.options.output.path;
25-
}
26-
27-
const devServer = compiler.options.devServer;
28-
29-
if (!devServer || !devServer.outputPath || devServer.outputPath === '/') {
30-
throw new Error('CopyWebpackPlugin: to use webpack-dev-server, devServer.outputPath must be defined in the webpack config');
31-
}
32-
33-
return devServer.outputPath;
34-
};
35-
3614
function CopyWebpackPlugin(patterns = [], options = {}) {
3715
if (!_.isArray(patterns)) {
3816
throw new Error('CopyWebpackPlugin: patterns must be an array');
3917
}
4018

4119
const apply = (compiler) => {
4220
const webpackContext = compiler.options.context;
43-
const outputPath = getOutputDir(compiler);
21+
const outputPath = compiler.options.output.path;
4422
const fileDependencies = [];
4523
const contextDependencies = [];
4624
const webpackIgnore = options.ignore || [];
4725
const copyUnmodified = options.copyUnmodified;
48-
let writtenAssets;
49-
let lastGlobalUpdate;
50-
51-
lastGlobalUpdate = 0;
26+
const writtenAssetHashes = {};
5227

5328
compiler.plugin('emit', (compilation, cb) => {
54-
writtenAssets = new Set();
5529

5630
Promise.each(patterns, (pattern) => {
5731
let relDest;
@@ -102,11 +76,8 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
10276
flatten: pattern.flatten,
10377
forceWrite,
10478
ignoreList,
105-
lastGlobalUpdate,
106-
relDirDest: relDest
107-
})
108-
.then((assets) => {
109-
writtenAssets = union(writtenAssets, assets);
79+
relDirDest: relDest,
80+
writtenAssetHashes
11081
});
11182
}
11283

@@ -166,18 +137,12 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
166137
compilation,
167138
copyUnmodified,
168139
forceWrite,
169-
lastGlobalUpdate,
170-
relFileDest
171-
})
172-
.then((asset) => {
173-
writtenAssets.add(asset);
140+
relFileDest,
141+
writtenAssetHashes
174142
});
175143
});
176144
});
177145
})
178-
.then(() => {
179-
lastGlobalUpdate = _.now();
180-
})
181146
.catch((err) => {
182147
compilation.errors.push(err);
183148
})
@@ -201,35 +166,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
201166
}
202167
});
203168

204-
// Write files to file system if webpack-dev-server
205-
206-
if (!isDevServer(compiler)) {
207-
callback();
208-
209-
return;
210-
}
211-
212-
const writeFilePromises = [];
213-
214-
_.forEach(compilation.assets, (asset, assetPath) => {
215-
// If this is not our asset, ignore it
216-
if (!writtenAssets.has(assetPath)) {
217-
return;
218-
}
219-
220-
const outputFilePath = path.join(outputPath, assetPath);
221-
const absOutputPath = path.resolve(process.cwd(), outputFilePath);
222-
223-
writeFilePromises.push(fs.mkdirsAsync(path.dirname(absOutputPath))
224-
.then(() => {
225-
return fs.writeFileAsync(absOutputPath, asset.source());
226-
}));
227-
});
228-
229-
Promise.all(writeFilePromises)
230-
.then(() => {
231-
callback();
232-
});
169+
callback();
233170
});
234171
};
235172

src/writeDirectoryToAssets.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default (opts) => {
1616
const forceWrite = opts.forceWrite;
1717
const ignoreList = opts.ignoreList;
1818
const copyUnmodified = opts.copyUnmodified;
19-
const lastGlobalUpdate = opts.lastGlobalUpdate;
19+
const writtenAssetHashes = opts.writtenAssetHashes;
2020

2121
return dir.filesAsync(absDirSrc)
2222
.map((absFileSrc) => {
@@ -46,8 +46,8 @@ export default (opts) => {
4646
compilation,
4747
copyUnmodified,
4848
forceWrite,
49-
lastGlobalUpdate,
50-
relFileDest
49+
relFileDest,
50+
writtenAssetHashes
5151
});
5252
});
5353
};

src/writeFileToAssets.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import Promise from 'bluebird';
44
const fs = Promise.promisifyAll(require('fs-extra'));
55
/* eslint-enable */
66

7+
import {
8+
createHash
9+
} from 'crypto';
10+
711
export default (opts) => {
812
const compilation = opts.compilation;
913
// ensure forward slashes
1014
const relFileDest = opts.relFileDest.replace(/\\/g, '/');
1115
const absFileSrc = opts.absFileSrc;
1216
const forceWrite = opts.forceWrite;
13-
const lastGlobalUpdate = opts.lastGlobalUpdate;
1417
const copyUnmodified = opts.copyUnmodified;
18+
const writtenAssetHashes = opts.writtenAssetHashes;
1519

1620
if (compilation.assets[relFileDest] && !forceWrite) {
1721
return Promise.resolve();
@@ -20,19 +24,33 @@ export default (opts) => {
2024
return fs
2125
.statAsync(absFileSrc)
2226
.then((stat) => {
23-
if (stat.isDirectory() || !copyUnmodified && stat.mtime.getTime() < lastGlobalUpdate) {
24-
return null;
27+
28+
function addToAssets() {
29+
compilation.assets[relFileDest] = {
30+
size () {
31+
return stat.size;
32+
},
33+
source () {
34+
return fs.readFileSync(absFileSrc);
35+
}
36+
};
37+
38+
return relFileDest;
39+
}
40+
41+
if (copyUnmodified) {
42+
return addToAssets();
2543
}
2644

27-
compilation.assets[relFileDest] = {
28-
size () {
29-
return stat.size;
30-
},
31-
source () {
32-
return fs.readFileSync(absFileSrc);
45+
return fs.readFileAsync(absFileSrc)
46+
.then((contents) => {
47+
const hash = createHash('sha256').update(contents).digest('hex');
48+
if (writtenAssetHashes[relFileDest] && writtenAssetHashes[relFileDest] === hash) {
49+
return;
3350
}
34-
};
3551

36-
return relFileDest;
52+
writtenAssetHashes[relFileDest] = hash;
53+
return addToAssets();
54+
});
3755
});
3856
};

tests/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ describe('apply function', () => {
132132
options: opts.options,
133133
patterns: opts.patterns
134134
})
135-
// mtime is only measured in whole seconds
136-
.delay(1000)
137135
.then(() => {
138136
// Change a file
139137
fs.appendFileSync(opts.newFileLoc1, 'extra');

0 commit comments

Comments
 (0)