Skip to content

Commit

Permalink
add esModule support (#349)
Browse files Browse the repository at this point in the history
* add alternative module exports

* add esModule test and docs
  • Loading branch information
HerringtonDarkholme authored and yyx990803 committed Sep 14, 2016
1 parent cd74def commit 9ce2eee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/en/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@
- type: `Object`

Pass options to the template rendering engine (via [consolidate](https://github.com/tj/consolidate.js)) if you are using a non-html templating language.

### esModule

- ^9.4.3
- type: `Boolean`
- default: `undefined`

Whether to emit esModule compatible code. By default vue-loader will emit default export in commonjs format like `module.exports = ....`. When `esModule` is set to true, default export will be transpiled into `exports.__esModule = true; exports = ...`. Useful for interoperating with transpiler other than Bable, like TypeScript.
6 changes: 5 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ module.exports = function (content) {
'")}\n'
}
// final export
output += '\nmodule.exports = __vue_exports__\n'
if (options.esModule) {
output += '\nexports.__esModule = true;\nexports["default"] = __vue_exports__\n'
} else {
output += '\nmodule.exports = __vue_exports__\n'
}
} else {
// inject-loader support
output +=
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,24 @@ describe('vue-loader', function () {
done()
})
})

it('support es compatible modules', function (done) {
test({
entry: './test/fixtures/basic.vue',
vue: {
esModule: true
}
}, function (window, module, rawModule) {
expect(rawModule.__esModule).to.equal(true)
var vnode = mockRender(rawModule.default, {
msg: 'hi'
})
expect(vnode.tag).to.equal('h2')
expect(vnode.data.staticClass).to.equal('red')
expect(vnode.children[0]).to.equal('hi')

expect(rawModule.default.data().msg).to.contain('Hello from Component A!')
done()
})
})
})

0 comments on commit 9ce2eee

Please sign in to comment.