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

Enable ESM and TS based config files #10785

Merged
merged 44 commits into from Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
25716e7
add `jiti` and `detective-typescript` dependencies
RobinMalfait Mar 13, 2023
e1bc353
use `jiti` and `detective-typescript`
RobinMalfait Mar 13, 2023
f1614c8
use `@swc/core` instead of the built-in `babel` form `jiti`
RobinMalfait Mar 13, 2023
dff3dc9
update changelog
RobinMalfait Mar 13, 2023
de52eb7
add `jiti` and `detective-typescript` dependencies to `stable`
RobinMalfait Mar 14, 2023
ca82433
use `sucrase` to transform the configs
RobinMalfait Mar 14, 2023
44fffbb
add `sucrase` dependency to `stable` engine
RobinMalfait Mar 14, 2023
ff9540c
make loading the config easier
RobinMalfait Mar 14, 2023
96f683e
use abstracted loading config utils
RobinMalfait Mar 14, 2023
3a4a421
WIP: make `load` related files public API
RobinMalfait Mar 14, 2023
59f2594
use new config loader in PostCSS plugin
RobinMalfait Mar 14, 2023
08909e0
add list of default config files to look for
RobinMalfait Mar 14, 2023
7498f39
cleanup unused arguments
RobinMalfait Mar 14, 2023
90e3d8f
find default config path when using CLI
RobinMalfait Mar 14, 2023
ebec943
improve `init` command
RobinMalfait Mar 14, 2023
ddacbc8
make eslint happy
RobinMalfait Mar 14, 2023
b9dfa14
keep all files in `stubs` folder
RobinMalfait Mar 14, 2023
30cf66f
add `tailwind.config.js` stub file
RobinMalfait Mar 14, 2023
b3c6086
Initialize PostCSS config using the same format as Tailwind config
adamwathan Mar 14, 2023
33c3b04
Rename config content stubs to config.*.js
adamwathan Mar 14, 2023
13b03d7
Improve option descriptions for init options
adamwathan Mar 14, 2023
8283c2d
Remove unused code, remove `constants` file
adamwathan Mar 14, 2023
9604485
Fix TS warning
adamwathan Mar 14, 2023
76296e7
apply CLI changes to the Oxide version
RobinMalfait Mar 14, 2023
1ec55a2
update `--help` output in CLI tests
RobinMalfait Mar 14, 2023
f114cb0
WIP: make tests work on CI
RobinMalfait Mar 14, 2023
2367d02
wip
thecrypticace Mar 15, 2023
74d51f6
remove unused `fs`
RobinMalfait Mar 15, 2023
5f83b57
Fix init tests
thecrypticace Mar 15, 2023
8435352
bump `napi-derive`
RobinMalfait Mar 15, 2023
eeb8eb4
list extensions we are interested in
RobinMalfait Mar 15, 2023
407ab20
no-op the `removeFile` if file doesn't exist
RobinMalfait Mar 15, 2023
cbfae98
ensure all `init` flags work
RobinMalfait Mar 15, 2023
6813c0c
ensure we cleanup the new files
RobinMalfait Mar 15, 2023
1fd103f
test ESM/CJS generation based on package.json
RobinMalfait Mar 15, 2023
3254069
remove unnecessary test
RobinMalfait Mar 15, 2023
e3102e8
only look for `TypeScript` files when the entryFile is `TypeScript` a…
RobinMalfait Mar 15, 2023
eaac06c
refactor `load` to be `loadConfig`
RobinMalfait Mar 15, 2023
9fb6160
use the `config.full.js` stub instead of the `defaultConfig.stub.js` …
RobinMalfait Mar 15, 2023
2e385d5
apply `load` -> `loadConfig` changes to `Oxide` engine CLI
RobinMalfait Mar 15, 2023
9bcf8ba
ensure we write the config file in the Oxide engine
RobinMalfait Mar 15, 2023
444b526
improve type in Oxide engine CLI
RobinMalfait Mar 15, 2023
f40fdc4
catch errors instead of checking if the file exists
RobinMalfait Mar 15, 2023
e853901
ensure we publish the correct stub files
RobinMalfait Mar 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `justify-normal` and `justify-stretch` utilities ([#10560](https://github.com/tailwindlabs/tailwindcss/pull/10560))
- Add `content-normal` and `content-stretch` utilities ([#10645](https://github.com/tailwindlabs/tailwindcss/pull/10645))
- Add `line-clamp` utilities from `@tailwindcss/line-clamp` to core ([#10768](https://github.com/tailwindlabs/tailwindcss/pull/10768))
- Enable ESM and TS based config files ([#10785](https://github.com/tailwindlabs/tailwindcss/pull/10785))

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion integrations/execute.js
Expand Up @@ -26,7 +26,7 @@ module.exports = function $(command, options = {}) {
let args = options.shell
? [command]
: (() => {
let args = command.split(' ')
let args = command.trim().split(/\s+/)
command = args.shift()
command =
command === 'node'
Expand Down
6 changes: 4 additions & 2 deletions integrations/io.js
Expand Up @@ -97,10 +97,12 @@ module.exports = function ({
},
async removeFile(file) {
let filePath = path.resolve(toolRoot, file)

if (!fileCache[filePath]) {
fileCache[filePath] = await fs.readFile(filePath, 'utf8')
fileCache[filePath] = await fs.readFile(filePath, 'utf8').catch(() => null)
}
await fs.unlink(filePath)

await fs.unlink(filePath).catch(() => null)
},
async readOutputFile(file) {
file = await resolveFile(file, absoluteOutputFolder)
Expand Down
82 changes: 62 additions & 20 deletions integrations/tailwindcss-cli/tests/cli.test.js
Expand Up @@ -532,6 +532,46 @@ describe('Build command', () => {
})

describe('Init command', () => {
it.each([
{ flags: [], name: 'tailwind.config.js' },
{ flags: ['--ts'], name: 'tailwind.config.ts' },
{ flags: ['--esm'], name: 'tailwind.config.js' },
{ flags: ['--full'], name: 'tailwind.config.js' },
{ flags: ['--ts', '--full'], name: 'tailwind.config.ts' },
{ flags: ['--esm', '--full'], name: 'tailwind.config.js' },
])('works with all these flags: %j', async ({ flags, name }) => {
cleanupFile(name)
await removeFile(name)

let { combined } = await $(`${EXECUTABLE} init ${flags.join(' ')}`)

expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: ${name}
"
`)

expect(await fileExists(name)).toBe(true)

let content = await readOutputFile(`../${name}`)

if (flags.includes('--ts') || flags.includes('--esm')) {
expect(content).toContain('export default')
expect(content).not.toContain('module.exports =')
} else {
expect(content).toContain('module.exports =')
expect(content).not.toContain('export default')
}

if (flags.includes('--ts')) {
expect(content).toContain('satisfies Config')
}

if (flags.includes('--full')) {
expect(content.split('\n').length).toBeGreaterThan(50)
}
})

test('--full', async () => {
cleanupFile('full.config.js')

Expand Down Expand Up @@ -577,14 +617,19 @@ describe('Init command', () => {
tailwindcss init [options]

Options:
-f, --full Initialize a full \`tailwind.config.js\` file
--esm Initialize configuration file as ESM
--ts Initialize configuration file as TypeScript
-p, --postcss Initialize a \`postcss.config.js\` file
-f, --full Include the default values for all options in the generated configuration file
-h, --help Display usage information
`)
)
})

test('--help in ESM package', async () => {
test('ESM config is created by default in an ESM project', async () => {
cleanupFile('tailwind.config.js')
await removeFile('tailwind.config.js')

let pkg = await readOutputFile('../package.json')

await writeInputFile(
Expand All @@ -595,50 +640,47 @@ describe('Init command', () => {
})
)

let { combined } = await $(`${EXECUTABLE} init --help`)
let { combined } = await $(`${EXECUTABLE} init`)

expect(dedent(combined)).toEqual(
dedent(`
tailwindcss v${version}
expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: tailwind.config.js
"
`)

Usage:
tailwindcss init [options]
expect(await fileExists('./tailwind.config.js')).toBe(true)

Options:
-f, --full Initialize a full \`tailwind.config.cjs\` file
-p, --postcss Initialize a \`postcss.config.cjs\` file
-h, --help Display usage information
`)
)
// Not a clean way to test this.
expect(await readOutputFile('../tailwind.config.js')).toContain('export default')

await writeInputFile('../package.json', pkg)
})

test('cjs config created when in ESM package', async () => {
cleanupFile('tailwind.config.cjs')
test('CJS config is created by default in a non-ESM project', async () => {
cleanupFile('tailwind.config.js')
await removeFile('tailwind.config.js')

let pkg = await readOutputFile('../package.json')

await writeInputFile(
'../package.json',
JSON.stringify({
...JSON.parse(pkg),
type: 'module',
})
)

let { combined } = await $(`${EXECUTABLE} init`)

expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: tailwind.config.cjs
Created Tailwind CSS config file: tailwind.config.js
"
`)

expect(await fileExists('./tailwind.config.cjs')).toBe(true)
expect(await fileExists('./tailwind.config.js')).toBe(true)

// Not a clean way to test this.
expect(await readOutputFile('../tailwind.config.cjs')).toContain('module.exports =')
expect(await readOutputFile('../tailwind.config.js')).toContain('module.exports')

await writeInputFile('../package.json', pkg)
})
Expand Down
4 changes: 4 additions & 0 deletions loadConfig.d.ts
@@ -0,0 +1,4 @@
import type { Config } from './types/config'

declare function loadConfig(path: string): Config
export = loadConfig
RobinMalfait marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions loadConfig.js
@@ -0,0 +1,2 @@
let loadConfig = require('./lib/public/load-config')
module.exports = (loadConfig.__esModule ? loadConfig : { default: loadConfig }).default
21 changes: 15 additions & 6 deletions oxide/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion oxide/crates/node/Cargo.toml
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]
[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.10.0", default-features = false, features = ["napi4"] }
napi-derive = "2.9.1"
napi-derive = "2.11.1"
tailwindcss-core = { path = "../core" }
rayon = "1.5.3"

Expand Down