Adding a license header from an external file in Webpack

I have an external license file named "LICENSE" and a web package . BannerPlugin . I could copy / paste the contents of LICENSE into the string field for BannerPlugin;. But it is big and ugly.

It would be much cleaner if I could use a text or raw loader instead: BannerPlugin(require("raw!./LICENSE"))

When I try to do this, I get a message "Error: Cannot find module 'raw!./LICENSE'", presumably because require was not configured early enough. Is there a way to do what I'm trying? I searched for a long time and continue to return the entire license line to the BannerPlugin candy.

Change: adding my main webpack.config file:

// 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
    })
  ]
};
+5
1

@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
    })
  ]
};
+6

Source: https://habr.com/ru/post/1624709/


All Articles