From 0fe84a947d2060da6eea1c97ad5cdfed7584db0d Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Tue, 13 Sep 2016 19:22:17 +0800 Subject: [PATCH 1/2] add alternative module exports --- lib/loader.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/loader.js b/lib/loader.js index 196d8307d..abd3d7ba0 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -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 += From 41c339fa8efeee101fa31c45c51dfc8b4a5522dd Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Tue, 13 Sep 2016 22:59:09 +0800 Subject: [PATCH 2/2] add esModule test and docs --- docs/en/options.md | 8 ++++++++ test/test.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/docs/en/options.md b/docs/en/options.md index 61a1f2b47..d11e1eefd 100644 --- a/docs/en/options.md +++ b/docs/en/options.md @@ -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. diff --git a/test/test.js b/test/test.js index abe47a253..056599c18 100644 --- a/test/test.js +++ b/test/test.js @@ -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() + }) + }) })