Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
add hmr options, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aelbore committed Apr 30, 2020
1 parent 8f2ab82 commit 4559435
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 50 deletions.
39 changes: 21 additions & 18 deletions src/build/hmr-build.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { hmrPlugin, watch as rollupWatch } from '../libs'
import { CreateRollupConfigOptions, _createTSRollupConfig } from '../config/config'
import { CreateRollupConfigOptions, _createTSRollupConfig, RollupConfigBase } from '../config/config'

import { rollupBuild } from './build'

export async function hmrBuild(options: CreateRollupConfigOptions) {
const config = options.config as RollupConfigBase

const { inputOptions, outputOptions } = _createTSRollupConfig(options)
await rollupBuild({ inputOptions, outputOptions })

const { input, plugins, watch } = inputOptions
const { sourcemap, file, format } = outputOptions

rollupWatch({
input,
plugins: [
...(plugins as any[]),
hmrPlugin({
public: 'public',
clearConsole: false,
write: true
})
],
watch,
output: {
sourcemap,
file,
format
}
})
config.hmr &&
rollupWatch({
input,
plugins: [
...(plugins as any[]),
hmrPlugin({
public: 'public',
clearConsole: false,
write: true
})
],
watch,
output: {
sourcemap,
file,
format
}
})
}
84 changes: 55 additions & 29 deletions src/build/hrm-build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,8 @@ import { hmrBuild } from './hmr-build'
describe('hrmBuild', () => {
let libs: typeof import('../libs')
let config: typeof import('../config/config')

before(async () => {
[ libs, config ] = await Promise.all([ import('../libs'), import('../config/config') ])
})

afterEach(() => {
sinon.restore()
mockfs.restore()
})

it('should build and watch with config', async () => {
mockfs({
'dist': {},
'./src/input.ts': `import * as fs from 'fs'`
})

const name = 'aria-hmr'

const configOptions: TSRollupConfig = {
input: './src/index.js',
external: [ 'fs' ],
plugins: [],
output: {
file: 'public/bundle.js',
sourcemap: true,
format: 'es'
}
}

function createStubs(configOptions: TSRollupConfig) {
const options = {
inputOptions: {
input: './src/input.ts',
Expand All @@ -56,10 +29,63 @@ describe('hrmBuild', () => {
const watchStub = sinon.stub(libs, 'watch').returns(void 0)
const hmrPluginStub = sinon.stub(libs, 'hmrPlugin').returns(void 0)

return { createTSRollupConfigStub, watchStub, hmrPluginStub }
}

function createConfigOptions(opts?: TSRollupConfig) {
const configOptions: TSRollupConfig = {
input: './src/index.js',
external: [ 'fs' ],
plugins: [],
output: {
file: 'public/bundle.js',
sourcemap: true,
format: 'es'
},
...(opts ?? {})
}
return configOptions
}

before(async () => {
[ libs, config ] = await Promise.all([ import('../libs'), import('../config/config') ])
})

beforeEach(() => {
mockfs({
'dist': {},
'./src/input.ts': `import * as fs from 'fs'`
})
})

afterEach(() => {
sinon.restore()
mockfs.restore()
})

it('should build and watch with config hmr disabled', async () => {
const name = 'aria-hmr'

const configOptions = createConfigOptions()
const { watchStub, createTSRollupConfigStub, hmrPluginStub } = createStubs(configOptions)

await hmrBuild({ config: configOptions, name })

expect(createTSRollupConfigStub.called).toBeTrue()
expect(watchStub.called).toBeFalse()
expect(hmrPluginStub.called).toBeFalse()
})

it('should build and watch with config hmr enable', async () => {
const name = 'aria-hmr'

const configOptions = createConfigOptions({ hmr: true })
const { watchStub, createTSRollupConfigStub, hmrPluginStub } = createStubs(configOptions)

await hmrBuild({ config: configOptions, name })

expect(watchStub.called).toBeTrue()
expect(createTSRollupConfigStub.called).toBeTrue()
expect(watchStub.called).toBeTrue()
expect(hmrPluginStub.called).toBeTrue()
})

Expand Down
1 change: 1 addition & 0 deletions src/config/base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface RollupConfigBase {
replace?: KeyValue
compress?: boolean
watch?: WatcherOptions
hmr?: boolean
}

export interface CreateRollupConfigOptions {
Expand Down
5 changes: 2 additions & 3 deletions src/config/ts-rollup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function createTSRollupConfig(options: TSRollupConfig) {

export function _createTSRollupConfig(options: CreateRollupConfigOptions) {
const { config, name } = options
const { resolveOpts, commonOpts, input, tsconfig, watch } = config as TSRollupConfig
const { resolveOpts, commonOpts, input, tsconfig } = config as TSRollupConfig

const _plugins = (config as TSRollupConfig).plugins
const beforePlugins = Array.isArray(_plugins)
Expand All @@ -107,8 +107,7 @@ export function _createTSRollupConfig(options: CreateRollupConfigOptions) {

const configOptions: RollupConfigBase = {
...config,
plugins,
watch
plugins
}

const { inputOptions, outputOptions } = createRollupConfig({ config: configOptions, name })
Expand Down

0 comments on commit 4559435

Please sign in to comment.