Skip to content

Commit b1f4ce7

Browse files
committed
Reduce dist size
1 parent 03d261c commit b1f4ce7

File tree

3 files changed

+74
-25
lines changed

3 files changed

+74
-25
lines changed

.config/rollup.base.config.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ const require = createRequire(import.meta.url)
3232

3333
const {
3434
BABEL_RUNTIME,
35+
CONSTANTS,
3536
LATEST,
3637
ROLLUP_ENTRY_SUFFIX,
3738
ROLLUP_EXTERNAL_SUFFIX,
3839
SLASH_NODE_MODULES_SLASH,
40+
VENDOR,
3941
babelConfigPath,
4042
rootPackageJsonPath,
4143
rootPath,
@@ -45,7 +47,7 @@ const {
4547

4648
const SOCKET_INTEROP = '_socketInterop'
4749

48-
const constantsSrcPath = path.join(rootSrcPath, 'constants.ts')
50+
const constantsSrcPath = path.join(rootSrcPath, `${CONSTANTS}.ts`)
4951

5052
const babelConfig = require(babelConfigPath)
5153
const tsPlugin = require('rollup-plugin-ts')
@@ -320,10 +322,10 @@ function ${SOCKET_INTEROP}(e) {
320322
manualChunks: id_ => {
321323
const id = normalizeId(id_)
322324
if (id === constantsSrcPath) {
323-
return 'constants'
325+
return CONSTANTS
324326
}
325327
if (id.includes(SLASH_NODE_MODULES_SLASH)) {
326-
return 'vendor'
328+
return VENDOR
327329
}
328330
return null
329331
}

.config/rollup.dist.config.mjs

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
1+
import {
2+
copyFileSync,
3+
existsSync,
4+
mkdirSync,
5+
rmSync,
6+
writeFileSync
7+
} from 'node:fs'
28
import path from 'node:path'
39

410
import { globSync as tinyGlobSync } from 'tinyglobby'
@@ -23,29 +29,48 @@ import {
2329

2430
const {
2531
BABEL_RUNTIME,
32+
CONSTANTS,
33+
MODULE_SYNC,
34+
REQUIRE,
2635
ROLLUP_EXTERNAL_SUFFIX,
36+
VENDOR,
2737
depStatsPath,
2838
rootDistPath,
2939
rootPath,
3040
rootSrcPath
3141
} = constants
3242

33-
const CONSTANTS_JS = 'constants.js'
34-
const CONSTANTS_STUB_CODE = `'use strict'\n\nmodule.exports = require('../${CONSTANTS_JS}')\n`
43+
const CONSTANTS_JS = `${CONSTANTS}.js`
44+
const CONSTANTS_STUB_CODE = createStubCode(`../${CONSTANTS_JS}`)
45+
const VENDOR_JS = `${VENDOR}.js`
3546

3647
const distConstantsPath = path.join(rootDistPath, CONSTANTS_JS)
37-
const distModuleSyncPath = path.join(rootDistPath, 'module-sync')
38-
const distRequirePath = path.join(rootDistPath, 'require')
48+
const distModuleSyncPath = path.join(rootDistPath, MODULE_SYNC)
49+
const distRequirePath = path.join(rootDistPath, REQUIRE)
3950

4051
const editablePkgJson = readPackageJsonSync(rootPath, { editable: true })
4152

4253
const processEnvTapRegExp =
4354
/\bprocess\.env(?:\.TAP|\[['"]TAP['"]\])(\s*\?[^:]+:\s*)?/g
4455

45-
function removeDtsFilesSync(distPath) {
46-
for (const filepath of tinyGlobSync(['**/*.d.ts'], {
56+
function createStubCode(relFilepath) {
57+
return `'use strict'\n\nmodule.exports = require('${relFilepath}')\n`
58+
}
59+
60+
function moveDtsFilesSync(namePattern, srcPath, destPath) {
61+
for (const filepath of tinyGlobSync([`**/${namePattern}.d.ts{.map,}`], {
4762
absolute: true,
48-
cwd: distPath
63+
cwd: srcPath
64+
})) {
65+
copyFileSync(filepath, path.join(destPath, path.basename(filepath)))
66+
rmSync(filepath)
67+
}
68+
}
69+
70+
function removeDtsFilesSync(namePattern, srcPath) {
71+
for (const filepath of tinyGlobSync([`**/${namePattern}.d.ts{.map,}`], {
72+
absolute: true,
73+
cwd: srcPath
4974
})) {
5075
rmSync(filepath)
5176
}
@@ -129,20 +154,15 @@ export default () => {
129154
return true
130155
},
131156
plugins: [
132-
// When process.env['TAP'] is found either remove it, if part of a ternary
133-
// operation, or replace it with `false`.
134-
socketModifyPlugin({
135-
find: processEnvTapRegExp,
136-
replace: (match, ternary) => (ternary ? '' : 'false')
137-
}),
138157
{
139158
generateBundle(_options, bundle) {
140-
const constantsBundle = bundle[CONSTANTS_JS]
141-
if (constantsBundle) {
142-
mkdirSync(rootDistPath, { recursive: true })
143-
writeFileSync(distConstantsPath, constantsBundle.code, 'utf8')
144-
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
159+
const data = bundle[CONSTANTS_JS]
160+
if (data?.type === 'chunk') {
161+
data.code = CONSTANTS_STUB_CODE
145162
}
163+
},
164+
writeBundle() {
165+
removeDtsFilesSync(CONSTANTS, distModuleSyncPath)
146166
}
147167
}
148168
]
@@ -166,14 +186,33 @@ export default () => {
166186
}
167187
],
168188
plugins: [
189+
// When process.env['TAP'] is found either remove it, if part of a ternary
190+
// operation, or replace it with `false`.
191+
socketModifyPlugin({
192+
find: processEnvTapRegExp,
193+
replace: (_match, ternary) => (ternary ? '' : 'false')
194+
}),
169195
{
170196
generateBundle(_options, bundle) {
171-
if (bundle[CONSTANTS_JS]) {
172-
bundle[CONSTANTS_JS].code = CONSTANTS_STUB_CODE
197+
for (const basename of Object.keys(bundle)) {
198+
const data = bundle[basename]
199+
if (data.type === 'chunk') {
200+
if (basename === CONSTANTS_JS) {
201+
mkdirSync(rootDistPath, { recursive: true })
202+
writeFileSync(distConstantsPath, data.code, 'utf8')
203+
data.code = CONSTANTS_STUB_CODE
204+
} else if (
205+
basename !== VENDOR_JS &&
206+
!data.code.includes(`'./${VENDOR_JS}'`)
207+
) {
208+
data.code = createStubCode(`../${MODULE_SYNC}/${basename}`)
209+
}
210+
}
173211
}
174212
},
175213
writeBundle() {
176-
removeDtsFilesSync(distRequirePath)
214+
moveDtsFilesSync(CONSTANTS, distRequirePath, rootDistPath)
215+
removeDtsFilesSync('*', distRequirePath)
177216
updateDepStatsSync(requireConfig.meta.depStats)
178217
}
179218
}

scripts/constants.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const {
99
[kInternalsSymbol]: { createConstantsObject }
1010
} = registryConstants
1111

12+
const CONSTANTS = 'constants'
13+
const MODULE_SYNC = 'module-sync'
14+
const REQUIRE = 'require'
1215
const ROLLUP_ENTRY_SUFFIX = '?commonjs-entry'
1316
const ROLLUP_EXTERNAL_SUFFIX = '?commonjs-external'
1417
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
18+
const VENDOR = 'vendor'
1519

1620
const lazyBabelConfigPath = () =>
1721
// Lazily access constants.rootConfigPath.
@@ -45,9 +49,13 @@ const lazyTsconfigPath = () =>
4549

4650
const constants = createConstantsObject(
4751
{
52+
CONSTANTS,
53+
MODULE_SYNC,
54+
REQUIRE,
4855
ROLLUP_ENTRY_SUFFIX,
4956
ROLLUP_EXTERNAL_SUFFIX,
5057
SLASH_NODE_MODULES_SLASH,
58+
VENDOR,
5159
babelConfigPath: undefined,
5260
depStatsPath: undefined,
5361
rootConfigPath: undefined,

0 commit comments

Comments
 (0)