Skip to content

Commit 6ec67b3

Browse files
committed
feat: update internals to take advantage of webpack 3 module concatenation
BREAKING CHANGE: This is enabled by default and makes `*.vue` files export an ES module by default, so require('foo.vue') will no longer work. Can be turned off by using `esModule: false` in vue-loader options.
1 parent 33880e8 commit 6ec67b3

File tree

8 files changed

+295
-127
lines changed

8 files changed

+295
-127
lines changed

.babelrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"presets": ["es2015"]
2+
"presets": [
3+
["env", { "targets": { "node": 7 }, "modules": false }]
4+
]
35
}

lib/loader.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ module.exports = function (content) {
4949

5050
var loaderContext = this
5151
var query = loaderUtils.getOptions(this) || {}
52-
var options = Object.assign({}, this.options.vue, this.vue, query)
52+
var options = Object.assign({
53+
esModule: !query.inject
54+
}, this.options.vue, this.vue, query)
5355
// #824 avoid multiple webpack runs complaining about unknown option
5456
Object.defineProperty(this.options, '__vueOptions__', {
5557
value: options,

lib/template-compiler/index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,28 @@ module.exports = function (html) {
4545
`\n Error compiling template:\n${pad(html)}\n` +
4646
compiled.errors.map(e => ` - ${e}`).join('\n') + '\n'
4747
)
48-
code = 'module.exports={render:function(){},staticRenderFns:[]}'
48+
code = vueOptions.esModule
49+
? `export function render () {}\nexport var staticRenderFns = []`
50+
: 'module.exports={render:function(){},staticRenderFns:[]}'
4951
} else {
5052
var bubleOptions = options.buble
51-
code = transpile('module.exports={' +
52-
'render:' + toFunction(compiled.render) + ',' +
53-
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
54-
'}', bubleOptions)
53+
code = transpile(
54+
'var render = ' + toFunction(compiled.render) + '\n' +
55+
'var staticRenderFns = [' + compiled.staticRenderFns.map(toFunction).join(',') + ']',
56+
bubleOptions
57+
) + '\n'
5558
// mark with stripped (this enables Vue to use correct runtime proxy detection)
5659
if (!isProduction && (
5760
!bubleOptions ||
5861
!bubleOptions.transforms ||
5962
bubleOptions.transforms.stripWith !== false
6063
)) {
61-
code += `\nmodule.exports.render._withStripped = true`
64+
code += `render._withStripped = true\n`
6265
}
66+
var exports = `{ render: render, staticRenderFns: staticRenderFns }`
67+
code += vueOptions.esModule
68+
? `export default ${exports}`
69+
: `module.exports = ${exports}`
6370
}
6471
// hot-reload
6572
if (!isServer && !isProduction) {
@@ -76,7 +83,7 @@ module.exports = function (html) {
7683
}
7784

7885
function toFunction (code) {
79-
return 'function (){' + beautify(code, {
86+
return 'function () {' + beautify(code, {
8087
indent_size: 2 // eslint-disable-line camelcase
8188
}) + '}'
8289
}

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,24 @@
4141
"source-map": "^0.5.6",
4242
"vue-hot-reload-api": "^2.1.0",
4343
"vue-style-loader": "^3.0.0",
44-
"vue-template-es2015-compiler": "^1.2.2"
44+
"vue-template-es2015-compiler": "^1.5.3"
4545
},
4646
"peerDependencies": {
4747
"css-loader": "*",
4848
"vue-template-compiler": "^2.0.0"
4949
},
5050
"devDependencies": {
51-
"babel-core": "^6.8.0",
52-
"babel-loader": "^6.2.4",
53-
"babel-preset-es2015": "^6.6.0",
51+
"babel-core": "^6.25.0",
52+
"babel-loader": "^7.0.0",
53+
"babel-preset-env": "^1.5.2",
5454
"chai": "^3.0.0",
5555
"coffee-loader": "^0.7.2",
5656
"coffee-script": "^1.10.0",
5757
"css-loader": "^0.26.0",
5858
"eslint": "^3.14.1",
5959
"eslint-plugin-vue-libs": "^1.2.0",
6060
"expose-loader": "^0.7.1",
61-
"extract-text-webpack-plugin": "^2.0.0-rc.0",
61+
"extract-text-webpack-plugin": "^2.1.2",
6262
"file-loader": "^0.10.0",
6363
"inject-loader": "^2.0.0",
6464
"js-yaml": "^3.8.2",
@@ -80,6 +80,6 @@
8080
"vue": "^2.3.2",
8181
"vue-server-renderer": "^2.3.2",
8282
"vue-template-compiler": "^2.3.2",
83-
"webpack": "^2.2.0"
83+
"webpack": "^3.0.0"
8484
}
8585
}

test/fixtures/inject.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
</template>
44

55
<script>
6-
import SomeService from './service'
6+
const SomeService = require('./service')
77
8-
export default {
8+
module.exports = {
99
data () {
1010
return {
1111
msg: SomeService.msg

test/fixtures/ssr-style.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Vue = require('vue')
2-
var App = require('./ssr-style.vue')
2+
var App = require('./ssr-style.vue').default
33

44
module.exports = () => new Vue({
55
render: h => h(App)

test/test.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ var globalConfig = {
2828
loader: loaderPath
2929
}
3030
]
31-
}
31+
},
32+
plugins: [
33+
new webpack.optimize.ModuleConcatenationPlugin()
34+
]
3235
}
3336

3437
function bundle (options, cb) {
@@ -321,7 +324,8 @@ describe('vue-loader', function () {
321324
}, (code, warnings) => {
322325
var css = mfs.readFileSync('/test.output.css').toString()
323326
css = normalizeNewline(css)
324-
expect(css).to.contain('h1 {\n color: #f00;\n}\n\nh2 {\n color: green;\n}')
327+
expect(css).to.contain('h1 {\n color: #f00;\n}')
328+
expect(css).to.contain('h2 {\n color: green;\n}')
325329
done()
326330
})
327331
})
@@ -338,7 +342,8 @@ describe('vue-loader', function () {
338342
}, (code, warnings) => {
339343
var css = mfs.readFileSync('/test.output.css').toString()
340344
css = normalizeNewline(css)
341-
expect(css).to.contain('h1 {\n color: #f00;\n}\n\nh2 {\n color: green;\n}')
345+
expect(css).to.contain('h1 {\n color: #f00;\n}')
346+
expect(css).to.contain('h2 {\n color: green;\n}')
342347
done()
343348
})
344349
})
@@ -356,7 +361,8 @@ describe('vue-loader', function () {
356361
}, (code, warnings) => {
357362
var css = mfs.readFileSync('/test.output.css').toString()
358363
css = normalizeNewline(css)
359-
expect(css).to.contain('h1 {\n color: #f00;\n}\n\nh2 {\n color: green;\n}')
364+
expect(css).to.contain('h1 {\n color: #f00;\n}')
365+
expect(css).to.contain('h2 {\n color: green;\n}')
360366
done()
361367
})
362368
})
@@ -584,7 +590,7 @@ describe('vue-loader', function () {
584590
return m.exports;
585591
}
586592

587-
var output = requireFromString(code, './test.build.js')
593+
var output = interopDefault(requireFromString(code, './test.build.js'))
588594
var mockInstance = {}
589595

590596
output.beforeCreate.forEach(hook => hook.call(mockInstance))

0 commit comments

Comments
 (0)