161 lines
3.4 KiB
Markdown
161 lines
3.4 KiB
Markdown
# Yarn Spinner Loader
|
|
|
|
Load and compile Yarn Spinner project files (`.yarnproject`) for various build tools (esbuild, rollup, webpack, vite).
|
|
|
|
## Features
|
|
|
|
- ✅ Parse and validate `.yarnproject` files against the official JSON schema
|
|
- ✅ Load and compile `.yarn` dialogue files using glob patterns
|
|
- ✅ Support for multiple build tools (esbuild, rollup, webpack, vite)
|
|
- ✅ TypeScript support with full type definitions
|
|
- ✅ Comprehensive test coverage with real fixtures
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install yarn-spinner-loader
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Core API
|
|
|
|
```typescript
|
|
import { loadYarnProject } from 'yarn-spinner-loader';
|
|
|
|
const result = await loadYarnProject('path/to/project.yarnproject');
|
|
|
|
console.log(result.project.projectName);
|
|
console.log(result.program); // Merged IRProgram from all .yarn files
|
|
```
|
|
|
|
### With esbuild
|
|
|
|
```typescript
|
|
import { build } from 'esbuild';
|
|
import { yarnSpinnerPlugin } from 'yarn-spinner-loader/esbuild';
|
|
|
|
build({
|
|
entryPoints: ['src/index.ts'],
|
|
plugins: [yarnSpinnerPlugin()],
|
|
bundle: true,
|
|
outfile: 'dist/bundle.js',
|
|
});
|
|
```
|
|
|
|
### With Vite
|
|
|
|
```typescript
|
|
// vite.config.ts
|
|
import { defineConfig } from 'vite';
|
|
import { yarnSpinnerVite } from 'yarn-spinner-loader/vite';
|
|
|
|
export default defineConfig({
|
|
plugins: [yarnSpinnerVite()],
|
|
});
|
|
```
|
|
|
|
### With Rollup
|
|
|
|
```typescript
|
|
// rollup.config.js
|
|
import { yarnSpinnerRollup } from 'yarn-spinner-loader/rollup';
|
|
|
|
export default {
|
|
input: 'src/main.js',
|
|
plugins: [yarnSpinnerRollup()],
|
|
output: {
|
|
file: 'dist/bundle.js',
|
|
format: 'es',
|
|
},
|
|
};
|
|
```
|
|
|
|
### With Webpack
|
|
|
|
```javascript
|
|
// webpack.config.js
|
|
module.exports = {
|
|
module: {
|
|
rules: [
|
|
{
|
|
test: /\.yarnproject$/,
|
|
use: 'yarn-spinner-loader/webpack',
|
|
},
|
|
],
|
|
},
|
|
};
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### `loadYarnProject(path, options?)`
|
|
|
|
Load and compile a `.yarnproject` file and all its referenced `.yarn` files.
|
|
|
|
**Parameters:**
|
|
- `path`: Path to the `.yarnproject` file
|
|
- `options.baseDir`: Base directory for resolving glob patterns (default: directory of `.yarnproject` file)
|
|
|
|
**Returns:** `Promise<LoadResult>`
|
|
|
|
```typescript
|
|
interface LoadResult {
|
|
project: YarnProject;
|
|
baseDir: string;
|
|
program: IRProgram;
|
|
}
|
|
```
|
|
|
|
### `loadYarnProjectSync(path, options?)`
|
|
|
|
Synchronous version of `loadYarnProject`.
|
|
|
|
### `validateYarnProject(config)`
|
|
|
|
Validate a parsed `.yarnproject` object against the JSON schema.
|
|
|
|
**Returns:**
|
|
```typescript
|
|
{ valid: true; data: YarnProject } | { valid: false; errors: ValidationError[] }
|
|
```
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
yarn-spinner-loader/
|
|
├── src/
|
|
│ ├── loader/ # Core loader implementation
|
|
│ │ ├── index.ts # YarnProjectLoader
|
|
│ │ ├── validator.ts # JSON Schema validation
|
|
│ │ └── types.ts # TypeScript types
|
|
│ ├── plugins/ # Build tool plugins
|
|
│ │ ├── esbuild.ts
|
|
│ │ ├── rollup.ts
|
|
│ │ ├── vite.ts
|
|
│ │ └── webpack.ts
|
|
│ ├── yarn-spinner/ # Yarn Spinner parser (existing)
|
|
│ └── index.ts # Main entry point
|
|
├── tests/
|
|
│ ├── fixtures/ # Test fixtures
|
|
│ │ ├── simple/
|
|
│ │ ├── localised/
|
|
│ │ └── complex/
|
|
│ ├── validator.test.ts
|
|
│ └── loader.test.ts
|
|
└── package.json
|
|
```
|
|
|
|
## Testing
|
|
|
|
Run tests with vitest:
|
|
|
|
```bash
|
|
npm test # Watch mode
|
|
npm run test:run # Run once
|
|
```
|
|
|
|
## License
|
|
|
|
MIT
|