Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stripping local debug code so can that dead-code-elimination produces smaller bundles with fewer function calls (part 1) #1606

Merged
merged 9 commits into from
Sep 11, 2024
Merged
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({
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
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]
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved
? [
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
Loading