Skip to content

Commit 3df1ae9

Browse files
committed
Change API for overriding the build config
1 parent c28e3c7 commit 3df1ae9

File tree

10 files changed

+38
-106
lines changed

10 files changed

+38
-106
lines changed

packages/build/src/build-async.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { log } from '@create-figma-plugin/common'
2-
import fs from 'fs-extra'
32

43
import { BuildOptions } from './types/build.js'
54
import { buildBundlesAsync } from './utilities/build-bundles-async/build-bundles-async.js'
@@ -11,26 +10,8 @@ import { typeCheckAsync } from './utilities/type-check-async/type-check-async.js
1110
export async function buildAsync(
1211
options: BuildOptions & { clearPreviousLine: boolean }
1312
): Promise<void> {
14-
const {
15-
minify,
16-
typecheck,
17-
mainConfigFilePath,
18-
uiConfigFilePath,
19-
clearPreviousLine
20-
} = options
13+
const { minify, typecheck, clearPreviousLine } = options
2114
try {
22-
if (
23-
mainConfigFilePath !== null &&
24-
(await fs.pathExists(mainConfigFilePath)) === false
25-
) {
26-
throw new Error(`Main config file does not exist: ${mainConfigFilePath}`)
27-
}
28-
if (
29-
uiConfigFilePath !== null &&
30-
(await fs.pathExists(uiConfigFilePath)) === false
31-
) {
32-
throw new Error(`UI config file does not exist: ${uiConfigFilePath}`)
33-
}
3415
if (typecheck === true) {
3516
const getTypeCheckElapsedTime = trackElapsedTime()
3617
await buildCssModulesTypingsAsync() // This must occur before `typeCheckAsync`
@@ -42,18 +23,15 @@ export async function buildAsync(
4223
})
4324
log.info('Building...')
4425
const getBuildElapsedTime = trackElapsedTime()
45-
await Promise.all([
46-
buildBundlesAsync({ mainConfigFilePath, minify, uiConfigFilePath }),
47-
buildManifestAsync(minify)
48-
])
26+
await Promise.all([buildBundlesAsync(minify), buildManifestAsync(minify)])
4927
const buildElapsedTime = getBuildElapsedTime()
5028
log.success(`Built in ${buildElapsedTime}`, { clearPreviousLine })
5129
} else {
5230
log.info('Building...')
5331
const getBuildElapsedTime = trackElapsedTime()
5432
await Promise.all([
5533
buildCssModulesTypingsAsync(),
56-
buildBundlesAsync({ mainConfigFilePath, minify, uiConfigFilePath }),
34+
buildBundlesAsync(minify),
5735
buildManifestAsync(minify)
5836
])
5937
const buildElapsedTime = getBuildElapsedTime()

packages/build/src/cli.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22
import { log } from '@create-figma-plugin/common'
3-
import { resolve } from 'path'
43
import sade from 'sade'
54

65
import { buildAsync } from './build-async.js'
@@ -10,32 +9,16 @@ import { watchAsync } from './watch-async/watch-async.js'
109
sade('build-figma-plugin', true)
1110
.describe('Build a Figma plugin')
1211
.option('-m, --minify', 'Minify the plugin bundle', false)
13-
.option(
14-
'--main-config',
15-
'Path to a JavaScript file for customizing the esbuild config for generating the plugin’s main bundle',
16-
''
17-
)
1812
.option('-t, --typecheck', 'Type check the plugin code before build', false)
19-
.option(
20-
'--ui-config',
21-
'Path to a JavaScript file for customizing the esbuild config for generating the plugin’s UI bundle',
22-
''
23-
)
2413
.option('-w, --watch', 'Rebuild the plugin on code changes', false)
2514
.action(async function (options: {
2615
minify: boolean
2716
typecheck: boolean
2817
watch: boolean
29-
['main-config']: string
30-
['ui-config']: string
3118
}): Promise<void> {
3219
const buildOptions: BuildOptions = {
33-
mainConfigFilePath:
34-
options['main-config'] === '' ? null : resolve(options['main-config']),
3520
minify: options.minify,
36-
typecheck: options.typecheck,
37-
uiConfigFilePath:
38-
options['ui-config'] === '' ? null : resolve(options['ui-config'])
21+
typecheck: options.typecheck
3922
}
4023
if (options.watch === true) {
4124
log.clearViewport()

packages/build/src/types/build.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export type BuildOptions = {
2-
readonly mainConfigFilePath: null | string
32
readonly minify: boolean
43
readonly typecheck: boolean
5-
readonly uiConfigFilePath: null | string
64
}

packages/build/src/utilities/build-bundles-async/build-bundles-async.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,47 @@ import {
88
readConfigAsync
99
} from '@create-figma-plugin/common'
1010
import { build, BuildOptions } from 'esbuild'
11+
import fs from 'fs-extra'
1112
import indentString from 'indent-string'
12-
import { join } from 'path'
13+
import { join, resolve } from 'path'
1314

1415
import { esbuildCssModulesPlugin } from './esbuild-css-modules-plugin.js'
1516

1617
interface EntryFile extends ConfigFile {
1718
readonly commandId: string
1819
}
1920

20-
export async function buildBundlesAsync(options: {
21-
minify: boolean
22-
mainConfigFilePath: null | string
23-
uiConfigFilePath: null | string
24-
}): Promise<void> {
25-
const { minify, uiConfigFilePath, mainConfigFilePath } = options
21+
export async function buildBundlesAsync(minify: boolean): Promise<void> {
2622
const config = await readConfigAsync()
2723
await Promise.all([
2824
buildMainBundleAsync({
2925
config,
30-
esbuildConfigFilePath: mainConfigFilePath,
3126
minify
3227
}),
3328
buildUiBundleAsync({
3429
config,
35-
esbuildConfigFilePath: uiConfigFilePath,
3630
minify
3731
})
3832
])
3933
}
4034

4135
async function createEsbuildConfig(
4236
buildOptions: BuildOptions,
43-
esbuildConfigFilePath: null | string
37+
esbuildConfigFilePath: string
4438
): Promise<BuildOptions> {
45-
if (esbuildConfigFilePath === null) {
39+
const absolutePath = resolve(esbuildConfigFilePath)
40+
if ((await fs.pathExists(absolutePath)) === false) {
4641
return buildOptions
4742
}
48-
const { default: overrideEsbuildConfig } = await import(esbuildConfigFilePath)
43+
const { default: overrideEsbuildConfig } = await import(absolutePath)
4944
return overrideEsbuildConfig(buildOptions)
5045
}
5146

5247
async function buildMainBundleAsync(options: {
5348
config: Config
5449
minify: boolean
55-
esbuildConfigFilePath: null | string
5650
}): Promise<void> {
57-
const { config, minify, esbuildConfigFilePath } = options
51+
const { config, minify } = options
5852
const js = createMainEntryFile(config)
5953
try {
6054
const esbuildConfig: BuildOptions = {
@@ -71,7 +65,12 @@ async function buildMainBundleAsync(options: {
7165
// See https://esbuild.github.io/content-types/#javascript
7266
target: 'es2017'
7367
}
74-
await build(await createEsbuildConfig(esbuildConfig, esbuildConfigFilePath))
68+
await build(
69+
await createEsbuildConfig(
70+
esbuildConfig,
71+
constants.build.mainConfigFilePath
72+
)
73+
)
7574
} catch (error) {
7675
throw new Error(formatEsbuildErrorMessage(error.message))
7776
}
@@ -99,9 +98,8 @@ function createMainEntryFile(config: Config): string {
9998
async function buildUiBundleAsync(options: {
10099
config: Config
101100
minify: boolean
102-
esbuildConfigFilePath: null | string
103101
}): Promise<void> {
104-
const { config, minify, esbuildConfigFilePath } = options
102+
const { config, minify } = options
105103
const js = createUiEntryFile(config)
106104
if (js === null) {
107105
return
@@ -121,7 +119,9 @@ async function buildUiBundleAsync(options: {
121119
},
122120
target: 'chrome58'
123121
}
124-
await build(await createEsbuildConfig(esbuildConfig, esbuildConfigFilePath))
122+
await build(
123+
await createEsbuildConfig(esbuildConfig, constants.build.uiConfigFilePath)
124+
)
125125
} catch (error) {
126126
throw new Error(formatEsbuildErrorMessage(error.message))
127127
}

packages/build/src/watch-async/watch-async.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const cssRegex = /\.css$/
1414
const packageJsonRegex = /^package\.json$/
1515

1616
export async function watchAsync(options: BuildOptions): Promise<void> {
17-
const { minify, uiConfigFilePath, mainConfigFilePath, typecheck } = options
17+
const { minify, typecheck } = options
1818
if (typecheck === true) {
1919
await typeCheckAsync(true)
2020
}
@@ -43,9 +43,7 @@ export async function watchAsync(options: BuildOptions): Promise<void> {
4343
promises.push(buildCssModulesTypingsAsync())
4444
}
4545
}
46-
promises.push(
47-
buildBundlesAsync({ mainConfigFilePath, minify, uiConfigFilePath })
48-
)
46+
promises.push(buildBundlesAsync(minify))
4947
await Promise.all(promises)
5048
log.success(`Built in ${getElapsedTime()}`)
5149
if (typecheck === false) {

packages/build/test/build-async.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ test('no config', async function (t) {
1919
await symlinkCreateFigmaPluginTsConfigAsync()
2020
await buildAsync({
2121
clearPreviousLine: false,
22-
mainConfigFilePath: null,
2322
minify: false,
24-
typecheck: true,
25-
uiConfigFilePath: null
23+
typecheck: true
2624
})
2725
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
2826
t.deepEqual(manifestJson, {
@@ -46,10 +44,8 @@ test('basic command', async function (t) {
4644
await symlinkCreateFigmaPluginTsConfigAsync()
4745
await buildAsync({
4846
clearPreviousLine: false,
49-
mainConfigFilePath: null,
5047
minify: false,
51-
typecheck: true,
52-
uiConfigFilePath: null
48+
typecheck: true
5349
})
5450
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
5551
t.deepEqual(manifestJson, {
@@ -73,10 +69,8 @@ test('command with UI', async function (t) {
7369
await symlinkCreateFigmaPluginTsConfigAsync()
7470
await buildAsync({
7571
clearPreviousLine: false,
76-
mainConfigFilePath: null,
7772
minify: false,
78-
typecheck: true,
79-
uiConfigFilePath: null
73+
typecheck: true
8074
})
8175
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
8276
t.deepEqual(manifestJson, {
@@ -101,10 +95,8 @@ test('multiple menu commands', async function (t) {
10195
await symlinkCreateFigmaPluginTsConfigAsync()
10296
await buildAsync({
10397
clearPreviousLine: false,
104-
mainConfigFilePath: null,
10598
minify: false,
106-
typecheck: true,
107-
uiConfigFilePath: null
99+
typecheck: true
108100
})
109101
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
110102
t.deepEqual(manifestJson, {
@@ -142,10 +134,8 @@ test('nested menu commands', async function (t) {
142134
await symlinkCreateFigmaPluginTsConfigAsync()
143135
await buildAsync({
144136
clearPreviousLine: false,
145-
mainConfigFilePath: null,
146137
minify: false,
147-
typecheck: true,
148-
uiConfigFilePath: null
138+
typecheck: true
149139
})
150140
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
151141
t.deepEqual(manifestJson, {
@@ -180,10 +170,8 @@ test('relaunch button', async function (t) {
180170
await symlinkCreateFigmaPluginTsConfigAsync()
181171
await buildAsync({
182172
clearPreviousLine: false,
183-
mainConfigFilePath: null,
184173
minify: false,
185-
typecheck: true,
186-
uiConfigFilePath: null
174+
typecheck: true
187175
})
188176
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
189177
t.deepEqual(manifestJson, {
@@ -220,10 +208,8 @@ test('custom styles', async function (t) {
220208
await symlinkCreateFigmaPluginTsConfigAsync()
221209
await buildAsync({
222210
clearPreviousLine: false,
223-
mainConfigFilePath: null,
224211
minify: false,
225-
typecheck: true,
226-
uiConfigFilePath: null
212+
typecheck: true
227213
})
228214
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
229215
t.deepEqual(manifestJson, {
@@ -249,10 +235,8 @@ test('preact', async function (t) {
249235
await symlinkCreateFigmaPluginTsConfigAsync()
250236
await buildAsync({
251237
clearPreviousLine: false,
252-
mainConfigFilePath: null,
253238
minify: false,
254-
typecheck: true,
255-
uiConfigFilePath: null
239+
typecheck: true
256240
})
257241
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
258242
t.deepEqual(manifestJson, {
@@ -277,10 +261,8 @@ test('esbuild main config', async function (t) {
277261
await symlinkCreateFigmaPluginTsConfigAsync()
278262
await buildAsync({
279263
clearPreviousLine: false,
280-
mainConfigFilePath: join(process.cwd(), 'esbuild.main.config.js'),
281264
minify: false,
282-
typecheck: true,
283-
uiConfigFilePath: null
265+
typecheck: true
284266
})
285267
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
286268
t.deepEqual(manifestJson, {
@@ -306,10 +288,8 @@ test('esbuild ui config', async function (t) {
306288
await symlinkCreateFigmaPluginTsConfigAsync()
307289
await buildAsync({
308290
clearPreviousLine: false,
309-
mainConfigFilePath: null,
310291
minify: false,
311-
typecheck: true,
312-
uiConfigFilePath: join(process.cwd(), 'esbuild.ui.config.js')
292+
typecheck: true
313293
})
314294
const manifestJson = JSON.parse(await fs.readFile('manifest.json', 'utf8'))
315295
t.deepEqual(manifestJson, {
@@ -323,7 +303,7 @@ test('esbuild ui config', async function (t) {
323303
t.true(await fs.pathExists('build/ui.js'))
324304
const uiJs = await fs.readFile('build/ui.js', 'utf8')
325305
t.true(/\/\/ comment appended to ui\.js/.test(uiJs) === true)
326-
// await cleanUpAsync()
306+
await cleanUpAsync()
327307
})
328308

329309
async function symlinkFigmaPluginTypingsAsync(): Promise<void> {

packages/build/test/fixtures/10-esbuild-ui-config/manifest.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/common/src/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ export const constants = {
22
apiVersion: '1.0.0',
33
build: {
44
directoryName: 'build',
5+
mainConfigFilePath: 'build-figma-plugin.main.js',
56
manifestFilePath: 'manifest.json',
67
pluginCodeFilePath: 'build/main.js',
7-
pluginUiFilePath: 'build/ui.js'
8+
pluginUiFilePath: 'build/ui.js',
9+
uiConfigFilePath: 'build-figma-plugin.ui.js'
810
},
911
defaultTemplate: 'default',
1012
packageJson: {

0 commit comments

Comments
 (0)