With Webpack
Installโ
- Yarn
- NPM
yarn add --dev @griffel/webpack-loader
npm install --save-dev @griffel/webpack-loader
Usageโ
Webpack documentation: Loaders
Within your webpack configuration object, you'll need to add the @griffel/webpack-loader
to the list of modules, like so:
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: '@griffel/webpack-loader',
},
},
// If your project uses TypeScript
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: '@griffel/webpack-loader',
options: {
babelOptions: {
presets: ['@babel/preset-typescript'],
},
},
},
},
],
},
};
While the loader itself has a short circuit to avoid processing (invoking Babel transforms) it's better to reduce the scope of processed files. For example, you can enforce a restriction to have makeStyles()
calls only in .styles.ts
files:
module.exports = {
module: {
rules: [
{
test: /\.styles.ts$/,
exclude: /node_modules/,
use: {
loader: '@griffel/webpack-loader',
options: {
babelOptions: {
presets: ['@babel/preset-typescript'],
},
},
},
},
],
},
};
Usage with Next.jsโ
Next.js lets users tweak Webpack's config so the same options are applicable to its config.
// next.config.js
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }) => {
config.module.rules.unshift({
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: '@griffel/webpack-loader',
},
],
});
// If your project uses TypeScript
config.module.rules.unshift({
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: '@griffel/webpack-loader',
options: {
babelOptions: {
presets: ['next/babel'],
},
},
},
],
});
return config;
},
};
Configurationโ
Please check the README of @griffel/webpack-loader
to check how to configure module evaluation and imports.