Skip to content

How to correctly parse the chunk resources in the library packaged by another webpack in webpack5? #15947

@llllllllllx

Description

@llllllllllx

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() => 2

After 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions