nuxt / module-builder

Complete solution to build and ship Nuxt modules.
MIT License
236 stars 25 forks source link

feature request: allow extend builder externals #53

Open AndreyYolkin opened 2 years ago

AndreyYolkin commented 2 years ago

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.

pi0 commented 2 years ago

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

AndreyYolkin commented 2 years ago

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

AndreyYolkin commented 2 years ago

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

gluharry commented 1 year ago

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.

madskronborg commented 1 year ago

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:

devonik commented 1 year ago

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

danielroe commented 1 year ago

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.

devonik commented 1 year ago

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 💯

danielroe commented 1 year ago

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

robinscholz commented 11 months ago

@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.