Skip to content

Commit

Permalink
feat(css): add plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Oct 17, 2023
1 parent 869037c commit df2f685
Show file tree
Hide file tree
Showing 4 changed files with 1,688 additions and 49 deletions.
33 changes: 33 additions & 0 deletions packages/css/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# CSS Plugin

Enhanced inject CSS into the razzle bundle

## Install

```
npm install -D @rambler-tech/razzle-css
```

or

```
yarn add -D @rambler-tech/razzle-css
```

## Usage

Add the plugin to `razzle.config.js`

```js
const CssPlugin = require('@rambler-tech/razzle-css')

module.exports = {
plugins: [
CssPlugin()
],
modifyWebpackConfig({webpackConfig}) {
// ...
return webpackConfig
}
}
```
83 changes: 83 additions & 0 deletions packages/css/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-disable sonarjs/no-duplicate-string */
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const makeLoaderFinder = require('razzle-dev-utils/makeLoaderFinder')

module.exports = () => ({
modifyWebpackConfig({env: {target, dev: isDev}, webpackConfig}) {
const isServer = target === 'node'

const localIdentName = isDev ? '[path][local]' : '[contenthash:base64:8]'

let loaders
let vendorCSSLoaders

if (isServer) {
loaders = [
{
loader: 'css-loader',
options: {
esModule: true,
modules: {
localIdentName,
exportOnlyLocals: true
}
}
},
'postcss-loader'
]

vendorCSSLoaders = ['css-loader']
} else if (isDev) {
loaders = [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {localIdentName}
}
},
'postcss-loader'
]

vendorCSSLoaders = ['style-loader', 'css-loader', 'postcss-loader']
} else {
loaders = [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: {localIdentName}
}
},
'postcss-loader'
]

vendorCSSLoaders = [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader'
]
}

const cssLoader = webpackConfig.module.rules.find(
makeLoaderFinder('css-loader')
)

cssLoader.test = /\.css$/
cssLoader.exclude = [/node_modules/]
cssLoader.use = loaders

webpackConfig.module.rules.push({
test: /\.css$/,
include: [/node_modules/],
use: vendorCSSLoaders
})

if (!isServer) {
webpackConfig.plugins.push(new CssMinimizerPlugin())
}

return webpackConfig
}
})
22 changes: 22 additions & 0 deletions packages/css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@rambler-tech/razzle-css",
"version": "0.0.0",
"main": "index.js",
"license": "MIT",
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"dependencies": {
"css-loader": "^6.8.1",
"css-minimizer-webpack-plugin": "^5.0.1",
"mini-css-extract-plugin": "^2.7.6",
"postcss-loader": "^7.3.3",
"razzle-dev-utils": "^4.2.18",
"style-loader": "^3.3.3"
},
"peerDependencies": {
"razzle": ">=4",
"webpack": ">=5"
}
}
Loading

0 comments on commit df2f685

Please sign in to comment.