Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Jan 14, 2025
1 parent 260c32a commit c640dfd
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 65 deletions.
11 changes: 6 additions & 5 deletions inlang/packages/paraglide-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@
"check": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@inlang/sdk": "workspace:*",
"@inlang/recommend-sherlock": "workspace:*",
"@inlang/sdk": "workspace:*",
"commander": "11.1.0",
"consola": "3.2.3",
"json5": "2.2.3",
"unplugin": "^2.1.2",
"typescript": "^5.7.3"
"typescript": "^5.7.3",
"unplugin": "^2.1.2"
},
"devDependencies": {
"@inlang/plugin-message-format": "workspace:*",
"@eslint/js": "^9.12.0",
"@inlang/plugin-message-format": "workspace:*",
"@opral/tsconfig": "workspace:*",
"@rollup/plugin-virtual": "3.0.1",
"@ts-morph/bootstrap": "0.20.0",
"@types/node": "^22.10.5",
"@vitest/coverage-v8": "2.0.5",
"prettier": "^3.4.2",
"eslint": "^9.12.0",
"memfs": "4.6.0",
"prettier": "^3.4.2",
"rolldown": "1.0.0-beta.1",
"rollup": "3.29.1",
"typescript-eslint": "^8.15.0",
"vitest": "2.0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test("compiles as ts", async () => {
*
*/
/* @__NO_SIDE_EFFECTS__ */
const blue_moon_bottle = (inputs: { age: NonNullable<unknown> } , options { locale?: "en" | "en-US",/** @deprecated use \`locale\` instead */languageTag?: "en" | "en-US" } = {}) : string => {
const blue_moon_bottle = (inputs: { age: NonNullable<unknown> } , options: { locale?: "en" | "en-US",/** @deprecated use \`locale\` instead */languageTag?: "en" | "en-US" } = {}) : string => {
const locale = options.locale ?? options.languageTag ?? getLocale()
if (locale === "en") return en.blue_moon_bottle(inputs)
if (locale === "en-US") return en_US.blue_moon_bottle(inputs)
Expand Down
4 changes: 2 additions & 2 deletions inlang/packages/paraglide-js/src/compiler/compileBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const compileBundleFunction = (args: {
* ${emitTs ? "" : jsDocBundleFunctionTypes({ inputs, locales: args.availableLocales })}
*/
/* @__NO_SIDE_EFFECTS__ */
const ${jsIdentifier(args.bundle.id)} = (inputs${emitTs ? tsInputType(inputs) : ""} ${hasInputs ? "" : "= {}"}, options ${emitTs ? tsOptionsType(args.availableLocales) : ""} = {}) ${emitTs ? ": string" : ""} => {
const ${jsIdentifier(args.bundle.id)} = (inputs${emitTs ? tsInputType(inputs) : ""} ${hasInputs ? "" : "= {}"}, options${emitTs ? tsOptionsType(args.availableLocales) : ""} = {}) ${emitTs ? ": string" : ""} => {
const locale = options.locale ?? options.languageTag ?? getLocale()
${args.availableLocales
.map(
Expand All @@ -120,7 +120,7 @@ const ${jsIdentifier(args.bundle.id)} = (inputs${emitTs ? tsInputType(inputs) :

function tsOptionsType(locales: string[]): string {
const localesUnion = locales.map((locale) => `"${locale}"`).join(" | ");
return `{ locale?: ${localesUnion},/** @deprecated use \`locale\` instead */languageTag?: ${localesUnion} }`;
return `: { locale?: ${localesUnion},/** @deprecated use \`locale\` instead */languageTag?: ${localesUnion} }`;
}

function tsInputType(inputs: { name: string }[]): string {
Expand Down
49 changes: 25 additions & 24 deletions inlang/packages/paraglide-js/src/compiler/compileProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
compileProject,
type ParaglideCompilerOptions,
} from "./compileProject.js";
import { rollup as _rollup } from "rollup";
import virtual from "@rollup/plugin-virtual";
import { rolldown } from "rolldown";

beforeEach(() => {
// reset the imports to make sure that the runtime is reloaded
Expand Down Expand Up @@ -82,18 +82,19 @@ test("emitPrettierIgnore", async () => {
});

describe.each([
{ outputStructure: "locale-modules", emitTs: false },
// { outputStructure: "locale-modules", emitTs: false },
{ outputStructure: "locale-modules", emitTs: true },
{ outputStructure: "message-modules", emitTs: false },
// { outputStructure: "message-modules", emitTs: false },
// { outputStructure: "message-modules", emitTs: true },
] satisfies Array<ParaglideCompilerOptions>)(
"options",
async (compilerOptions) => {
const output = await compileProject({ project, compilerOptions });
describe("tree-shaking", () => {
test("should tree-shake unused messages", async () => {
const code = await bundleCode(
output,
`import * as m from "./paraglide/messages"
`import * as m from "./paraglide/messages.js"
console.log(m.sad_penguin_bundle())`
);
Expand All @@ -114,7 +115,7 @@ describe.each([
test("should not treeshake messages that are used", async () => {
const code = await bundleCode(
output,
`import * as m from "./paraglide/messages"
`import * as m from "./paraglide/messages.js"
console.log(
m.sad_penguin_bundle(),
Expand Down Expand Up @@ -145,8 +146,8 @@ describe.each([
// The compiled output needs to be bundled into one file to be dynamically imported.
const code = await bundleCode(
output,
`export * as m from "./paraglide/messages"
export * as runtime from "./paraglide/runtime"`
`export * as m from "./paraglide/messages.js"
export * as runtime from "./paraglide/runtime.js"`
);

// test is a direct result of a bug
Expand Down Expand Up @@ -288,8 +289,8 @@ describe.each([
});
const code = await bundleCode(
output,
`export * as m from "./paraglide/messages"
export * as runtime from "./paraglide/runtime"`
`export * as m from "./paraglide/messages.js"
export * as runtime from "./paraglide/runtime.js"`
);
const { m, runtime } = await importCode(code);

Expand Down Expand Up @@ -357,8 +358,8 @@ describe.each([

const code = await bundleCode(
output,
`export * as m from "./paraglide/messages"
export * as runtime from "./paraglide/runtime"`
`export * as m from "./paraglide/messages.js"
export * as runtime from "./paraglide/runtime.js"`
);
const { m, runtime } = await importCode(code);

Expand All @@ -385,14 +386,14 @@ describe.each([
});

for (const [fileName, code] of Object.entries(output)) {
if (fileName.endsWith(".js")) {
if (fileName.endsWith(".js") || fileName.endsWith(".ts")) {
project.createSourceFile(fileName, code);
}
}
project.createSourceFile(
"test.ts",
`
import * as runtime from "./runtime"
import * as runtime from "./runtime.js"
// --------- RUNTIME ---------
Expand Down Expand Up @@ -443,14 +444,14 @@ describe.each([
});

for (const [fileName, code] of Object.entries(output)) {
if (fileName.endsWith(".js")) {
if (fileName.endsWith(".js") || fileName.endsWith(".ts")) {
project.createSourceFile(fileName, code);
}
}
project.createSourceFile(
"test.ts",
`
import * as runtime from "./runtime"
import * as runtime from "./runtime.js"
// --------- RUNTIME ---------
Expand Down Expand Up @@ -504,14 +505,14 @@ describe.each([
});

for (const [fileName, code] of Object.entries(output)) {
if (fileName.endsWith(".js")) {
if (fileName.endsWith(".js") || fileName.endsWith(".ts")) {
project.createSourceFile(fileName, code);
}
}
project.createSourceFile(
"test.ts",
`
import * as m from "./messages"
import * as m from "./messages.js"
// --------- MESSAGES ---------
Expand Down Expand Up @@ -551,17 +552,19 @@ describe.each([
);

async function bundleCode(output: Record<string, string>, file: string) {
const bundle = await _rollup({
input: "main.js",
output: {
minifyInternalExports: false,
const bundle = await rolldown({
input: ["main.js"],
resolve: {
extensionAlias: {
".js": [".ts", ".js"],
},
},
plugins: [
// @ts-expect-error - rollup types are not up to date
virtual({
...Object.fromEntries(
Object.entries(output).map(([fileName, code]) => [
"paraglide/" + fileName.replace(".js", "").replace(".ts", ""),
"paraglide/" + fileName,
code,
])
),
Expand Down Expand Up @@ -737,8 +740,6 @@ for (const bundle of mockBundles) {
await insertBundleNested(project.db, bundle);
}

const output = await compileProject({ project });

function createBundleNested(args: {
id: string;
declarations?: Declaration[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const compileProject = async (args: {
}

for (const file in output) {
if (file.endsWith(".js")) {
if (file.endsWith(".js") || file.endsWith(".ts")) {
output[file] = `// @ts-nocheck\n${output[file]}`;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function generateLocaleModules(
fallbackMap: Record<string, string | undefined>,
emitTs: boolean
): Record<string, string> {

const fileExt = emitTs ? ".ts" : ".js";

const indexFile = [
"/* eslint-disable */",
'import { getLocale } from "./runtime.js"',
Expand All @@ -22,12 +25,10 @@ export function generateLocaleModules(
compiledBundles.map(({ bundle }) => bundle.code).join("\n"),
].join("\n");

const ending = emitTs ? ".ts" : ".js";

const output: Record<string, string> = {
["runtime" + ending]: createRuntime(settings, emitTs),
["registry" + ending]: createRegistry(emitTs),
["messages" + ending]: indexFile,
["runtime" + fileExt]: createRuntime(settings, emitTs),
["registry" + fileExt]: createRegistry(emitTs),
["messages" + fileExt]: indexFile,
};

// generate message files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export function generateMessageModules(
fallbackMap: Record<string, string | undefined>,
emitTs: boolean
): Record<string, string> {
const ending = emitTs ? ".ts" : ".js";
const fileExt = emitTs ? ".ts" : ".js";

const output: Record<string, string> = {
["runtime" + ending]: createRuntime(settings, emitTs),
["registry" + ending]: createRegistry(emitTs),
["runtime" + fileExt]: createRuntime(settings, emitTs),
["registry" + fileExt]: createRegistry(emitTs),
};

// messages index file
output["messages" + ending] = [
output["messages" + fileExt] = [
"/* eslint-disable */",
...compiledBundles.map(
({ bundle }) => `export * from './messages/${bundle.node.id}/index.js'`
Expand Down
39 changes: 16 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c640dfd

Please sign in to comment.