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

在 ts 开启allowJS的情况下,默认仍会复制源 js 到 output,可能造成覆盖导致编译结果异常 #1

Closed
ktKongTong opened this issue May 30, 2024 · 0 comments · Fixed by #2

Comments

@ktKongTong
Copy link
Contributor

migrate from cordiverse/yakumo#19

从你的使用场景来看,或许应该改一下那个后缀名判断的条件,把 js 什么的也加进来。你觉得呢?

挺好的,也许改成可配置更好。

  const loaders = config.atsc?.loaders || ['.yaml', '.yml']
  const ignore = config.atsc?.ignore || ['.ts', '.mts', '.cts', '.tsx', '.mjs', '.cjs', '.js', '.jsx']

  const files = await globby(['**'], {
    cwd: resolve(cwd, rootDir),
    onlyFiles: true,
  })


  await Promise.all(files.map(async (file) => {
    const ext = extname(file)
    if (ignore.includes(ext)) return
    const src = resolve(cwd, rootDir, file)
    const dest = resolve(cwd, outDir, file)
    await fs.mkdir(dirname(dest), { recursive: true })
    if (!loaders.includes(ext)) {
      await fs.copyFile(src, dest)
    } else if (['.yaml', '.yml'].includes(ext)) {
      const data = yaml.load(await fs.readFile(src, 'utf8'))
      await fs.writeFile(dest.slice(0, -ext.length) + '.json', JSON.stringify(data) || '')
    }
  }))

另外我不是很明白这里 loaders 的意思,

atsc 在这里只对.yaml,.yml做了处理,即使在 loaders 中配置了其他项,其结果也是 ignore。

如果 loader 指:需要额外加载的文件后缀。也许由用户明确指出需要加载的项会更好,你怎么看?

if(loaders.includes(ext) && ['.yaml', '.yml'].includes(ext)) {
   const data = yaml.load(await fs.readFile(src, 'utf8'))
    await fs.writeFile(dest.slice(0, -ext.length) + '.json', JSON.stringify(data) || '')
}else if(loaders.includes(ext)) {
     await fs.copyFile(src, dest)
}

但是这样的代码又会引入新的问题,所有的 .yaml.yml 都将被处理为 json。

因此可以移除 loader(用 ingore 替代)。引入配置项,converters: ['.yaml', '.yml'],指示需要转换的项 yaml,yml

  const converters = config.atsc?.converters || ['.yaml', '.yml']
  const ignore = config.atsc?.ignore || ['.ts', '.mts', '.cts', '.tsx', '.mjs', '.cjs', '.js', '.jsx']

  const files = await globby(['**'], {
    cwd: resolve(cwd, rootDir),
    onlyFiles: true,
  })


  await Promise.all(files.map(async (file) => {
    const ext = extname(file)
    if (ignore.includes(ext)) return
    const src = resolve(cwd, rootDir, file)
    const dest = resolve(cwd, outDir, file)
    await fs.mkdir(dirname(dest), { recursive: true })
    if(converters.includes(ext) && ['.yml', '.yaml'].includes()) {
       const data = yaml.load(await fs.readFile(src, 'utf8'))
        await fs.writeFile(dest.slice(0, -ext.length) + '.json', JSON.stringify(data) || '')
    }else {
         await fs.copyFile(src, dest)
    }
  }))

你觉得呢?

既然话题转移到 atsc 上,那我就关闭 issue 和 pr,在 atsc 仓库新开好了。

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

Successfully merging a pull request may close this issue.

1 participant