Razzle TypeScript Plugin
razzle-plugin-typescript integrates TypeScript support into Razzle applications, allowing developers to use TypeScript for both client-side and server-side code. It provides granular configuration options for underlying tools like `ts-loader` and `fork-ts-checker-webpack-plugin`, enabling custom setups for compilation and type checking. The package is currently at version 4.2.18, receiving updates in sync with the core Razzle framework. A key consideration is that Razzle now includes built-in TypeScript support via Babel. As such, this plugin is primarily recommended for projects requiring advanced or specific TypeScript configurations, such as applying certain Babel transforms to `.ts` files, rather than for basic TypeScript integration, where the native Razzle support is generally sufficient and simpler.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Your `razzle.config.js` is using ES module `import` syntax (e.g., `import { name } from 'module';`) but your `package.json` does not have `"type": "module"` set, or it's being treated as CommonJS.fixIn your `package.json`, add `"type": "module"` at the top level to enable ESM. Alternatively, rewrite your `razzle.config.js` to use CommonJS `require()` and `module.exports` syntax. -
Error: Can't resolve 'typescript' in '...' or similar webpack build failures related to TypeScript files.
cause The `typescript` package or its associated Webpack loaders (`ts-loader`) are not installed as dependencies, or your `tsconfig.json` is misconfigured.fixEnsure `typescript`, `ts-loader`, and `fork-ts-checker-webpack-plugin` are installed as dev dependencies (`yarn add -D typescript ts-loader fork-ts-checker-webpack-plugin`). Verify that a valid `tsconfig.json` exists in your project root and is correctly configured for your project structure. -
TSXXXX: (some TypeScript compilation error)
cause These are standard TypeScript compilation errors. While the plugin enables TS, it doesn't solve code-specific issues. Errors can stem from type mismatches, missing definitions, incorrect `tsconfig.json` settings (e.g., `strict` mode, `jsx` option), or incompatible dependencies.fixAddress the specific TypeScript error message by correcting your code, adjusting your `tsconfig.json` compiler options (e.g., `skipLibCheck`, `noImplicitAny`, `jsx`), or installing `@types/` packages for untyped dependencies. Check `forkTsChecker` options for specific linting rules if applicable.
Warnings
- gotcha Razzle now includes built-in TypeScript support leveraging Babel. This plugin is generally not recommended for new projects unless specific advanced configurations, such as applying Babel transforms to TypeScript files, are required. Using Razzle's native support is often simpler and preferred.
- breaking Starting with Razzle 4.2.18, `razzle.config.js` now supports `type:module` in your `package.json` for ESM syntax. Older configurations might encounter `SyntaxError` if they mix CommonJS exports with ESM `import` statements without properly declaring `"type": "module"` in `package.json` or vice-versa.
- gotcha This plugin relies on `ts-loader` and `fork-ts-checker-webpack-plugin`. These packages, along with `typescript` itself, are peer dependencies or expected dependencies and must be installed separately in your project.
Install
-
npm install razzle-plugin-typescript -
yarn add razzle-plugin-typescript -
pnpm add razzle-plugin-typescript
Imports
- typescript plugin (default)
import { typescript } from 'razzle-plugin-typescript'module.exports = { plugins: ['typescript'], }; - typescript plugin (with options)
const config = require('razzle-plugin-typescript'); // ... then try to manipulate config.pluginsmodule.exports = { plugins: [ { name: 'typescript', options: { useBabel: true, // Example: Enable Babel processing for TS files tsLoader: { transpileOnly: false } }, }, ], }; - TypeScript types (example)
import type { WebpackOptions } from 'webpack';
Quickstart
/* razzle.config.js */
const RazzlePluginTypescript = require('razzle-plugin-typescript'); // Not strictly imported, but for context of plugin availability
module.exports = {
plugins: [
{
name: 'typescript',
options: {
// Set useBabel to true if you want to keep using babel for JS/TS interoperability,
// or if you want to apply any babel transforms to typescript files.
useBabel: false,
// Override ts-loader options
tsLoader: {
transpileOnly: true,
experimentalWatchApi: true,
},
// Override fork-ts-checker-webpack-plugin options for type checking
forkTsChecker: {
eslint: {
files: ['*.js', '*.jsx', '*.ts', '*.tsx'],
},
async: process.env.NODE_ENV === 'development',
formatter: 'codeframe',
},
},
},
],
// Optional: Add '.ts' and '.tsx' to Razzle's default extensions
modifyWebpackConfig(config, { target, dev }, webpack) {
config.resolve.extensions = [...config.resolve.extensions, '.ts', '.tsx'];
return config;
}
};
// Ensure required peer dependencies are installed:
// yarn add -D typescript ts-loader fork-ts-checker-webpack-plugin
// Also, ensure a tsconfig.json exists in your project root.