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

refactor(build): typescript + support inner exports #1336

Merged
merged 2 commits into from
Jan 16, 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
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
"lint:types": "tsc",
"lint:eslint": "eslint packages",
"lint:all": "yarn lint:eslint && yarn lint:types",
"release:build": "node ./scripts/build/",
"release:test": "node ./scripts/test.js",
"release:build": "node -r @swc-node/register ./scripts/build/index.ts",
"release:test": "node -r @swc-node/register ./scripts/test.ts",
"version:next": "lerna version --exact --force-publish --no-private --preid next --create-release github --conventional-commits --conventional-prerelease --yes",
"version:latest": "lerna version --exact --force-publish --no-private --create-release github --conventional-commits --yes",
"release:latest": "lerna publish from-package --contents build --dist-tag latest --yes",
"release:next": "lerna publish from-package --contents build --canary --preid next --pre-dist-tag next --yes",
"release:latest": "lerna publish from-package --dist-tag latest --yes",
"release:next": "lerna publish from-package --canary --preid next --pre-dist-tag next --yes",
"build:docs": "cd docs && ./vercel-build.sh",
"size-limit": "size-limit"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"devDependencies": {
"@babel/core": "^7.11.6",
Expand All @@ -37,11 +37,16 @@
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@size-limit/preset-small-lib": "^4.10.1",
"@swc/jest": "^0.2.24",
"@swc/core": "^1.3.26",
"swc-node": "^1.0.0",
"@testing-library/react": "^11.0.4",
"@types/babel-types": "^7.0.9",
"@types/jest": "^26.0.14",
"@types/node": "14.14.31",
"@types/ramda": "^0.27.23",
"@types/react": "^16.9.51",
"@types/rimraf": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"babel-code-frame": "^6.26.0",
Expand Down Expand Up @@ -76,7 +81,6 @@
"node-emoji": "^1.10.0",
"npm-cli-login": "^0.1.1",
"ora": "^5.1.0",
"pkg-up": "^3.1.0",
"prettier": "^2.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
Expand All @@ -94,7 +98,6 @@
"set-tz": "^0.2.0",
"size-limit": "^4.10.1",
"strip-ansi": "^6.0.0",
"targz": "^1.0.1",
"tmp": "^0.2.1",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3",
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-plugin-extract-messages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@lingui/babel-plugin-extract-messages",
"version": "3.15.0",
"description": "Babel plugin for collecting messages from source code for internationalization",
"main": "index.js",
"main": "./build/index.js",
"author": {
"name": "Tomáš Ehrlich",
"email": "tomas.ehrlich@gmail.com"
Expand All @@ -17,6 +17,11 @@
"i9n",
"translation"
],
"files": [
"LICENSE",
"README.md",
"build/"
],
"repository": {
"type": "git",
"url": "https://github.com/lingui/js-lingui.git"
Expand Down
17 changes: 11 additions & 6 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
"name": "Tomáš Ehrlich",
"email": "tomas.ehrlich@gmail.com"
},
"main": "index.js",
"main": "./build/index.js",
"bin": {
"lingui": "./lingui.js"
"lingui": "./build/lingui.js"
},
"exports": {
".": {
"require": "./build/index.js"
},
"./api": {
"require": "./build/api/index.js"
}
},
"engines": {
"node": ">=10.0.0"
},
"files": [
"LICENSE",
"README.md",
"api",
"services",
"lingui.js",
"lingui-*.js"
"build/"
],
"dependencies": {
"@babel/generator": "^7.11.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from "@babel/types"
import generate, {GeneratorOptions} from "@babel/generator"
import {compileMessage} from "@lingui/core"
import {compileMessage} from "@lingui/core/compile"
import pseudoLocalize from "./pseudoLocalize"

export type CompiledCatalogNamespace = "cjs" | "es" | "ts" | string
Expand Down
31 changes: 24 additions & 7 deletions packages/cli/src/api/detect.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
import fs from "fs"
import nodePath from "path"
import pkgUp from "pkg-up"
import cliPackageJson from "../../package.json";

export const projectType = {
CRA: "CRA",
REACT: "REACT",
}

function getPackageJson() {
const packageJsonPath = pkgUp.sync()
type PackageJson = {
name: string
dependencies: Record<string, string>
devDependencies: Record<string, string>
}

function getPackageJson(cwd?: string): {path: string, content: PackageJson} {
const packageJsonPath = pkgUp.sync({
cwd
})

try {
const json = fs.readFileSync(packageJsonPath, "utf8")
return JSON.parse(json)
return {path: packageJsonPath, content: JSON.parse(json)}
} catch (e) {
console.error(e)
return null
}
}

function hasDependency(pkg, name) {


function hasDependency(pkg: PackageJson, name: string) {
return (
(pkg.dependencies && pkg.dependencies[name]) ||
(pkg.devDependencies && pkg.devDependencies[name])
)
}

function detectFramework(pkg) {
function detectFramework(pkg: PackageJson) {
if (hasDependency(pkg, "react-scripts")) {
return projectType.CRA
}
Expand All @@ -38,8 +50,13 @@ function detectFramework(pkg) {
}

export function detect() {
const pkg = getPackageJson()
let pkg = getPackageJson()

if (!pkg) return null

return detectFramework(pkg)
if (pkg.content.name === cliPackageJson.name) {
pkg = getPackageJson(nodePath.dirname(pkg.path))
}

return detectFramework(pkg.content)
}
2 changes: 1 addition & 1 deletion packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Tomáš Ehrlich",
"email": "tomas.ehrlich@gmail.com"
},
"main": "index.js",
"main": "build/index.js",
"engines": {
"node": ">=10.0.0"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/core/compile.entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* this is Rollup Entry
*/
export * from "./src/compile"
5 changes: 5 additions & 0 deletions packages/core/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* this is used to point to the right source when development with workspaces
*/
// eslint-disable-next-line import/no-unresolved
export * from "./build/compile"
1 change: 0 additions & 1 deletion packages/core/dev.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/formats.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/core/npm/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (process.env.NODE_ENV === "production") {
module.exports = require("./cjs/compile.production.min.js")
} else {
module.exports = require("./cjs/compile.development.js")
}
5 changes: 0 additions & 5 deletions packages/core/npm/dev.js

This file was deleted.

9 changes: 9 additions & 0 deletions packages/core/npm/esm/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
compileMessage as compileMessageProd
} from './compile.production.min.js';

import {
compileMessage as compileMessageDev
} from './compile.development.js';

export const compileMessage = process.env.NODE_ENV === 'production' ? compileMessageProd : compileMessageDev;
18 changes: 0 additions & 18 deletions packages/core/npm/esm/dev.js

This file was deleted.

20 changes: 11 additions & 9 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": "index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"main": "./build/index.js",
"module": "./build/esm/index.js",
"types": "./build/cjs/index.d.ts",
"author": {
"name": "Tomáš Ehrlich",
"email": "tomas.ehrlich@gmail.com"
Expand All @@ -27,21 +27,23 @@
"url": "https://github.com/lingui/js-lingui/issues"
},
"engines": {
"node": ">=10.0.0"
"node": ">=14.0.0"
},
"exports": {
".": {
"require": "./index.js",
"import": "./esm/index.js"
"require": "./build/index.js",
"import": "./build/esm/index.js"
},
"./compile": {
"require": "./build/compile.js",
"import": "./build/esm/compile.js"
},
"./package.json": "./package.json"
},
"files": [
"LICENSE",
"README.md",
"index.js",
"cjs/",
"esm/"
"build/"
],
"dependencies": {
"@babel/runtime": "^7.11.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {compileMessage as compile} from "./compileMessage"
import { mockEnv, mockConsole } from "@lingui/jest-mocks"
import { interpolate } from "./context"
import {Locale, Locales} from "./i18n"
import { interpolate } from "../context"
import {Locale, Locales} from "../i18n"

describe("compile", () => {
const englishPlurals = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Content, parse, Token} from "@messageformat/parser"
import {CompiledMessage, CompiledMessageToken} from "./i18n"
import {CompiledMessage, CompiledMessageToken} from "../i18n"

type MapTextFn = (value: string) => string;

Expand Down Expand Up @@ -52,6 +52,7 @@ function processTokens(tokens: Array<Token>, mapText?: MapTextFn): CompiledMessa
}

// Message -> (Params -> String)
/** @internal */
export function compileMessage(
message: string,
mapText: MapTextFn = (v) => v,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/compile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './compileMessage';
4 changes: 0 additions & 4 deletions packages/core/src/dev/index.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/core/src/dev/loadLocaleData.test.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/core/src/dev/loadLocaleData.ts

This file was deleted.

4 changes: 2 additions & 2 deletions 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 * as icu from "./dev"
import { compileMessage } from "./compile"
import { EventEmitter } from "./eventEmitter"
import type {PluralCategory} from "make-plural"

Expand Down Expand Up @@ -206,7 +206,7 @@ export class I18n extends EventEmitter<Events> {

if (process.env.NODE_ENV !== "production") {
translation = isString(translation)
? icu.compile(translation)
? compileMessage(translation)
: translation
}

Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export {
Locales,
} from "./i18n"

export {
compileMessage
} from './compileMessage'

// Default i18n object
import { setupI18n } from "./i18n"
export const i18n = setupI18n()
Expand Down
Loading