Skip to content

Commit e96f457

Browse files
committed
build: remove comments when minifying + inline all enums
1 parent 8fff4ca commit e96f457

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

rollup.config.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const pkg = require(resolve(`package.json`))
4646
const packageOptions = pkg.buildOptions || {}
4747
const name = packageOptions.filename || path.basename(packageDir)
4848

49+
const banner = `/**
50+
* ${pkg.name} v${masterVersion}
51+
* (c) 2018-present Yuxi (Evan) You and Vue contributors
52+
* @license MIT
53+
**/`
54+
4955
const [enumPlugin, enumDefines] = inlineEnums()
5056

5157
/** @type {Record<PackageFormat, OutputOptions>} */
@@ -136,11 +142,7 @@ function createConfig(format, output, plugins = []) {
136142
(isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
137143
!packageOptions.enableNonBrowserBranches
138144

139-
output.banner = `/**
140-
* ${pkg.name} v${masterVersion}
141-
* (c) 2018-present Yuxi (Evan) You and Vue contributors
142-
* @license MIT
143-
**/`
145+
output.banner = banner
144146

145147
output.exports = isCompatPackage ? 'auto' : 'named'
146148
if (isCJSBuild) {
@@ -372,24 +374,21 @@ function createMinifiedConfig(/** @type {PackageFormat} */ format) {
372374
{
373375
name: 'swc-minify',
374376

375-
async renderChunk(
376-
contents,
377-
_,
378-
{ format, sourcemap, sourcemapExcludeSources },
379-
) {
380-
const { code, map } = await minifySwc(contents, {
377+
async renderChunk(contents, _, { format }) {
378+
const { code } = await minifySwc(contents, {
381379
module: format === 'es',
380+
format: {
381+
comments: false,
382+
},
382383
compress: {
383384
ecma: 2016,
384385
pure_getters: true,
385386
},
386387
safari10: true,
387388
mangle: true,
388-
sourceMap: !!sourcemap,
389-
inlineSourcesContent: !sourcemapExcludeSources,
390389
})
391390

392-
return { code, map: map || null }
391+
return { code: banner + code, map: null }
393392
},
394393
},
395394
],

scripts/create-rolldown-config.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export function createConfigsForPackage({
5050
const packageOptions = pkg.buildOptions || {}
5151
const name = packageOptions.filename || path.basename(packageDir)
5252

53+
const banner = `/**!
54+
* ${pkg.name} v${masterVersion}
55+
* (c) 2018-present Yuxi (Evan) You and Vue contributors
56+
* @license MIT
57+
**/`
58+
5359
/** @type {Record<PackageFormat, import('rolldown').OutputOptions>} */
5460
const outputConfigs = {
5561
'esm-bundler': {
@@ -134,12 +140,7 @@ export function createConfigsForPackage({
134140
(isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
135141
!packageOptions.enableNonBrowserBranches
136142

137-
output.banner = `/**
138-
* ${pkg.name} v${masterVersion}
139-
* (c) 2018-present Yuxi (Evan) You and Vue contributors
140-
* @license MIT
141-
**/`
142-
143+
output.banner = banner
143144
output.exports = isCompatPackage ? 'auto' : 'named'
144145
if (isCJSBuild) {
145146
output.esModule = true
@@ -354,28 +355,21 @@ export function createConfigsForPackage({
354355
[
355356
{
356357
name: 'swc-minify',
357-
async renderChunk(
358-
contents,
359-
_,
360-
{
361-
format,
362-
sourcemap,
363-
// @ts-expect-error not supported yet
364-
sourcemapExcludeSources,
365-
},
366-
) {
367-
const { code, map } = await minifySwc(contents, {
358+
async renderChunk(contents, _, { format }) {
359+
const { code } = await minifySwc(contents, {
368360
module: format === 'es',
361+
format: {
362+
comments: false,
363+
},
369364
compress: {
370365
ecma: 2016,
371366
pure_getters: true,
372367
},
373368
safari10: true,
374369
mangle: true,
375-
sourceMap: !!sourcemap,
376-
inlineSourcesContent: !sourcemapExcludeSources,
377370
})
378-
return { code, map: map || null }
371+
// swc removes banner
372+
return { code: banner + code, map: null }
379373
},
380374
},
381375
],

scripts/inline-enums.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function scanEnums() {
4949
const defines = Object.create(null)
5050

5151
// 1. grep for files with exported enum
52-
const { stdout } = spawnSync('git', ['grep', `export enum`])
52+
const { stdout } = spawnSync('git', ['grep', `enum `])
5353
const files = [
5454
...new Set(
5555
stdout
@@ -74,12 +74,19 @@ export function scanEnums() {
7474
/** @type {Set<string>} */
7575
const enumIds = new Set()
7676
for (const node of res.program.body) {
77+
let decl
78+
if (node.type === 'TSEnumDeclaration') {
79+
decl = node
80+
}
7781
if (
7882
node.type === 'ExportNamedDeclaration' &&
7983
node.declaration &&
8084
node.declaration.type === 'TSEnumDeclaration'
8185
) {
82-
const decl = node.declaration
86+
decl = node.declaration
87+
}
88+
89+
if (decl) {
8390
const id = decl.id.name
8491
if (enumIds.has(id)) {
8592
throw new Error(

0 commit comments

Comments
 (0)