diff --git a/lib/loader.js b/lib/loader.js index 4cb2135..c4d00c3 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -24,6 +24,7 @@ var hasBuble = !!tryRequire('buble-loader') // for mp js var { compileMP, compileMPScript } = require('./mp-compiler') +var { defaultStylePart } = require('./mp-compiler/util') var rewriterInjectRE = /\b(css(?:-loader)?(?:\?[^!]+)?)(?:!|$)/ @@ -99,6 +100,12 @@ module.exports = function (content) { var output = '' var parts = parse(content, fileName, this.sourceMap) + + // fix #153: 根组件没有 style 模块,不生成页面的 wxss,补齐内容方便加载 vendor.wxss + if (!parts.styles.length) { + parts.styles.push(defaultStylePart) + } + var hasScoped = parts.styles.some(function (s) { return s.scoped }) var hasComment = parts.template && parts.template.attrs && parts.template.attrs.comments diff --git a/lib/mp-compiler/util.js b/lib/mp-compiler/util.js index b4caea6..80b064a 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -114,7 +114,23 @@ function getPathPrefix (src) { return `${'../'.repeat(length)}` } +const defaultStylePart = { + type: 'style', + content: '\n', + start: 0, + attrs: {}, + end: 1, + map: { + version: 3, + sources: [], + names: [], + mappings: '', + sourcesContent: [] + } +} + module.exports = { + defaultStylePart, cacheFileInfo, getFileInfo, getCompNameBySrc, diff --git a/lib/selector.js b/lib/selector.js index 7a72775..facb9e0 100644 --- a/lib/selector.js +++ b/lib/selector.js @@ -5,6 +5,7 @@ var path = require('path') var parse = require('./parser') var loaderUtils = require('loader-utils') +var { defaultStylePart } = require('./mp-compiler/util') module.exports = function (content) { this.cacheable() @@ -16,5 +17,6 @@ module.exports = function (content) { if (Array.isArray(part)) { part = part[query.index] } + part = part || defaultStylePart this.callback(null, part.content, part.map) } diff --git a/package.json b/package.json index e84b5ac..68741d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpvue-loader", - "version": "1.0.8", + "version": "1.0.9", "description": "mpvue single-file component loader for Webpack", "main": "index.js", "repository": { @@ -67,7 +67,7 @@ }, "peerDependencies": { "css-loader": "*", - "mpvue-template-compiler": "^1.0.6" + "mpvue-template-compiler": "^1.0.7" }, "devDependencies": { "babel-core": "^6.25.0", @@ -90,7 +90,7 @@ "marked": "^0.3.6", "memory-fs": "^0.4.1", "mkdirp": "^0.5.1", - "mpvue-template-compiler": "^1.0.6", + "mpvue-template-compiler": "^1.0.7", "mocha": "^3.4.2", "node-libs-browser": "^2.0.0", "normalize-newline": "^3.0.0",