|
| 1 | +# AGENTS.md - Symfony Webpack Encore |
| 2 | + |
| 3 | +This file provides guidance for AI coding agents working on this codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Symfony Webpack Encore is a JavaScript/Node.js library that wraps Webpack, providing a clean |
| 8 | +and powerful API for bundling JavaScript modules, pre-processing CSS & JS, and compiling assets. |
| 9 | + |
| 10 | +- **Package**: `@symfony/webpack-encore` |
| 11 | +- **License**: MIT |
| 12 | +- **Node.js**: `^22.13.0 || >=24.0` |
| 13 | + |
| 14 | +## Environment Setup |
| 15 | + |
| 16 | +**IMPORTANT**: Before running any command, ensure you are using the correct Node.js version: |
| 17 | + |
| 18 | +```bash |
| 19 | +nvm use 22 |
| 20 | +``` |
| 21 | + |
| 22 | +Then install dependencies: |
| 23 | + |
| 24 | +```bash |
| 25 | +yarn install |
| 26 | +``` |
| 27 | + |
| 28 | +## Build/Lint/Test Commands |
| 29 | + |
| 30 | +### Linting |
| 31 | + |
| 32 | +```bash |
| 33 | +yarn lint |
| 34 | +yarn lint --fix |
| 35 | +``` |
| 36 | + |
| 37 | +Runs ESLint on all source files (`lib/`, `test/`, `index.js`). |
| 38 | + |
| 39 | +### Testing |
| 40 | + |
| 41 | +```bash |
| 42 | +# Run all tests |
| 43 | +yarn test |
| 44 | + |
| 45 | +# Run main test suite (excludes persistent cache tests) |
| 46 | +yarn test:main |
| 47 | + |
| 48 | +# Run persistent cache tests only |
| 49 | +yarn test:persistent-cache |
| 50 | + |
| 51 | +# Run a single test file |
| 52 | +yarn mocha test/WebpackConfig.js |
| 53 | +yarn mocha test/loaders/sass.js |
| 54 | + |
| 55 | +# Run tests matching a pattern (grep) |
| 56 | +yarn mocha test --grep "setOutputPath" |
| 57 | + |
| 58 | +# Run functional tests only |
| 59 | +yarn mocha test/functional.js |
| 60 | +``` |
| 61 | + |
| 62 | +Test files mirror the source structure: `lib/loaders/sass.js` → `test/loaders/sass.js` |
| 63 | + |
| 64 | +## Code Style Guidelines |
| 65 | + |
| 66 | +### File Header (Required) |
| 67 | + |
| 68 | +Every JavaScript file MUST start with this header: |
| 69 | + |
| 70 | +```javascript |
| 71 | +/* |
| 72 | + * This file is part of the Symfony Webpack Encore package. |
| 73 | + * |
| 74 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 75 | + * |
| 76 | + * For the full copyright and license information, please view the LICENSE |
| 77 | + * file that was distributed with this source code. |
| 78 | + */ |
| 79 | + |
| 80 | +'use strict'; |
| 81 | +``` |
| 82 | + |
| 83 | +### Formatting Rules |
| 84 | + |
| 85 | +- **Indentation**: 4 spaces |
| 86 | +- **Quotes**: Single quotes (`'string'`) |
| 87 | +- **Semicolons**: Required |
| 88 | +- **Trailing whitespace**: Not allowed |
| 89 | +- **End of file**: Must have newline |
| 90 | +- **Line endings**: LF (Unix-style) |
| 91 | +- **Object braces**: Spaces inside (`{ key: value }`) |
| 92 | +- **Function parens**: No space before (`function()` not `function ()`) |
| 93 | +- **Equality**: Always use `===` and `!==` (eqeqeq) |
| 94 | + |
| 95 | +### Import Style (CommonJS) |
| 96 | + |
| 97 | +```javascript |
| 98 | +'use strict'; |
| 99 | + |
| 100 | +// Node.js built-ins first |
| 101 | +const path = require('path'); |
| 102 | +const fs = require('fs'); |
| 103 | + |
| 104 | +// External packages |
| 105 | +const webpack = require('webpack'); |
| 106 | +const semver = require('semver'); |
| 107 | + |
| 108 | +// Internal modules (relative paths) |
| 109 | +const WebpackConfig = require('./lib/WebpackConfig'); |
| 110 | +const logger = require('./logger'); |
| 111 | +const applyOptionsCallback = require('./utils/apply-options-callback'); |
| 112 | +``` |
| 113 | + |
| 114 | +### JSDoc Type Imports |
| 115 | + |
| 116 | +Use JSDoc `@import` for type information: |
| 117 | + |
| 118 | +```javascript |
| 119 | +/** |
| 120 | + * @import WebpackConfig from '../WebpackConfig' |
| 121 | + */ |
| 122 | + |
| 123 | +/** |
| 124 | + * @import webpack from 'webpack' |
| 125 | + */ |
| 126 | + |
| 127 | +/** |
| 128 | + * @import { OptionsCallback } from './utils/apply-options-callback.js' |
| 129 | + */ |
| 130 | +``` |
| 131 | + |
| 132 | +### Naming Conventions |
| 133 | + |
| 134 | +| Type | Convention | Examples | |
| 135 | +|------|------------|----------| |
| 136 | +| Files (general) | kebab-case | `config-generator.js`, `path-util.js` | |
| 137 | +| Files (classes) | PascalCase | `WebpackConfig.js`, `RuntimeConfig.js` | |
| 138 | +| Variables/Functions | camelCase | `webpackConfig`, `getLoaders()` | |
| 139 | +| Classes | PascalCase | `WebpackConfig`, `RuntimeConfig` | |
| 140 | +| Constants | UPPER_SNAKE_CASE | (rarely used) | |
| 141 | + |
| 142 | +### Method Naming Patterns |
| 143 | + |
| 144 | +- **Getters**: `getLoaders()`, `getContext()`, `getWebpackConfig()` |
| 145 | +- **Setters**: `setOutputPath()`, `setPublicPath()` |
| 146 | +- **Enablers**: `enableVersioning()`, `enableSassLoader()`, `enableVueLoader()` |
| 147 | +- **Configurers**: `configureBabel()`, `configureDefinePlugin()` |
| 148 | +- **Validators**: `validateRuntimeConfig()`, `validateNameIsNewEntry()` |
| 149 | +- **Booleans**: Prefix with `use`, `is`, `should`, `has`: |
| 150 | + - `useVersioning`, `useSourceMaps`, `isProduction()`, `shouldUseSingleRuntimeChunk` |
| 151 | + |
| 152 | +### Error Handling |
| 153 | + |
| 154 | +Throw descriptive errors with context: |
| 155 | + |
| 156 | +```javascript |
| 157 | +if (typeof callback !== 'function') { |
| 158 | + throw new Error('Argument 1 to configureBabel() must be a callback function or null.'); |
| 159 | +} |
| 160 | + |
| 161 | +// Include valid options in error messages |
| 162 | +throw new Error(`Invalid option "${optionKey}" passed to method(). Valid keys are ${Object.keys(validOptions).join(', ')}`); |
| 163 | +``` |
| 164 | + |
| 165 | +Use the logger for warnings, recommendations, and deprecations: |
| 166 | + |
| 167 | +```javascript |
| 168 | +const logger = require('./logger'); |
| 169 | + |
| 170 | +logger.warning('The value passed to setPublicPath() should usually start with "/"'); |
| 171 | +logger.recommendation('To create a smaller build, see https://symfony.com/doc/...'); |
| 172 | +logger.deprecation('The "https" option is deprecated, use "server" instead'); |
| 173 | +``` |
| 174 | + |
| 175 | +### Test Structure |
| 176 | + |
| 177 | +Tests use Mocha with Chai assertions: |
| 178 | + |
| 179 | +```javascript |
| 180 | +const expect = require('chai').expect; |
| 181 | +const WebpackConfig = require('../lib/WebpackConfig'); |
| 182 | + |
| 183 | +describe('WebpackConfig object', function() { |
| 184 | + describe('setOutputPath', function() { |
| 185 | + it('use absolute, existent path', function() { |
| 186 | + const config = createConfig(); |
| 187 | + config.setOutputPath(__dirname); |
| 188 | + expect(config.outputPath).to.equal(__dirname); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
| 192 | +``` |
| 193 | + |
| 194 | +Test helpers are in `test/helpers/`: |
| 195 | +- `setup.js`: `createWebpackConfig()`, `runWebpack()`, `createTestAppDir()` |
| 196 | +- `assert.js`: Custom assertions for webpack output |
| 197 | + |
| 198 | +## Directory Structure |
| 199 | + |
| 200 | +``` |
| 201 | +lib/ # Core library source |
| 202 | +├── config/ # Configuration parsing & validation |
| 203 | +├── loaders/ # Webpack loader configurations |
| 204 | +├── plugins/ # Webpack plugin configurations |
| 205 | +├── utils/ # Utility functions |
| 206 | +├── friendly-errors/ # Custom error handlers |
| 207 | +├── WebpackConfig.js # Main configuration class |
| 208 | +└── config-generator.js # Webpack config generator |
| 209 | +
|
| 210 | +test/ # Tests (mirrors lib/ structure) |
| 211 | +fixtures/ # Test fixtures |
| 212 | +``` |
| 213 | + |
| 214 | +## Contributing Guidelines |
| 215 | + |
| 216 | +- Always add tests and ensure they pass |
| 217 | +- Never break backward compatibility |
| 218 | +- Update CHANGELOG.md for new features/deprecations |
| 219 | +- Features must be submitted against the latest branch |
0 commit comments