Skip to content

Commit 9ce2eee

Browse files
HerringtonDarkholmeyyx990803
authored andcommitted
add esModule support (#349)
* add alternative module exports * add esModule test and docs
1 parent cd74def commit 9ce2eee

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docs/en/options.md

+8
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@
7373
- type: `Object`
7474

7575
Pass options to the template rendering engine (via [consolidate](https://github.com/tj/consolidate.js)) if you are using a non-html templating language.
76+
77+
### esModule
78+
79+
- ^9.4.3
80+
- type: `Boolean`
81+
- default: `undefined`
82+
83+
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.

lib/loader.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ module.exports = function (content) {
236236
'")}\n'
237237
}
238238
// final export
239-
output += '\nmodule.exports = __vue_exports__\n'
239+
if (options.esModule) {
240+
output += '\nexports.__esModule = true;\nexports["default"] = __vue_exports__\n'
241+
} else {
242+
output += '\nmodule.exports = __vue_exports__\n'
243+
}
240244
} else {
241245
// inject-loader support
242246
output +=

test/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,24 @@ describe('vue-loader', function () {
348348
done()
349349
})
350350
})
351+
352+
it('support es compatible modules', function (done) {
353+
test({
354+
entry: './test/fixtures/basic.vue',
355+
vue: {
356+
esModule: true
357+
}
358+
}, function (window, module, rawModule) {
359+
expect(rawModule.__esModule).to.equal(true)
360+
var vnode = mockRender(rawModule.default, {
361+
msg: 'hi'
362+
})
363+
expect(vnode.tag).to.equal('h2')
364+
expect(vnode.data.staticClass).to.equal('red')
365+
expect(vnode.children[0]).to.equal('hi')
366+
367+
expect(rawModule.default.data().msg).to.contain('Hello from Component A!')
368+
done()
369+
})
370+
})
351371
})

0 commit comments

Comments
 (0)