How can I extract the CSS from a compiled JS file into my own application.css? I want to detail this question by using an example:
Using Webpack 4, Rails webpacker and Vue.js I have built a SPA. I have added the external Vue component Vue-infinite-loading. This component offers a dist JS file I've added:
In application.js:
import InfiniteLoading from 'vue-infinite-loading'
This JS includes the CSS and adds the styles into the <head>
tag.
I'm looking for a way to avoid this in production env. I want to move the CSS to my own application.css
file (generated by Webpack).
I found the following (bad) solution:
The external component uses LESS, which I don't use in my project. But if I add LESS to my project, I can use the source files of the component and use MiniCssExtractPlugin to extract the styles:
In application.js:
import InfiniteLoading from 'vue-infinite-loading/src/index'
In environment.js:
....
const isProduction = process.env.NODE_ENV === 'production'
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
environment.loaders.append('less', {
test: /\.less$/,
use: [
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
'less-loader'
],
})
....
But adding LESS to my project only to extract styles from an external component seems not right to me. Is there a better way?
It is possible to extract styles from a JS file?