Webpack ProvidePlugin vs externals?

后端 未结 3 430
别那么骄傲
别那么骄傲 2020-11-28 18:12

I\'m exploring the idea of using Webpack with Backbone.js.

I\'ve followed the quick start guide and has a general idea of how Webpack works, but I\'m unclear on how

3条回答
  •  感动是毒
    2020-11-28 18:46

    I know this is an old post but thought it would be useful to mention that the webpack script loader may be useful in this case as well. From the webpack docs:

    "script: Executes a JavaScript file once in global context (like in script tag), requires are not parsed."

    http://webpack.github.io/docs/list-of-loaders.html

    https://github.com/webpack/script-loader

    I have found this particularly useful when migrating older build processes that concat JS vendor files and app files together. A word of warning is that the script loader seems only to work through overloading require() and doesn't work as far as I can tell by being specified within a webpack.config file. Although, many argue that overloading require is bad practice, it can be quite useful for concating vendor and app script in one bundle, and at the same time exposing JS Globals that don't have to be shimmed into addition webpack bundles. For example:

    require('script!jquery-cookie/jquery.cookie');
    require('script!history.js/scripts/bundled-uncompressed/html4+html5/jquery.history');
    require('script!momentjs');
    
    require('./scripts/main.js');
    

    This would make $.cookie, History, and moment globally available inside and outside of this bundle, and bundle these vendor libs with the main.js script and all it's required files.

    Also, useful with this technique is:

    resolve: {
      extensions: ["", ".js"],
      modulesDirectories: ['node_modules', 'bower_components']
    },
    plugins: [
      new webpack.ResolverPlugin(
        new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
       )
    ]
    

    which is using Bower, will look at the main file in each required libraries package.json. In the above example, History.js doesn't have a main file specified, so the path to the file is necessary.

提交回复
热议问题