Skip to content

Commit

Permalink
fix: use format to minify (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 18, 2021
1 parent b7315b0 commit 973d96b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { existsSync, statSync } from 'fs'
import { extname, resolve, dirname, join } from 'path'
import { Plugin, PluginContext } from 'rollup'
import { Plugin, PluginContext, InternalModuleFormat } from 'rollup'
import {
transform,
Loader,
formatMessages,
Message,
CommonOptions,
Format,
} from 'esbuild'
import { createFilter, FilterPattern } from '@rollup/pluginutils'
import { getOptions } from './options'
import { bundle } from './bundle'

const getEsbuildFormat = (rollupFormat: InternalModuleFormat): Format | undefined => {
if (rollupFormat === 'es') {
return 'esm';
}
if (rollupFormat === 'cjs' || rollupFormat === 'iife') {
return rollupFormat;
}
}

const defaultLoaders: { [ext: string]: Loader } = {
'.js': 'js',
'.jsx': 'jsx',
Expand Down Expand Up @@ -190,14 +200,16 @@ export default (options: Options = {}): Plugin => {
)
},

async renderChunk(code) {
async renderChunk(code, _, rollupOptions) {
if (
options.minify ||
options.minifyWhitespace ||
options.minifyIdentifiers ||
options.minifySyntax
) {
const format = getEsbuildFormat(rollupOptions.format);
const result = await transform(code, {
format,
loader: 'js',
minify: options.minify,
minifyWhitespace: options.minifyWhitespace,
Expand Down
68 changes: 65 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import fs from 'fs'
import { rollup, Plugin as RollupPlugin } from 'rollup'
import { rollup, Plugin as RollupPlugin, ModuleFormat } from 'rollup'
import esbuild, { Options } from '../src'

const realFs = (folderName: string, files: Record<string, string>) => {
Expand All @@ -22,11 +22,13 @@ const build = async (
sourcemap = false,
rollupPlugins = [],
dir = '.',
format = 'esm',
}: {
input?: string | string[]
sourcemap?: boolean
rollupPlugins?: RollupPlugin[]
dir?: string
format?: ModuleFormat
} = {}
) => {
const build = await rollup({
Expand All @@ -35,7 +37,7 @@ const build = async (
),
plugins: [esbuild(options), ...rollupPlugins],
})
const { output } = await build.generate({ format: 'esm', sourcemap })
const { output } = await build.generate({ format, sourcemap })
return output
}

Expand Down Expand Up @@ -86,7 +88,7 @@ test('minify', async () => {
})
const output = await build({ minify: true }, { dir })
expect(output[0].code).toMatchInlineSnapshot(`
"class Foo{render(){return React.createElement(\\"div\\",{className:\\"hehe\\"},\\"hello there!!!\\")}}console.log(Foo);
"class e{render(){return React.createElement(\\"div\\",{className:\\"hehe\\"},\\"hello there!!!\\")}}console.log(e);
"
`)
})
Expand Down Expand Up @@ -130,6 +132,66 @@ test('minify syntax only', async () => {
`)
})

test('minify cjs', async () => {
const dir = realFs(getTestName(), {
'./fixture/index.js': `
const minifyMe = true
console.log(minifyMe);
`,
})
const output = await build(
{ minify: true },
{
dir,
format: 'commonjs',
}
)
expect(output[0].code).toMatchInlineSnapshot(`
"\\"use strict\\";const e=!0;console.log(e);
"
`)
})

test('minify iife', async () => {
const dir = realFs(getTestName(), {
'./fixture/index.js': `
const minifyMe = true
console.log(minifyMe);
`,
})
const output = await build(
{ minify: true },
{
dir,
format: 'iife',
}
)
expect(output[0].code).toMatchInlineSnapshot(`
"(()=>{(function(){\\"use strict\\";console.log(!0)})();})();
"
`)
})

test('minify umd', async () => {
const dir = realFs(getTestName(), {
'./fixture/index.js': `
const minifyMe = true
console.log(minifyMe);
`,
})
const output = await build(
{ minify: true },
{
dir,
format: 'umd',
}
)
expect(output[0].code).toMatchInlineSnapshot(`
"(function(n){typeof define==\\"function\\"&&define.amd?define(n):n()})(function(){\\"use strict\\";console.log(!0)});
"
`)
})

test('legal comments none', async () => {
const dir = realFs(getTestName(), {
'./fixture/index.js': `/** @preserve comment */
Expand Down

0 comments on commit 973d96b

Please sign in to comment.