Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean code #65

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
NODE_OPTIONS= export NODE_OPTIONS=--no-warnings
ROLLUP_CMD = ${NODE_OPTIONS} && pnpm exec rollup --config rollup.config.mjs

install:
@echo "Setup pnpm package manager..."
@corepack enable
pnpm install

build:
@echo "Building..."
@pnpm exec rollup --config rollup.config.ts --configPlugin swc3
$(ROLLUP_CMD)


build-pub: install build

dev:
@echo "Starting development server..."
@pnpm exec rollup --config rollup.config.ts --configPlugin swc3 --watch
$(ROLLUP_CMD) --watch

test:
@echo "Running tests..."
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.ts → rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { builtinModules, createRequire } from 'module'
import { builtinModules } from 'module'
import { defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'
import { minify, swc } from 'rollup-plugin-swc3'
import packageJson from './package.json' with { type: 'json' }

const _require = createRequire(import.meta.url)
const { dependencies } = _require('./package.json')
const { dependencies } = packageJson

const external = [...Object.keys(dependencies), ...builtinModules]

Expand Down
2 changes: 1 addition & 1 deletion src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createTarBall() {
const promises = options.dests.map(dest => {
const expected = slash(path.resolve(options.root, dest + '.tar' + (options.gz ? '.gz' : '')))
const parent = slash(path.dirname(expected))
if (options.root !== parent) {
if (slash(options.root) !== parent) {
fs.mkdirSync(parent, { recursive: true })
}
return new Promise<void>((resolve, reject) => {
Expand Down
31 changes: 9 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,6 @@ function handleOutputOption(conf: ResolvedConfig) {
return outputs
}

async function hijackGenerateBundle(plugin: Plugin, afterHook: GenerateBundle) {
const hook = plugin.generateBundle
if (typeof hook === 'object' && hook.handler) {
const fn = hook.handler
hook.handler = async function handler(this, ...args: any) {
await fn.apply(this, args)
await afterHook.apply(this, args)
}
}
if (typeof hook === 'function') {
plugin.generateBundle = async function handler(this, ...args: any) {
await hook.apply(this, args)
await afterHook.apply(this, args)
}
}
}

async function handleStaticFiles(config: ResolvedConfig, callback: (file: string, assets: string) => Promise<void>) {
const baseCondit = VITE_COPY_PUBLIC_DIR in config.build ? config.build.copyPublicDir : true
if (config.publicDir && baseCondit && fs.existsSync(config.publicDir)) {
Expand Down Expand Up @@ -111,9 +94,6 @@ function tarball(opts: ViteTarballPluginOptions = {}): Plugin {
statics.push(file)
})
}
const plugin = config.plugins.find(p => p.name === VITE_INTERNAL_ANALYSIS_PLUGIN)
if (!plugin) throw new Error("[vite-plugin-tarball] can't be work in versions lower than vite at 2.0.0")

// create dest dir
tarball.setup({ dests, root, gz })
},
Expand Down Expand Up @@ -223,9 +203,16 @@ function compression<T extends UserCompressionOptions, A extends Algorithm>(
// issue #63
// more and more plugin use are starting specify plugin order. So we should do a check for vite's version.
const [major, minor] = rollupVersion.split('.')
// rollup support object hook at 2.78.0
// rollup support object hook at 2.78.0 (vite 3.1.0)
// https://github.com/rollup/rollup/pull/4600
if (+major <= 2 && +minor < 78) {
hijackGenerateBundle(viteAnalyzerPlugin, generateBundle)
const hook = viteAnalyzerPlugin.generateBundle
if (typeof hook === 'function') {
plugin.generateBundle = async function handler(this, ...args: any) {
await hook.apply(this, args)
await generateBundle.apply(this, args)
}
}
return
}

Expand Down
Loading