forked from karlicoss/grasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
113 lines (97 loc) · 3.42 KB
/
rollup.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
import assert from 'assert'
import fs from 'fs'
const { globSync } = import('node:fs')
import path from 'path'
import { fileURLToPath } from 'url'
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import {generateManifest} from './generate_manifest.js'
const env = {
RELEASE: process.env.RELEASE,
PUBLISH: process.env.PUBLISH,
MANIFEST: process.env.MANIFEST,
}
const target = process.env.TARGET; assert(target)
const manifest_version = process.env.MANIFEST; assert(manifest_version)
const ext_id = process.env.EXT_ID; assert(ext_id)
const release = env.RELEASE === 'YES' // TODO use --environment=production for rollup?
const publish = env.PUBLISH === 'YES'
const thisDir = path.dirname(fileURLToPath(import.meta.url)); assert(path.isAbsolute(thisDir))
const srcDir = path.join(thisDir, 'src')
const buildDir = path.join(thisDir, 'dist', target)
// kinda annoying it's not a builtin..
function cleanOutputDir() {
return {
name: 'clean-output-dir',
buildStart(options) {
const outDir = buildDir
// we don't just want to rm -rf outputDir to respect if it's a symlink or something like that
if (!fs.existsSync(outDir)) {
return
}
fs.readdirSync(outDir).forEach(f => {
// console.debug("removing %s", f)
fs.rmSync(path.join(outDir, f), {recursive: true})
})
},
}
}
function generateManifestPlugin() {
return {
name: 'generate-manifest',
generateBundle(outputOptions, bundle) {
const manifest = generateManifest({
target: target,
version: manifest_version,
release: release,
ext_id: ext_id,
})
const mjs = JSON.stringify(manifest, null, 2)
const outputPath = path.join(outputOptions.dir, 'manifest.json')
fs.mkdirSync(outputOptions.dir, { recursive: true })
fs.writeFileSync(outputPath, mjs, 'utf8')
}
}
}
const compile = inputs => { return {
input: inputs,
output: {
dir: buildDir,
// format: 'esm', // default??
// format: 'iife', // inlines? e.g. could use for bg page if we disable splitting..
// huh! so if I build all files in one go, it figures out the shared files properly it seems
// however it still inlines webextension stuff into one of the files? e.g. common
manualChunks: id => { // ugh, seems a bit shit?
if (id.includes('webextension-polyfill')) {
return 'webextension-polyfill' // move it in a separate chunk
}
},
},
plugins: [
cleanOutputDir(),
copy({
targets: [
{src: 'src/**/*.html', dest: buildDir},
{src: 'src/**/*.png' , dest: buildDir},
],
flatten: false,
}),
typescript({
outDir: buildDir,
noEmitOnError: true, // fail on errors
}),
commonjs(), // needed for webext polyfill
nodeResolve(),
generateManifestPlugin(),
],
}}
export default [
compile([
path.join(srcDir, 'background.ts'),
path.join(srcDir, 'options_page.ts'),
path.join(srcDir, 'popup.ts'),
path.join(srcDir, 'detect_dark_mode.ts'),
]),
]