-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebpack.config.js
More file actions
126 lines (114 loc) · 3.34 KB
/
webpack.config.js
File metadata and controls
126 lines (114 loc) · 3.34 KB
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
const secrets = require('./secrets.json')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin')
const path = require('path')
if (process.env.DESIGN_TOOL === 'figma') {
design_tool_dist = 'figma/dist'
design_tool_root = 'figma'
} else if (process.env.DESIGN_TOOL === 'penpot') {
design_tool_dist = 'penpot/dist'
design_tool_root = 'penpot'
} else {
console.error('Invalid design tool. Please set the mode to figma or penpot.')
process.exit(1)
}
if (process.env.NODE_ENV === 'production') {
PLUGIN_URL = 'https://super-tidy.netlify.app/dist/index.html'
} else {
PLUGIN_URL = 'http://localhost:3000/dist/index.html'
}
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: {
ui: './src/App.js', // The entry point for your UI code
core: './src/Core.js', // The entry point for your plugin code
},
module: {
rules: [
// Enables including CSS by doing "import './file.css'" in your JavaScript code
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
// Disable URL processing to avoid issues with data URLs
url: false,
// Handle imports properly
import: true,
},
}
]
},
// Handle images and assets
{
test: /\.(png|jpg|gif|webp|svg)$/,
type: 'asset/inline' // Webpack 5 way to inline assets
},
// JavaScript/ES6+ processing
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
// Webpack tries these extensions for you if you omit the extension like "import './file'"
resolve: {
extensions: ['.js'],
alias: {
'@': path.resolve(__dirname, './'),
src: path.resolve(__dirname, 'src/'),
leo: path.resolve(__dirname, 'node_modules/@basiclines/leo/dist/'),
}
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, design_tool_dist), // Compile into a folder called "dist"
clean: true, // Clean output directory before build
environment: {
// Ensure compatibility with older environments
arrowFunction: false,
const: false,
destructuring: false,
forOf: false,
module: false,
}
},
// Modern webpack 5 optimizations
optimization: {
// Enable tree shaking for better bundle size
usedExports: true,
sideEffects: false,
// Disable code splitting for Figma plugin compatibility
splitChunks: false,
},
// Tells Webpack to generate "ui.html" and to inline "ui.js" into it
plugins: [
new webpack.DefinePlugin({
'WP_ENV': JSON.stringify(process.env.NODE_ENV),
'WP_AMPLITUDE_KEY': JSON.stringify(secrets.AMPLITUDE_KEY),
'WP_GUMROAD_PRODUCT_ID': JSON.stringify(secrets.GUMROAD_PRODUCT_ID),
'WP_DESIGN_TOOL': JSON.stringify(process.env.DESIGN_TOOL),
'WP_PLUGIN_URL': JSON.stringify(PLUGIN_URL),
// Define globals for Figma plugin environment
'self': 'globalThis',
'global': 'globalThis',
}),
new HtmlWebpackPlugin({
templateContent: `<root-ui></root-ui>`,
filename: 'index.html',
chunks: ['ui'],
}),
new HtmlInlineScriptPlugin()
]
})