Skip to content

Commit 0185c19

Browse files
authored
feat: Don't mangle in no-external builds (#2008)
* Don't mangle in no-external builds * Don't mangle at all for no-external * Fix tests * Add some extra caution to rollup * Maybe we can do non-property mangling for noexternal builds
1 parent 2e14c5b commit 0185c19

File tree

2 files changed

+134
-128
lines changed

2 files changed

+134
-128
lines changed

rollup.config.js

Lines changed: 126 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const WRITE_MANGLED_PROPERTIES = process.env.WRITE_MANGLED_PROPERTIES
1818
const nameCachePath = './terser-mangled-names.json'
1919
let nameCache = {}
2020

21-
const plugins = (es5) => [
21+
const plugins = (es5, noExternal) => [
2222
json(),
2323
resolve({ browser: true }),
2424
typescript({ sourceMap: true, outDir: './dist' }),
@@ -78,139 +78,145 @@ const plugins = (es5) => [
7878
format: {
7979
comments: false,
8080
},
81-
mangle: es5
82-
? true // Don't mangle properties in the es5 build, as it relies on helpers which don't work well with mangling.
83-
: {
84-
// Note:
85-
// PROPERTY MANGLING CAN BREAK YOUR CODE
86-
// But we use it anyway because it's incredible for bundle size, you just need to develop with it in mind.
87-
// Any properties that start with _ will be mangled, which can be a problem if anything with that pattern is
88-
// part of the public interface, or if any API responses we use matches that regex.
89-
// Fix specific instances of this by adding the property to the reserved list.
90-
properties: {
91-
regex: /^_(?!_)/, // only mangle properties that start with a single _
92-
reserved: [
93-
// list any exceptions that shouldn't be mangled, and please add an explanation:
81+
mangle:
82+
// Don't mangle properties in no-external builds, as it is used in Browser extensions which have to go through a review process with e.g. Google, and they can be weird about obfuscated code.
83+
// Don't mangle properties in the es5 build, as it relies on helpers which don't work well with mangling.
84+
noExternal || es5
85+
? true
86+
: {
87+
// Note:
88+
// PROPERTY MANGLING CAN BREAK YOUR CODE
89+
// But we use it anyway because it's incredible for bundle size, you just need to develop with it in mind.
90+
// Any properties that start with _ will be mangled, which can be a problem if anything with that pattern is
91+
// part of the public interface, or if any API responses we use matches that regex.
92+
// Fix specific instances of this by adding the property to the reserved list.
93+
properties: {
94+
regex: /^_(?!_)/, // only mangle properties that start with a single _
95+
reserved: [
96+
// list any exceptions that shouldn't be mangled, and please add an explanation:
9497

95-
// referenced in snippet, MUST be preserved
96-
'_i',
97-
'__SV',
98+
// referenced in snippet, MUST be preserved
99+
'_i',
100+
'__SV',
98101

99-
// used in decide request, MUST be preserved
100-
'_',
102+
// used in decide request, MUST be preserved
103+
'_',
101104

102-
// used in config
103-
'_url',
104-
'_batchKey',
105-
'_noTruncate',
106-
'_onCapture',
105+
// used in config
106+
'_url',
107+
'_batchKey',
108+
'_noTruncate',
109+
'_onCapture',
107110

108-
// used in surveys, however, this shouldn't be needed
109-
// TODO: figure out how to remove them
110-
'_posthog',
111-
'_instance',
112-
'_surveyEventReceiver',
111+
// used in surveys, however, this shouldn't be needed
112+
// TODO: figure out how to remove them
113+
'_posthog',
114+
'_instance',
115+
'_surveyEventReceiver',
113116

114-
// part of setup/teardown code, preserve these out of caution
115-
'_init',
116-
'_dom_loaded',
117-
'_execute_array',
118-
'_handle_unload',
117+
// part of setup/teardown code, preserve these out of caution
118+
'_init',
119+
'_dom_loaded',
120+
'_execute_array',
121+
'_handle_unload',
119122

120-
// playwright uses these
121-
'_forceAllowLocalhostNetworkCapture',
122-
'_is_bot',
123-
'__ph_loaded',
124-
'_sessionActivityTimestamp',
125-
'_sessionStartTimestamp',
126-
'_sessionTimeoutMs',
123+
// playwright uses these
124+
'_forceAllowLocalhostNetworkCapture',
125+
'_is_bot',
126+
'__ph_loaded',
127+
'_sessionActivityTimestamp',
128+
'_sessionStartTimestamp',
129+
'_sessionTimeoutMs',
127130

128-
// set on global window object (the ones using __ are not mangled anyway BUT be abundantly cautious)
129-
'_POSTHOG_REMOTE_CONFIG',
130-
'__POSTHOG_INSTRUMENTED__',
131-
'__PosthogExtensions__',
132-
'__posthog_wrapped__',
133-
'__Posthog__',
134-
'_patchFetch',
135-
'_patchXHR',
131+
// set on global window object (the ones using __ are not mangled anyway BUT be abundantly cautious)
132+
'_POSTHOG_REMOTE_CONFIG',
133+
'__POSTHOG_INSTRUMENTED__',
134+
'__PosthogExtensions__',
135+
'__posthog_wrapped__',
136+
'__Posthog__',
137+
'_patchFetch',
138+
'_patchXHR',
136139

137-
// part of the public API (none start with _ so are not mangled anyway BUT be abundantly cautious)
138-
'capture',
139-
'identify',
140-
'alias',
141-
'set',
142-
'set_once',
143-
'set_config',
144-
'register',
145-
'register_once',
146-
'unregister',
147-
'opt_out_capturing',
148-
'has_opted_out_capturing',
149-
'opt_in_capturing',
150-
'reset',
151-
'isFeatureEnabled',
152-
'onFeatureFlags',
153-
'getSurveys',
154-
'getActiveMatchingSurveys',
155-
'captureException',
156-
'posthog',
157-
'version',
158-
'surveys',
159-
'calculateEventProperties',
140+
// set as part of lazy-loading (doesn't start with _ BUT be abundantly cautious)
141+
'loadExternalDependency',
160142

161-
// possibly used by naughty users - we should decide if we want make these part of the public API, but be cautious for now
162-
'_isIdentified',
163-
'_is_bot',
164-
'_calculate_event_properties', // deprecated in favour of calculateEventProperties
143+
// part of the public API (none start with _ so are not mangled anyway BUT be abundantly cautious)
144+
'capture',
145+
'identify',
146+
'alias',
147+
'set',
148+
'set_once',
149+
'set_config',
150+
'register',
151+
'register_once',
152+
'unregister',
153+
'opt_out_capturing',
154+
'has_opted_out_capturing',
155+
'opt_in_capturing',
156+
'reset',
157+
'isFeatureEnabled',
158+
'onFeatureFlags',
159+
'getSurveys',
160+
'getActiveMatchingSurveys',
161+
'captureException',
162+
'posthog',
163+
'version',
164+
'surveys',
165+
'calculateEventProperties',
165166

166-
// URL parameters
167-
'__posthog_debug',
167+
// possibly used by naughty users - we should decide if we want make these part of the public API, but be cautious for now
168+
'_isIdentified',
169+
'_is_bot',
170+
'_calculate_event_properties', // deprecated in favour of calculateEventProperties
168171

169-
// attribution params, not used in a way that would be mangled but be cautious
170-
'_kx',
172+
// URL parameters
173+
'__posthog_debug',
171174

172-
// used in rrweb source
173-
'_rrweb',
174-
'_root',
175-
'_css',
176-
'_opts',
177-
'_cssText',
178-
'__context',
179-
'_mappings',
180-
'_processor',
181-
'_args',
182-
'__ln',
183-
'_unchangedStyles',
184-
'__rrweb_original__',
185-
'_Departed',
186-
'_onload',
187-
'_onclick',
188-
'_oncontextmenu',
189-
'_ondblclick',
190-
'_onmousedown',
191-
'_onmouseenter',
192-
'_onmouseleave',
193-
'_onmousemove',
194-
'_onmouseout',
195-
'_onmouseover',
175+
// attribution params, not used in a way that would be mangled but be cautious
176+
'_kx',
196177

197-
// Helpers added by the e.g. es5 build. We don't use this, but they can be a starting point if we try to get the es5 build mangled in the future
198-
'_invoke',
199-
'__proto__',
200-
'__await',
201-
'_createClass',
202-
'_classCallCheck',
203-
'__esModule',
204-
'__publicField2',
205-
'__symbol__',
178+
// used in rrweb source
179+
'_rrweb',
180+
'_root',
181+
'_css',
182+
'_opts',
183+
'_cssText',
184+
'__context',
185+
'_mappings',
186+
'_processor',
187+
'_args',
188+
'__ln',
189+
'_unchangedStyles',
190+
'__rrweb_original__',
191+
'_Departed',
192+
'_onload',
193+
'_onclick',
194+
'_oncontextmenu',
195+
'_ondblclick',
196+
'_onmousedown',
197+
'_onmouseenter',
198+
'_onmouseleave',
199+
'_onmousemove',
200+
'_onmouseout',
201+
'_onmouseover',
206202

207-
// found in terser-mangled-names.json and couldn't attribute source, so preserve out of caution,
208-
'_sb',
209-
'_mirror',
210-
'_map',
211-
],
203+
// Helpers added by the e.g. es5 build. We don't use this, but they can be a starting point if we try to get the es5 build mangled in the future
204+
'_invoke',
205+
'__proto__',
206+
'__await',
207+
'_createClass',
208+
'_classCallCheck',
209+
'__esModule',
210+
'__publicField2',
211+
'__symbol__',
212+
213+
// found in terser-mangled-names.json and couldn't attribute source, so preserve out of caution,
214+
'_sb',
215+
'_mirror',
216+
'_map',
217+
],
218+
},
212219
},
213-
},
214220
}),
215221
{
216222
name: 'save-terser-mangled-names',
@@ -264,7 +270,7 @@ const entrypointTargets = entrypoints.map((file) => {
264270

265271
const fileName = fileParts.join('.')
266272

267-
const pluginsForThisFile = plugins(fileName.includes('es5'))
273+
const pluginsForThisFile = plugins(fileName.includes('es5'), fileName.includes('no-external'))
268274

269275
// we're allowed to console log in this file :)
270276
// eslint-disable-next-line no-console

src/__tests__/entrypoints/module.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ describe('Array entrypoint', () => {
1212
)
1313

1414
it('should not contain any script loaders', () => {
15-
expect(arrayJs).toContain('loadExternalDependency=')
16-
expect(arrayFullJs).toContain('loadExternalDependency=')
17-
expect(arrayNoExternalJs).not.toContain('loadExternalDependency=')
18-
expect(arrayFullNoExternalJs).not.toContain('loadExternalDependency=')
15+
expect(arrayJs).toContain('__PosthogExtensions__.loadExternalDependency=')
16+
expect(arrayFullJs).toContain('__PosthogExtensions__.loadExternalDependency=')
17+
expect(arrayNoExternalJs).not.toContain('__PosthogExtensions__.loadExternalDependency=')
18+
expect(arrayFullNoExternalJs).not.toContain('__PosthogExtensions__.loadExternalDependency=')
1919
})
2020
})
2121

@@ -30,9 +30,9 @@ describe('Module entrypoint', () => {
3030

3131
it('should not contain any script loaders', () => {
3232
// For the module loader, the code isn't minified
33-
expect(moduleJs).toContain('loadExternalDependency=')
34-
expect(moduleFullJs).toContain('loadExternalDependency=')
35-
expect(moduleNoExternalJs).not.toContain('loadExternalDependency=')
36-
expect(moduleFullNoExternalJs).not.toContain('loadExternalDependency=')
33+
expect(moduleJs).toContain('__PosthogExtensions__.loadExternalDependency=')
34+
expect(moduleFullJs).toContain('__PosthogExtensions__.loadExternalDependency=')
35+
expect(moduleNoExternalJs).not.toContain('__PosthogExtensions__.loadExternalDependency=')
36+
expect(moduleFullNoExternalJs).not.toContain('__PosthogExtensions__.loadExternalDependency=')
3737
})
3838
})

0 commit comments

Comments
 (0)