tailwindcss/integrations/oxide/postinstall.test.ts
Philipp Spiess 47bb007eae
Download platform specific package if optionalDependencies are skipped (#17929)
Closes #15806

This PR adds a new `postinstall` script to `@tailwindcss/oxide` that
will attempt to download the platform-specific optional dependency to
avoid issues when the package manager does not do that automatically
(see #15806). The implementation for this is fairly simple: The npm
package is downloaded from the official npm servers and extracted into
the `node_modules` directory of the `@tailwidncss/oxide` installation.

## Test plan 

Since we still lack a solid repro of #15806, the way I tested this
change was to manually remove all automatically-installed optional
dependencies and then running the postinstall script manually. The
script then downloads the right version package which makes the import
to `@tailwidncss/oxide` work. An appropriate integration test was added
too.

I furthermore also validated that:

- This works across CI platforms [ci-all]
- The postinstall script bails out when running `pnpm install` in the
dev setup. This is necessary since doing the initial install won't have
any binary dependencies yet so it would download invalid versions from
npm (as the version numbers locally refer to the last released version).
We can safely bail out here though since this was never an issue with
local development.
- The postinstall script does not do anything when the
`@tailwindcss/oxide` library is added _unless_ the issue is detected.

[ci-all]

---------

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2025-05-09 14:55:02 +02:00

74 lines
2.3 KiB
TypeScript

import fs from 'node:fs/promises'
import path from 'node:path'
import { js, json, test } from '../utils'
test(
'@tailwindcss/oxide will fail when architecture-specific packages are missing',
{
fs: {
'package.json': json`
{
"dependencies": {
"@tailwindcss/oxide": "workspace:^"
}
}
`,
'test.js': js`
try {
let Scanner = require('@tailwindcss/oxide')
console.log('SUCCESS: @tailwindcss/oxide loaded successfully', Scanner)
} catch (error) {
console.log('FAILURE: Failed to load @tailwindcss/oxide:', error.message)
}
`,
},
},
async ({ exec, root, expect, fs }) => {
await removePlatformSpecificExtensions(path.join(root, 'node_modules'))
// Get last published version
let version = (await exec('npm show @tailwindcss/oxide version')).trim()
// Ensure that we don't depend on a specific version number in the download
// script in case we bump the version number in the repository and CI is run
// before a release
let packageJson = JSON.parse(await fs.read('node_modules/@tailwindcss/oxide/package.json'))
packageJson.version = version
await fs.write(
'node_modules/@tailwindcss/oxide/package.json',
JSON.stringify(packageJson, null, 2),
)
let opts = {
// Ensure that we don't include any node paths from the test runner
env: { NODE_PATH: '' },
}
expect(await exec('node test.js', opts)).toMatch(/FAILURE/)
// Now run the post-install script
await exec('node node_modules/@tailwindcss/oxide/scripts/install.js', opts)
expect(await exec('node test.js', opts)).toMatch(/SUCCESS/)
},
)
async function removePlatformSpecificExtensions(directory: string) {
let entries = await fs.readdir(directory, { withFileTypes: true })
for (let entry of entries) {
let fullPath = path.join(directory, entry.name)
if (entry.name.startsWith('oxide-')) {
if (entry.isSymbolicLink()) {
await fs.unlink(fullPath)
} else if (entry.isFile()) {
await fs.unlink(fullPath)
} else if (entry.isDirectory()) {
await fs.rm(fullPath, { recursive: true, force: true })
}
} else if (entry.isDirectory()) {
await removePlatformSpecificExtensions(fullPath)
}
}
}