module-builder icon indicating copy to clipboard operation
module-builder copied to clipboard

feature request: allow extend builder externals

Open AndreyYolkin opened this issue 1 year ago • 10 comments

Let's imagine that I'm creating some helpers which I want to use in my modules definition. Some of them so specific and never will be included in @nuxt/kit, but I use some kit methods to create them.

Just an example:

import { extendViteConfig } from "@nuxt/kit";
import { defu } from 'defu'

export const addProjectAlias = (alias, path) => {
  extendViteConfig(config => {
    const newConfig = defu(config, {
      resolve: {
        alias: {
          [alias]: path
        }
      }
    })
    config.resolve.alias = newConfig.resolve.alias
  })
}

This function relies on @nuxt/kit and defu and if I define it in module definition, module builds successfully, but after moving this snippet to another package (e.g. @yolkin/config) @nuxt/module-builder tries to inline lots of files, because @yolkin/config is not listed in externals: https://github.com/nuxt/module-builder/blob/6453ed0bc104f7ef167f9f16bc012968d1b9261d/src/build.ts#L31-L41

So, I want to have an ability to extend externals to have an ability to build my modules with extended functions without inlining the whole nuxt inside of it.

AndreyYolkin avatar Sep 25 '22 21:09 AndreyYolkin

Nice idea to support option. BTW have you tried adding a build.config to add custom externals?

pi0 avatar Sep 26 '22 10:09 pi0

No, because I didn't find any mentions about build.config. Gonna try and answer later about it 💪

AndreyYolkin avatar Sep 26 '22 13:09 AndreyYolkin

Nice idea to support option. BTW have you tried adding a build.config to add custom externals?

Yes, I added build.config.ts in the module package root and specified own externals in it. And it works. I think, that information about extra configuration should be provided in README

AndreyYolkin avatar Sep 28 '22 10:09 AndreyYolkin

I had a very similar issue that I have been tackling for the past several days. Defining the problematic module in build.config as an external helped me resolve it.

+1 for this to be provided in a README.

harryyaprakov avatar Apr 27 '23 12:04 harryyaprakov

Would it be possible to share an example build.config.ts configuration, where extra externals are added while we wait for it to be added to the README? :eyes:

madskronborg avatar May 24 '23 07:05 madskronborg

In my case the defu package was the issue and preventing me to release.

Add build.config.ts to your root directory and fill it with `import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({ externals: [ 'defu', ], }) `

You can also see the build.config.ts in the root of this project https://github.com/nuxt/module-builder/blob/main/build.config.ts

devonik avatar Aug 12 '23 15:08 devonik

The right solution in most cases is adding the packages concerned to your package.json as either dependencies or peerDependencies. If your module is going to use them or import them, they should be declared as dependencies.

Specifying externals manually is an escape hatch and should be used with caution.

danielroe avatar Aug 12 '23 16:08 danielroe

Ah thanks for the info. I just added defu to "dependencies" (it was missing somehow) and the build warning was gone withouth any extra build config 💯

devonik avatar Aug 12 '23 16:08 devonik

Here is an example of how to configure it in a build.config:

https://github.com/nuxt/nuxt/blob/main/packages/schema/build.config.ts#L21

danielroe avatar Aug 12 '23 16:08 danielroe

@danielroe Is there a way to tell the module-builder to transpile a dependency?

We have a monorepo with a utils package written in Typescript. One of the functions is imported from a .ts file into the module. When building the module this leads to an error. I'd like to explicitly include the file in the final bundle somehow, without having to bundle the utils package prior.

robinscholz avatar Dec 14 '23 09:12 robinscholz