Require JS files dynamically on runtime using webpack

前端 未结 3 1882
予麋鹿
予麋鹿 2020-12-24 07:50

I am trying to port a library from grunt/requirejs to webpack and stumbled upon a problem, that might be a game-breaker for this endeavor.

The library I try to port

3条回答
  •  滥情空心
    2020-12-24 08:46

    So I found that my requirement to have some files loaded on runtime, that are only available on "app-compile-time" and not on "library-compile-time" is not easily possible with webpack.

    I will change the mechanism, so that my library doesn't require the files anymore, but needs to be passed the required modules. Somewhat tells me, this is gonna be the better API anyways.

    edit to clarify:

    Basically, instead of:

    # in my library
    load = (path_to_file) ->
      (require path_to_file).do_something()
    
    # in my app (using the 'compiled' libary)
    cool_library.load("file_that_exists_in_my_app")
    

    I do this:

    # in my library
    load = (module) ->
      module.do_something()
    
    # in my app (using the 'compiled' libary)
    module = require("file_that_exists_in_my_app")
    cool_library.load(module)
    

    The first code worked in require.js but not in webpack.

    In hindsight i feel its pretty wrong to have a 3rd-party-library load files at runtime anyway.

提交回复
热议问题