Skip to content

Using .postcssrc.ts results into transpilation error #14758

@ElPrudi

Description

@ElPrudi

Describe the bug

Using the typescript format for a postcss config, ts-node does not respect the tsconfig.json target and transpiles the import syntax to require() resulting into an error with "type": "module" in package.json.

Workaround: change it from .postcssrc.ts to .postcssrc.js

Reproduction

https://stackblitz.com/edit/vitejs-vite-l8rdtt

Steps to reproduce

Just use this config in a file called .postcssrc.ts:

import type { Config } from 'postcss-load-config'
import cssnano from 'cssnano'

export default ({ env }): Config => ({
    plugins: [
        cssnano({
            preset: 'default'
        })
    ]
})

System Info

vite version 4.5.0
node: 18.17.1

Used Package Manager

npm

Logs

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ...\.postcssrc.ts
require() of ES modules is not supported.
require() of ...\.postcssrc.ts from ...\node_modules\vite\dist\node\chunks\dep-bb8a8339.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from ...\package.json.

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions