ZachJW34 / nx-plus

Collection of Nx Community Plugins
MIT License
300 stars 52 forks source link

Is there a way to not build the commonJS version of a buildable library? #250

Closed gregveres closed 2 years ago

gregveres commented 2 years ago

Description

I am converting a monolithic Vue app over to Nx. It will end breaking the app into 4 apps with hundreds of shared libraries. The reason there are so many shared libraries is because, from what I have found out so far, any dynamically loaded component needs to live in its own library. My entire app is dynamically loaded through the router.

What I have noticed is that when doing a build, three versions of the library are built: commonJS umd umd-min

Is there a way to not build the commonJS version? I don't see that I need the commonJS version. In fact, I don't see why I need the umd version. Shouldn't it only build the umd-min version?

I thought this might be something that Nx is doing, but the executor is @nx-plus/vue/:library.

    "build": {
      "executor": "@nx-plus/vue:library",
      "options": {
        "dest": "dist/libs/domain/box-league/dynamic/tabs/admin/next-session",
        "entry": "libs/domain/box-league/dynamic/tabs/admin/next-session/src/index.ts",
        "tsConfig": "libs/domain/box-league/dynamic/tabs/admin/next-session/tsconfig.lib.json"
      }
    },
ZachJW34 commented 2 years ago

You can specify what formats you want to build, we have a test for it.

So you should be able to run nx build my-lib --formats umd-min to only output the minified version.

gregveres commented 2 years ago

ah thanks. I didn't see that.

gregveres commented 2 years ago

@ZachJW34 I am trying to figure out the correct command to do a production build. I don't recall where I found it, but this is the command that I am using:

"scripts": {
    "build": "nx run-many --target=build --all --parallel=2 --formats umd-min",
    "production": "nx build ui --prod --with-deps --parallel=3 --formats umd-min",
  },

for production, I run yarn production. This is now taking 23 minutes after having moved half of the app into libraries. So I started investigating and realized that the production build was building all three versions (commonJs, umd and umd-min) of the libraries. So I added the --formats umd-min. But it doesn't seem to be working. I looked at the Nx cache folder and I see the build products for all three formats and the build log shows that all three formats are being created.

Would you be able to share your production build command?

gregveres commented 2 years ago

And the when I do this, the vue app project fails because it doesn't recognize --formats as an option.

gregveres commented 2 years ago

OK, here is what I am doing now. I got the build back down to 7 1/2 minutes. I think that is as fast as I am going to get it on my system before moving to vue 3 and vite.

I am using this production script:

"scripts": {
    "production": "nx build ui --prod --with-deps --parallel=4",
  },

the parallel 4 keeps my 16 core cpu at 100% for most of the run. I then added "formats": "udm" to every one of my libraries' project.json file. This cut the build to 1/3 the time. And udm was about 40 seconds faster than doing udm-min, which seems to make sense since it is doing less work. The final app build does a minimization step anyway.

There seemed to be no difference in performance between commonjs and udm.

If there is a better way, please let me know. This works for me for now.