As I couldn't find in the figma docs a way how to produce a html from a ui.html template including a ui.js(which is created by webpack from a ui.ts) here my webpack config.
You need to install also the html-webpack-plugin (see https://webpack.js.org/plugins/html-webpack-plugin/ ) in order to create html pages from a template.
Note I used that to develop a plugin for the figma desktop app.
May be the other suggestions in this topic work for plugins loaded in the browser...I can't tell that for now.
In any case the <script type="module">
suggestion didn't work for me: the module simply wasn't requested in the network tab.
(And the electron desktop browser loads the ui.html page as a data:text/html;base64
blob, so it should be clear that this is not a normal http server access of ui.html .)
//webpack.config.js
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = (env, argv) => ({
mode: argv.mode === 'production' ? 'production' : 'development',
// This is necessary because Figma's 'eval' works differently than normal eval
devtool: argv.mode === 'production' ? false : 'inline-source-map',
entry: {
code: './src/code.ts', // This is the entry point for our plugin code running in the figma context.
ui: './src/ui.ts' // This is the entry point for the ui code running in the context of ui.html.
},
module: {
rules: [
// Converts TypeScript code to JavaScript
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
}
//,{ test: /\.pug$/, loader: "pug-loader", exclude: /node_modules/ } //use that if you have a .pug template
],
},
// Webpack tries these extensions for you if you omit the extension like "import './file'"
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist') //output is in the 'dist/' dir
},
plugins: [
new HtmlWebpackPlugin({
title: 'My First Figma Plugin',
filename: 'ui.html', //produces a 'dist/ui.html' file
template: 'src/uitempl.html' //use a template file
})
]
});
The template file
<!--src/uitempl.html-->
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<h2>My Exporter</h2>
<button id="export">Export</button>
<button id="cancel">Cancel</button>
<script><%= compilation.assets["ui.js"].source() %></script>
</body>
</html>
The htmlWebpackPlugin uses some magic markers to inject title and source code.
So for example
<%= compilation.assets["ui.js"].source() %>
is replaced with the source code of ui.js
.