从Webpack中的外部文件添加许可证头

Ada*_*ooz 6 javascript requirejs webpack

我有一个名为"LICENSE"的外部许可证文件和webpack.BannerPlugin.我可以将LICENSE的内容复制/粘贴到BannerPlugin的字符串字段中.但它很大,也很难看.

如果我可以使用文本或原始加载器代替它会更加清晰: BannerPlugin(require("raw!./LICENSE"))

当我尝试这个时,我得到了"Error: Cannot find module 'raw!./LICENSE'",大概是因为还没有及早配置.有办法做我正在尝试的事情吗?我已经搜索了很多,并继续将整个许可证字符串放入BannerPlugin conf.

编辑:添加我的基本webpack.config文件:

// webpack.config.js
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: "./dev/main.js",
  devtools: "source-map",
  output: {
    path: "./bin",
    filename: "[name].js"
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader")
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("bundle.css"),
    new webpack.BannerPlugin("Copyright 2016 Adam Mooz.  Released under the MIT license"),
    new webpack.optimize.UglifyJsPlugin({
      minimize: true
    }),
    new HtmlWebpackPlugin({
      title: "Grocery List",
      hash: true
    })
  ]
};
Run Code Online (Sandbox Code Playgroud)

Ada*_*ooz 6

@zerkms提供了答案:使用nodejs的FS api.通过使用定义fs var fs = require("fs");,我可以使用它fs.readFileSync来读取文件.webpack.BannerPlugin(fs.readFileSync('./LICENSE', 'utf8'))

我的新wepack文件如下所示:

// webpack.config.js
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var fs = require("fs");

module.exports = {
  entry: "./dev/main.js",
  devtools: "source-map",
  output: {
    path: "./bin",
    filename: "[name].js"
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style-loader", "css-loader")
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("bundle.css"),
    new webpack.BannerPlugin(fs.readFileSync('./LICENSE', 'utf8')),
    new webpack.optimize.UglifyJsPlugin({
      minimize: true
    }),
    new HtmlWebpackPlugin({
      title: "Grocery List",
      hash: true
    })
  ]
};
Run Code Online (Sandbox Code Playgroud)