ncc icon indicating copy to clipboard operation
ncc copied to clipboard

weired path.resolve with dirname

Open Sczlog opened this issue 3 years ago • 3 comments

code before package:

const createFile = ({ path, fileName, content, withPrefix }) =>
  fs.writeFileSync(
    resolve(__dirname, path, `./${fileName}`),
    `${withPrefix ? filePrefix : ""}${content}`,
    _.noop,
  );

code after package

const createFile = ({ path, fileName, content, withPrefix }) =>
  fs.writeFileSync(
    __nccwpck_require__.ab + "src/" + path + '\\' + fileName,
    `${withPrefix ? filePrefix : ""}${content}`,
    _.noop,
  );

if path is an absolute path, the behavior will not as same as code before tranformation

Sczlog avatar Jan 18 '22 06:01 Sczlog

I can't reproduce with @vercel/[email protected]

I had to add the missing variables fs and _ but it doesn't transform resolve like you suggested.

Input

const fs = require('fs')
const _ = { noop: () => {} }
const createFile = ({ path, fileName, content, withPrefix }) =>
  fs.writeFileSync(
    resolve(__dirname, path, `./${fileName}`),
    `${withPrefix ? filePrefix : ""}${content}`,
    _.noop,
  );

Output

(() => {
const fs = __nccwpck_require__(147)
const _ = { noop: () => {} }
const createFile = ({ path, fileName, content, withPrefix }) =>
  fs.writeFileSync(
    resolve(__dirname, path, `./${fileName}`),
    `${withPrefix ? filePrefix : ""}${content}`,
    _.noop,
  );
})();

styfle avatar Feb 04 '22 16:02 styfle

@styfle It work well when I write the code directly, just like you mentioned, but if the code is from a dependency it become what i have write, I am using https://github.com/acacode/swagger-typescript-api, and not sure about the root cause.

Sczlog avatar Mar 11 '22 06:03 Sczlog

Can you share the steps to reproduce?

styfle avatar Mar 19 '22 02:03 styfle