From 76c54300fea8a62722d20514dc06378c3fb94956 Mon Sep 17 00:00:00 2001 From: sunhao Date: Thu, 19 Sep 2019 12:52:39 +0800 Subject: [PATCH] feat: [dp] remove chokidar --- {lib => packages}/start/CHANGELOG.md | 0 {lib => packages}/start/bin/start | 0 packages/start/bin/utils.js | 118 +++++++++++++++++++++++++++ {lib => packages}/start/package.json | 2 +- {lib => packages}/start/webpack.js | 19 +++-- {lib => packages}/start/yarn.lock | 0 script/build.js | 2 +- 7 files changed, 134 insertions(+), 7 deletions(-) rename {lib => packages}/start/CHANGELOG.md (100%) rename {lib => packages}/start/bin/start (100%) create mode 100644 packages/start/bin/utils.js rename {lib => packages}/start/package.json (97%) rename {lib => packages}/start/webpack.js (78%) rename {lib => packages}/start/yarn.lock (100%) diff --git a/lib/start/CHANGELOG.md b/packages/start/CHANGELOG.md similarity index 100% rename from lib/start/CHANGELOG.md rename to packages/start/CHANGELOG.md diff --git a/lib/start/bin/start b/packages/start/bin/start similarity index 100% rename from lib/start/bin/start rename to packages/start/bin/start diff --git a/packages/start/bin/utils.js b/packages/start/bin/utils.js new file mode 100644 index 0000000..f78a2ce --- /dev/null +++ b/packages/start/bin/utils.js @@ -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, +} diff --git a/lib/start/package.json b/packages/start/package.json similarity index 97% rename from lib/start/package.json rename to packages/start/package.json index a16eeac..8805460 100644 --- a/lib/start/package.json +++ b/packages/start/package.json @@ -1,6 +1,6 @@ { "name": "@bestminr/start", - "version": "1.7.2", + "version": "1.7.3", "license": "MIT", "files": [ "bin/*", diff --git a/lib/start/webpack.js b/packages/start/webpack.js similarity index 78% rename from lib/start/webpack.js rename to packages/start/webpack.js index ae0d828..5e222cf 100644 --- a/lib/start/webpack.js +++ b/packages/start/webpack.js @@ -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'] @@ -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, @@ -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) { @@ -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 diff --git a/lib/start/yarn.lock b/packages/start/yarn.lock similarity index 100% rename from lib/start/yarn.lock rename to packages/start/yarn.lock diff --git a/script/build.js b/script/build.js index 796cb16..027b64f 100644 --- a/script/build.js +++ b/script/build.js @@ -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)