Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: allow extend builder externals #53

Open
AndreyYolkin opened this issue Sep 25, 2022 · 10 comments
Open

feature request: allow extend builder externals #53

AndreyYolkin opened this issue Sep 25, 2022 · 10 comments

Comments

@AndreyYolkin
Copy link

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:

externals: [
'@nuxt/schema',
'@nuxt/schema-edge',
'@nuxt/kit',
'@nuxt/kit-edge',
'nuxt',
'nuxt-edge',
'nuxt3',
'vue',
'vue-demi'
],

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
Copy link
Member

pi0 commented Sep 26, 2022

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

@AndreyYolkin
Copy link
Author

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

@AndreyYolkin
Copy link
Author

AndreyYolkin commented Sep 28, 2022

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
Copy link

gluharry commented Apr 27, 2023

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
Copy link

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? 👀

@devonik
Copy link

devonik commented Aug 12, 2023

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
Copy link
Member

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
Copy link

devonik commented Aug 12, 2023

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
Copy link
Member

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants