Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bundle dependencies inline and fix es build #116

Merged
merged 15 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
"README.md",
"dist/index.d.ts"
],
"dependencies": {
"dom-event-types": "^1.0.0",
"lodash": "^4.17.15"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.8.4",
Expand All @@ -26,11 +22,12 @@
"@rollup/plugin-replace": "^2.3.2",
"@types/estree": "^0.0.42",
"@types/jest": "^24.9.1",
"@types/lodash": "^4.14.149",
"@types/node": "12.12.35",
"@vue/compiler-dom": "^3.0.0-beta.10",
"@vue/compiler-sfc": "^3.0.0-beta.10",
"babel-jest": "^25.2.3",
"babel-preset-jest": "^25.2.1",
"dom-event-types": "^1.0.0",
"flush-promises": "^1.0.2",
"husky": "^4.2.3",
"jest": "^25.1.0",
Expand All @@ -48,6 +45,7 @@
"vuex": "^4.0.0-beta.1"
},
"peerDependencies": {
"@vue/compiler-dom": "^3.0.0-beta.10",
"@vue/compiler-sfc": "^3.0.0-beta.10",
"vue": "^3.0.0-beta.10"
},
Expand Down
17 changes: 5 additions & 12 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ function createEntry(options) {
input,
external: [
'vue',
'lodash/mergeWith',
'lodash/isString'
'@vue/compiler-dom'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed in master right now, but will be very soon for this feature when merged, so it's fine to include it here.

],
plugins: [
replace({
Expand All @@ -41,30 +40,25 @@ function createEntry(options) {
format,
globals: {
vue: 'Vue',
'lodash/mergeWith': '_.mergeWith',
'lodash/isString': '_.isString',
'@vue/compiler-dom': 'VueCompilerDOM'
}
}
}

if (['es', 'cjs'].includes(format)) {
config.external.push('dom-event-types')
}

if (format === 'es') {
config.output.file = isBrowser ? pkg.browser : pkg.module
config.output.file = pkg.module
}
if (format === 'cjs') {
config.output.file = pkg.main
}
console.log('file is', config.output.file)
console.log(`Building ${format}: ${config.output.file}`)

config.plugins.push(
ts({
check: format === 'es' && isBrowser,
tsconfigOverride: {
compilerOptions: {
declaration: format === 'es' && isBrowser,
declaration: format === 'es',
target: 'es5', // not sure what this should be?
module: format === 'cjs' ? 'es2015' : 'esnext'
},
Expand All @@ -78,7 +72,6 @@ function createEntry(options) {

export default [
createEntry({ format: 'es', input: 'src/index.ts', isBrowser: false }),
createEntry({ format: 'es', input: 'src/index.ts', isBrowser: true }),
createEntry({ format: 'iife', input: 'src/index.ts', isBrowser: true }),
createEntry({ format: 'cjs', input: 'src/index.ts', isBrowser: false }),
]
2 changes: 1 addition & 1 deletion src/create-dom-event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as eventTypes from 'dom-event-types'
import eventTypes from 'dom-event-types'

interface TriggerOptions {
code?: String
Expand Down
4 changes: 3 additions & 1 deletion src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export function mount(
ref: MOUNT_COMPONENT_REF
})

const global = mergeGlobalProperties(config.global, options?.global)
component.components = { ...component.components, ...global.components }

// create the wrapper component
const Parent = defineComponent({
name: MOUNT_PARENT_NAME,
Expand All @@ -142,7 +145,6 @@ export function mount(

// create the app
const app = createApp(Parent)
const global = mergeGlobalProperties(config.global, options?.global)

// global mocks mixin
if (global?.mocks) {
Expand Down
41 changes: 21 additions & 20 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import isString from 'lodash/isString'
import mergeWith from 'lodash/mergeWith'

import { GlobalMountOptions } from './types'

const isString = (val: unknown): val is string => typeof val === 'string'

// Deep merge function, adapted from from https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
// Merge a `source` object to a `target` recursively
const merge = (target: object, source: object) => {
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
for (const key of Object.keys(source)) {
if (!target[key]) {
target[key] = source[key]
} else {
if (source[key] instanceof Object) {
Object.assign(source[key], merge(target[key], source[key]))
}
}
}

Object.assign(target || {}, source)
}

function mergeGlobalProperties(
configGlobal: GlobalMountOptions = {},
mountGlobal: GlobalMountOptions = {}
): GlobalMountOptions {
return mergeWith(
{},
configGlobal,
mountGlobal,
(objValue, srcValue, key: keyof GlobalMountOptions) => {
switch (key) {
case 'mocks':
case 'provide':
case 'components':
case 'directives':
return { ...objValue, ...srcValue }
case 'plugins':
case 'mixins':
return [...(objValue || []), ...(srcValue || [])].filter(Boolean)
}
}
)
merge(configGlobal, mountGlobal)
return configGlobal
}

export { isString, mergeGlobalProperties }
24 changes: 18 additions & 6 deletions tests/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { h } from 'vue'
import { config, mount } from '../src'
import Hello from './components/Hello.vue'

Expand All @@ -15,16 +16,27 @@ describe('config', () => {

describe('components', () => {
const Component = {
template: '<div>{{ msg }} <hello/></div>',
components: { Hello },
template: '<div>{{ msg }} <Hello /></div>',
props: ['msg']
}

it('allows setting components globally', () => {
config.global.components = { Hello }
const wrapper1 = mount(Component, { props: { msg: 'Wrapper1' } })
const wrapper2 = mount(Component, { props: { msg: 'Wrapper2' } })
expect(wrapper1.text()).toEqual('Wrapper1 Hello world')
expect(wrapper2.text()).toEqual('Wrapper2 Hello world')
const HelloLocal = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test if the locally registered component is overwritten properly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this in the other test (I think?)

I will check.

name: 'Hello',
render() {
return h('div', 'Hello Local')
}
}
config.global.components = { Hello: HelloLocal }
const wrapper1 = mount(Component, {
props: { msg: 'Wrapper1' }
})
const wrapper2 = mount(Component, {
props: { msg: 'Wrapper2' }
})
expect(wrapper1.text()).toEqual('Wrapper1 Hello Local')
expect(wrapper2.text()).toEqual('Wrapper2 Hello Local')
})

it('allows overwriting globally set component config on a per mount instance', () => {
Expand Down
8 changes: 8 additions & 0 deletions tests/mountingOptions/stubs.global.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import ComponentWithoutName from '../components/ComponentWithoutName.vue'
import ComponentWithSlots from '../components/ComponentWithSlots.vue'

describe('mounting options: stubs', () => {
beforeEach(() => {
config.global.stubs = {}
})

afterEach(() => {
config.global.stubs = {}
})

it('handles Array syntax', () => {
const Foo = {
name: 'Foo',
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,6 @@
dependencies:
jest-diff "^24.3.0"

"@types/lodash@^4.14.149":
version "4.14.150"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.150.tgz#649fe44684c3f1fcb6164d943c5a61977e8cf0bd"
integrity sha512-kMNLM5JBcasgYscD9x/Gvr6lTAv2NVgsKtet/hm93qMyf/D1pt+7jeEZklKJKxMVmXjxbRVQQGfqDSfipYCO6w==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -1204,7 +1199,7 @@
estree-walker "^0.8.1"
source-map "^0.6.1"

"@vue/compiler-dom@3.0.0-beta.10":
"@vue/compiler-dom@3.0.0-beta.10", "@vue/compiler-dom@^3.0.0-beta.10":
version "3.0.0-beta.10"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.10.tgz#5b35df447eb96cb7faed37b76a8a9aca71a87c67"
integrity sha512-S1Qqc74Hc3BnHjORzWJvG4Fj5B4O8aqTF1Oyd+Px65CB6qkbAaqTLneYnM5by/78j8inmt4FCHOf48L+gzChRA==
Expand Down