/powerbi-visuals-webpack-plugin

powerbi-visuals-webpack-plugin

Primary LanguageJavaScriptMIT LicenseMIT

powerbi-visuals-webpack-plugin

npm version npm

This plugin allows developing custom visuals by using webpack to build a visual package.

Provides following functionality:

  • Creates assets for developer server
  • Creates *.pbiviz package.

How to use the plugin

Plugin options

Plugin config description

  var defaultOptions = {
      visual: {
        name: "Visual name",
        displayName: "Visual name for displaying in visuals panel",
        guid: `Unique GUID for the visual (generated by plugin)`,
        visualClassName: "Visual class name, it is used by visual plugin to create instance of the visual",
        version: "Visual version",
        description: "Visual description",
        supportUrl: "URL for support"
    },
    author: "Author of the visual",
    apiVersion: "API version",
    stringResources: [
      "Paths to localization files"
    ],
    capabilities: {
        // Visual capabilities
    },
    iconImage: "Icon file as base64 string",
    devMode: "development mode",
    packageOutPath: "location to create *.pbiviz file",
    packageNameWithGUID: true,
    packageNameWithVersion: true,
    cssStyles: "styles",
    generateResources: "it is used --resources flag in pbiviz tools",
    generatePbiviz: "it is used by --no-pbiviz flag in pbiviz tools"
  };

Webpack configuration

The sample of config webpack 4.

const path = require('path');
const fs = require('fs');
const _ = require('lodash');
// for icon file
const base64Img = require('image-to-base64');
// werbpack plugin
const PowerBICustomVisualsWebpackPlugin = require('powerbi-visuals-webpack-plugin');
const WatchIgnorePlugin = require("webpack").WatchIgnorePlugin;
// file encoding
const encoding = "utf8";

// string resources
const stringResourcesPath = path.join("stringResources", "en-US", "resources.json");
// visual configuration json path
const pbivizPath = "./pbiviz.json";
const pbivizFile = require(path.join(__dirname, pbivizPath));

const stringResources = JSON.parse(fs.readFileSync(stringResourcesPath, encoding));

// the visual capabilities content
const capabilitiesPath = "./capabilities.json";
const capabilitiesFile = require(path.join(__dirname, capabilitiesPath));

module.exports = {
    entry: "./.tmp/precompile/visualPlugin.ts",
    devtool: 'source-map',
    mode: "development",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ]
    },
    output: {
        path: path.join(__dirname, "/.tmp/drop"),
        publicPath: 'assets',
        filename: "visual.js",
        libraryTarget: 'var',
        library: 'CustomVisual' // it should be CustomVisual, the name is used by visual plugin to create instance of the visual
    },
    devServer: {
        disableHostCheck: true,
        contentBase: path.join(__dirname, ".tmp/drop"), // path with assets for dev server, they are generated by webpack plugin
        compress: true,
        port: 8080, // dev server port
        hot: false,
        inline: false,
        // cert files for dev server
        https: {
			pfx: fs.readFileSync(path.join(__dirname, 'certs', 'PowerBICustomVisualTest_public.pfx')),
			passphrase: '0000000000000000' // the cert's password (change with your own!)
        },
        headers: {
            "access-control-allow-origin": "*",
            "cache-control": "public, max-age=0"
        },
    },
    plugins: [
        // custom visuals plugin instance with options
        new PowerBICustomVisualsWebpackPlugin({
            ...pbivizFile,
            capabilities: capabilitiesFile,
            packageOutPath: path.join(__dirname, "distr"),
            devMode: false,
            stringResources: [
				"stringResources/de-DE.json",
				"stringResources/en-US.json"
			],
            visualSourceLocation: "../../src/index.ts" // path to visual class file
        }),
        // visual plugin regenerates with the visual source, but it does not require relaunching dev server
        new WatchIgnorePlugin([
            path.join(__dirname, "src", "visualPlugin.js"),
        ]),
    ]
};

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.