Comments (15)
hi @roman-rr, I think this is something you should tackle via your webpack config. Did you research about this?
Please share your config.
from webpack-bundle-tracker.
Hello @fjsj thank you for fast reply.
webpack.common.js
const path = require('path');
const webpack = require('webpack');
const BundleTracker = require('webpack-bundle-tracker');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const scripts_dir = '../assets/';
module.exports = {
context: __dirname,
entry: {
'home': scripts_dir + 'home.bundle.js',
'article': scripts_dir + 'article.bundle.js',
'article-locked': scripts_dir + 'article-locked.bundle.js',
'amp': scripts_dir + 'amp.bundle.js',
'default': scripts_dir + 'default.bundle.js',
'create-article': scripts_dir + 'create-article.bundle.js'
},
output: {
path: path.resolve('./assets/dist/'),
filename: "[name]-[hash].bundle.js",
chunkFilename: "[name]-[hash].bundle.js"
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
plugins: [
new BundleTracker({filename: './webpack-stats.json'}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: 'jquery'
})
],
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
],
}
],
}
}
webpack.prod.js
const glob = require('glob-all')
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const PurgecssPlugin = require('purgecss-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
module.exports = merge(common, {
mode: 'production',
plugins: [
new MiniCssExtractPlugin({
filename: '[name]-[hash].bundle.css',
chunkFilename: '[name]-[hash].bundle.css',
}),
// Two ways to make specific configs for specific entries:
// - wait official support
// - make separated configurations
new PurgecssPlugin({
safelist: {
greedy: [/^pswp/, /^cropper/]
},
paths: glob.sync([
`./apps/**/*.html`,
`./assets/scripts/**/*.js`
])
}),
new ImageminPlugin(),
new CopyWebpackPlugin({
patterns: [
{ from: '../assets/images', to: '../dist/images' }
],
}),
]
});
from webpack-bundle-tracker.
@roman-rr is this still an issue?
from webpack-bundle-tracker.
@JanMalte Yes, still need it. To preload fonts with rel as described here.
<link rel="preload" href="/assets/{{FONT_HASH_NAME}}.woff2" as="font" type="font/woff2" crossorigin>
from webpack-bundle-tracker.
Actually this is not a issue of webpack-bundle-tracker
but just a config issue.
You would need to add a rule for your font files to enable iterating over all chunks and add them to your preload HTML tags.
webpack.common.js
// ...
module.exports = {
// ...
module: {
rules: [
// ...
{
test: /\.(woff2|ttf|eot)$/,
type: "asset/resource",
generator: {
filename: "fonts/[contenthash][ext]",
},
}
],
}
}
If you are using https://github.com/django-webpack/django-webpack-loader you could use the Advanced Usage to load only special file types in your preload section or write a customer template to load only chunks matching the fonts/.*
pattern
from webpack-bundle-tracker.
@JanMalte Thank you for explanation, let me try at first.
from webpack-bundle-tracker.
@JanMalte @fjsj
Sorry for delay. But the issue still actual for me.
Just tried add some configuration, but the main point of this issue is the output of webpack-stats.json
.
As you mentioned this part:
{
test: /\.(woff2|ttf|eot)$/,
type: "asset/resource",
generator: {
filename: "fonts/[contenthash][ext]",
},
}
This is works to collect fonts into the folder, but it doesn't save font's to bundle by arrays in webpack-stats.json
.
For example I need to have inside of webpack-stats.json
:
"chunks":{
"home":[
"e4e24724b5fcfe64adb9.woff2",
"vendors-node_modules_mini-css-extract-plugin_dist_hmr_hotModuleReplacement_js-node_modules_we-a0114e-f096b527166df6583ac8.bundle.js",
"assets_custom-libs_semantic_parts_container_css-assets_custom-libs_semantic_parts_grid_css-as-b1ee4e.css",
"home.css",
"home-f096b527166df6583ac8.bundle.js"
]
}
But in fact, webpack-stats.json
doesn't contains font files, especially woff2
inside of chunks
object, and I'm unable to pick it in template with django-webpack-loader.
Maybe I miss something important ?
Once again, what I need is to have font files in chunks object to pick it by bundles.
from webpack-bundle-tracker.
Hi @roman-rr, thanks for the follow-up. I've managed to reproduce this scenario on my end. My bet is that it won't include the font files in the chunks list because the general use-case is to have those fonts imported inside the JS or CSS chunks.
For your use case, could you test the following approach?
- Add the rule from #106 (comment) to tell Webpack how to process the font files
- Ensure that the font file is imported somewhere in your JS code (it doesn't need to be used by the code, just be imported)
- This step is necessary to tell Bundle Tracker to include the font file on
webpack-stats.json
, although it will be included in the "assets" dictionary, not in the "chunks" one - It can be done by doing
import FontFile from "<font/file/path.woff2>"
in yourindex.js
(or the file you're using as the entrypoint)
- This step is necessary to tell Bundle Tracker to include the font file on
- In the backend code, create a template tag named something like
get_font_assets
and register it. The code should look like this:
@register.simple_tag
def get_font_assets(config='DEFAULT'):
assets = get_loader(config).get_assets().get('assets')
font_assets = []
for asset_name, asset in assets.items():
if asset_name.endswith((".ttf", ".woff2", ".eot")):
font_assets.append(asset)
return font_assets
And then in your template, you can use it by calling {% get_font_assets %}
after loading it there.
The output of this tag as it's written above is something like this:
[{'name': 'fonts/font-name-hash.ttf', 'path': '/path/to/file/font-name-hash.ttf'}]
This way you can loop through these objects and use the paths in the template to load the fonts.
If you prefer, you can rewrite the template tag a bit to already return a list of the HTML tags (something like it's done on https://github.com/django-webpack/django-webpack-loader/blob/master/webpack_loader/utils.py#L59).
Please let me know if that works for your scenario.
from webpack-bundle-tracker.
@rvlb This is make sense, thank you. I will try.
from webpack-bundle-tracker.
It seems get_as_tags
could be expanded to work with fonts in general? Please feel free to open a PR with that improvement.
from webpack-bundle-tracker.
@rvlb @fjsj
Check once again and still unable to achieve clear solution with such way.
With #106 (comment) this method I'm able to get bunch of all fonts using in all bundles.
This is not a solution, because I need to preload only bundle font for page optimization, but not preload all font assets.
Of course, I can change font names in generator, and rename fonts to include appropriate bundle names.
generator: {
filename: "fonts/[name]-[contenthash][ext]",
},
}
But this will be messy, in case where we have different fonts in different bundles.
What I request
Collect woff2
files imported in entry points by chunks, e.g.
"chunks":{
"home":[
"e4e24724b5fcfe64adb9.woff2",
"home-f096b527166df6583ac8.bundle.js"
],
"article":[
"bce34k24b5fcfr34bdb5.woff2",
"bce35k24d5fsfrx4bdg5.woff2",
"article-40f6bc27366kf6d83sc2.bundle.js"
]
}
Does it possible ?
from webpack-bundle-tracker.
Hi @roman-rr, thanks for the comment.
Unfortunately that'd be a limitation from what we we receive from the Webpack compiler.
The files parsing on webpack-bundle-tracker is done in 2 steps:
When we parse the assets the compiler sends to us, we don't have the information regarding which chunks those files were used (the parameters we get are the file object, with data such as content and size, and the filename).
When we parse the chunks, we get access to the file list for a given chunk, but it's limited to what the compiler provides us (namely the JS and CSS bundles), so we don't have access to the assets used there.
My thinking on this scenario is that the most common case here is to have each JS/CSS compiled file to handle the assets they use in each chunk, so that'd be why Webpack won't provide this complete file list for us.
from webpack-bundle-tracker.
@rvlb Is that a feature that I can try to request from webpack and address to their directly?
from webpack-bundle-tracker.
Yes, it'd be necessary for Webpack to either:
- Make this method return all assets files that are used in a given chunk, not only the JS and CSS files
- or, make the compilation assets that we use here return the chunk(s) were each asset is used.
There may be some Webpack setting already in place that I'm not aware of that could already allow our package to get that info. If it exists, there might be a possibility that someone on the webpack repo would point to the right direction.
from webpack-bundle-tracker.
@rvlb Thank you very much. I will ask there, and will let you know in future.
from webpack-bundle-tracker.
Related Issues (20)
- Set output default indent to 2
- Setting "path" option no longer works HOT 4
- Losing data when `deleteOriginalAssets` is used in compression plugin HOT 2
- Error when updating to v1.0.0 HOT 1
- move tests to github actions? HOT 2
- @sentry/webpack-plugin deletefilesafterupload Breaks Build
- Optional chaining breaks compatibility with Node.js version 12 HOT 1
- ERR_REQUIRE_ESM Build failure after some other package in my app installed new strip-ansi HOT 6
- Missing assets section of webpack-stats in ci github action agent build HOT 3
- BundleTracker raises errors as it tries to re-create existing folders HOT 7
- Implement Jazzband guidelines for webpack-bundle-tracker HOT 6
- TypeError [ERR_INVALID_ARG_TYPE] with Webpack 5 and relativePath: true HOT 3
- 1.0.0-alpha.1 version not generating webpack-stats.json file HOT 3
- NPM version
- Can't switch from NPM to yarn or the other way around
- Release 1.0.0 HOT 2
- Add CI to run automated tests HOT 1
- Issue when using BundleTracker for multiple webpack configuration HOT 1
- Double-check @types dependencies, some of then might not be used HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webpack-bundle-tracker.