-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docz-core): add initial gatsby bundler algorithm
- Loading branch information
1 parent
b2091fb
commit 9249e34
Showing
23 changed files
with
289 additions
and
924 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,3 @@ | ||
import * as fs from 'fs-extra' | ||
import * as logger from 'signale' | ||
import * as envDotProp from 'env-dot-prop' | ||
import webpack, { Configuration as CFG } from 'webpack' | ||
import chalk from 'chalk' | ||
|
||
const FSR = require('react-dev-utils/FileSizeReporter') | ||
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages') | ||
|
||
import * as paths from '../config/paths' | ||
|
||
const { measureFileSizesBeforeBuild, printFileSizesAfterBuild } = FSR | ||
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024 | ||
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024 | ||
|
||
const hasCiEnvVar = () => envDotProp.get('ci', false, { parse: true }) | ||
|
||
const copyPublicFolder = async ( | ||
dest: string, | ||
publicDir: string | ||
): Promise<void> => { | ||
if (await fs.pathExists(publicDir)) { | ||
await fs.copy(publicDir, paths.distPublic(dest), { | ||
dereference: true, | ||
filter: file => file !== paths.indexHtml, | ||
}) | ||
} | ||
} | ||
|
||
const compile = (config: CFG) => | ||
new Promise((resolve, reject) => { | ||
let compiler | ||
try { | ||
compiler = webpack(config) | ||
} catch (err) { | ||
onError(err) | ||
} | ||
compiler && | ||
compiler.run((err, stats) => { | ||
if (err) reject(err) | ||
resolve(stats) | ||
}) | ||
}) | ||
|
||
const builder = async (config: CFG, previousFileSizes: any) => | ||
new Promise(async (resolve, reject) => { | ||
try { | ||
const stats: any = await compile(config) | ||
const messages = formatWebpackMessages(stats.toJson({}, true)) | ||
|
||
if (messages.errors.length) { | ||
return reject(new Error(messages.errors.join('\n\n'))) | ||
} | ||
|
||
if (hasCiEnvVar() && messages.warnings.length) { | ||
logger.warn( | ||
'\nTreating warnings as errors because process.env.CI = true.\n' + | ||
'Most CI servers set it automatically.\n' | ||
) | ||
return reject(new Error(messages.warnings.join('\n\n'))) | ||
} | ||
|
||
return resolve({ | ||
stats, | ||
previousFileSizes, | ||
warnings: messages.warnings, | ||
}) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
|
||
const onSuccess = ( | ||
dist: string, | ||
{ stats, previousFileSizes, warnings }: any | ||
) => { | ||
if (warnings.length) { | ||
logger.log() | ||
logger.warn('Compiled with warnings.\n') | ||
logger.warn(warnings.join('\n\n')) | ||
logger.warn( | ||
'\nSearch for the ' + | ||
chalk.underline(chalk.yellow('keywords')) + | ||
' to learn more about each warning.' | ||
) | ||
logger.warn( | ||
'To ignore, add ' + | ||
chalk.cyan('// eslint-disable-next-line') + | ||
' to the line before.\n' | ||
) | ||
} | ||
|
||
logger.log() | ||
logger.log(`File sizes after gzip:\n`) | ||
printFileSizesAfterBuild( | ||
stats, | ||
previousFileSizes, | ||
dist, | ||
WARN_AFTER_BUNDLE_GZIP_SIZE, | ||
WARN_AFTER_CHUNK_GZIP_SIZE | ||
) | ||
logger.log() | ||
} | ||
|
||
const onError = (err: Error) => { | ||
logger.log() | ||
logger.fatal(err) | ||
process.exit(1) | ||
logger.log() | ||
} | ||
|
||
export const build = async (config: CFG, dist: string, publicDir: string) => { | ||
const interactive = new logger.Signale({ interactive: true, scope: 'build' }) | ||
|
||
try { | ||
interactive.start('Creating an optimized bundle') | ||
|
||
await fs.ensureDir(dist) | ||
const previousFileSizes = await measureFileSizesBeforeBuild(dist) | ||
|
||
await fs.emptyDir(dist) | ||
await copyPublicFolder(dist, publicDir) | ||
|
||
const result = await builder(config, previousFileSizes) | ||
|
||
interactive.success('Build successfully created') | ||
onSuccess(dist, result) | ||
} catch (err) { | ||
logger.fatal(chalk.red('Failed to compile.\n')) | ||
onError(err) | ||
} | ||
export const build = async (config: any, dist: string, publicDir: string) => { | ||
return null | ||
} |
Oops, something went wrong.