Path to the js file with hash in generated html #12
Replies: 9 comments
-
Currently the plugin can't do that, but it's on the TODO list. There are so-called "asset manifest plugins" that replace normal filename with hashed version. |
Beta Was this translation helpful? Give feedback.
-
I understand, thank you |
Beta Was this translation helpful? Give feedback.
-
Hello @AndreyGudin, I have good news, finally the resolving of scripts in pug is realized :-) The new version html
head
link(rel='stylesheet' href=require('./styles.scss'))
script(src=require('./main.js'))
body Output: <!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/assets/css/styles.05e4dd86.css">
<script src="/assets/js/main.f4b855d8.js"></script>
</head>
<body></body>
</html> webpack.config.js const PugPlugin = require('pug-plugin');
module.exports = {
output: {
publicPath: '/',
// js output filename
filename: 'assets/js/[name].[contenthash:8].js',
},
entry: {
// all scripts and styles can be defined directly in pug
'index': './src/index.pug',
},
plugins: [
new PugPlugin({
modules: [
PugPlugin.extractCss({
// css output filename
filename: 'assets/css/[name].[contenthash:8].css',
}),
],
}),
],
module: {
rules: [
{
test: /\.(pug)$/,
loader: PugPlugin.loader,
options: {
method: 'render',
}
},
{
test: /\.scss$/i,
use: ['css-loader', 'scss-loader'],
},
],
},
}; See the test case. |
Beta Was this translation helpful? Give feedback.
-
@AndreyGudin can you please test in your project whether it works for you? |
Beta Was this translation helpful? Give feedback.
-
Hi @webdiscus. It's working in my project.
There is a problem, for example, if the js file is in the same directory as the html file, then the path becomes invalid. But anyway, this is great work.! Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello @AndreyGudin, can you please create a simple repo with the reproducible problem and describe in readme what is expected as correct result.
filename: (pathData, assetInfo) {
if (something) {
return 'some/path/to/filename'
}
pathData.filename; // original filename
} The structure of
Tipp, to use a specific output name:
script(src=require(`./src/pages/about/about.js`)) output <script src="/pages/about/scripts.80da.js">
You can see the test case options-outputpath. Here is the example how generate scripts and styles in same directory as the html file. In the P.S. I have the idea to use the output: {
path: path.join(__dirname, 'public/'),
filename: 'pages/[name].js',
},
entry: {
'about/index': './src/pages/about/main.pug'
},
plugins: [
new PugPlugin({
modules: [
...
{
test: /\.pug$/,
outputPath: 'pages/', // relative by the webpack output.path
},
],
}),
], ./src/pages/about/main.pug script(src=require(`./src/pages/about/scripts.js`)) then the output files:
What do you think about this? |
Beta Was this translation helpful? Give feedback.
-
Yet one thing.. |
Beta Was this translation helpful? Give feedback.
-
HI @webdiscus. Sorry for delay. Files index.html and script.js are in one folder, but path in head of index.html is:
I expected
I think the key-name of webpack entry is fine, less confusion. By default, it is the key-name of webpack entry in all plugins. |
Beta Was this translation helpful? Give feedback.
-
Hello @AndreyGudin
The pug-plugin do not affect on script filename. It is defined in If the generated output filename is
That would "nice to have", but has many problems by realisation. For example, we have one entry-point with the name entry: {
'page/home': './src/pages/home/index.pug',
} This pug file contains more scripts from different folder but with same filename. script(src=require(`./src/pages/home/index.js`)) -> page/home/index.js
script(src=require(`./src/scripts/libs/gallery/index.js`)) -> page/home/index.1.js (sufix `.1` is for an unique file) It will be even more confusion than it is now. |
Beta Was this translation helpful? Give feedback.
-
[Question]
Can pug-plugin resolve the path to the js file with name with hash and put it in to head of html file? Or does it needs another module, maybe babel?
Beta Was this translation helpful? Give feedback.
All reactions