-
-
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.
chore(docz-core): refac build command
- Loading branch information
1 parent
ef7abd2
commit 2bd3b5b
Showing
8 changed files
with
215 additions
and
177 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,125 @@ | ||
import * as fs from 'fs-extra' | ||
import chalk from 'chalk' | ||
import logger from 'signale' | ||
import webpack, { Configuration } from 'webpack' | ||
import FSR from 'react-dev-utils/FileSizeReporter' | ||
import formatWebpackMessages from 'react-dev-utils/formatWebpackMessages' | ||
import printBuildError from 'react-dev-utils/printBuildError' | ||
|
||
import * as paths from '../../config/paths' | ||
|
||
process.env.BABEL_ENV = process.env.BABEL_ENV || 'production' | ||
process.env.NODE_ENV = process.env.NODE_ENV || 'production' | ||
|
||
const { measureFileSizesBeforeBuild, printFileSizesAfterBuild } = FSR | ||
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024 | ||
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024 | ||
|
||
const hasCiEnvVar = () => | ||
process.env.CI && | ||
(typeof process.env.CI !== 'string' || | ||
process.env.CI.toLowerCase() !== 'false') | ||
|
||
const copyPublicFolder = async (): Promise<void> => { | ||
if (await fs.pathExists(paths.appPublic)) { | ||
await fs.copySync(paths.appPublic, paths.distPublic, { | ||
dereference: true, | ||
filter: file => file !== paths.indexHtml, | ||
}) | ||
} | ||
} | ||
|
||
const compile = (config: Configuration) => | ||
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: Configuration, previousFileSizes: any) => { | ||
logger.start('Creating an optimized production build...') | ||
|
||
return 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 = ({ stats, previousFileSizes, warnings }: any) => { | ||
if (warnings.length) { | ||
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' | ||
) | ||
} else { | ||
logger.success(chalk.green('Compiled successfully.\n')) | ||
} | ||
|
||
logger.log('File sizes after gzip:\n') | ||
printFileSizesAfterBuild( | ||
stats, | ||
previousFileSizes, | ||
paths.dist, | ||
WARN_AFTER_BUNDLE_GZIP_SIZE, | ||
WARN_AFTER_CHUNK_GZIP_SIZE | ||
) | ||
logger.log() | ||
} | ||
|
||
const onError = (err: Error) => { | ||
logger.fatal(chalk.red('Failed to compile.\n')) | ||
printBuildError(err) | ||
process.exit(1) | ||
} | ||
|
||
export const build = async (config: Configuration) => { | ||
try { | ||
const previousFileSizes = await measureFileSizesBeforeBuild(paths.dist) | ||
|
||
await fs.emptyDir(paths.dist) | ||
await copyPublicFolder() | ||
|
||
const result = await builder(config, previousFileSizes) | ||
onSuccess(result) | ||
} catch (err) { | ||
onError(err) | ||
} | ||
} |
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,39 +1,20 @@ | ||
import webpack, { Configuration as CFG } from 'webpack' | ||
import serve from 'webpack-serve' | ||
import detectPort from 'detect-port' | ||
import { Configuration as CFG } from 'webpack' | ||
|
||
import { devServerConfig } from './devserver' | ||
import { createConfig } from './config' | ||
import { Bundler, BundlerServer } from '../../Bundler' | ||
import { Bundler } from '../../Bundler' | ||
import { Config as Args } from '../../commands/args' | ||
import { createConfig } from './config' | ||
import { server } from './server' | ||
import { build } from './build' | ||
|
||
export type Env = 'production' | 'development' | ||
|
||
export const server = (args: Args) => async ( | ||
config: CFG | ||
): Promise<BundlerServer> => { | ||
const compiler = webpack(config) | ||
const port = await detectPort(args.port) | ||
const devserver = devServerConfig({ ...args, port }, compiler, config) | ||
|
||
return { | ||
start: async () => { | ||
const instance = await serve(devserver) | ||
|
||
return { | ||
on: instance.on, | ||
close: instance.close, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
export const bundler = (args: Args, env: Env): Bundler<CFG> => { | ||
const config: any = createConfig(args, env).toConfig() | ||
|
||
return new Bundler({ | ||
args, | ||
config, | ||
build, | ||
server: server(args), | ||
}) | ||
} |
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,26 @@ | ||
import webpack, { Configuration as CFG } from 'webpack' | ||
import serve from 'webpack-serve' | ||
import detectPort from 'detect-port' | ||
|
||
import { devServerConfig } from './devserver' | ||
import { BundlerServer } from '../../Bundler' | ||
import { Config as Args } from '../../commands/args' | ||
|
||
export const server = (args: Args) => async ( | ||
config: CFG | ||
): Promise<BundlerServer> => { | ||
const compiler = webpack(config) | ||
const port = await detectPort(args.port) | ||
const devserver = devServerConfig({ ...args, port }, compiler, config) | ||
|
||
return { | ||
start: async () => { | ||
const instance = await serve(devserver) | ||
|
||
return { | ||
on: instance.on, | ||
close: instance.close, | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.