From b1ccb7be57a7e3d2b457c24ec5e6ad1df317148a Mon Sep 17 00:00:00 2001 From: webfansplz <308241863@qq.com> Date: Thu, 31 Aug 2023 19:28:10 +0800 Subject: [PATCH] fix: cjs bundle --- playground/vite.config.ts | 2 +- playground/{vue.config.mjs => vue.config.js} | 6 ++---- pnpm-lock.yaml | 19 +++---------------- src/core/print.ts | 2 +- src/core/unplugin.ts | 14 ++++++++++++++ src/index.ts | 16 ++-------------- src/nuxt.ts | 8 ++------ src/vite.ts | 7 ++++--- src/webpack.ts | 5 ++--- tsup.config.ts | 5 ++++- 10 files changed, 35 insertions(+), 49 deletions(-) rename playground/{vue.config.mjs => vue.config.js} (87%) create mode 100644 src/core/unplugin.ts diff --git a/playground/vite.config.ts b/playground/vite.config.ts index bf19e03..0b884ce 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -1,6 +1,6 @@ import { defineConfig } from 'vite' import Inspect from 'vite-plugin-inspect' -import Unplugin from '../src/vite' +import Unplugin from '../dist/vite' export default defineConfig({ plugins: [ diff --git a/playground/vue.config.mjs b/playground/vue.config.js similarity index 87% rename from playground/vue.config.mjs rename to playground/vue.config.js index ae55130..e758235 100644 --- a/playground/vue.config.mjs +++ b/playground/vue.config.js @@ -1,9 +1,7 @@ -import Unplugin from '../dist/webpack.js' - -export default { +module.exports = { configureWebpack: { plugins: [ - Unplugin({ + require('../dist/webpack.cjs')({ info: [ async () => { await new Promise(resolve => setTimeout(resolve, 1000)) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 46a934e..5c1ec8d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4240,20 +4240,12 @@ packages: resolution: {integrity: sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==} dev: true - /anymatch@3.1.2: - resolution: {integrity: sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==} - engines: {node: '>= 8'} - dependencies: - normalize-path: 3.0.0 - picomatch: 2.3.1 - /anymatch@3.1.3: resolution: {integrity: sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==} engines: {node: '>= 8'} dependencies: normalize-path: 3.0.0 picomatch: 2.3.1 - dev: true /aproba@2.0.0: resolution: {integrity: sha512-lYe4Gx7QT+MKGbDsA+Z+he/Wtef0BiwDOlK/XkBrdfsh9J/jPPXbX0tE9x9cl27Tmu5gg3QUbUrQYa/y+KOHPQ==} @@ -4807,7 +4799,7 @@ packages: resolution: {integrity: sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==} engines: {node: '>= 8.10.0'} dependencies: - anymatch: 3.1.2 + anymatch: 3.1.3 braces: 3.0.2 glob-parent: 5.1.2 is-binary-path: 2.1.0 @@ -7779,11 +7771,6 @@ packages: type-check: 0.4.0 dev: true - /lilconfig@2.0.5: - resolution: {integrity: sha512-xaYmXZtTHPAw5m+xLN8ab9C+3a8YmV3asNSPOATITbtwrfbwaLJj8h66H1WMIpALCkqsIzK3h7oQ+PdX+LQ9Eg==} - engines: {node: '>=10'} - dev: true - /lilconfig@2.1.0: resolution: {integrity: sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==} engines: {node: '>=10'} @@ -9261,8 +9248,8 @@ packages: ts-node: optional: true dependencies: - lilconfig: 2.0.5 - yaml: 2.1.1 + lilconfig: 2.1.0 + yaml: 2.3.2 dev: true /postcss-loader@6.2.1(postcss@8.4.29)(webpack@5.88.2): diff --git a/src/core/print.ts b/src/core/print.ts index 4fcd608..bf45d5b 100644 --- a/src/core/print.ts +++ b/src/core/print.ts @@ -1,4 +1,5 @@ import * as korlorist from 'kolorist' +import boxen from 'boxen' import type { MessageValue, Options } from '../types' function log(msg: string | void) { @@ -8,7 +9,6 @@ function log(msg: string | void) { } async function printWithBoxen(res: MessageValue) { - const { default: boxen } = await import('boxen') res.borderStyle = res.borderStyle || 'none' // @ts-expect-error missing type log(boxen(res.text, res)) diff --git a/src/core/unplugin.ts b/src/core/unplugin.ts new file mode 100644 index 0000000..83deec2 --- /dev/null +++ b/src/core/unplugin.ts @@ -0,0 +1,14 @@ +import type { UnpluginFactory } from 'unplugin' +import { createUnplugin } from 'unplugin' +import type { Options } from '../types' +import { vite, webpack } from '.' + +export const unpluginFactory: UnpluginFactory = options => ({ + name: 'unplugin-printer', + vite: vite(options), + webpack: webpack(options), +}) + +export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory) + +export default unplugin diff --git a/src/index.ts b/src/index.ts index 5dc5fd7..7ff0636 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,2 @@ -import type { UnpluginFactory } from 'unplugin' -import { createUnplugin } from 'unplugin' -import type { Options } from './types' -import { vite, webpack } from './core' - -export const unpluginFactory: UnpluginFactory = options => ({ - name: 'unplugin-printer', - vite: vite(options), - webpack: webpack(options), -}) - -export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory) - -export default unplugin +export * from './types' +export { default } from './core/unplugin' diff --git a/src/nuxt.ts b/src/nuxt.ts index c7aacae..23f9397 100644 --- a/src/nuxt.ts +++ b/src/nuxt.ts @@ -3,16 +3,12 @@ import type { Options } from './types' import '@nuxt/schema' import { print } from './core/print' -export interface ModuleOptions extends Options { - -} - -export default defineNuxtModule({ +export default defineNuxtModule({ meta: { name: 'nuxt-unplugin-printer', configKey: 'unpluginPrinter', }, - setup(options, nuxt) { + setup(options: Options, nuxt) { nuxt.hook('vite:serverCreated', (_, { isServer }) => { if (isServer) print(options.info) diff --git a/src/vite.ts b/src/vite.ts index 151a5ce..dc0431b 100644 --- a/src/vite.ts +++ b/src/vite.ts @@ -1,4 +1,5 @@ -import { createVitePlugin } from 'unplugin' -import { unpluginFactory } from '.' +import type { Plugin } from 'vite' +import type { Options } from './types' +import unplugin from '.' -export default createVitePlugin(unpluginFactory) +export default unplugin.vite as (options?: Options | undefined) => Plugin diff --git a/src/webpack.ts b/src/webpack.ts index ce9ebdc..83091ee 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -1,4 +1,3 @@ -import { createWebpackPlugin } from 'unplugin' -import { unpluginFactory } from '.' +import unplugin from '.' -export default createWebpackPlugin(unpluginFactory) +export default unplugin.webpack diff --git a/tsup.config.ts b/tsup.config.ts index f7752ee..8dda4e9 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,12 +1,15 @@ import type { Options } from 'tsup' export default { - entryPoints: [ + entry: [ 'src/*.ts', ], clean: true, format: ['cjs', 'esm'], dts: true, + splitting: true, onSuccess: 'npm run build:fix', + shims: false, + noExternal: ['boxen'], external: ['korlorist'], }