Files
next.js/test/e2e/app-dir/webpack-loader-resource-query/webpack-loader-resource-query.test.js
Tobias KoppersandGitHub 7e9d7cbd60 Turbopack: add support for matching loaders on resource queries (#88644)
### What?

Part of  PACK-1007

Adds `condition.query` support to Turbopack's loader rules, enabling loaders to match based on import query strings (e.g., `import './file?raw'`). This mirrors webpack's `resourceQuery` functionality.

The `query` condition accepts either a string (exact match) or RegExp (pattern match).

Adds tests and documentation for the new `condition.query` option.

### How to use

```js
// next.config.js
module.exports = {
  turbopack: {
    rules: {
      '*.txt': [
        // String: exact match
        { condition: { query: '?raw' }, loaders: ['raw-loader'], as: '*.js' },
        // RegExp: pattern match  
        { condition: { query: /\?transform/ }, loaders: ['transform-loader'], as: '*.js' },
      ],
    },
  },
}
```
2026-01-16 21:12:37 +01:00

29 lines
740 B
JavaScript

import { nextTestSetup } from 'e2e-utils'
describe('webpack-loader-resource-query', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) return
it('should pass query to loader', async () => {
await next.render$('/')
expect(next.cliOutput).toContain('resource query: ?test=hi')
})
it('should apply loader based on resourceQuery', async () => {
const $ = await next.render$('/')
const text = $('#reversed').text()
expect(text).toBe('dlroW olleH')
})
it('should apply loader based on resourceQuery regex', async () => {
const $ = await next.render$('/')
const text = $('#upper').text()
expect(text).toBe('HELLO WORLD')
})
})