unbuild icon indicating copy to clipboard operation
unbuild copied to clipboard

"" Is imported by "", but could not be resolved – treating it as an external dependency.

Open ElYaiko opened this issue 1 year ago • 2 comments

Environment

2.0.0 Node 19

Reproduction

Describe the bug

When using unbuild I get:

"~~/utils/misc" is imported by "src/utils/intl.ts", but could not be resolved – treating it as an external dependency.

But it is defined in the tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ESNext"],
    "module": "esnext",
    "target": "esnext",
    "rootDir": "./",
    "baseUrl": "./src",
    "outDir": "./dist",
    "moduleResolution": "bundler",
    "moduleDetection": "force",
    "allowImportingTsExtensions": true,
    "noEmit": true,
    // "composite": true,
    "strict": true,
    "downlevelIteration": true,
    "skipLibCheck": true,
    "jsx": "react-jsx",
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noUnusedLocals": false,
    "allowJs": true,
    "paths": {
      "~~": ["."],
      "~~/*": ["./*"]
    },
    "types": [
      "bun-types" // add Bun global
    ]
  },
  // "include": [
  //   "src"
  // ],
  "exclude": [
    "dist"
  ]

Additional context

No response

Logs

No response

ElYaiko avatar May 07 '24 03:05 ElYaiko

unbuild does not use typescript to build your app. It uses rollup, so setting a path there doesn't tell rollup how to resolve it.

danielroe avatar May 07 '24 08:05 danielroe

Like what Daniel said you should tell rollup how to resolve alias.

// build.config.ts
import { URL, fileURLToPath } from 'node:url';
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
  alias: {
    '~~': fileURLToPath(new URL('./', import.meta.url)),
  },
  rollup: {
    inlineDependencies: true,
  },
});

Btw you still need inlineDependencies: true to use alias in unbuild, which was a bug fixed in #384 and released in v3.0.0-rc.2.

s3xysteak avatar Jul 18 '24 06:07 s3xysteak