forked from Uniswap/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.cosmos.js
84 lines (74 loc) · 2.33 KB
/
webpack.cosmos.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
/* eslint-disable @typescript-eslint/no-var-requires */
const EventEmitter = require('events')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const rollup = require('rollup')
const { config: rollupConfig, assets: assetConfigs } = require('./rollup.config.js')
const path = require('path')
process.env.NODE_ENV = 'cosmos'
require('dotenv').config()
class RollupPlugin extends EventEmitter {
static PLUGIN_NAME = 'rollup-watch'
constructor({ config, assetConfigs, watch }) {
super()
this.initialized = Promise.all([
...assetConfigs.map(this.rollup),
new Promise((resolve) => {
if (watch) {
this.watch(config)
this.on('START', () => console.log('rollup building...'))
.on('BUNDLE_END', ({ input, duration }) => console.log(`rollup built ${input} in ${duration}ms`))
.once('END', resolve)
} else {
this.rollup(config).then(resolve)
}
}),
])
}
async rollup(config) {
const build = await rollup.rollup(config)
await build.write(config.output)
}
watch(config) {
rollup.watch(config).on('event', (e) => {
this.emit(e.code, e)
if (e.result) {
e.result.close()
}
})
}
apply(compiler) {
// Wait for rollup to generate initial assets before compilation.
compiler.hooks.beforeCompile.tapPromise(RollupPlugin.PLUGIN_NAME, () => this.initialized)
// It's possible to invalidate the watcher on BUILD_END, but that will duplicate builds due to webpack's watcher.
// It's also possible to ignore dist/ in webpack's watcher, and explicitly update the files in webpack, but it would
// be prohibitively complex. Instead, webpack just watches rollup's output.
}
}
module.exports = (webpackConfig) => {
const { mode, module, resolve } = webpackConfig
const { rules } = module
return {
...webpackConfig,
resolve: {
...resolve,
alias: {
'@uniswap/widgets': path.resolve(__dirname, 'dist/'),
},
},
module: {
rules: [
...rules,
{
test: /\.json$/i,
type: 'javascript/auto',
use: ['json-loader'],
},
],
},
plugins: [
new RollupPlugin({ config: rollupConfig, assetConfigs, watch: mode !== 'production' }),
new HtmlWebpackPlugin(),
],
stats: 'errors-warnings',
}
}