-
Notifications
You must be signed in to change notification settings - Fork 11
/
webpack.config.js
111 lines (99 loc) · 3.4 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const path = require('path');
const webpack = require('webpack');
const dotenv = require('dotenv');
const defaultEnvKeys = ['ANALYTICS_URL', 'FEEDBACK_FORM_URL'];
module.exports = (env, argv) => {
// call dotenv and it will return an Object with a parsed key
const envFile = dotenv.config().parsed || {};
defaultEnvKeys.forEach((key) => {
if (!envFile[key]) {
envFile[key] = '';
}
});
// reduce it to a nice object, the same as before
const envKeys = Object.keys(envFile).reduce((prev, next) => {
const keys = { ...prev };
keys[`process.env.${next}`] = JSON.stringify(envFile[next]);
return keys;
}, {});
return {
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',
devServer: {
open: ['/ui.html']
// static: {
// directory: path.join(__dirname, 'dist')
// }
},
entry: {
ui: './src/ui.js', // entry point for ui code
code: './src/code.js' // entry point for plugin code
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false,
envName: argv.mode === 'production' ? 'production' : 'development'
}
}
},
// enables including CSS by doing "import './file.css'" in your code
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
// enables including SCSS by doing "import './file.scss'" in your code
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader'
]
},
// allows you to use "<%= require('./file.svg') %>" in your HTML code to get a data URI
// { test: /\.(png|jpg|gif|webp|svg|zip)$/, loader: [{ loader: 'url-loader' }] }
{
test: /\.svg/,
type: 'asset/inline'
}
]
},
// webpack tries these extensions for you if you omit the extension like "import './file'"
resolve: { extensions: ['.js'] },
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist'), // compile into a folder called "dist"
publicPath: '/'
},
// tells webpack to generate "ui.html" and to inline "ui.js" into it
plugins: [
new webpack.DefinePlugin({
global: {}, // fix missing symbol error when running in developer VM
...envKeys,
'process.env.VERSION': JSON.stringify(process.env.npm_package_version),
'process.env.ISPROD': argv.mode === 'production'
}),
new HtmlWebpackPlugin({
cache: false, // https://github.com/figma/plugin-samples/pull/88/commits/ba20d48aace9931ce7ab53753cf87aa297435de1
inject: 'body',
template: './src/ui.html',
filename: 'ui.html',
chunks: ['ui']
}),
new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/ui/])
]
};
};