-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import chalk from 'chalk'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import filesize from 'filesize'; | ||
import { sync as gzipSize } from 'gzip-size'; | ||
import webpack from 'webpack'; | ||
import recursive from 'recursive-readdir'; | ||
import stripAnsi from 'strip-ansi'; | ||
import getPaths from './config/paths'; | ||
import getConfig from './utils/getConfig'; | ||
import applyWebpackConfig, { warnIfExists } from './utils/applyWebpackConfig'; | ||
|
||
process.env.NODE_ENV = process.env.NODE_ENV || 'production'; | ||
|
||
const argv = require('yargs') | ||
.usage('Usage: roadhog buildDll [options]') | ||
.help('h') | ||
.argv; | ||
|
||
let rcConfig; | ||
let outputPath; | ||
let appBuild; | ||
let config; | ||
|
||
export function build(argv) { | ||
const paths = getPaths(argv.cwd); | ||
|
||
try { | ||
rcConfig = getConfig(process.env.NODE_ENV, argv.cwd); | ||
} catch (e) { | ||
console.log(chalk.red('Failed to parse .roadhogrc config.')); | ||
console.log(); | ||
console.log(e.message); | ||
process.exit(1); | ||
} | ||
|
||
if (!rcConfig.dllPlugin) { | ||
console.log(chalk.red('dllPlugin config not found in .roadhogrc')); | ||
process.exit(1); | ||
} | ||
|
||
appBuild = paths.dllNodeModule; | ||
config = applyWebpackConfig( | ||
require('./config/webpack.config.dll')(argv, rcConfig, paths), | ||
process.env.NODE_ENV, | ||
); | ||
|
||
return new Promise((resolve) => { | ||
// First, read the current file sizes in build directory. | ||
// This lets us display how much they changed later. | ||
recursive(appBuild, (err, fileNames) => { | ||
const previousSizeMap = (fileNames || []) | ||
.filter(fileName => /\.(js|css)$/.test(fileName)) | ||
.reduce((memo, fileName) => { | ||
const contents = fs.readFileSync(fileName); | ||
const key = removeFileNameHash(fileName); | ||
memo[key] = gzipSize(contents); | ||
return memo; | ||
}, {}); | ||
|
||
// Remove all content but keep the directory so that | ||
// if you're in it, you don't end up in Trash | ||
fs.emptyDirSync(appBuild); | ||
|
||
// Start the webpack build | ||
realBuild(previousSizeMap, resolve, argv); | ||
}); | ||
}); | ||
} | ||
|
||
// Input: /User/dan/app/build/static/js/main.82be8.js | ||
// Output: /static/js/main.js | ||
function removeFileNameHash(fileName) { | ||
return fileName | ||
.replace(appBuild, '') | ||
.replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); | ||
} | ||
|
||
// Input: 1024, 2048 | ||
// Output: "(+1 KB)" | ||
function getDifferenceLabel(currentSize, previousSize) { | ||
const FIFTY_KILOBYTES = 1024 * 50; | ||
const difference = currentSize - previousSize; | ||
const fileSize = !Number.isNaN(difference) ? filesize(difference) : 0; | ||
if (difference >= FIFTY_KILOBYTES) { | ||
return chalk.red(`+${fileSize}`); | ||
} else if (difference < FIFTY_KILOBYTES && difference > 0) { | ||
return chalk.yellow(`+${fileSize}`); | ||
} else if (difference < 0) { | ||
return chalk.green(fileSize); | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
// Print a detailed summary of build files. | ||
function printFileSizes(stats, previousSizeMap) { | ||
const assets = stats.toJson().assets | ||
.filter(asset => /\.(js|css)$/.test(asset.name)) | ||
.map((asset) => { | ||
const fileContents = fs.readFileSync(`${appBuild}/${asset.name}`); | ||
const size = gzipSize(fileContents); | ||
const previousSize = previousSizeMap[removeFileNameHash(asset.name)]; | ||
const difference = getDifferenceLabel(size, previousSize); | ||
return { | ||
folder: path.join(appBuild, path.dirname(asset.name)), | ||
name: path.basename(asset.name), | ||
size, | ||
sizeLabel: filesize(size) + (difference ? ` (${difference})` : ''), | ||
}; | ||
}); | ||
assets.sort((a, b) => b.size - a.size); | ||
const longestSizeLabelLength = Math.max.apply( | ||
null, | ||
assets.map(a => stripAnsi(a.sizeLabel).length), | ||
); | ||
assets.forEach((asset) => { | ||
let sizeLabel = asset.sizeLabel; | ||
const sizeLength = stripAnsi(sizeLabel).length; | ||
if (sizeLength < longestSizeLabelLength) { | ||
const rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength); | ||
sizeLabel += rightPadding; | ||
} | ||
console.log( | ||
` ${sizeLabel} ${chalk.dim(asset.folder + path.sep)}${chalk.cyan(asset.name)}`, | ||
); | ||
}); | ||
} | ||
|
||
// Print out errors | ||
function printErrors(summary, errors) { | ||
console.log(chalk.red(summary)); | ||
console.log(); | ||
errors.forEach((err) => { | ||
console.log(err.message || err); | ||
console.log(); | ||
}); | ||
} | ||
|
||
function doneHandler(previousSizeMap, argv, resolve, err, stats) { | ||
if (err) { | ||
printErrors('Failed to compile.', [err]); | ||
process.exit(1); | ||
} | ||
|
||
if (stats.compilation.errors.length) { | ||
printErrors('Failed to compile.', stats.compilation.errors); | ||
process.exit(1); | ||
} | ||
|
||
warnIfExists(); | ||
|
||
console.log(chalk.green('Compiled successfully.')); | ||
console.log(); | ||
|
||
console.log('File sizes after gzip:'); | ||
console.log(); | ||
printFileSizes(stats, previousSizeMap); | ||
console.log(); | ||
|
||
resolve(); | ||
} | ||
|
||
// Create the production build and print the deployment instructions. | ||
function realBuild(previousSizeMap, resolve, argv) { | ||
console.log('Creating dll bundle...'); | ||
|
||
const compiler = webpack(config); | ||
const done = doneHandler.bind(null, previousSizeMap, argv, resolve); | ||
compiler.run(done); | ||
} | ||
|
||
// Run. | ||
if (require.main === module) { | ||
build({ ...argv, cwd: process.cwd() }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import webpack from 'webpack'; | ||
import { join } from 'path'; | ||
|
||
import pullAll from 'lodash.pullall'; | ||
import uniq from 'lodash.uniq'; | ||
|
||
export default function (argv, rcConfig, paths) { | ||
const appBuild = paths.dllNodeModule; | ||
const pkg = require(join(paths.appDirectory, 'package.json')); // disable-eslint-line | ||
|
||
const { include, exclude } = rcConfig.dllPlugin || {}; | ||
|
||
const dependencyNames = Object.keys(pkg.dependencies); | ||
const includeDependencies = uniq(dependencyNames.concat(include || [])); | ||
|
||
return { | ||
entry: { | ||
roadhog: pullAll(includeDependencies, exclude), | ||
}, | ||
output: { | ||
path: appBuild, | ||
filename: '[name].dll.js', | ||
library: '[name]', | ||
}, | ||
plugins: [ | ||
new webpack.DllPlugin({ | ||
path: join(appBuild, '[name].json'), | ||
name: '[name]', | ||
context: paths.appSrc, | ||
}), | ||
], | ||
resolve: { | ||
root: paths.appDirectory, | ||
modulesDirectories: ['node_modules'], | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters