Skip to content

Commit

Permalink
fix: show a warning if @swc/core was not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 8, 2021
1 parent a6e06e8 commit 213ac91
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function runEsbuild(
skipNodeModulesBundle: options.skipNodeModulesBundle,
tsconfigResolvePaths: options.tsconfigResolvePaths,
}),
options.tsconfigDecoratorMetadata && swcPlugin(),
options.tsconfigDecoratorMetadata && swcPlugin({ logger }),
nativeNodeModulesPlugin(),
postcssPlugin({ css }),
sveltePlugin({ css }),
Expand Down Expand Up @@ -226,7 +226,7 @@ export async function runEsbuild(
// Manually write files
if (result && result.outputFiles) {
const timeInMs = Date.now() - startTime
logger.success(format, `Build success in ${Math.floor(timeInMs)}ms`)
logger.success(format, `⚡️ Build success in ${Math.floor(timeInMs)}ms`)

await Promise.all(
result.outputFiles.map(async (file) => {
Expand Down
7 changes: 6 additions & 1 deletion src/esbuild/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
*/
import { JscConfig } from '@swc/core'
import { Plugin } from 'esbuild'
import { Logger } from '../log'
import { localRequire } from '../utils'

export const swcPlugin = (): Plugin => {
export const swcPlugin = ({ logger }: { logger: Logger }): Plugin => {
return {
name: 'swc',

setup(build) {
const swc: typeof import('@swc/core') = localRequire('@swc/core')

if (!swc) {
logger.warn(
'CLI',
`You have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin`
)
return
}

Expand Down
42 changes: 35 additions & 7 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import * as colors from 'colorette'

type LOG_TYPE = 'info' | 'success' | 'error' | 'warn'

const colorize = (type: LOG_TYPE, data: any, onlyImportant = false) => {
if (onlyImportant && (type === 'info' || type === 'success')) return data

const color =
type === 'info'
? 'blue'
: type === 'error'
? 'red'
: type === 'warn'
? 'yellow'
: 'green'
return colors[color](data)
}

export const makeLabel = (
name: string | undefined,
input: string,
type: 'info' | 'success' | 'error'
type: LOG_TYPE
) => {
return [
name && `${colors.dim('[')}${name.toUpperCase()}${colors.dim(']')}`,
colors[type === 'info' ? 'blue' : type === 'error' ? 'red' : 'green'](
input.toUpperCase()
),
colorize(type, input.toUpperCase()),
]
.filter(Boolean)
.join(colors.dim(' '))
Expand Down Expand Up @@ -41,15 +55,29 @@ export const createLogger = (name?: string) => {
return this.log(label, 'error', ...args)
},

log(label: string, type: 'info' | 'success' | 'error', ...data: unknown[]) {
warn(label: string, ...args: any[]) {
return this.log(label, 'warn', ...args)
},

log(
label: string,
type: 'info' | 'success' | 'error' | 'warn',
...data: unknown[]
) {
switch (type) {
case 'error': {
return console.error(makeLabel(name, label, type), ...data)
return console.error(
makeLabel(name, label, type),
...data.map((item) => colorize(type, item, true))
)
}
default:
if (silent) return

console.log(makeLabel(name, label, type), ...data)
console.log(
makeLabel(name, label, type),
...data.map((item) => colorize(type, item, true))
)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async function runRollup(options: RollupConfig) {
logger.info('dts', 'Build start')
const bundle = await rollup(options.inputConfig)
await bundle.write(options.outputConfig)
logger.success('dts', `Build success in ${getDuration()}`)
logger.success('dts', `⚡️ Build success in ${getDuration()}`)
} catch (error) {
logger.error('dts', 'Build error')
parentPort?.postMessage('error')
Expand All @@ -200,7 +200,7 @@ async function watchRollup(options: {
if (event.code === 'START') {
logger.info('dts', 'Build start')
} else if (event.code === 'BUNDLE_END') {
logger.success('dts', `Build success in ${event.duration}ms`)
logger.success('dts', `⚡️ Build success in ${event.duration}ms`)
parentPort?.postMessage('success')
} else if (event.code === 'ERROR') {
logger.error('dts', 'Build failed')
Expand Down

0 comments on commit 213ac91

Please sign in to comment.