Why import bootstrap css with webpack instead of just linking to it?

Why import bootstrap css with webpack instead of just linking to it?



I have been learning webpack for use in a Single Page App.



One of the examples is using:


import 'bootstrap/dist/css/bootstrap.min.css';



along with a loader like this:



test: /.css$/,
use: ['style-loader', 'css-loader']



This works to inject bootstrap css into main.js and it then gets injected into the dom at runtime.



Ok fine. But why? I don't see a benefit to this over just have a normal link.



The other side of the code is that this is now increasing the size of the bundle (my app bundle is already over 5 megs) is just going to increase the startup time vs using a CDN.



Am I missing anything?



Update



I think I found the answer to this: The next step is to extract the imported css to a css file with MiniCssExtractPlugin like explained here





if you're using Bootstrap css with webpack, I would recommend using an additional plugin like PurifyCss which will allow you to 'treeshake' all the bootstrap classes you do not actually use and therefore will reduce your bundle size. npmjs.com/package/purifycss-webpack
– amyloula
Aug 30 at 21:59




1 Answer
1



It will not make any difference when you have one dependency. But if you have a bunch of third party libraries , you can bundle and minify in to one. It will give you advantage when your application gone in to production.



And also other benefits would be converting .scss in to css



Sample web pack configuration


module.exports =
mode: 'development',
entry:
'main.app.bundle': ['main.ts', "./somestyle.css"]
,
optimization:
splitChunks:

cacheGroups:
commons:
test: /[\/]node_modules[\/]/,
name: 'vendors',
chunks: 'all'



,
output:
publicPath: '/dist/',
filename: '[id].js',
chunkFilename: "[name].js",
path: path.resolve(__dirname, 'dist'),

,
module:
rules: [c)ss$/,
use: [
'exports-loader?module.exports.toString()',

loader: MiniCssExtractPlugin.loader,
,
'css-loader',
'sass-loader',
]
,


// This plugin will allow us to use html templates when we get to the angularJS app
test: /.html$/,
exclude: /node_modules/,
loader: 'html-loader',
,

test: /.tsx?$/,
loader: 'ts-loader',

]
,
node:
fs: 'empty'
,
resolve:
modules: [
__dirname,
'node_modules',
],
extensions: [".ts", ".tsx", ".js"]
,
plugins: [
new CleanWebpackPlugin(['dist']),
new HashOutput(
validateOutput: false,
),
new MiniCssExtractPlugin(
filename: 'application.bundle.css',
chunkFilename: '[name].css'
)
],
devtool: 'source-map',
externals: ,
devServer:
historyApiFallback: true

;





plus 1 for the example - very helpful
– Greg Gum
Aug 31 at 17:36



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)