提交 78f41bbb 编写于 作者: fxy060608's avatar fxy060608

feat: bin/uni.js

上级 7b12ab74
......@@ -115,9 +115,7 @@ export default defineConfig({
preserveEntrySignatures: 'strict',
plugins: rollupPlugins,
onwarn: (msg, warn) => {
if (
!(msg.message || msg).includes('external module "vue" but never used')
) {
if (!String(msg).includes('external module "vue" but never used')) {
warn(msg)
}
},
......
#!/usr/bin/env node
const debugIndex = process.argv.findIndex((arg) => /^(?:-d|--debug)$/.test(arg))
const filterIndex = process.argv.findIndex((arg) =>
/^(?:-f|--filter)$/.test(arg)
)
if (debugIndex > 0) {
let value = process.argv[debugIndex + 1]
if (!value || value.startsWith('-')) {
value = 'vite:*'
} else {
// support debugging multiple flags with comma-separated list
value = value
.split(',')
.map((v) => `vite:${v}`)
.join(',')
}
process.env.DEBUG = value
if (filterIndex > 0) {
const filter = process.argv[filterIndex + 1]
if (filter && !filter.startsWith('-')) {
process.env.VITE_DEBUG_FILTER = filter
}
}
}
require('../dist/cli')
......@@ -2,6 +2,9 @@
"name": "@dcloudio/vite-plugin-uni",
"version": "3.0.0",
"description": "uni-app vite plugin",
"bin": {
"uni": "bin/uni.js"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
......@@ -23,6 +26,8 @@
"dependencies": {
"@rollup/pluginutils": "^4.1.0",
"autoprefixer": "^10.2.5",
"cac": "^6.7.3",
"chalk": "^4.1.1",
"debug": "^4.3.1",
"estree-walker": "^2.0.1",
"fs-extra": "^9.0.1",
......
import { cac } from 'cac'
import chalk from 'chalk'
import {
LogLevel,
ServerOptions,
build,
createServer,
createLogger,
BuildOptions,
} from 'vite'
import { initEnv, PLATFORMS } from './utils'
const cli = cac('uni')
export interface CliOptions {
'--'?: string[]
platform?: string
p?: string
ssr?: boolean
debug?: boolean | string
d?: boolean | string
filter?: string
f?: string
logLevel?: LogLevel
l?: LogLevel
clearScreen?: boolean
}
function cleanOptions(options: CliOptions) {
const ret = { ...options }
delete ret['--']
delete ret.platform
delete ret.p
delete ret.ssr
delete ret.debug
delete ret.d
delete ret.filter
delete ret.f
delete ret.logLevel
delete ret.l
delete ret.clearScreen
return ret
}
cli
.option('-p,--platform [platform]', '[string] ' + PLATFORMS.join(' | '), {
default: 'h5',
})
.option('-ssr', '[boolean] server-side rendering', {
default: false,
})
.option('-l, --logLevel <level>', `[string] silent | error | warn | all`)
.option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
.option('-f, --filter <filter>', `[string] filter debug logs`)
cli
.command('')
.alias('dev')
.option('--host [host]', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--cors', `[boolean] enable CORS`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option(
'--force',
`[boolean] force the optimizer to ignore the cache and re-bundle`
)
.action(async (options: CliOptions) => {
initEnv(options)
try {
const server = await createServer({
root: process.env.VITE_ROOT_DIR,
logLevel: options.logLevel,
clearScreen: options.clearScreen,
server: cleanOptions(options) as ServerOptions,
})
await server.listen()
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting dev server:\n${e.stack}`)
)
process.exit(1)
}
})
cli
.command('build')
.option('--outDir <dir>', `[string] output directory (default: dist)`)
.option(
'--assetsInlineLimit <number>',
`[number] static asset base64 inline threshold in bytes (default: 4096)`
)
.option(
'--sourcemap',
`[boolean] output source maps for build (default: false)`
)
.option(
'--minify [minifier]',
`[boolean | "terser" | "esbuild"] enable/disable minification, ` +
`or specify minifier to use (default: terser)`
)
.option('--manifest', `[boolean] emit build manifest json`)
.option('--ssrManifest', `[boolean] emit ssr manifest json`)
.option(
'--emptyOutDir',
`[boolean] force empty outDir when it's outside of root`
)
.option('-m, --mode <mode>', `[string] set env mode`)
.option('-w, --watch', `[boolean] rebuilds when modules have changed on disk`)
.action(async (options: CliOptions) => {
initEnv(options)
try {
await build({
root: process.env.VITE_ROOT_DIR,
logLevel: options.logLevel,
clearScreen: options.clearScreen,
build: cleanOptions(options) as BuildOptions,
})
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error during build:\n${e.stack}`)
)
process.exit(1)
}
})
cli.help()
cli.version(require('../../package.json').version)
cli.parse()
import path from 'path'
import { CliOptions } from '.'
export const PLATFORMS = [
'app',
'h5',
'mp-alipay',
'mp-baidu',
'mp-qq',
'mp-toutiao',
'mp-weixin',
'quickapp-webview-huawei',
'quickapp-webview-union',
]
export function initEnv(options: CliOptions) {
process.env.VITE_ROOT_DIR = process.env.UNI_INPUT_DIR || process.cwd()
process.env.UNI_INPUT_DIR =
process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), 'src')
process.env.UNI_PLATFORM = options.platform as UniApp.PLATFORM
}
......@@ -6,7 +6,7 @@ import { EXTNAME_VUE, parseVueRequest } from '@dcloudio/uni-cli-shared'
import { UniPluginFilterOptions } from '.'
const debugScoped = debug('uni:scoped')
const debugScoped = debug('vite:uni:scoped')
const SCOPED_RE = /<style\s[^>]*scoped[^>]*>/i
......
......@@ -19,7 +19,7 @@ import { uniCssScopedPlugin } from './cssScoped'
import { uniRenderjsPlugin } from './renderjs'
import { uniPreVuePlugin } from './preVue'
const debugPlugin = debug('uni:plugin')
const debugPlugin = debug('vite:uni:plugin')
export interface UniPluginFilterOptions extends VitePluginUniResolvedOptions {
include?: FilterPattern
......
......@@ -51,8 +51,8 @@ export interface InjectOptions {
| Function
}
const debugInject = debug('uni:inject')
const debugInjectTry = debug('uni:inject-try')
const debugInject = debug('vite:uni:inject')
const debugInjectTry = debug('vite:uni:inject-try')
export function uniInjectPlugin(options: InjectOptions): Plugin {
if (!options) throw new Error('Missing options')
......
......@@ -6,7 +6,7 @@ import { Plugin } from 'vite'
import { parseVueRequest } from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '../..'
const debugPageVue = debug('uni:page-vue')
const debugPageVue = debug('vite:uni:page-vue')
export function uniPageVuePlugin(
options: VitePluginUniResolvedOptions
......
......@@ -11,9 +11,9 @@ import {
} from '@dcloudio/uni-cli-shared'
import { UniPluginFilterOptions } from '.'
const debugPreJs = debug('uni:pre-js')
const debugPreHtml = debug('uni:pre-html')
const debugPreJsTry = debug('uni:pre-js-try')
const debugPreJs = debug('vite:uni:pre-js')
const debugPreHtml = debug('vite:uni:pre-html')
const debugPreJsTry = debug('vite:uni:pre-js-try')
const PRE_JS_EXTNAME = ['.json', '.css'].concat(EXTNAME_VUE).concat(EXTNAME_JS)
const PRE_HTML_EXTNAME = EXTNAME_VUE
......
......@@ -5,8 +5,8 @@ import { preJs } from '@dcloudio/uni-cli-shared'
import { UniPluginFilterOptions } from '.'
const debugPre = debug('uni:pre-css')
const debugPreTry = debug('uni:pre-css-try')
const debugPre = debug('vite:uni:pre-css')
const debugPreTry = debug('vite:uni:pre-css-try')
const cssLangs = `\\.(less|sass|scss|styl|stylus|postcss)($|\\?)`
const cssLangRE = new RegExp(cssLangs)
/**
......
......@@ -11,7 +11,7 @@ import { parse } from '@vue/compiler-dom'
import { MagicString } from '@vue/compiler-sfc'
import { EXTNAME_VUE, parseVueRequest } from '@dcloudio/uni-cli-shared'
const debugPreVue = debug('uni:pre-vue')
const debugPreVue = debug('vite:uni:pre-vue')
const WXS_LANG_RE = /lang=["|'](renderjs|wxs)["|']/
......
......@@ -4,7 +4,7 @@ import { rewriteDefault } from '@vue/compiler-sfc'
import { parseVueRequest } from '@dcloudio/uni-cli-shared'
const debugRenderjs = debug('uni:renderjs')
const debugRenderjs = debug('vite:uni:renderjs')
export function uniRenderjsPlugin(): Plugin {
return {
......
......@@ -5,7 +5,7 @@ import { cleanUrl } from '@dcloudio/uni-cli-shared'
import { UniPluginFilterOptions } from '.'
import { createPublicFileFilter } from '../../utils'
const debugStatic = debug('uni:static')
const debugStatic = debug('vite:uni:static')
/**
* 提供static等目录静态资源加载
* @param _options
......
......@@ -10,7 +10,7 @@ import { VitePluginUniResolvedOptions } from '..'
import { uniStaticMiddleware } from './middlewares/static'
import { createPublicFileFilter } from '../utils'
const debugStatic = debug('uni:static')
const debugStatic = debug('vite:uni:static')
/**
* devServer时提供static等目录的静态资源服务
* @param server
......
......@@ -8,7 +8,7 @@ import { parseManifestJson, parsePagesJson } from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '..'
import { initEasycomsOnce, initFeatures } from '../utils'
const debugHmr = debug('uni:hmr')
const debugHmr = debug('vite:uni:hmr')
async function invalidate(file: string, moduleGraph: ModuleGraph) {
const mods = await moduleGraph.getModulesByFile(slash(file))
......
......@@ -4,7 +4,7 @@ import { Plugin } from 'vite'
import { VitePluginUniResolvedOptions } from '..'
import { BUILT_IN_MODULES } from '../utils'
const debugResolve = debug('uni:resolve')
const debugResolve = debug('vite:uni:resolve')
export function createResolveId(
_options: VitePluginUniResolvedOptions
......
......@@ -22,7 +22,7 @@ interface EasycomCustom {
[key: string]: string
}
export const debugEasycom = debug('uni:easycom')
export const debugEasycom = debug('vite:uni:easycom')
const easycoms: EasycomMatcher[] = []
......
......@@ -64,6 +64,9 @@ async function build(target) {
env: Object.assign({ FORMAT: 'es' }, process.env),
}
)
if (target === 'size-check') {
return
}
return await execa(
'vite',
['build', '--config', path.resolve(pkgDir, 'vite.config.ts')],
......
......@@ -1676,6 +1676,11 @@ bytes-iec@^3.1.1:
resolved "https://registry.yarnpkg.com/bytes-iec/-/bytes-iec-3.1.1.tgz#94cd36bf95c2c22a82002c247df8772d1d591083"
integrity sha512-fey6+4jDK7TFtFg/klGSvNKJctyU7n2aQdnM+CO0ruLPbqqMOM8Tio0Pc+deqUeVKX1tL5DQep1zQ7+37aTAsA==
cac@^6.7.3:
version "6.7.3"
resolved "https://registry.yarnpkg.com/cac/-/cac-6.7.3.tgz#10410b8611677990cc2e3c8b576d471c1d71b768"
integrity sha512-ECVqVZh74qgSuZG9YOt2OJPI3wGcf+EwwuF/XIOYqZBD0KZYLtgPWqFPxmDPQ6joxI1nOlvVgRV6VT53Ooyocg==
cache-base@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册