Skip to content

Commit a3b6b1c

Browse files
committed
feat: minify
1 parent a403eed commit a3b6b1c

File tree

8 files changed

+208
-35
lines changed

8 files changed

+208
-35
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
"prepublishOnly": "pnpm run build"
6666
},
6767
"dependencies": {
68+
"@ampproject/remapping": "^2.3.0",
69+
"oxc-minify": "^0.48.2",
6870
"oxc-resolver": "^4.0.0",
6971
"oxc-transform": "^0.48.2",
7072
"unplugin": "^2.1.2",

pnpm-lock.yaml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/options.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { MinifyOptions } from 'oxc-minify'
12
import type { NapiResolveOptions } from 'oxc-resolver'
23
import type { TransformOptions } from 'oxc-transform'
34
import type { FilterPattern } from 'unplugin-utils'
@@ -6,9 +7,11 @@ export interface Options {
67
include?: FilterPattern
78
exclude?: FilterPattern
89
enforce?: 'pre' | 'post' | undefined
9-
transform?: TransformOptions
10-
resolve?: NapiResolveOptions
10+
transform?: Omit<TransformOptions, 'sourcemap'> | false
11+
resolve?: NapiResolveOptions | false
1112
resolveNodeModules?: boolean
13+
minify?: Omit<MinifyOptions, 'sourcemap'> | false
14+
sourcemap?: boolean
1215
}
1316

1417
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
@@ -26,5 +29,7 @@ export function resolveOptions(options: Options): OptionsResolved {
2629
transform: options.transform || {},
2730
resolve: options.resolve || {},
2831
resolveNodeModules: options.resolveNodeModules || false,
32+
minify: options.minify || false,
33+
sourcemap: options.sourcemap || false,
2934
}
3035
}

src/index.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import path from 'node:path'
2+
import remapping from '@ampproject/remapping'
3+
import { minify } from 'oxc-minify'
24
import { ResolverFactory } from 'oxc-resolver'
35
import { transform } from 'oxc-transform'
46
import { createUnplugin, type UnpluginInstance } from 'unplugin'
@@ -15,33 +17,57 @@ export const Oxc: UnpluginInstance<Options | undefined, false> = createUnplugin(
1517
name,
1618
enforce: options.enforce,
1719

18-
async resolveId(id, importer) {
19-
if (!importer) return
20-
if (!options.resolveNodeModules && id[0] !== '.' && id[0] !== '/')
21-
return
20+
resolveId:
21+
options.resolve !== false
22+
? async (id, importer) => {
23+
if (!importer) return
24+
if (!options.resolveNodeModules && id[0] !== '.' && id[0] !== '/')
25+
return
2226

23-
const resolver = new ResolverFactory({
24-
extensions: ['.ts', '.mts', '.cts', '.tsx'],
25-
...options.resolve,
26-
})
27-
const directory = path.dirname(importer)
28-
const resolved = await resolver.async(directory, id)
27+
const resolver = new ResolverFactory({
28+
extensions: ['.ts', '.mts', '.cts', '.tsx'],
29+
...options.resolve,
30+
})
31+
const directory = path.dirname(importer)
32+
const resolved = await resolver.async(directory, id)
2933

30-
if (resolved.path) return resolved.path
31-
},
34+
if (resolved.path) return resolved.path
35+
}
36+
: undefined,
3237

3338
transformInclude(id) {
3439
return filter(id)
3540
},
3641

37-
transform(code, id) {
38-
const result = transform(id, code, options.transform)
42+
transform:
43+
options.transform !== false || options.minify !== false
44+
? (code, id) => {
45+
let map
46+
if (options.transform !== false) {
47+
const result = transform(id, code, {
48+
...options.transform,
49+
sourcemap: options.sourcemap,
50+
})
51+
code = result.code
52+
map = result.map
53+
}
3954

40-
return {
41-
code: result.code,
42-
map: result.map,
43-
}
44-
},
55+
if (options.minify !== false) {
56+
const result = minify(id, code, {
57+
...options.minify,
58+
sourcemap: options.sourcemap,
59+
})
60+
code = result.code
61+
if (map && result.map) {
62+
map = remapping([result.map, map], () => null, {})
63+
} else {
64+
map = result.map
65+
}
66+
}
67+
68+
return { code, map }
69+
}
70+
: undefined,
4571
}
4672
},
4773
)

tests/__snapshots__/rollup.test.ts.snap

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`rollup > basic.ts 1`] = `
4-
"// basic.js
3+
exports[`rollup > entry-basic.ts 1`] = `
4+
"// entry-basic.js
55
import { jsx } from 'react/jsx-runtime';
66
77
const node = jsx("div", {});
@@ -30,10 +30,13 @@ export { Color, foo, node, num, optionalChain };
3030
"
3131
`;
3232

33-
exports[`rollup > mod.ts 1`] = `
34-
"// mod.js
35-
function foo() {}
33+
exports[`rollup > entry-minify.ts 1`] = `
34+
"// entry-minify.js
35+
function foo(){return 100}try{foo();}catch(e){}
3636
3737
export { foo };
38-
"
38+
//# sourceMappingURL=entry-minify.js.map
39+
40+
// entry-minify.js.map
41+
{"version":3,"file":"entry-minify.js","sources":["tests/fixtures/entry-minify.ts"],"sourcesContent":["export function foo(): number {\\n return 100\\n}\\n\\ntry {\\n foo()\\n} catch (err) {}\\n"],"names":["err"],"mappings":"AAAO,SAAS,GAAA,EAAc,CAC5B,OACD,GAAA,CAED,GAAI,CACF,GAAK,GACN,CAAQA,MAAAA,CAAAA,CAAK;;;;"}"
3942
`;

tests/fixtures/entry-minify.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function foo(): number {
2+
return 100
3+
}
4+
5+
try {
6+
foo()
7+
} catch (err) {}

tests/rollup.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import UnpluginOxc from '../src/rollup'
66
describe('rollup', async () => {
77
const { dirname } = import.meta
88
await testFixtures(
9-
'*.ts',
9+
'entry-*.ts',
1010
async (args, id) => {
11-
const { snapshot } = await rollupBuild(id, [
12-
UnpluginOxc({
13-
transform: {
14-
target: 'es2015',
15-
},
16-
}),
17-
])
11+
const { snapshot } = await rollupBuild(
12+
id,
13+
[
14+
UnpluginOxc({
15+
sourcemap: id.includes('minify'),
16+
transform: { target: 'es2015' },
17+
minify: id.includes('minify')
18+
? { compress: { target: 'es2015' } }
19+
: false,
20+
}),
21+
],
22+
undefined,
23+
{ sourcemap: id.includes('minify') },
24+
)
1825
return snapshot
1926
},
2027
{ cwd: path.resolve(dirname, 'fixtures'), promise: true },

0 commit comments

Comments
 (0)