Skip to content

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

Closed

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.

Activity

  1. vankop commented on Jun 20, 2022

    @vankop
    Contributor

    library.type=module need some enhancement, but unfortunately this is low priority...

  2. webpack-bot commented on Sep 20, 2022

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  3. added
    status: staleNo author response after a needs: request; close candidate
    on Sep 20, 2022
  4. alexander-akait commented on Sep 22, 2022

    @alexander-akait
    Member

    bump

  5. removed
    status: staleNo author response after a needs: request; close candidate
    on Sep 22, 2022
  6. added
    status: staleNo author response after a needs: request; close candidate
    on Dec 23, 2022
  7. webpack-bot commented on Jan 7, 2023

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

  8. removed
    status: staleNo author response after a needs: request; close candidate
    on Jan 13, 2023
  9. added
    status: staleNo author response after a needs: request; close candidate
    on Apr 15, 2023
  10. removed
    status: staleNo author response after a needs: request; close candidate
    on Apr 30, 2023
  11. moved this from Ready for Merge to Priority - Low in webpack 5/6on Apr 30, 2023
  12. alexander-akait commented on May 29, 2023

    @alexander-akait
    Member

    Let's close in favor #17121

  13. moved this from Priority - Low to Ready for Merge in webpack 5/6on May 29, 2023
  14. moved this from Ready for Merge to Shipped in webpack 5/6on May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    webpack-5Concerns the webpack 5 line

    Type

    No type

    Fields

    Priority

    None yet

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions