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

时间:2016-01-18 02:44:13

标签: 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
    })
  ]
};

1 个答案:

答案 0 :(得分: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
    })
  ]
};