diff --git a/packages/cli/src/api/compile.ts b/packages/cli/src/api/compile.ts index 5b4daee2c..b64350c1d 100644 --- a/packages/cli/src/api/compile.ts +++ b/packages/cli/src/api/compile.ts @@ -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 diff --git a/packages/core/compile.entry.js b/packages/core/compile.entry.js new file mode 100644 index 000000000..56dea88c3 --- /dev/null +++ b/packages/core/compile.entry.js @@ -0,0 +1,4 @@ +/** + * this is Rollup Entry + */ +export * from "./src/compile" diff --git a/packages/core/compile.js b/packages/core/compile.js new file mode 100644 index 000000000..0dcd42e9e --- /dev/null +++ b/packages/core/compile.js @@ -0,0 +1,4 @@ +/** + * this is used to point to the right source when development with workspaces + */ +export * from "./build/compile" diff --git a/packages/core/npm/compile.js b/packages/core/npm/compile.js new file mode 100644 index 000000000..6a8c16d3c --- /dev/null +++ b/packages/core/npm/compile.js @@ -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") +} diff --git a/packages/core/npm/esm/compile.js b/packages/core/npm/esm/compile.js new file mode 100644 index 000000000..f7fcf4a8d --- /dev/null +++ b/packages/core/npm/esm/compile.js @@ -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; diff --git a/packages/core/package.json b/packages/core/package.json index 0fe605eee..c69dc5e13 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -34,12 +34,17 @@ "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", + "compile.js", "cjs/", "esm/" ], diff --git a/packages/core/src/compileMessage.test.ts b/packages/core/src/compile/compileMessage.test.ts similarity index 98% rename from packages/core/src/compileMessage.test.ts rename to packages/core/src/compile/compileMessage.test.ts index d2de5bc70..e6acc6b03 100644 --- a/packages/core/src/compileMessage.test.ts +++ b/packages/core/src/compile/compileMessage.test.ts @@ -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 = { diff --git a/packages/core/src/compileMessage.ts b/packages/core/src/compile/compileMessage.ts similarity index 95% rename from packages/core/src/compileMessage.ts rename to packages/core/src/compile/compileMessage.ts index d2809f962..ef860384d 100644 --- a/packages/core/src/compileMessage.ts +++ b/packages/core/src/compile/compileMessage.ts @@ -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; @@ -52,6 +52,7 @@ function processTokens(tokens: Array, mapText?: MapTextFn): CompiledMessa } // Message -> (Params -> String) +/** @internal */ export function compileMessage( message: string, mapText: MapTextFn = (v) => v, diff --git a/packages/core/src/compile/index.ts b/packages/core/src/compile/index.ts new file mode 100644 index 000000000..7ba52a74b --- /dev/null +++ b/packages/core/src/compile/index.ts @@ -0,0 +1 @@ +export * from './compileMessage'; diff --git a/packages/core/src/dev/index.ts b/packages/core/src/dev/index.ts index 32ce04ffa..1c79b9b49 100644 --- a/packages/core/src/dev/index.ts +++ b/packages/core/src/dev/index.ts @@ -1,4 +1,4 @@ -import {compileMessage as compile} from "../compileMessage" +import {compileMessage as compile} from "../compile/compileMessage" import loadLocaleData from "./loadLocaleData" export { compile, loadLocaleData } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d29ca6e6a..1e1db5314 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -10,10 +10,6 @@ export { Locales, } from "./i18n" -export { - compileMessage -} from './compileMessage' - // Default i18n object import { setupI18n } from "./i18n" export const i18n = setupI18n() diff --git a/packages/remote-loader/src/browserCompiler.ts b/packages/remote-loader/src/browserCompiler.ts index af0544262..f454d561d 100644 --- a/packages/remote-loader/src/browserCompiler.ts +++ b/packages/remote-loader/src/browserCompiler.ts @@ -1,4 +1,4 @@ -import { compileMessage } from "@lingui/core" +import { compileMessage } from "@lingui/core/compile" export function createBrowserCompiledCatalog(messages: Record) { return Object.keys(messages).reduce((obj, key: string) => { diff --git a/scripts/build/bundles.ts b/scripts/build/bundles.ts index 1b90143ea..0beae5a0b 100644 --- a/scripts/build/bundles.ts +++ b/scripts/build/bundles.ts @@ -7,10 +7,18 @@ export enum BundleType { export type BundleDef = { type: BundleType, - label?: string, externals?: string[] + /** + * Optional. Default index.js + */ + entry?: string; + + /** + * Name of package in packages/* folder + */ packageName: string + label?: string, // all following not used now moduleType?: unknown, global?: unknown, @@ -23,6 +31,13 @@ export const bundles: BundleDef[] = [ packageName: "core", externals: [] }, + { + type: BundleType.UNIVERSAL, + packageName: "core", + entry: 'compile.entry.js', + label: 'compile', + externals: [] + }, { type: BundleType.UNIVERSAL, packageName: 'react', diff --git a/scripts/build/rollup.ts b/scripts/build/rollup.ts index 9ac9d5be6..4644ecc47 100644 --- a/scripts/build/rollup.ts +++ b/scripts/build/rollup.ts @@ -269,7 +269,7 @@ async function build(bundle: BundleDef, bundleType: RollupBundleType) { ) const rollupConfig = { - input: path.resolve(getPackageDir(bundle.packageName), 'index.js'), + input: path.resolve(getPackageDir(bundle.packageName), bundle.entry || 'index.js'), treeshake: { moduleSideEffects, }, diff --git a/scripts/jest/config.integration.js b/scripts/jest/config.integration.js index ef42d6e23..0106e8d9c 100644 --- a/scripts/jest/config.integration.js +++ b/scripts/jest/config.integration.js @@ -1,4 +1,4 @@ -const { readdirSync, statSync } = require("fs") +const { readdirSync } = require("fs") const { join } = require("path") const sourceConfig = require("./config.unit") @@ -33,7 +33,12 @@ packages ] = `/packages/${name}/build/$1` }) -module.exports = Object.assign({}, sourceConfig, { +/** + * @type {import('jest').Config} + */ +module.exports = { + ...sourceConfig, + roots: ["/packages/"], testPathIgnorePatterns: ["/node_modules/"], // Redirect imports to the compiled bundles @@ -49,4 +54,4 @@ module.exports = Object.assign({}, sourceConfig, { haste: { throwOnModuleCollision: false, } -}) +} diff --git a/scripts/jest/config.unit.js b/scripts/jest/config.unit.js index 33acd3a49..447b89d09 100644 --- a/scripts/jest/config.unit.js +++ b/scripts/jest/config.unit.js @@ -1,3 +1,9 @@ +const { pathsToModuleNameMapper } = require('ts-jest'); +const tsConfig = require( '../../tsconfig.json'); + +/** + * @type {import('jest').Config} + */ module.exports = { roots: ["/packages/"], rootDir: process.cwd(), @@ -18,6 +24,9 @@ module.exports = { ], coverageReporters: ["html", "lcov", "text"], modulePathIgnorePatterns: ["/build"], + moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, { + prefix: '/', + }), reporters: ["default", "jest-junit"], setupFiles: ['set-tz/utc'], diff --git a/tsconfig.json b/tsconfig.json index fab2cff6d..c2c42ed2e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,11 @@ "baseUrl": "./", "pretty": true, "esModuleInterop": true, + "stripInternal": true, "target": "es2017", "paths": { "@lingui/core": ["./packages/core/src"], + "@lingui/core/compile": ["./packages/core/src/compile/index.ts"], "@lingui/react": ["./packages/react/src"], "@lingui/conf": ["./packages/conf"], "@lingui/macro": ["./packages/macro/src"]