Skip to content

Commit

Permalink
refactor: remove the need for specifying trnasformInclude
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `trnasformInclude` option has been removed and is no
longer necessary. This allows full dynamic imports to custom file types
to automatically qualify for the transform pipeline.

    - All requests that accept `*/*` AND is not declared an asset type
    will now qualify for the transform pipeline.

    - To exclude an asset type from being transformed when requested
    directly, declare it as asset via `config.assetsInclude`.
  • Loading branch information
yyx990803 committed Jan 5, 2021
1 parent 034bbcd commit 99522d0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 47 deletions.
15 changes: 4 additions & 11 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,13 @@ export default ({ command, mode }) => {
- **Type:** `string | RegExp | (string | RegExp)[]`
- **Related:** [Asset Handling](/guide/features#asset-handling)

Specify additional file types to be treated as static assets (so that importing them will return their resolved URL).
Specify additional file types to be treated as static assets so that:

### transformInclude
- They will be excluded from the plugin transform pipeline when referenced from HTML or directly requested over `fetch` or XHR.

- **Type:** `string | RegExp | (string | RegExp)[]`

By default, all statically analyzable `import` requests in your code are statically treated as part of the transform pipeline. However, if you are using full dynamic import to import non-js file types, for example:

```js
// dynamicPath is a non-JS file type, e.g. "./foo.gql"
import(dynamicPath).then(/* ... */)
```
- Importing them from JS will return their resolved URL string (this can be overwritten if you have a `enforce: 'pre'` plugin to handle the asset type differently).

Vite will not be able to know that the file needs to be transformed to JavaScript (instead of being served directly as a static file). `transformInclude` allows you to explicitly declare the file type to always be transformed and served as JavaScript.
The built-in asset type list can be found [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts).

### dedupe

Expand Down
8 changes: 0 additions & 8 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ export default function myPlugin() {
return {
name: 'transform-file',

config() {
return {
// this is necessary for Vite to know that the file format should be
// handled as JS when it's used in import(dynamicPath) calls.
transformInclude: fileRegex
}
},

transform(src, id) {
if (fileRegex.test(id)) {
return {
Expand Down
16 changes: 2 additions & 14 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ export interface UserConfig {
* Or set to `false` to disable esbuild.
*/
esbuild?: ESBuildOptions | false
/**
* Specify additional files to be treated as source file (included into the
* transform pipeline).
*/
transformInclude?: string | RegExp | (string | RegExp)[]
/**
* Specify additional files to be treated as static assets.
*/
Expand Down Expand Up @@ -101,7 +96,7 @@ export interface UserConfig {
}

export type ResolvedConfig = Readonly<
Omit<UserConfig, 'plugins' | 'assetsInclude' | 'transformInclude'> & {
Omit<UserConfig, 'plugins' | 'assetsInclude'> & {
configPath: string | undefined
inlineConfig: UserConfig
root: string
Expand All @@ -114,7 +109,6 @@ export type ResolvedConfig = Readonly<
server: ServerOptions
build: Required<BuildOptions>
assetsInclude: (file: string) => boolean
transformInclude: (file: string) => boolean
logger: Logger
}
>
Expand Down Expand Up @@ -195,9 +189,6 @@ export async function resolveConfig(
const assetsFilter = config.assetsInclude
? createFilter(config.assetsInclude)
: () => false
const transformFilter = config.transformInclude
? createFilter(config.transformInclude)
: () => false

const resolved = {
...config,
Expand All @@ -222,9 +213,6 @@ export async function resolveConfig(
assetsInclude(file: string) {
return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
},
transformInclude(file: string) {
return transformFilter(file)
},
logger: createLogger(config.logLevel)
}

Expand Down Expand Up @@ -278,7 +266,7 @@ function mergeConfig(
if (key === 'alias') {
merged[key] = mergeAlias(existing, value)
continue
} else if (key === 'transformInclude' || key === 'assetsInclude') {
} else if (key === 'assetsInclude') {
merged[key] = [].concat(existing, value)
continue
}
Expand Down
38 changes: 30 additions & 8 deletions packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,37 @@ export const CLIENT_PUBLIC_PATH = `/@vite/client`
export const CLIENT_ENTRY = require.resolve('vite/dist/client/client.js')
export const CLIENT_DIR = path.dirname(CLIENT_ENTRY)

const knownAssetTypes = [
// images
'png',
'jpe?g',
'gif',
'svg',
'ico',
'webp',
'avif',

// media
'mp4',
'webm',
'ogg',
'mp3',
'wav',
'flac',
'aac',

// fonts
'woff2?',
'eot',
'ttf',
'otf',

// other
'wasm'
]

export const DEFAULT_ASSETS_RE = new RegExp(
`\\.(` +
// images
`png|jpe?g|gif|svg|ico|webp|` +
// media
`mp4|webm|ogg|mp3|wav|flac|aac|` +
// fonts
`woff2?|eot|ttf|otf` +
`)(\\?.*)?$`
`\\.(` + knownAssetTypes.join('|') + `)(\\?.*)?$`
)

export const DEP_VERSION_RE = /[\?&](v=[\w\.-]+)\b/
8 changes: 2 additions & 6 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function transformMiddleware(
server: ViteDevServer
): Connect.NextHandleFunction {
const {
config: { root, logger },
config: { root, logger, assetsInclude },
moduleGraph
} = server

Expand Down Expand Up @@ -78,16 +78,12 @@ export function transformMiddleware(
)
}

// Only apply the transform pipeline to:
// - requests that initiate from ESM imports (any extension)
// - CSS (even not from ESM)
// - Source maps (only for resolving)
if (
isJSRequest(url) ||
isImportRequest(url) ||
isCSSRequest(url) ||
isHTMLProxy(url) ||
server.config.transformInclude(withoutQuery)
(req.headers.accept === '*/*' && !assetsInclude(withoutQuery))
) {
// strip ?import
url = removeImportQuery(url)
Expand Down

0 comments on commit 99522d0

Please sign in to comment.