Expose javascript globals bundled via webpack

后端 未结 5 2059
鱼传尺愫
鱼传尺愫 2020-12-09 03:54

The Problem

I feel like this should be more straightforward than it is. I need to access all my javascript libraries from the frontend and because I\'m integrating

5条回答
  •  醉话见心
    2020-12-09 04:23

    Note: This is not the ideal scenario but because I have a constant amount of new globals being added, I needed to make a plugin to bundle my javascript for me.

    webpack-raw-bundler

    This simply stacks your code together to be included on the frontend. Here is my usage example:

    Usage

    From the old:

    
    
    
    

    To the new:

    
    
    

    Installing to the config

      var RawBundlerPlugin = require('webpack-raw-bundler');
    
      module.exports = {
        plugins: [
           new RawBundlerPlugin({
                 excludedFilenames: [/angulartics/],
                 readEncoding: "utf-8",
                 includeFilePathComments: false,
                 bundles: [ "vendor.js", "styles.css" ],
                 "vendor.js": [
                    'js/*.js'
                 ],
                 "styles.css": [
                    'css/bootstrap.css',
                    'css/edits.css'
                 ]
           })
        ]
     }
    

    A Fair Warning:

    This should not be your go-to solution, but I had a bad case that made this the easiest option. Using expose-loader and import or window['module'] = require('module.js') is much safer as that is what webpack was built around. However, if you are having some headaches and just want a simple bundler, feel free to use this plugin.

提交回复
热议问题