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

feat(web): downcompile helper treeshaking 🧩 #8964

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions common/models/templates/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ cd "$(dirname "$THIS_SCRIPT")"
################################ Main script ################################

builder_describe "Builds the predictive-text model template implementation module" \
"@../../web/keyman-version" \
"@../wordbreakers" \
"@/common/web/keyman-version" \
"@/common/web/tslib" \
"@/common/models/wordbreakers" \
"clean" \
"configure" \
"build" \
Expand Down
1 change: 1 addition & 0 deletions common/models/templates/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"moduleResolution": "node16",
"sourceMap": true,
"sourceRoot": "/common/models/templates/src",
"importHelpers": true,
"inlineSources": true,
"strict": true,
"lib": ["es6"],
Expand Down
8 changes: 3 additions & 5 deletions common/models/wordbreakers/src/default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ function findBoundaries(text: string, options?: DefaultWordBreakerOptions): numb
}
// Do not break across certain punctuation
// WB6: (Don't break before apostrophes in contractions)
const SET_ALL_MIDLETTER = [WordBreakProperty.MidLetter, ...SET_MIDNUMLETQ];
const SET_ALL_MIDLETTER = [WordBreakProperty.MidLetter].concat(SET_MIDNUMLETQ);
if(state.match(null, SET_AHLETTER, SET_ALL_MIDLETTER, SET_AHLETTER)) {
continue;
}
Expand Down Expand Up @@ -474,7 +474,7 @@ function findBoundaries(text: string, options?: DefaultWordBreakerOptions): numb
}
// Do not break within sequences, such as 3.2, 3,456.789
// WB11
const SET_ALL_MIDNUM = [WordBreakProperty.MidNum, ...SET_MIDNUMLETQ];
const SET_ALL_MIDNUM = [WordBreakProperty.MidNum].concat(SET_MIDNUMLETQ);
if(state.match([WordBreakProperty.Numeric], SET_ALL_MIDNUM, [WordBreakProperty.Numeric], null)) {
continue;
}
Expand All @@ -488,9 +488,7 @@ function findBoundaries(text: string, options?: DefaultWordBreakerOptions): numb
}
// Do not break from extenders (e.g., U+202F NARROW NO-BREAK SPACE)
// WB13a
const SET_NUM_KAT_LET = [WordBreakProperty.Katakana,
WordBreakProperty.Numeric,
...SET_AHLETTER];
const SET_NUM_KAT_LET = [WordBreakProperty.Katakana, WordBreakProperty.Numeric].concat(SET_AHLETTER);
if(state.match(null, SET_NUM_KAT_LET, [WordBreakProperty.ExtendNumLet], null)) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions common/models/wordbreakers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"moduleResolution": "Node16",
"declaration": true,
"sourceMap": true,
"importHelpers": true,
"inlineSources": true,
"sourceRoot": "/common/models/wordbreakers/src",
"strict": true,
Expand Down
11 changes: 10 additions & 1 deletion common/web/lm-worker/build-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import esbuild from 'esbuild';
import { spawn } from 'child_process';
import fs from 'fs';
import { determineNeededDowncompileHelpers, buildTslibTreeshaker } from '@keymanapp/tslib/esbuild-tools';

/*
* Refer to https://github.com/microsoft/TypeScript/issues/13721#issuecomment-307259227 -
Expand Down Expand Up @@ -92,7 +92,12 @@ await esbuild.build({
target: "es5"
});

// The one that's actually a component of our releases.

const embeddedWorkerBuildOptions = {
alias: {
'tslib': '@keymanapp/tslib'
},
bundle: true,
sourcemap: true,
format: "iife",
Expand All @@ -106,6 +111,10 @@ const embeddedWorkerBuildOptions = {
target: "es5"
}

// Prepare the needed setup for `tslib` treeshaking.
const unusedHelpers = await determineNeededDowncompileHelpers(embeddedWorkerBuildOptions);
embeddedWorkerBuildOptions.plugins = [buildTslibTreeshaker(unusedHelpers), ...embeddedWorkerBuildOptions.plugins];

// Direct-use version
await esbuild.build(embeddedWorkerBuildOptions);

Expand Down
1 change: 1 addition & 0 deletions common/web/lm-worker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ WORKER_OUTPUT_FILENAME=build/lib/worker-main.js
builder_describe \
"Compiles the Language Modeling Layer for common use in predictive text and autocorrective applications." \
"@/common/web/keyman-version" \
"@/common/web/tslib" \
"@/common/models/wordbreakers" \
"@/common/models/templates" \
"@/common/tools/sourcemap-path-remapper" \
Expand Down
3 changes: 2 additions & 1 deletion common/web/lm-worker/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"allowJs": false,
"declaration": true,
"module": "es6",
"moduleResolution": "node",
"moduleResolution": "node16",
"importHelpers": true,
"inlineSourceMap": true,
"inlineSources": true,
"sourceRoot": "/common/web/lm-worker/src",
Expand Down
13 changes: 13 additions & 0 deletions common/web/tslib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
"name": "@keymanapp/tslib",
"description": "An ES5 + esbuild-compatible wrapper for the 'tslib' library",
"main": "./build/index.js",
"exports": {
".": {
"types": "./build/index.d.ts",
"import": "./build/index.js"
},
"./esbuild-tools": {
"types": "./build/esbuild-tools.d.ts",
"import": "./build/esbuild-tools.js"
}
},
"scripts": {
"build": "gosh ./build.sh",
"clean": "tsc -b --clean",
Expand All @@ -11,5 +21,8 @@
"tslib": "^2.5.2",
"typescript": "^4.9.5"
},
"devDependencies": {
"esbuild": "^0.15.16"
},
"type": "module"
}
Loading