-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for bundling using Rollup (#59)
Co-authored-by: Jason Miller <developit@users.noreply.github.com>
- Loading branch information
1 parent
6be5fa7
commit 4346b0b
Showing
28 changed files
with
466 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function render(container) { | ||
let div = document.createElement('div'); | ||
div.id = 'hello-world'; | ||
div.textContent = 'Hello World!'; | ||
|
||
container.appendChild(div); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { render } from './index'; | ||
|
||
describe('Hello World', () => { | ||
it('should not be bundled using webpack', () => { | ||
// eslint-disable-next-line camelcase | ||
expect(typeof __webpack_require__).toBe('undefined'); | ||
}); | ||
|
||
it('should be rendered to container', () => { | ||
render(document.body); | ||
|
||
let element = document.getElementById('hello-world'); | ||
expect(element).toBeTruthy(); | ||
expect(element.textContent).toBe('Hello World!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "karmatic-e2e-default", | ||
"description": "Test default config in karmatic.", | ||
"private": true, | ||
"scripts": { | ||
"test:debug": "cross-env NODE_PRESERVE_SYMLINKS_MAIN=1 NODE_PRESERVE_SYMLINKS=1 node ./node_modules/karmatic/dist/cli.js debug" | ||
}, | ||
"dependencies": { | ||
"karmatic": "file:../.." | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function box(value) { | ||
return { _value: value }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { box } from './index'; | ||
|
||
describe('Box', () => { | ||
it('should not be bundled using webpack', () => { | ||
// eslint-disable-next-line camelcase | ||
expect(typeof __webpack_require__).toBe('undefined'); | ||
}); | ||
|
||
it('should have a __v property', () => { | ||
const boxed = box(1); | ||
expect('_value' in boxed).toBe(false); | ||
expect(boxed.__v).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "karmatic-e2e-rollup-custom", | ||
"description": "Test custom rollup config in karmatic", | ||
"private": true, | ||
"scripts": { | ||
"test:debug": "cross-env NODE_PRESERVE_SYMLINKS_MAIN=1 NODE_PRESERVE_SYMLINKS=1 node ./node_modules/karmatic/dist/cli.js debug" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.10.3", | ||
"@rollup/plugin-babel": "5.2.0", | ||
"@rollup/plugin-commonjs": "^14.0.0", | ||
"@rollup/plugin-node-resolve": "^8.4.0", | ||
"babel-plugin-transform-rename-properties": "^0.1.0", | ||
"rollup": "^2.3.0", | ||
"karmatic": "file:../../" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import nodeResolve from '@rollup/plugin-node-resolve'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import babel from '@rollup/plugin-babel'; | ||
|
||
export default { | ||
input: 'index.js', | ||
output: { | ||
dir: 'dist', | ||
format: 'es', | ||
}, | ||
plugins: [ | ||
babel({ | ||
babelHelpers: 'bundled', | ||
plugins: [ | ||
[ | ||
'babel-plugin-transform-rename-properties', | ||
{ rename: { _value: '__v' } }, | ||
], | ||
], | ||
}), | ||
nodeResolve(), | ||
commonjs(), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "karmatic-e2e-rollup-default", | ||
"description": "Test default rollup config in karmatic. Mildly complex src implementation to verify coverage works", | ||
"private": true, | ||
"scripts": { | ||
"test:debug": "cross-env NODE_PRESERVE_SYMLINKS_MAIN=1 NODE_PRESERVE_SYMLINKS=1 node ./node_modules/karmatic/dist/cli.js debug" | ||
}, | ||
"dependencies": { | ||
"rollup": "^2.3.0", | ||
"karmatic": "file:../.." | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export function combine(input1, input2) { | ||
const isInput1Array = Array.isArray(input1); | ||
const isInput2Array = Array.isArray(input2); | ||
|
||
if (typeof input1 !== typeof input2 || isInput1Array !== isInput2Array) { | ||
const input1Type = isInput1Array ? 'array' : typeof input1; | ||
const input2Type = isInput2Array ? 'array' : typeof input2; | ||
throw new Error( | ||
`Types of inputs are not the same: input1=${input1Type}, input2=${input2Type}` | ||
); | ||
} | ||
|
||
if (typeof input1 == 'string' || typeof input1 == 'number') { | ||
return input1 + input2; | ||
} | ||
|
||
if (isInput1Array) { | ||
return [...input1, ...input2]; | ||
} | ||
|
||
if (typeof input1 == 'object') { | ||
return { | ||
...input1, | ||
...input2, | ||
}; | ||
} | ||
|
||
throw new Error(`Unsupported type: ${typeof input1}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { combine } from '../src/index'; | ||
|
||
describe('combine', () => { | ||
it('should concatenate strings', () => { | ||
expect(combine('a', 'b')).toBe('ab'); | ||
}); | ||
|
||
it('should add numbers', () => { | ||
expect(combine(1, 2)).toBe(3); | ||
}); | ||
|
||
it('should concatenate arrays', () => { | ||
expect(combine([1, 2], ['a', 'b'])).toEqual([1, 2, 'a', 'b']); | ||
}); | ||
|
||
it('should merge objects', () => { | ||
expect(combine({ a: 1, b: 2 }, { c: 'c', d: 'd', a: 'a' })).toEqual({ | ||
a: 'a', | ||
b: 2, | ||
c: 'c', | ||
d: 'd', | ||
}); | ||
}); | ||
|
||
it('throw an error if types do not match', () => { | ||
expect(() => combine('a', 1)).toThrow(); | ||
expect(() => combine(1, 'a')).toThrow(); | ||
|
||
expect(() => combine([1], 1)).toThrow(); | ||
expect(() => combine(1, [1])).toThrow(); | ||
|
||
expect(() => combine({}, 2)).toThrow(); | ||
expect(() => combine(1, {})).toThrow(); | ||
}); | ||
|
||
it('throw an error if type is unsupported', () => { | ||
expect(() => | ||
combine( | ||
() => {}, | ||
() => {} | ||
) | ||
).toThrow(); | ||
|
||
expect(() => combine(true, false)).toThrow(); | ||
|
||
expect(() => combine(Symbol.for('a'), Symbol.for('b'))).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** @jsx createElement */ | ||
|
||
describe('Custom JSX Pragma', () => { | ||
it('should use custom babel config', () => { | ||
let h = jasmine.createSpy('h'); | ||
let createElement = jasmine.createSpy('createElement'); | ||
let React = { createElement: jasmine.createSpy('React.createElement') }; | ||
|
||
<div id="foo">hello</div>; | ||
|
||
expect(h).not.toHaveBeenCalled(); | ||
expect(React.createElement).not.toHaveBeenCalled(); | ||
expect(createElement).toHaveBeenCalledWith('div', { id: 'foo' }, 'hello'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); | ||
|
||
describe('Basic test functions', () => { | ||
it('should be not bundled using webpack', () => { | ||
// eslint-disable-next-line camelcase | ||
expect(typeof __webpack_require__).toBe('undefined'); | ||
}); | ||
|
||
it('should work', () => { | ||
expect(1).toEqual(1); | ||
}); | ||
|
||
it('should handle deep equality', () => { | ||
expect({ foo: 1 }).toEqual({ foo: 1 }); | ||
}); | ||
|
||
it('should handle async tests', async () => { | ||
let start = Date.now(); | ||
await sleep(100); | ||
|
||
let now = Date.now(); | ||
expect(now - start).toBeGreaterThan(50); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
describe('jest-style', () => { | ||
describe('not.stringContaining', () => { | ||
const expected = 'Hello world!'; | ||
|
||
it('matches if the received value does not contain the expected substring', () => { | ||
expect('How are you?').toEqual(expect.not.stringContaining(expected)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.