-
Notifications
You must be signed in to change notification settings - Fork 4k
/
build.js
99 lines (86 loc) · 2.83 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* External dependencies
*/
const fs = require( 'fs' );
const glob = require( 'glob' ).sync;
const mkdirp = require( 'mkdirp' ).sync;
const path = require( 'path' );
const rimraf = require( 'rimraf' ).sync;
const webpack = require( 'webpack' );
const fixturesPath = path.join( __dirname, 'fixtures' );
const configFixtures = fs.readdirSync( fixturesPath ).sort();
jest.useRealTimers();
describe( 'DependencyExtractionWebpackPlugin', () => {
afterAll( () => rimraf( path.join( __dirname, 'build' ) ) );
describe.each( configFixtures )( 'Webpack `%s`', ( configCase ) => {
const testDirectory = path.join( fixturesPath, configCase );
const outputDirectory = path.join( __dirname, 'build', configCase );
beforeEach( () => {
rimraf( outputDirectory );
mkdirp( outputDirectory );
} );
// This afterEach is necessary to prevent watched tests from retriggering on every run.
afterEach( () => rimraf( outputDirectory ) );
test( 'should produce expected output', () =>
new Promise( ( resolve ) => {
const options = Object.assign(
{
context: testDirectory,
entry: './index.js',
mode: 'production',
optimization: {
minimize: false,
chunkIds: 'named',
moduleIds: 'named',
},
output: {},
},
require( path.join( testDirectory, 'webpack.config.js' ) )
);
options.output.path = outputDirectory;
webpack( options, ( err, stats ) => {
expect( err ).toBeNull();
const assetFiles = glob(
`${ outputDirectory }/+(*.asset|assets).@(json|php)`
);
const hasCombinedAssets = ( options.plugins || [] ).some(
( plugin ) => !! ( plugin.options || {} ).combineAssets
);
const entrypointCount =
typeof options.entry === 'object'
? Object.keys( options.entry ).length
: 1;
const expectedLength = hasCombinedAssets
? 1
: entrypointCount;
expect( assetFiles ).toHaveLength( expectedLength );
// Asset files should match.
assetFiles.forEach( ( assetFile ) => {
expect(
fs.readFileSync( assetFile, 'utf-8' )
).toMatchSnapshot( 'Asset file should match snapshot' );
} );
const compareByModuleIdentifier = ( m1, m2 ) => {
const i1 = m1.identifier();
const i2 = m2.identifier();
if ( i1 < i2 ) return -1;
if ( i1 > i2 ) return 1;
return 0;
};
// Webpack stats external modules should match.
const externalModules = stats.compilation.modules
.filter( ( { externalType } ) => externalType )
.sort( compareByModuleIdentifier )
.map( ( module ) => ( {
externalType: module.externalType,
request: module.request,
userRequest: module.userRequest,
} ) );
expect( externalModules ).toMatchSnapshot(
'External modules should match snapshot'
);
resolve();
} );
} ) );
} );
} );