Skip to content

Commit

Permalink
Fix production stripping in the production bundles (#1606)
Browse files Browse the repository at this point in the history
* Fix prod stripping

* WIP - put utilities to be stripped in their own package, so we can inline the package

* WIP: it turns out terser can't do what we want so we have to do a custom babel plugin...

* Revert "WIP: it turns out terser can't do what we want so we have to do a custom babel plugin..."

This reverts commit 5cddba2b0a4616350ba4d9da572863e77cad0ffe.

* Revert "WIP - put utilities to be stripped in their own package, so we can inline the package"

This reverts commit 57406f687a7991b8b35c907b1e7483bb40a4d521.

* Add babel plugin that removes code from @glimmer/debug that terser couldn't figure out -- I also left some notes about the problem that terser is running in to -- it seems to be related to indirection of identity functions that 'passes' isn't smart enough to figure out

We get big wins (side-wise) from sideEffects: false, but things break.

This babel plugin is feeling more and more brittle, the more we want to
strip.

* Inline glimmer-debug and hide all the metadata

* Convert more of the Checkers to the NoopChecker

* Tell the Tests how to handle VM_LOCAL_DEV
  • Loading branch information
NullVoxPopuli authored Sep 11, 2024
1 parent 10eae74 commit 3768842
Show file tree
Hide file tree
Showing 15 changed files with 1,697 additions and 1,550 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"build": "dotenv -- turbo build",
"build:control": "rollup -c rollup.config.mjs",
"build:flags": "RETAIN_FLAGS=true ember build --env production --suppress-sizes",
"link:all": "esyes ./bin/link-all.mts",
"clean": "node ./bin/clean.mjs",
"link:all": "esyes ./bin/link-all.mts",
"lint": "npm-run-all lint:*",
"lint:files": "turbo lint",
"lint:format": "prettier -c .",
"lint:types": "tsc -b",
"lintfix": "pnpm turbo test:lint -- --fix && prettier -w .",
"start": "ember serve --port=7357",
"start": "vite",
"test": "node bin/run-tests.mjs",
"test:babel-plugins": "yarn workspace @glimmer/vm-babel-plugins test",
"test:browserstack": "ember test --test-port=7774 --host 127.0.0.1 --config-file=testem-browserstack.js",
Expand Down
1 change: 0 additions & 1 deletion packages/@glimmer-workspace/build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export {
type PackageJSON,
type ViteConfig as ViteExport,
} from './lib/config.js';
export { default as importMeta } from './lib/import-meta.js';
1 change: 0 additions & 1 deletion packages/@glimmer-workspace/build/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { Package } from './lib/config.js';
export { default as importMeta } from './lib/import-meta.js';
export { default as inline } from './lib/inline.js';
58 changes: 52 additions & 6 deletions packages/@glimmer-workspace/build/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
// @ts-check

import { existsSync, readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

Expand All @@ -11,9 +11,10 @@ import * as insert from 'rollup-plugin-insert';
import rollupTS from 'rollup-plugin-ts';
import ts from 'typescript';

import importMeta from './import-meta.js';
import inline from './inline.js';

const require = createRequire(import.meta.url);

// eslint-disable-next-line import/no-named-as-default-member
const { ModuleKind, ModuleResolutionKind, ScriptTarget, ImportsNotUsedAsValues } = ts;

Expand Down Expand Up @@ -96,7 +97,10 @@ export function typescript(pkg, config) {
return rollupTS({
transpiler: 'babel',
transpileOnly: true,
babelConfig: { presets },
babelConfig: {
presets,
plugins: [require.resolve('@glimmer/local-debug-babel-plugin')],
},
/**
* This shouldn't be required, but it is.
* If we use @rollup/plugin-babel, we can remove this.
Expand All @@ -108,7 +112,7 @@ export function typescript(pkg, config) {

/** @type {['is' | 'startsWith', string[], 'inline' | 'external'][]} */
const EXTERNAL_OPTIONS = [
['is', ['tslib', '@glimmer/local-debug-flags'], 'inline'],
['is', ['tslib', '@glimmer/local-debug-flags', '@glimmer/debug'], 'inline'],
['is', ['@handlebars/parser', 'simple-html-tokenizer', 'babel-plugin-debug-macros'], 'external'],
['startsWith', ['.', '/', '#', '@babel/runtime/', process.cwd().replace(/\\/gu, '/')], 'inline'],
['startsWith', ['@glimmer/', '@simple-dom/', '@babel/', 'node:'], 'external'],
Expand Down Expand Up @@ -315,7 +319,31 @@ export class Package {
commonjs(),
nodeResolve(),
...this.replacements(env),
...(env === 'prod' ? [terser()] : []),
...(env === 'prod'
? [
terser({
module: true,
// to debug the output, uncomment this so you can read the
// identifiers, unchanged
// mangle: false,
compress: {
passes: 3,
},
}),
]
: [
terser({
module: true,
mangle: false,
compress: {
passes: 3,
},
format: {
comments: 'all',
beautify: true,
},
}),
]),
postcss(),
typescript(this.#package, {
target: ScriptTarget.ES2022,
Expand Down Expand Up @@ -356,7 +384,20 @@ export class Package {
*/
replacements(env) {
return env === 'prod'
? [importMeta]
? [
replace({
preventAssignment: true,
values: {
// Intended to be left in the build during publish
// currently compiled away to `@glimmer/debug`
'import.meta.env.MODE': '"production"',
'import.meta.env.DEV': 'false',
'import.meta.env.PROD': 'true',
// Not exposed at publish, compiled away
'import.meta.env.VM_LOCAL_DEV': 'false',
},
}),
]
: [
replace({
preventAssignment: true,
Expand Down Expand Up @@ -417,10 +458,15 @@ export class Package {

return {
input: resolve(root, ts),
treeshake: {
// moduleSideEffects: false,
moduleSideEffects: (id, external) => !external,
},
output: {
file: resolve(root, 'dist', env, file),
format,
sourcemap: true,
hoistTransitiveImports: false,
exports: format === 'cjs' ? 'named' : 'auto',
},
onwarn: (warning, warn) => {
Expand Down
4 changes: 0 additions & 4 deletions packages/@glimmer-workspace/build/lib/import-meta.d.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/@glimmer-workspace/build/lib/import-meta.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/@glimmer-workspace/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"test:types": "tsc --noEmit -p ../tsconfig.json"
},
"dependencies": {
"@glimmer/local-debug-babel-plugin": "workspace:*",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
Expand Down
54 changes: 52 additions & 2 deletions packages/@glimmer/debug/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
export { debug, debugSlice, logOpcode } from './lib/debug';
export * from './lib/metadata';
export {
buildEnum,
buildMetas,
buildSingleMeta,
META_KIND,
normalize,
normalizeAll,
normalizeParsed,
OPERAND_TYPES,
strip,
} from './lib/metadata';
export { opcodeMetadata } from './lib/opcode-metadata';
export * from './lib/stack-check';
export {
check,
CheckArray,
CheckBlockSymbolTable,
CheckBoolean,
CheckDict,
CheckDocumentFragment,
CheckElement,
CheckFunction,
CheckHandle,
CheckInstanceof,
CheckInterface,
CheckMaybe,
CheckNode,
CheckNumber,
CheckObject,
CheckOption,
CheckOr,
CheckPrimitive,
CheckProgramSymbolTable,
CheckSafeString,
CheckString,
CheckUndefined,
CheckUnknown,
recordStackSize,
wrap,
} from './lib/stack-check';

// Types are optimized await automatically
export type {
NormalizedMetadata,
NormalizedOpcodes,
Operand,
OperandList,
OperandName,
OperandType,
RawOperandFormat,
RawOperandMetadata,
Stack,
} from './lib/metadata';
export type { Checker } from './lib/stack-check';
Loading

0 comments on commit 3768842

Please sign in to comment.