-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
webpack version: 5.73.0
The content of this library is roughly as follows
// wepback.config.js
module.exports = {
entry: './entry.js',
output: {
filename: 'index.js',
chunkFilename: 'chunk.[chunkhash:8].js',
path: path.join(__dirname, './dist'),
publicPath: 'auto',
library: {
type: 'module',
},
},
experiments: {
outputModule: true,
},
}
// entry.js
export default async () => {
const num = (await import('./test.js')).default
return 1 + num()
}
//test.js
export default() => 2After build, index.js and chunk.xxxx.js are generated in dist folder
After calling this library in another webapck project, the brief code is as follows
import getNumber from 'xx/dist/index.js'
window.onload = async() => {
console.log(await getNumber())
}After the code is executed, it will report the error Not allowed to load local resource: file:///Users/os/xxx/dist/chunk.xxxx.js
Then I checked the function __webpack_require__.f.j that loads the chunks resource in the index.js file in the library, the url of the chunk resource in the generated code is
var url = __webpack_require__.p + __webpack_require__.u(chunkId);I think this is the reason for the error, because the variable url always returns a file address (file://xxx/xxx/), I think it should be the address that uses the URL for processing, such as
var url = new URL(__webpack_require__.u(chunkId), import.meta.url)But after I modified the code, it still could not be parsed correctly until the value of __webpack_require__.u(chunkId) was directly filled in, which is the file name of the chunk
var url = new URL('chunk.xxxx.js', import.meta.url);At this time, the call is normal, and the url of the chunks file can be parsed normally.