File size: 4,143 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const webpack = require( 'webpack' );

/**
 * Transform webpack output.filename and output.chunkFilename to CSS variants
 * @param {string|undefined} name filename, chunkFilename or undefined
 * @returns {string|undefined}     Transformed name or undefined
 */
function cssNameFromFilename( name ) {
	if ( name ) {
		const [ cssChunkFilename, chunkQueryString ] = name.split( '?', 2 );
		return cssChunkFilename.replace(
			/\.js$/i,
			'.css' + ( chunkQueryString ? `?${ chunkQueryString }` : '' )
		);
	}
}

function IncrementalProgressPlugin() {
	function createProgressHandler() {
		const startTime = Date.now();
		let lastShownBuildingMessageTime = null;
		let lastUnshownBuildingMessage = null;

		return ( percentage, msg, ...details ) => {
			const nowTime = Date.now();
			const timeString = ( ( nowTime - startTime ) / 1000 ).toFixed( 1 ) + 's';
			const percentageString = `${ Math.floor( percentage * 100 ) }%`;
			const detailsString = details
				.map( ( detail ) => {
					if ( ! detail ) {
						return '';
					}
					if ( detail.length > 40 ) {
						return `…${ detail.substr( detail.length - 39 ) }`;
					}
					return detail;
				} )
				.join( ' ' );
			const message = `${ timeString } ${ percentageString } ${ msg } ${ detailsString }`;

			// There are plenty of 'building' messages that make the log too long for CircleCI web UI.
			// Let's throttle the 'building' messages to one per second, while always showing the last one.
			if ( msg === 'building' ) {
				if ( lastShownBuildingMessageTime && nowTime - lastShownBuildingMessageTime < 1000 ) {
					// less than 1000ms since last message: ignore, but store for case it's the last one
					lastUnshownBuildingMessage = message;
					return;
				}

				// the message will be shown and its time recorded
				lastShownBuildingMessageTime = nowTime;
				lastUnshownBuildingMessage = null;
			} else if ( lastUnshownBuildingMessage ) {
				// The last 'building' message should always be shown, no matter the timing.
				// We do that on the first non-'building' message.
				console.log( lastUnshownBuildingMessage );
				lastUnshownBuildingMessage = null;
			}

			console.log( message );
		};
	}
	return new webpack.ProgressPlugin( createProgressHandler() );
}

const nodeModulesToTranspile = [
	// general form is <package-name>/.
	// The trailing slash makes sure we're not matching these as prefixes
	// In some cases we do want prefix style matching (lodash. for lodash.assign)
	'@automattic/calypso-polyfills/',
	'@automattic/lasagna/',
	'@automattic/react-virtualized/',
	'@github/webauthn-json/',
	'acorn-jsx/',
	'chalk/',
	'd3-array/',
	'd3-scale/',
	'debug/',
	'dom7/',
	'escape-string-regexp/',
	'filesize/',
	'gridicons/',
	'prismjs/',
	'punycode/',
	'query-string/',
	'react-spring/',
	'regenerate-unicode-properties/',
	'regexpu-core/',
	'split-on-first/',
	'strict-uri-encode/',
	'striptags/',
	'swiper/',
	'unicode-match-property-ecmascript/',
	'unicode-match-property-value-ecmascript/',
	'calypso/',
];

/**
 * Check to see if we should transpile certain files in node_modules
 * @param {string} filepath the path of the file to check
 * @returns {boolean} True if we should transpile it, false if not
 *
 * We had a thought to try to find the package.json and use the engines property
 * to determine what we should transpile, but not all libraries set engines properly
 * (see d3-array@2.0.0). Instead, we transpile libraries we know to have dropped Node 4 support
 * are likely to remain so going forward.
 */
function shouldTranspileDependency( filepath ) {
	// find the last index of node_modules and check from there
	// we want <working>/node_modules/a-package/node_modules/foo/index.js to only match foo, not a-package
	const marker = '/node_modules/';
	const lastIndex = filepath.lastIndexOf( marker );
	if ( lastIndex === -1 ) {
		// we're not in node_modules
		return false;
	}

	const checkFrom = lastIndex + marker.length;

	return nodeModulesToTranspile.some( ( modulePart ) =>
		filepath.startsWith( modulePart, checkFrom )
	);
}

module.exports = { cssNameFromFilename, IncrementalProgressPlugin, shouldTranspileDependency };