maizzle / framework

Quickly build HTML emails with Tailwind CSS.
https://maizzle.com
MIT License
1.24k stars 49 forks source link

Preview in the browser does not get updated even after beforeCreate is being called #1344

Closed ASHFAQPATWARI closed 1 month ago

ASHFAQPATWARI commented 2 months ago

I am trying to add i18next to the maizzle config. I have added a translations.json file and added it to server watch array. My code to add i18next looks like this

import i18next from "i18next";

  /** @type {import('@maizzle/framework').Config} */
  export default {
    t: i18next.t,
    lang: "en",
    build: {
      content: ["src/templates/**/*.html"],
      static: {
        source: ["src/images/**/*.*"],
        destination: "images",
      },
    },
    server: {
      port: 8080,
      reportFileSize: true,
      watch: ["./src/images/translations.json"],
    },
    async beforeCreate({ config }) {
      try {
        // TODO: setting config should work from here as expected. Commenting code here as config is coming as undefined
        // config.language = process.env.TEMPLATELANGUAGE || "en";
        console.log(
          "------------------------------------- running beforecreate --------------------------------"
        );

      const translations = await import("./src/images/translations.json", {
        assert: { type: "json" },
      });
      const lang = process.env.TEMPLATELANGUAGE || "en";
      // Await is important here to not let build advance before i18next has had time to finish initialization.
      await i18next.init({
        lng: lang,
        fallbackLng: "en",
        debug: false,
        resources: translations.default,
      });
    } catch (error) {
      console.log("error in config.js beforeCreate", error);
    }
  },
  async beforeRender({ html, config, matter }) {
    console.log("process.env ", process.env.TEMPLATELANGUAGE);
    config.language = process.env.TEMPLATELANGUAGE || "en";
  },
};

ISSUE 1 - However on changing the translations file, preview in browser is not refreshed not even on refresh. I need to start the server again to see the changes. ISSUE 2 - config in beforeCreate function is coming as undefined therefore we cant add anything to it

ASHFAQPATWARI commented 2 months ago

@cossssmin Can you suggest a better approach for doing localization in Maizzle.

cossssmin commented 2 months ago

beforeCreate runs only once, try dynamically importing the JSON in beforeRender. I'll look into issue 2, thanks for letting me know 👍

cossssmin commented 1 month ago

Fixed the beforeCreate issue with the config, will be available in next beta. Thanks!