Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit a32649f

Browse files
committed
fix(webpack): don't overwrite css files when outputting webpack files
don't overwrite css files when outputting webpack files
1 parent 6b158d3 commit a32649f

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/webpack.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { join } from 'path';
2+
3+
import * as webpack from './webpack';
4+
import { FileCache } from './util/file-cache';
5+
import * as helpers from './util/helpers';
6+
7+
describe('Webpack Task', () => {
8+
describe('writeBundleFilesToDisk', () => {
9+
it('should write all build artifacts to disk except css', () => {
10+
const appDir = join('some', 'fake', 'dir', 'myApp');
11+
const buildDir = join(appDir, 'www', 'build');
12+
13+
const context = {
14+
fileCache: new FileCache(),
15+
buildDir: buildDir
16+
};
17+
18+
const fileOnePath = join(buildDir, 'main.js');
19+
const fileTwoPath = join(buildDir, 'main.js.map');
20+
const fileThreePath = join(buildDir, '0.main.js');
21+
const fileFourPath = join(buildDir, '0.main.js.map');
22+
const fileFivePath = join(buildDir, '1.main.js');
23+
const fileSixPath = join(buildDir, '1.main.js.map');
24+
const fileSevenPath = join(appDir, 'pages', 'page-one.ts');
25+
const fileEightPath = join(appDir, 'pages', 'page-one.js');
26+
const fileNinePath = join(buildDir, 'main.css');
27+
const fileTenPath = join(buildDir, 'main.css.map');
28+
const fileElevenPath = join(buildDir, 'secondary.css');
29+
const fileTwelvePath = join(buildDir, 'secondary.css.map');
30+
31+
context.fileCache.set(fileOnePath, { path: fileOnePath, content: fileOnePath + 'content'});
32+
context.fileCache.set(fileTwoPath, { path: fileTwoPath, content: fileTwoPath + 'content'});
33+
context.fileCache.set(fileThreePath, { path: fileThreePath, content: fileThreePath + 'content'});
34+
context.fileCache.set(fileFourPath, { path: fileFourPath, content: fileFourPath + 'content'});
35+
context.fileCache.set(fileFivePath, { path: fileFivePath, content: fileFivePath + 'content'});
36+
context.fileCache.set(fileSixPath, { path: fileSixPath, content: fileSixPath + 'content'});
37+
context.fileCache.set(fileSevenPath, { path: fileSevenPath, content: fileSevenPath + 'content'});
38+
context.fileCache.set(fileEightPath, { path: fileEightPath, content: fileEightPath + 'content'});
39+
context.fileCache.set(fileNinePath, { path: fileNinePath, content: fileNinePath + 'content'});
40+
context.fileCache.set(fileTenPath, { path: fileTenPath, content: fileTenPath + 'content'});
41+
context.fileCache.set(fileElevenPath, { path: fileElevenPath, content: fileElevenPath + 'content'});
42+
context.fileCache.set(fileTwelvePath, { path: fileTwelvePath, content: fileTwelvePath + 'content'});
43+
44+
const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
45+
46+
const promise = webpack.writeBundleFilesToDisk(context);
47+
48+
return promise.then(() => {
49+
expect(writeFileSpy).toHaveBeenCalledTimes(6);
50+
expect(writeFileSpy.calls.all()[0].args[0]).toEqual(fileOnePath);
51+
expect(writeFileSpy.calls.all()[0].args[1]).toEqual(fileOnePath + 'content');
52+
53+
expect(writeFileSpy.calls.all()[1].args[0]).toEqual(fileTwoPath);
54+
expect(writeFileSpy.calls.all()[1].args[1]).toEqual(fileTwoPath + 'content');
55+
56+
expect(writeFileSpy.calls.all()[2].args[0]).toEqual(fileThreePath);
57+
expect(writeFileSpy.calls.all()[2].args[1]).toEqual(fileThreePath + 'content');
58+
59+
expect(writeFileSpy.calls.all()[3].args[0]).toEqual(fileFourPath);
60+
expect(writeFileSpy.calls.all()[3].args[1]).toEqual(fileFourPath + 'content');
61+
62+
expect(writeFileSpy.calls.all()[4].args[0]).toEqual(fileFivePath);
63+
expect(writeFileSpy.calls.all()[4].args[1]).toEqual(fileFivePath + 'content');
64+
65+
expect(writeFileSpy.calls.all()[5].args[0]).toEqual(fileSixPath);
66+
expect(writeFileSpy.calls.all()[5].args[1]).toEqual(fileSixPath + 'content');
67+
});
68+
});
69+
});
70+
});

src/webpack.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from 'events';
2-
import { join } from 'path';
2+
import { dirname, extname, join } from 'path';
33

44
import * as webpackApi from 'webpack';
55

@@ -95,7 +95,7 @@ function webpackBuildComplete(stats: any, context: BuildContext, webpackConfig:
9595

9696
// set the module files used in this bundle
9797
// this reference can be used elsewhere in the build (sass)
98-
const files = stats.compilation.modules.map((webpackObj: any) => {
98+
const files: string[] = stats.compilation.modules.map((webpackObj: any) => {
9999
if (webpackObj.resource) {
100100
return webpackObj.resource;
101101
} else {
@@ -112,8 +112,10 @@ function webpackBuildComplete(stats: any, context: BuildContext, webpackConfig:
112112
}
113113

114114
export function writeBundleFilesToDisk(context: BuildContext) {
115-
const buildFiles = context.fileCache.getAll().filter(file => file.path.indexOf(context.buildDir) >= 0);
116-
const promises = buildFiles.map(buildFile => writeFileAsync(buildFile.path, buildFile.content));
115+
const bundledFilesToWrite = context.fileCache.getAll().filter(file => {
116+
return dirname(file.path) === context.buildDir && (file.path.endsWith('.js') || file.path.endsWith('.js.map'));
117+
});
118+
const promises = bundledFilesToWrite.map(bundledFileToWrite => writeFileAsync(bundledFileToWrite.path, bundledFileToWrite.content));
117119
return Promise.all(promises);
118120
}
119121

0 commit comments

Comments
 (0)