Skip to content

Commit 737734a

Browse files
committed
supported weex
[todo] support hot reload support functional check [tips] not support CSS Modules not support CssSourceMap not support isServer not support isScoped not support img[src] not support vueOptions.transformToRequire not support vueOptions.preserveWhitespace
1 parent 92d97a6 commit 737734a

File tree

6 files changed

+212
-142
lines changed

6 files changed

+212
-142
lines changed

lib/loader.js

Lines changed: 105 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// @todo: support hot reload
2+
// @todo: support functional check
3+
// @tips: not support CSS Modules
4+
// @tips: not support CssSourceMap
5+
// @tips: not support isServer
6+
// @tips: not support isScoped
7+
18
var loaderUtils = require('loader-utils')
29
var assign = require('object-assign')
310
var parse = require('./parser')
@@ -10,10 +17,12 @@ var selectorPath = normalize.lib('selector')
1017
var templateLoaderPath = normalize.lib('template-loader')
1118
var templateCompilerPath = normalize.lib('template-compiler')
1219
var styleRewriterPath = normalize.lib('style-rewriter')
20+
var styleLoaderPath = normalize.lib('style-loader')
21+
var scriptLoaderPath = normalize.lib('script-loader')
1322

1423
// dep loaders
15-
var styleLoaderPath = normalize.dep('vue-style-loader')
16-
var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
24+
// var styleLoaderPath = normalize.dep('vue-style-loader')
25+
// var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
1726

