Skip to content

Commit

Permalink
feat: [dp] remove chokidar
Browse files Browse the repository at this point in the history
  • Loading branch information
imsunhao committed Sep 19, 2019
1 parent 927fde6 commit 76c5430
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 7 deletions.
File renamed without changes.
File renamed without changes.
118 changes: 118 additions & 0 deletions packages/start/bin/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const parseArgs = require('minimist')
const consola = require('consola')
const { version } = require('../package.json')

function getArgv() {
const alias = {
H: 'hostname',
p: 'port',
h: 'help',
e: 'entry',
o: 'output',
d: 'dll',
c: 'config-file',
fd: 'fileDescriptor',
ic: 'injectContext',
cl: 'clear',
v: 'version'
}
const string = ['H', 'c', 'fd', 'e', 'o', 'ic']
const argv = parseArgs(process.argv.slice(2), {
alias,
boolean: ['h', 'd', 'v', 'cl'],
string,
default: {
c: 'build.config.json'
}
})

if (argv.help) {
process.stderr.write(`
Description
Starts the application in ${process.env.NODE_ENV} mode
Options
--port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application
--clear, --cl clear cache
--dll, -d build webpack dll
--injectContext, -ic Path to injectContext file
--version, -v look over version
--config-file, -c Path to config file (default: build.config.json)
--help, -h Displays this message
`)
process.exit(0)
}

if (argv.version) {
process.stderr.write(version + '\n')
process.exit(0)
}

if (argv.hostname === '') {
consola.fatal('Provided hostname argument has no value')
process.exit(1)
}

if (argv.output === '') {
consola.fatal('Provided output argument has no value')
process.exit(1)
}

if (argv.entry === '') {
consola.fatal('Provided entry argument has no value')
process.exit(1)
}

string.forEach(index => {
if (Array.isArray(argv[index])) {
const value = argv[index][argv[index].length - 1]
argv[index] = value
argv[alias[index]] = value
}
})

if (argv.clear) {
consola.info('clear cache')
const rimraf = require('rimraf')
const resolve = require('path').resolve
const rootDir = resolve(argv._[0] || '.')

if (argv.output) {
path = resolve(rootDir, argv.output)
} else {
const fs = require('fs')

if (argv['config-file']) {
const configFile = resolve(rootDir, argv['config-file'])
if (!fs.existsSync(configFile)) {
consola.fatal('configFile is not exists', configFile)
return process.exit(1)
}

let options = {}

try {
const jsonString = fs.readFileSync(configFile, { encoding: 'utf-8' })
options = JSON.parse(jsonString)
} catch (error) {
consola.fatal('clear:', error)
return process.exit(1)
}

path = resolve(rootDir, options.output || './dist/build')
} else {
path = resolve(rootDir, './dist/build')
}
}

rimraf.sync(path)
}

argv.version = version

return argv
}

module.exports = {
getArgv,
}
2 changes: 1 addition & 1 deletion lib/start/package.json → packages/start/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bestminr/start",
"version": "1.7.2",
"version": "1.7.3",
"license": "MIT",
"files": [
"bin/*",
Expand Down
19 changes: 14 additions & 5 deletions lib/start/webpack.js → packages/start/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const path = require('path')
const _ = require('lodash')
const config = _.cloneDeep(require('../../webpack'))
const resolve = p => path.resolve(__dirname, '../../', p)
const CopyPlugin = require('copy-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin')
const nodeExternals = require('webpack-node-externals')

// config.mode = 'production'
const emptyModule = config.entry['empty-module']
Expand All @@ -17,7 +18,7 @@ const alias = {
'src-config-webpack.dll.config$': emptyModule,
'src-config-webpack.extensions.config$': emptyModule,
'src-utils-compiler$': emptyModule,
'chokidar': emptyModule,
chokidar: emptyModule
// 'friendly-errors-webpack-plugin': emptyModule,
// 'friendly-errors-webpack-plugin': emptyModule,
// 'fork-ts-checker-webpack-plugin': emptyModule,
Expand All @@ -30,11 +31,11 @@ const alias = {
}

// process.exit(0)
config.output.path = resolve('./lib/start/dist')
config.output.path = resolve('./packages/start/dist')
const plugins = [
new CopyPlugin([
{ from: resolve('./bin/utils.js'), to: resolve('./lib/start/bin') },
]),
{ from: resolve('./bin/utils.js'), to: resolve('./packages/start/bin') }
])
]

if (config.plugins) {
Expand All @@ -53,8 +54,16 @@ if (config.resolve && config.resolve.alias) {
}
}

config.externals = [
nodeExternals({
whitelist: [/\.css$/, /\?vue&type=style/, 'chokidar']
})
]

// console.dir(config, {
// depth: null
// })

// process.exit(0)

module.exports = config
File renamed without changes.
2 changes: 1 addition & 1 deletion script/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const webpack = require('webpack')
const config = require('../webpack')
const startConfig = require('../lib/start/webpack')
const startConfig = require('../packages/start/webpack')

const mainCompiler = webpack(config)

Expand Down

0 comments on commit 76c5430

Please sign in to comment.