diff --git a/Makefile b/Makefile index f0179f7..4ddb7e4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +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 @@ -5,14 +8,14 @@ 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..." diff --git a/rollup.config.ts b/rollup.config.mjs similarity index 80% rename from rollup.config.ts rename to rollup.config.mjs index e686344..4c4ae08 100644 --- a/rollup.config.ts +++ b/rollup.config.mjs @@ -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] diff --git a/src/compress.ts b/src/compress.ts index 4b727ce..06d2076 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -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((resolve, reject) => { diff --git a/src/index.ts b/src/index.ts index 3e42322..87736d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { const baseCondit = VITE_COPY_PUBLIC_DIR in config.build ? config.build.copyPublicDir : true if (config.publicDir && baseCondit && fs.existsSync(config.publicDir)) { @@ -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 }) }, @@ -223,9 +203,16 @@ function compression( // 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 }