__dirname path not correct
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 275
- PR merge metrics
- No merged PRs in 30d
Description
In npm package `@tdewolff/minify`
index.js
```
import { createRequire } from "node:module";
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const require = createRequire(import.meta.url);
const __dirname = dirname(fileURLToPath(import.meta.url));
console.log(__dirname) // ------here i print path🤣🤣---------
export const { string, config, file } = require('node-gyp-build')(__dirname);
export const version = require('./package.json').version;
```
now i involve it directly by **test.js**
```
import { string } from '@tdewolff/minify';
const s = string('text/html', 'Some text')
```
print **__dirname**:
```
/Users/chen/github/webpack-tdewolff-minify/node_modules/@tdewolff/minify
```
-----------------------------------------------------------------------------------------------------------
Whereas if i use tsup, change **test.js** to **test.ts** ,the compiled test.ts code to dist
```
"use strict";// node_modules/tsup/assets/cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
// node_modules/@tdewolff/minify/index.js
var _module = require('module');
var _path = require('path');
var _url = require('url');
var require2 = _module.createRequire.call(void 0, importMetaUrl);
var __dirname = _path.dirname.call(void 0, _url.fileURLToPath.call(void 0, importMetaUrl));
console.log(__dirname, "__dirname");
var { string, config, file } = require2("node-gyp-build")(__dirname);
var version = require2("./package.json").version;
console.log(js);
```
print **__dirname** :
```
/Users/chen/github/webpack-tdewolff-minify/dist
```
the path is different,obvious, the path is not compiled correctly !!!!🥲
```
/Users/chen/github/webpack-tdewolff-minify/node_modules/@tdewolff/minify // without tsup
/Users/chen/github/webpack-tdewolff-minify/dist // with tsup
```
Contributor guide
Research direction
Start with the package entry point index.js and compare its importMeta-based __dirname handling with the compiled test.ts output in dist, including tsup's cjs_shims.js. Reproduce the differing paths using the shown test.js and test.ts examples, then determine what behavior tsup should preserve and verify that the bundled output resolves node-gyp-build and package.json from the intended location.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- build-system, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100