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(build): fix tree shaking and bundle size #1343

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const babelConfig = require("./scripts/build/babel.config")

module.exports = babelConfig({ modules: true })
module.exports = babelConfig(true)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sourceConfig = require("./config.unit")
const sourceConfig = require("./jest.config")

/**
* @type {import('jest').Config}
Expand Down
8 changes: 4 additions & 4 deletions scripts/jest/config.unit.js → jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const tsConfig = require( '../../tsconfig.json');
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const tsConfig = require( './tsconfig.json');

/**
* @type {import('jest').Config}
Expand Down Expand Up @@ -30,9 +30,9 @@ module.exports = {

reporters: ["default", "jest-junit"],
setupFiles: ['set-tz/utc'],
setupFilesAfterEnv: [require.resolve("./env.js")],
setupFilesAfterEnv: [require.resolve("./scripts/jest/env.js")],
snapshotSerializers: [
"jest-serializer-html",
require.resolve("./stripAnsiSerializer.js"),
require.resolve("./scripts/jest/stripAnsiSerializer.js"),
],
}
46 changes: 19 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"license": "MIT",
"scripts": {
"test": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu JEST_JUNIT_OUTPUT=results/unit.xml jest -c scripts/jest/config.unit.js --no-cache",
"test:integration": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu JEST_JUNIT_OUTPUT=results/integration.xml jest -c scripts/jest/config.integration.js --no-cache",
"test": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu JEST_JUNIT_OUTPUT=results/unit.xml jest -c jest.config.js --no-cache",
"test:integration": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu JEST_JUNIT_OUTPUT=results/integration.xml jest -c jest.config.integration.js --no-cache",
"test:all": "yarn test && yarn test:integration",
"watch": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu jest -c scripts/jest/config.unit.js --watch",
"watch:integration": "cross-env TZ=UTC NODE_ICU_DATA=node_modules/full-icu jest -c scripts/jest/config.integration.js --watch",
Expand All @@ -36,10 +36,13 @@
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@babel/code-frame": "^7.10.4",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@size-limit/preset-small-lib": "^8.1.1",
"@swc/jest": "^0.2.24",
"@swc/core": "^1.3.26",
"swc-node": "^1.0.0",
"@swc/jest": "^0.2.24",
"@testing-library/react": "^11.0.4",
"@types/babel-types": "^7.0.9",
"@types/jest": "^26.0.14",
Expand All @@ -49,8 +52,6 @@
"@types/rimraf": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"babel-code-frame": "^6.26.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.5.2",
"babel-plugin-macros": "^2.8.0",
Expand All @@ -63,11 +64,9 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-react": "^7.21.4",
"filesize": "^6.1.0",
"fs-extra": "^9.0.1",
"full-icu": "^1.3.1",
"glob": "^7.1.4",
"gzip-size": "^5.1.1",
"jest": "^26.5.2",
"jest-junit": "^12.0.0",
"jest-serializer-html": "^7.0.0",
Expand All @@ -77,31 +76,21 @@
"mock-fs": "^4.13.0",
"mockdate": "^3.0.2",
"ncp": "^2.0.0",
"nixt": "^0.5.0",
"node-emoji": "^1.10.0",
"npm-cli-login": "^0.1.1",
"ora": "^5.1.0",
"prettier": "^2.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"regenerator-runtime": "^0.13.7",
"rimraf": "^3.0.2",
"rollup": "^2.29.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-prettier": "^2.1.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.27.3",
"rollup": "^3.10.0",
"rollup-plugin-dts": "^5.1.1",
"semver": "^7.3.2",
"set-tz": "^0.2.0",
"size-limit": "^8.1.1",
"strip-ansi": "^6.0.0",
"tmp": "^0.2.1",
"swc-node": "^1.0.0",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3",
"webpack": "^4.44.2",
"yalc": "^1.0.0-pre.45"
},
"workspaces": [
Expand All @@ -113,22 +102,25 @@
},
"size-limit": [
{
"path": "./packages/core/build/esm/core.production.min.js",
"path": "./packages/core/build/esm/index.js",
"import": "{ i18n }",
"limit": "3 kB"
},
{
"path": "./packages/detect-locale/build/esm/detect-locale.production.min.js",
"path": "./packages/detect-locale/build/esm/index.js",
"limit": "1 kB"
},
{
"path": "./packages/react/build/esm/react.production.min.js",
"path": "./packages/react/build/esm/index.js",
"limit": "3 kB",
"ignore": ["react"]
"ignore": [
"react"
]
},
{
"path": "./packages/remote-loader/build/esm/remote-loader.production.min.js",
"path": "./packages/remote-loader/build/esm/index.js",
"limit": "8 kB"
}
]
],
"dependencies": {}
}
2 changes: 1 addition & 1 deletion packages/babel-plugin-extract-messages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"url": "https://github.com/lingui/js-lingui/issues"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"dependencies": {
"@babel/generator": "^7.11.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"files": [
"LICENSE",
Expand Down
2 changes: 1 addition & 1 deletion packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"main": "build/index.js",
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"dependencies": {
"@babel/runtime": "^7.11.2",
Expand Down
6 changes: 1 addition & 5 deletions packages/core/npm/compile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/compile.production.min.js")
} else {
module.exports = require("./cjs/compile.development.js")
}
module.exports = require("./cjs/compile.js")
9 changes: 0 additions & 9 deletions packages/core/npm/esm/compile.js

This file was deleted.

18 changes: 0 additions & 18 deletions packages/core/npm/esm/index.js

This file was deleted.

6 changes: 1 addition & 5 deletions packages/core/npm/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/core.production.min.js")
} else {
module.exports = require("./cjs/core.development.js")
}
module.exports = require("./cjs/index.js")
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "3.15.0",
"sideEffects": false,
"description": "I18n tools for javascript",
"main": "./build/index.js",
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
"types": "./build/cjs/index.d.ts",
"types": "./build/index.d.ts",
"author": {
"name": "Tomáš Ehrlich",
"email": "tomas.ehrlich@gmail.com"
Expand All @@ -31,11 +31,11 @@
},
"exports": {
".": {
"require": "./build/index.js",
"require": "./build/cjs/index.js",
"import": "./build/esm/index.js"
},
"./compile": {
"require": "./build/compile.js",
"require": "./build/cjs/compile.js",
"import": "./build/esm/compile.js"
},
"./package.json": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { interpolate, UNICODE_REGEX } from "./context"
import { isString, isFunction } from "./essentials"
import { date, number } from "./formats"
import { compileMessage } from "./compile"
import { compileMessage } from "@lingui/core/compile"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very important change. If we would access this with relative path, tree shaker would not be able to eliminate this import.

Because of package import, tree shaker first looks into package's pakage.json, see {sideEffects: false} and then remove it from the bundle.

import { EventEmitter } from "./eventEmitter"
import type {PluralCategory} from "make-plural"

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export {
setupI18n,
I18n,
} from "./i18n"

export type {
AllMessages,
MessageDescriptor,
Messages,
Comment on lines 1 to 9
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without typescript plugin, rollup treat type only exports as regular, and obviously fail. Here we do a little help for him and manually mark them as type only.

Expand Down
33 changes: 0 additions & 33 deletions packages/detect-locale/npm/esm/index.js

This file was deleted.

6 changes: 1 addition & 5 deletions packages/detect-locale/npm/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/detect-locale.production.min.js")
} else {
module.exports = require("./cjs/detect-locale.development.js")
}
module.exports = require("./cjs/index.js")
8 changes: 4 additions & 4 deletions packages/detect-locale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "3.15.0",
"sideEffects": false,
"description": "@Lingui package to help you find the correct browser/server locale",
"main": "index.js",
"main": "./build/cjs/index.js",
"module": "./build/esm/index.js",
"types": "./build/cjs/index.d.ts",
"types": "./build/index.d.ts",
"license": "MIT",
"keywords": [
"i18n",
Expand All @@ -27,11 +27,11 @@
"url": "https://github.com/lingui/js-lingui/issues"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"exports": {
".": {
"require": "./build/index.js",
"require": "./build/cjs/index.js",
"import": "./build/esm/index.js"
},
"./package.json": "./package.json"
Expand Down
5 changes: 3 additions & 2 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"url": "https://github.com/lingui/js-lingui/issues"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"files": [
"LICENSE",
Expand All @@ -41,7 +41,8 @@
"loader-utils": "^2.0.0"
},
"devDependencies": {
"memory-fs": "^0.5.0"
"memory-fs": "^0.5.0",
"webpack": "^4.44.2"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was defined on monorepo root but actually used here, i fixed it.

},
"peerDependencies": {
"webpack": "^4.0.0 || ^5.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/macro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"url": "https://github.com/lingui/js-lingui/issues"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"files": [
"LICENSE",
Expand Down
12 changes: 0 additions & 12 deletions packages/react/npm/esm/index.js

This file was deleted.

6 changes: 1 addition & 5 deletions packages/react/npm/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/react.production.min.js")
} else {
module.exports = require("./cjs/react.development.js")
}
module.exports = require("./cjs/index.js")
Loading