diff --git a/packages/stryker-webpack-transpiler/package.json b/packages/stryker-webpack-transpiler/package.json index b8e6eb7541..479b06c2aa 100644 --- a/packages/stryker-webpack-transpiler/package.json +++ b/packages/stryker-webpack-transpiler/package.json @@ -52,8 +52,7 @@ "enhanced-resolve": "^4.0.0-beta.4", "lodash": "^4.17.4", "log4js": "^1.1.0", - "memory-fs": "^0.4.1", - "toposort": "^1.0.6" + "memory-fs": "^0.4.1" }, "initStrykerConfig": { "webpack": { diff --git a/packages/stryker-webpack-transpiler/src/compiler/FileSorter.ts b/packages/stryker-webpack-transpiler/src/compiler/FileSorter.ts deleted file mode 100644 index bc6de7cdc7..0000000000 --- a/packages/stryker-webpack-transpiler/src/compiler/FileSorter.ts +++ /dev/null @@ -1,69 +0,0 @@ -import * as path from 'path'; -import * as _ from 'lodash'; -import { File } from 'stryker-api/core'; -const toposort = require('toposort'); - -export interface Chunk { - id: string; - files: string[]; - parents?: Array; -} - -/** - * Responsible for sorting files given their webpack dependencies - * so they can be loaded in the browser in the correct order. - * - * Given this dependency graph - * A D - * / \ / - * B C - * - * The output order might be: - * D, A, B, C - * - * Note: This implementation is copied over from toposort - * - * @see https://www.npmjs.com/package/toposort#usage - */ -export default class FileSorter { - - static sort(files: File[], allChunks: Chunk[]): File[] { - const sortedChunks = this.sortChunks(allChunks); - const sortedFileNames = _.flatMap(sortedChunks, chunk => chunk.files.map(file => path.basename(file))); - files.sort((a, b) => { - const aName = path.basename(a.name); - const bName = path.basename(b.name); - return sortedFileNames.indexOf(aName) - sortedFileNames.indexOf(bName); - }); - return files; - } - - private static sortChunks(allChunks: Chunk[]): Chunk[] { - // We build a map (chunk-id -> chunk) for faster access during graph building. - const chunkMap: { [chunkId: string]: Chunk } = {}; - allChunks.forEach((chunk) => { - chunkMap[chunk.id] = chunk; - }); - - // Build the *edges* of the graph. - // It will contain an entry per parent -> child relationship - const edges: [Chunk, Chunk][] = []; - - allChunks.forEach(chunk => { - if (chunk.parents) { - chunk.parents.forEach(parentId => { - // webpack2 chunk.parents are chunks instead of string id(s) - const parentChunk = typeof parentId === 'string' ? chunkMap[parentId] : parentId; - // If the parent chunk does not exist (e.g. because of an excluded chunk) - // we ignore that parent - if (parentChunk) { - edges.push([parentChunk, chunk]); - } - }); - } - }); - - // We now perform a topological sorting on the input chunks and built edges - return toposort.array(allChunks, edges); - } -} \ No newline at end of file diff --git a/packages/stryker-webpack-transpiler/src/compiler/WebpackCompiler.ts b/packages/stryker-webpack-transpiler/src/compiler/WebpackCompiler.ts index e145e9acc0..7731f8cb33 100644 --- a/packages/stryker-webpack-transpiler/src/compiler/WebpackCompiler.ts +++ b/packages/stryker-webpack-transpiler/src/compiler/WebpackCompiler.ts @@ -3,7 +3,6 @@ import { Compiler, Configuration } from 'webpack'; import webpack from './Webpack'; import InputFileSystem from '../fs/InputFileSystem'; import OutputFileSystem from '../fs/OutputFileSystem'; -import FileSorter from './FileSorter'; export default class WebpackCompiler { private _compiler: Compiler; @@ -36,9 +35,8 @@ export default class WebpackCompiler { public emit(): Promise { return this.compile().then(stats => { const outputFiles = this._outputFS.collectFiles(); - const jsonStats = stats.toJson({ chunks: true }); this._outputFS.purge(); - return FileSorter.sort(outputFiles, jsonStats.chunks); + return outputFiles; }); } diff --git a/packages/stryker-webpack-transpiler/test/helpers/producers.ts b/packages/stryker-webpack-transpiler/test/helpers/producers.ts index fb565284d5..6d8216b2b5 100644 --- a/packages/stryker-webpack-transpiler/test/helpers/producers.ts +++ b/packages/stryker-webpack-transpiler/test/helpers/producers.ts @@ -1,7 +1,6 @@ import { Configuration, Stats } from 'webpack'; import { WebpackCompilerMock } from './mockInterfaces'; import * as sinon from 'sinon'; -import { Chunk } from '../../src/compiler/FileSorter'; import { StrykerWebpackConfig } from '../../src/WebpackTranspiler'; import { File } from 'stryker-api/core'; @@ -33,23 +32,6 @@ function createFactory(defaultFn: () => T): (overrides?: Partial) => T { return overrides => Object.assign(defaultFn(), overrides); } -export function createStats(chunks: Chunk[]) { - return { - hasErrors: () => false, - toJson() { - return { - chunks - }; - } - }; -} - -export const createChunk = createFactory(() => ({ - files: [], - id: '1', - parents: [] -})); - export const createStrykerWebpackConfig = createFactory(() => ({ configFile: undefined, silent: true, diff --git a/packages/stryker-webpack-transpiler/test/unit/compiler/FileSorterSpec.ts b/packages/stryker-webpack-transpiler/test/unit/compiler/FileSorterSpec.ts deleted file mode 100644 index d02ea9452c..0000000000 --- a/packages/stryker-webpack-transpiler/test/unit/compiler/FileSorterSpec.ts +++ /dev/null @@ -1,60 +0,0 @@ -import * as path from 'path'; -import { expect } from 'chai'; -import { File } from 'stryker-api/core'; -import FileSorter, { Chunk } from '../../../src/compiler/FileSorter'; -import { createChunk, createTextFile } from '../../helpers/producers'; - -describe('FileSorter', () => { - - let file2: File; - let file1: File; - let file4: File; - let file3: File; - let fooFile: File; - - beforeEach(() => { - file2 = createTextFile('2.js'); - file1 = createTextFile('1.js'); - file4 = createTextFile(path.resolve('4.js')); - file3 = createTextFile('3.js'); - fooFile = createTextFile('foo.js'); - }); - - - it('should sort chunks according to dependencies with pre-webpack 2 chunks', () => { - const output = FileSorter.sort([ - file2, - file1, - file4, - file3, - fooFile - ], [ - createChunk({ id: '1', files: ['1.js', 'foo.js'], parents: ['2', 'notExists'] }), - createChunk({ id: '2', files: ['2.js'], parents: ['3', '4'] }), - createChunk({ id: '3', files: ['3.js'] }), - createChunk({ id: '4', files: [path.resolve('4.js')], parents: ['3'] }) - ]); - expect(output).deep.eq([file3, file4, file2, file1, fooFile]); - }); - - it('should sort chunks according to dependencies with post-webpack 2 chunks', () => { - const chunk3: Chunk = createChunk({ id: '3', files: ['3.js'] }); - const chunk4: Chunk = createChunk({ id: '4', files: ['4.js'], parents: [chunk3] }); - const chunk2: Chunk = createChunk({ id: '2', files: ['2.js'], parents: [chunk3, chunk4] }); - const chunk1: Chunk = createChunk({ id: '1', files: ['1.js', 'foo.js'], parents: [chunk2] }); - const output = FileSorter.sort([ - file2, - file1, - file4, - file3, - fooFile - ], [ - chunk1, - chunk2, - chunk3, - chunk4 - ]); - expect(output).deep.eq([file3, file4, file2, file1, fooFile]); - }); - -}); \ No newline at end of file diff --git a/packages/stryker-webpack-transpiler/test/unit/compiler/WebpackCompilerSpec.ts b/packages/stryker-webpack-transpiler/test/unit/compiler/WebpackCompilerSpec.ts index ded78571b5..83e5e0fe44 100644 --- a/packages/stryker-webpack-transpiler/test/unit/compiler/WebpackCompilerSpec.ts +++ b/packages/stryker-webpack-transpiler/test/unit/compiler/WebpackCompilerSpec.ts @@ -1,20 +1,18 @@ import { expect } from 'chai'; import { File } from 'stryker-api/core'; -import { createFakeWebpackConfig, createTextFile, createWebpackMock, Mock, createMockInstance, createStats, createChunk } from '../../helpers/producers'; +import { createFakeWebpackConfig, createTextFile, createWebpackMock, Mock, createMockInstance } from '../../helpers/producers'; import { WebpackCompilerMock } from '../../helpers/mockInterfaces'; import InputFileSystem from '../../../src/fs/InputFileSystem'; import OutputFileSystem from '../../../src/fs/OutputFileSystem'; import WebpackCompiler from '../../../src/compiler/WebpackCompiler'; import * as webpack from '../../../src/compiler/Webpack'; import { Configuration } from 'webpack'; -import FileSorter, { Chunk } from '../../../src/compiler/FileSorter'; describe('WebpackCompiler', () => { let sut: WebpackCompiler; let inputFileSystemMock: Mock; let outputFileSystemMock: Mock; let webpackCompilerMock: WebpackCompilerMock; - let sortStub: sinon.SinonStub; let fakeWebpackConfig: Configuration; beforeEach(() => { @@ -22,7 +20,6 @@ describe('WebpackCompiler', () => { outputFileSystemMock = createMockInstance(OutputFileSystem); webpackCompilerMock = createWebpackMock(); fakeWebpackConfig = createFakeWebpackConfig(); - sortStub = sandbox.stub(FileSorter, 'sort'); sandbox.stub(webpack, 'default').returns(webpackCompilerMock); }); @@ -44,12 +41,10 @@ describe('WebpackCompiler', () => { describe('emit', () => { let webpackRunStub: sinon.SinonStub; - let chunks: Chunk[]; beforeEach(() => { - chunks = [createChunk(), createChunk()]; sut = new WebpackCompiler(fakeWebpackConfig, inputFileSystemMock as any, outputFileSystemMock as any); - webpackRunStub = sandbox.stub(webpackCompilerMock, 'run').callsArgWith(0, null, createStats(chunks)); + webpackRunStub = sandbox.stub(webpackCompilerMock, 'run').callsArgWith(0, null, { hasErrors: () => false }); }); it('should call the run function on the webpack compiler', async () => { @@ -61,23 +56,12 @@ describe('WebpackCompiler', () => { it('should collect files from the outputFS', async () => { const expectedFiles = [{ name: 'foobar' }]; outputFileSystemMock.collectFiles.returns(expectedFiles); - sortStub.returns(expectedFiles); const actualResult = await sut.emit(); expect(actualResult).eq(expectedFiles); expect(outputFileSystemMock.collectFiles).calledWith(); }); - it('should sort the files based on given chunks', async () => { - const expectedFiles = [{ name: 'foobar' }]; - const expectedResult = ['1', '2', '3']; - outputFileSystemMock.collectFiles.returns(expectedFiles); - sortStub.returns(expectedResult); - const actualResult = await sut.emit(); - expect(actualResult).deep.eq(expectedResult); - expect(sortStub).calledWith(expectedFiles, chunks); - }); - it('should return an error when the webpack compiler fails to compile', async () => { const fakeError: string = 'fakeError'; webpackRunStub.callsArgWith(0, new Error(fakeError));