html-webpack-plugin icon indicating copy to clipboard operation
html-webpack-plugin copied to clipboard

subdirectory filename with dynamic imports loads incorrect URL

Open vibhavsinha opened this issue 8 years ago • 6 comments

Description

html-webpack-plugin is capable of handling filename within a sub-directory. However, the default dynamic import seems to be breaking when using it in combination with subdirectory filename. I have tried setting up publicPath and output path, but such settings affect the path of all chunks. I have reproduced the issue with a minimal project below:

Error Message & Stack Trace

root loaded
Failed to load resource: the server responded with a status of 404 (Not Found)
Refused to execute script from 'http://127.0.0.1:8080/example/0.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Uncaught (in promise) Error: Loading chunk 0 failed.
(error: http://127.0.0.1:8080/example/0.js)
    at HTMLScriptElement.onScriptComplete (main.js:109)

Config

const HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  plugins: [
    new HTMLWebpackPlugin({
      filename: 'example/index.html',
    })
  ]
}

src/index.js

console.log('root loaded');

import('./part2.js').then((v) => {
  console.log(v);
});

src/part2.js

console.log('part2 loaded');

Generated

File structure

dist
├── 0.js
├── example
│   └── index.html
└── main.js

HTML file

<!DOCTYPE html>                                     
<html>                                              
  <head>                                            
    <meta charset="UTF-8">                          
    <title>Webpack App</title>                      
  </head>                                           
  <body>                                            
  <script type="text/javascript" src="../main.js"></script></body>                                       
</html>

Relevant Links

minimal reproduction on Github

Environment

➜  webpack-path-test git:(master) node -e "var os=require('os');console.log('Node.js ' + process.version + '\n' + os.platform() + ' ' + os.release())"
npm --version
npm ls webpack
npm ls html-webpack-plugin
Node.js v9.5.0            
linux 4.10.17-041017-generic                        
5.8.0                     
/home/vibhav/node-projects/webpack-path-test        
└── [email protected]         

/home/vibhav/node-projects/webpack-path-test        
└── [email protected]

vibhavsinha avatar Apr 18 '18 07:04 vibhavsinha

Thanks @vibhavsinha
Correct me if I am wrong but the dynamic important is done by webpack. So I guess the html-webpack-plugin can't help here.

jantimon avatar Apr 25 '18 10:04 jantimon

@vibhavsinha I also met this problem, have you solved it?

luo772435545 avatar May 12 '18 13:05 luo772435545

setting the public path to / should allow you to download dynamic files correctly

jantimon avatar May 15 '18 15:05 jantimon

webpackConfig.output.publicPath = '/'

HuBinAdd avatar May 21 '18 08:05 HuBinAdd

Note: folders. There are no folders to do as you do is ok. very sad

luo772435545 avatar May 21 '18 08:05 luo772435545

Closed as unrelated to this plugin

jantimon avatar May 21 '18 13:05 jantimon