unjs / unbuild

📦 A unified JavaScript build system
MIT License
2.3k stars 89 forks source link

It's unreasonable to trigger `Inlined implicit external` warning when the related alias was configured. #201

Open Plasticine-Yang opened 1 year ago

Plasticine-Yang commented 1 year ago

Environment

Node.js version: v18.12.1

Reproduction

https://github.com/Plasticine-Yang/plasticine-monitor

run pnpm build:browser can trigger the bug.

Describe the bug

It will trigger Inlined implicit external @/sdk when I don't config the alias for @, which is reasonable.

However, the @/sdk is a path alias point to packages/browser/src/sdk.ts, it is not a dependency package.

I also configured the paths option in tsconfig.json, but it is just for IDE and tsc to resolve the path, which is not related to the rollup under the unbuild.

So, it should be solved when I configure the alias @ pointing to packages/browser/src, but it still trigger the Inlined implicit external @/sdk warning.

I'm not sure whether it is a bug or a feature. The issue can be closed if it is a feature.

Additional context

The file imports a module with alias: packages/browser/src/plugins/error-monitor-plugin/index.ts:

import type { Plugin } from '@plasticine-monitor/core'

import { WebSDK } from '@/sdk'
import { monitorJSError } from './monitors/js-error'

/**
 * @description 监听运行时错误插件
 */
const errorMonitorPlugin = (): Plugin<WebSDK> => {
  return {
    install(webSDK) {
      monitorJSError(webSDK)
    },
  }
}

const __TEMP__ = WebSDK
console.log(__TEMP__)

export { errorMonitorPlugin }

The @/sdk points to packages/browser/src/sdk.ts.

build.config.ts

export default defineBuildConfig({
  entries: ['./src/index'],
  outDir: 'dist',
  clean: true,
  declaration: true,
  rollup: {
    emitCJS: true,
  },
  alias: {
    '@': resolve(__dirname, 'src'),
  },
})

image

Logs

No response

sechi747 commented 1 year ago

Same for me, have you resolved it?

Plasticine-Yang commented 1 year ago

Same for me, have you resolved it?

Not yet. No one responded to me, I could only cancel the alias.

sechi747 commented 1 year ago

Same for me, have you resolved it?

Not yet. No one responded to me, I could only cancel the alias.

I just add failOnWarn: false in build.config.ts, because the packaged files are correct. It just a warning not an error.

NamesMT commented 10 months ago

+1 simple aliases like ~/ for src/ also trigger "Inlined implicit external", this with the fact that alias breaks in stub mode and mkdist means unbuild doesn't support alias at all.

pi0 commented 10 months ago

It should not happen if aliases are configured to be bundled or externalized. Please provide a minimal reproduction (ie stackblitz/codesandbox).

NamesMT commented 10 months ago

Hello @pi0, heres a reproduction link:
https://codesandbox.io/p/github/NamesMT/starter-ts/draft/musing-albattani (run the reproduce script)

The warning is triggered using normal bundle mode and not mkdist, (but mkdist also breaks), alias is configured in both build.config.ts and tsconfig.json

Plasticine-Yang commented 10 months ago

Same for me, have you resolved it?

I just replace ungit with tsup.

gnclmorais commented 9 months ago

I ran into the same issue… :/

RIP21 commented 6 months ago

Even for simple aliases but with the mkdist builder it won't work. Let's say @lib alias to lib folder in case of mkdist doesn't work and stays @lib/whatever in the imports of the emitted files.

For anyone who is looking for a workaround, the only one I figured is to use vite in lib mode (config below). It's slower in case of the first run (13-15 seconds for my lib) (but very reliable as it uses tsc to emit declarations) but subsequent if you use vite build --watch is 8-9x faster. So just 2-3 seconds for my project.

// @ts-expect-error
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
import type { LibraryFormats, UserConfig } from 'vite'
import dts from 'vite-plugin-dts'
import noBundle from 'vite-plugin-no-bundle'
import tsconfigPaths from 'vite-tsconfig-paths'
import react from '@vitejs/plugin-react-swc'

export default defineConfig({
  {
    plugins: [
      tsconfigPaths(),
      react(),
      dts({ include: ['lib'], insertTypesEntry: true }),
      noBundle({
        root: './lib',
      }),
    ],
    build: {
      minify: false,
      sourcemap: true,
      copyPublicDir: false,
      reportCompressedSize: false,
      lib: {
        entry: {
          index: resolve(__dirname, 'lib/index.ts')
        },
        formats: ["es"],
      },
      rollupOptions: {
        plugins: [
          peerDepsExternal({
            includeDependencies: true,
          }),
        ],
      },
    },
  }
})
trandaison commented 6 months ago

@sechi747

I just add failOnWarn: false in build.config.ts, because the packaged files are correct. It just a warning not an error.

Can you explain a little bit more? I am facing with this problem with https://github.com/nuxt/module-builder (which is built on top of unbuild), but there is no documents that show how/where to set failOnWarn: false

sechi747 commented 6 months ago

@trandaison failOnWarn is a configurable option of unbuild. You can see its type definition here: https://github.com/unjs/unbuild/blob/main/src/types.ts#L87 But the nuxt/module-builder indeed has fixed build options and cannot be freely configured. You can see its build method here: https://github.com/nuxt/module-builder/blob/main/src/commands/build.ts#L34

trandaison commented 6 months ago

@sechi747 My current workaround solution:

// build.config.ts

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  failOnWarn: false,
});
HigherOrderLogic commented 3 months ago

Hi @pi0, is this issue on the roadmap? These false positive warnings are very annoying to work with.

olets commented 1 week ago

The same thing happens with package.json subpath imports https://nodejs.org/api/packages.html#subpath-imports

Here's a minimal reproduction https://stackblitz.com/edit/vitejs-vite-kwryzn?file=src%2Findex.ts

Lmk if I should open a new issue.

xsjcTony commented 3 hours ago

Indeed it's happening for my project as well. https://github.com/xsjcTony/remark-magic-link This should definitely be fixed as it forces you to set failOnWarn: false which is not safe.

xsjcTony commented 3 hours ago

There's seems to be the same issue before, and it's fixed https://github.com/unjs/unbuild/issues/383. Checked the release note, maybe try v3.0.0-rc2? https://github.com/unjs/unbuild/releases/tag/v3.0.0-rc.2