Skip to content

Commit

Permalink
refactor(build): push compileMessage as inner package of core
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Jan 12, 2023
1 parent 07638e5 commit 3341c1e
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 15 deletions.
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
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"
4 changes: 4 additions & 0 deletions packages/core/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* this is used to point to the right source when development with workspaces
*/
export * from "./build/compile"
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")
}
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;
5 changes: 5 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
],
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';
2 changes: 1 addition & 1 deletion packages/core/src/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {compileMessage as compile} from "../compileMessage"
import {compileMessage as compile} from "../compile/compileMessage"
import loadLocaleData from "./loadLocaleData"

export { compile, loadLocaleData }
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
2 changes: 1 addition & 1 deletion packages/remote-loader/src/browserCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compileMessage } from "@lingui/core"
import { compileMessage } from "@lingui/core/compile"

export function createBrowserCompiledCatalog(messages: Record<string, any>) {
return Object.keys(messages).reduce((obj, key: string) => {
Expand Down
17 changes: 16 additions & 1 deletion scripts/build/bundles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
11 changes: 8 additions & 3 deletions scripts/jest/config.integration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { readdirSync, statSync } = require("fs")
const { readdirSync } = require("fs")
const { join } = require("path")
const sourceConfig = require("./config.unit")

Expand Down Expand Up @@ -33,7 +33,12 @@ packages
] = `<rootDir>/packages/${name}/build/$1`
})

module.exports = Object.assign({}, sourceConfig, {
/**
* @type {import('jest').Config}
*/
module.exports = {
...sourceConfig,

roots: ["<rootDir>/packages/"],
testPathIgnorePatterns: ["/node_modules/"],
// Redirect imports to the compiled bundles
Expand All @@ -49,4 +54,4 @@ module.exports = Object.assign({}, sourceConfig, {
haste: {
throwOnModuleCollision: false,
}
})
}
9 changes: 9 additions & 0 deletions scripts/jest/config.unit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const { pathsToModuleNameMapper } = require('ts-jest');
const tsConfig = require( '../../tsconfig.json');

/**
* @type {import('jest').Config}
*/
module.exports = {
roots: ["<rootDir>/packages/"],
rootDir: process.cwd(),
Expand All @@ -18,6 +24,9 @@ module.exports = {
],
coverageReporters: ["html", "lcov", "text"],
modulePathIgnorePatterns: ["/build"],
moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, {
prefix: '<rootDir>/',
}),

reporters: ["default", "jest-junit"],
setupFiles: ['set-tz/utc'],
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 3341c1e

Please sign in to comment.