-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
138 lines (129 loc) · 4.44 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// see https://www.figma.com/plugin-docs/bundling-webpack/
const HtmlInlineScriptPlugin = require("html-inline-script-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const postcssNormalize = require("postcss-normalize");
const { DefinePlugin } = require("webpack");
const BROWSERS =
"last 5 chrome versions, last 5 firefox versions, last 1 safari major version";
module.exports = (env, argv) => {
const isEnvProduction = argv.mode === "production";
const isEnvDevelopment = !isEnvProduction;
return {
mode: isEnvProduction ? "production" : "development",
// This is necessary because Figma's 'eval' works differently than normal eval
devtool: isEnvDevelopment && "inline-source-map",
entry: {
ui: ["./src/index.tsx"],
},
module: {
rules: [
{
oneOf: [
{
test: /\.(js|jsx|ts|tsx)$/,
use: {
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-env",
{
targets: BROWSERS,
},
],
"@babel/preset-react",
"@babel/preset-typescript",
],
},
},
},
{
test: /\.s?css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 3,
sourceMap: true,
},
},
{
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: "postcss-loader",
options: {
postcssOptions: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
require("postcss-preset-env")({
autoprefixer: {
flexbox: "no-2009",
},
browsers: BROWSERS,
stage: 3,
}),
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
postcssNormalize(),
],
},
sourceMap: true,
},
},
{
loader: "resolve-url-loader",
options: {
root: path.resolve(__dirname, "src"),
sourceMap: true,
},
},
{
loader: "sass-loader",
options: {
sourceMap: true,
},
},
],
},
{
// Exclude `js` files to keep "css" loader working as it injects
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/^$/, /\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
// all assets need to be inline for Figma plugins
type: "asset/inline",
},
],
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js"],
modules: ["node_modules"],
},
output: {
filename: "[name].js",
path: path.resolve(__dirname, "dist"), // Compile into a folder called "dist"
publicPath: "/",
},
// Tells Webpack to generate "ui.html" and to inline "ui.ts" into it
plugins: [
new HtmlWebpackPlugin({
cache: false,
chunks: ["ui"],
filename: "./ui.html",
inject: "body",
template: "./src/ui.html",
}),
new DefinePlugin({}),
new HtmlInlineScriptPlugin([/^ui\.js$/]),
],
};
};