3

what I am trying to achieve

I am trying to synchronise locale files from a yarn workspace to multiple Vue apps in a monorepo so they can be used with i18next in each app. They need to be:

  • kept in sync during development
  • automatically updated when the translation files get updated
  • have them eventually in the dist folder so that they get deployed with the rest of the app (which should happen automatically when the files are in the public folder)

In order to keep the bundle size small, I can't just bundle the whole package - the files need to be copied individually with their original file names and stored in the public folder of each app and the UI library.

the problem

I am trying to configure CopyWebpackPlugin, but I am either

  • just getting an initial copy from translation/locales to public/locales on starting up the dev server
  • or the dev server ends up in a loop when I try to enable the writeToDisk option, plus it starts flooding the dist folder with hot reload files.

my vue.config.js *

module.exports = {
  devServer: {
    writeToDisk: true,
  },
  configureWebpack: {
    plugins: [
      new CopyPlugin({
        patterns: [
          {
            from: `${path.dirname(
              require.resolve(`@namespace/translations/package.json`)
            )}/locales`,
            to: "./public/locales",
            toType: "dir",
          },
        ],
      }),
    ],
  },

*based on instructions from https://webpack.js.org/plugins/copy-webpack-plugin/, it includes a reference to yarn workspaces

running yarn serve with this config results in a loop. The correct files are copied to the ./public folder, but at the same time, it creates the ./dist folder and floods it with ...hot-update.json files.

if I run yarn build the first time, the locale files get copied to the ./public folder, but not to the ./dist folder (so it seems it copies the files at the end of the process, so the latest files aren't included in the ./dist folder

current folder structure

Monorepo
└── packages
    ├── applications
    │   ├── app1
    │   │   ├── public
    │   │   └── dist
    │   ├── app2
    │   └── ...
    └── common
        ├── translations
        │   └── locales
        │       ├── en-GB
        │       │   └── common.json
        │       └── de-DE
        ├── ui
        └── ...

versions

@vue/cli 4.5.12
webpack@4.46.0
copy-webpack-plugin@6.4.1

Any help with getting this setup to work would be very much appreciated.

Thomas Kuhlmann
  • 943
  • 7
  • 18
  • 2
    Have you found a solution to this yet as I have this problem too? I haven't been able to find other real world examples of how to implement what the docs state in your link. – god_is_love Oct 29 '21 at 02:37
  • 1
    @god_is_love I actually ended up going down a different path - I used Chokidar, concurrently and rsync, i.e. ```"sync:ui": "rsync -r -I locales ../ui/public"``` for the actual sync, ```"sync:all": "concurrently \"yarn sync:ui\" \"yarn sync:web\" \"yarn sync:cloud\"",``` for starting multiple syncs in parallel, ```"chokidar": "chokidar \"locales/**/*.json\" -c \"yarn sync:all\"``` for watching the files, and vscode task to start it – Thomas Kuhlmann Nov 05 '21 at 12:31

0 Answers0