1827
var hasBabel = false
1928
try {
@@ -39,9 +48,11 @@ var checkNamedExports =
3948

4049
module.exports = function (content) {
4150
this.cacheable()
42-
var isServer = this.options.target === 'node'
51+
// var isServer = this.options.target === 'node'
52+
// var isServer = false
4353
var loaderContext = this
4454
var query = loaderUtils.parseQuery(this.query)
55+
var params = loaderUtils.parseQuery(this.resourceQuery)
4556
var options = this.options.__vueOptions__ = Object.assign({}, this.options.vue, this.vue, query)
4657
var filePath = this.resourcePath
4758
var fileName = path.basename(filePath)
@@ -50,16 +61,18 @@ module.exports = function (content) {
5061

5162
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
5263

53-
var needCssSourceMap =
54-
!isProduction &&
55-
this.sourceMap &&
56-
options.cssSourceMap !== false
64+
// var needCssSourceMap =
65+
// !isProduction &&
66+
// this.sourceMap &&
67+
// options.cssSourceMap !== false
68+
// var needCssSourceMap = false
5769

5870
var bubleOptions = hasBuble && options.buble ? '?' + JSON.stringify(options.buble) : ''
5971
var defaultLoaders = {
6072
html: templateCompilerPath + '?id=' + moduleId,
61-
css: (isServer ? '' : styleLoaderPath + '!') + 'css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
62-
js: hasBuble ? ('buble-loader' + bubleOptions) : hasBabel ? 'babel-loader' : ''
73+
// css: (isServer ? '' : styleLoaderPath + '!') + 'css-loader' + (needCssSourceMap ? '?sourceMap' : ''),
74+
css: styleLoaderPath,
75+
js: scriptLoaderPath + '!' + (hasBuble ? ('buble-loader' + bubleOptions) : hasBabel ? 'babel-loader' : '')
6376
}
6477

6578
// check if there are custom loaders specified via
@@ -185,45 +198,50 @@ module.exports = function (content) {
185198
}
186199

187200
var parts = parse(content, fileName, this.sourceMap)
188-
var hasScoped = parts.styles.some(function (s) { return s.scoped })
201+
// var hasScoped = parts.styles.some(function (s) { return s.scoped })
189202
var output = 'var __vue_exports__, __vue_options__\n'
190203

191204
// css modules
192-
output += 'var __vue_styles__ = {}\n'
193-
var cssModules = {}
205+
// output += 'var __vue_styles__ = {}\n'
206+
output += 'var __vue_styles__ = []\n'
207+
// var cssModules = {}
194208

195209
// add requires for styles
196210
if (parts.styles.length) {
197211
output += '\n/* styles */\n'
198212
parts.styles.forEach(function (style, i) {
199-
var moduleName = (style.module === true) ? '$style' : style.module
213+
// var moduleName = (style.module === true) ? '$style' : style.module
214+
// var moduleName
200215

201216
// require style
202-
if (isServer && !moduleName) return
217+
// if (isServer && !moduleName) return
203218
var requireString = style.src
204219
? getRequireForImport('styles', style, style.scoped)
205220
: getRequire('styles', style, i, style.scoped)
206221

207222
// setCssModule
208-
if (moduleName) {
209-
if (moduleName in cssModules) {
210-
loaderContext.emitError('CSS module name "' + moduleName + '" is not unique!')
211-
output += requireString
212-
} else {
213-
cssModules[moduleName] = true
214-
215-
// `style-loader` exposes the name-to-hash map directly
216-
// `css-loader` exposes it in `.locals`
217-
// We drop `style-loader` in SSR, and add `.locals` here.
218-
if (isServer) {
219-
requireString += '.locals'
220-
}
221-
222-
output += '__vue_styles__["' + moduleName + '"] = ' + requireString + '\n'
223-
}
224-
} else {
225-
output += requireString
226-
}
223+
// if (moduleName) {
224+
// if (moduleName in cssModules) {
225+
// loaderContext.emitError('CSS module name "' + moduleName + '" is not unique!')
226+
// output += requireString
227+
// } else {
228+
// cssModules[moduleName] = true
229+
230+
// // `style-loader` exposes the name-to-hash map directly
231+
// // `css-loader` exposes it in `.locals`
232+
// // We drop `style-loader` in SSR, and add `.locals` here.
233+
// // if (isServer) {
234+
// // requireString += '.locals'
235+
// // }
236+
237+
// output += '__vue_styles__["' + moduleName + '"] = ' + requireString + '\n'
238+
// }
239+
// } else {
240+
// output += requireString
241+
// }
242+
243+
// output += requireString
244+
output += '__vue_styles__.push(' + requireString + ')'
227245
})
228246
}
229247

@@ -272,53 +290,60 @@ module.exports = function (content) {
272290
}
273291

274292
// attach scoped id
275-
if (hasScoped) {
276-
exports += '__vue_options__._scopeId = "' + moduleId + '"\n'
277-
}
278-
279-
if (Object.keys(cssModules).length) {
280-
// inject style modules as computed properties
281-
exports +=
282-
'if (!__vue_options__.computed) __vue_options__.computed = {}\n' +
283-
'Object.keys(__vue_styles__).forEach(function (key) {\n' +
284-
'var module = __vue_styles__[key]\n' +
285-
'__vue_options__.computed[key] = function () { return module }\n' +
286-
'})\n'
287-
}
293+
// if (hasScoped) {
294+
// exports += '__vue_options__._scopeId = "' + moduleId + '"\n'
295+
// }
296+
297+
// if (Object.keys(cssModules).length) {
298+
// // inject style modules as computed properties
299+
// exports +=
300+
// 'if (!__vue_options__.computed) __vue_options__.computed = {}\n' +
301+
// 'Object.keys(__vue_styles__).forEach(function (key) {\n' +
302+
// 'var module = __vue_styles__[key]\n' +
303+
// '__vue_options__.computed[key] = function () { return module }\n' +
304+
// '})\n'
305+
// }
306+
307+
exports += '__vue_options__.style = __vue_options__.style || {}\n' +
308+
'__vue_styles__.forEach(function (module) {\n' +
309+
'for (var name in module) {\n' +
310+
'__vue_options__.style[name] = module[name]\n' +
311+
'}\n' +
312+
'})\n'
288313

289314
if (!query.inject) {
290315
output += exports
291316
// hot reload
292-
if (
293-
!isServer &&
294-
!isProduction &&
295-
(parts.script || parts.template)
296-
) {
297-
output +=
298-
'\n/* hot reload */\n' +
299-
'if (module.hot) {(function () {\n' +
300-
' var hotAPI = require("' + hotReloadAPIPath + '")\n' +
301-
' hotAPI.install(require("vue"), false)\n' +
302-
' if (!hotAPI.compatible) return\n' +
303-
' module.hot.accept()\n' +
304-
' if (!module.hot.data) {\n' +
305-
// initial insert
306-
' hotAPI.createRecord("' + moduleId + '", __vue_options__)\n' +
307-
' } else {\n' +
308-
// update
309-
' hotAPI.reload("' + moduleId + '", __vue_options__)\n' +
310-
' }\n' +
311-
'})()}\n'
312-
}
317+
// if (
318+
// !isServer &&
319+
// !isProduction &&
320+
// (parts.script || parts.template)
321+
// ) {
322+
// output +=
323+
// '\n/* hot reload */\n' +
324+
// 'if (module.hot) {(function () {\n' +
325+
// ' var hotAPI = require("' + hotReloadAPIPath + '")\n' +
326+
// ' hotAPI.install(require("vue"), false)\n' +
327+
// ' if (!hotAPI.compatible) return\n' +
328+
// ' module.hot.accept()\n' +
329+
// ' if (!module.hot.data) {\n' +
330+
// // initial insert
331+
// ' hotAPI.createRecord("' + moduleId + '", __vue_options__)\n' +
332+
// ' } else {\n' +
333+
// // update
334+
// ' hotAPI.reload("' + moduleId + '", __vue_options__)\n' +
335+
// ' }\n' +
336+
// '})()}\n'
337+
// }
313338
// check functional
314-
if (!isProduction) {
315-
output +=
316-
'if (__vue_options__.functional) {console.error("' +
317-
'[vue-loader] ' + fileName + ': functional components are not ' +
318-
'supported and should be defined in plain js files using render ' +
319-
'functions.' +
320-
'")}\n'
321-
}
339+
// if (!isProduction) {
340+
// output +=
341+
// 'if (__vue_options__.functional) {console.error("' +
342+
// '[vue-loader] ' + fileName + ': functional components are not ' +
343+
// 'supported and should be defined in plain js files using render ' +
344+
// 'functions.' +
345+
// '")}\n'
346+
// }
322347
// final export
323348
if (options.esModule) {
324349
output += '\nexports.__esModule = true;\nexports["default"] = __vue_exports__\n'
@@ -336,6 +361,11 @@ module.exports = function (content) {
336361
'}'
337362
}
338363

364+
if (params.entry) {
365+
output += 'module.exports.el = \'body\'\n' +
366+
'new Vue(module.exports)\n'
367+
}
368+
339369
// done
340370
return output
341371
}

lib/script-loader.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var REQUIRE_REG = /require\((["'])@weex\-module\/([^\)\1]+)\1\)/g
2+
3+
module.exports = function (content) {
4+
this.cacheable && this.cacheable()
5+
return content.replace(REQUIRE_REG, '__weex_require_module__($1$2$1)')
6+
}

lib/style-loader.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var styler = require('weex-styler')
2+
3+
module.exports = function (content) {
4+
this.cacheable && this.cacheable()
5+
return 'module.exports = ' + genStyleString(content)
6+
}
7+
8+
function genStyleString (input) {
9+
var output = '{}'
10+
styler.parse(input, function (err, obj) {
11+
if (err) {
12+
return
13+
}
14+
if (obj && obj.jsonStyle) {
15+
try {
16+
output = JSON.stringify(obj.jsonStyle, null, 2)
17+
} catch (e) {}
18+
}
19+
})
20+
return output
21+
}

0 commit comments

Comments
 (0)