From 611551757f3c63949d8fb6892528841e1d01616b Mon Sep 17 00:00:00 2001 From: aorz Date: Wed, 14 Mar 2018 15:53:46 +0800 Subject: [PATCH 1/5] new version 1.0.4 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 08ad212..a688dc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpvue-loader", - "version": "1.0.2", + "version": "1.0.4", "description": "mpvue single-file component loader for Webpack", "main": "index.js", "repository": { @@ -67,7 +67,7 @@ }, "peerDependencies": { "css-loader": "*", - "mpvue-template-compiler": "^1.0.1" + "mpvue-template-compiler": "^1.0.3" }, "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.1", + "mpvue-template-compiler": "^1.0.3", "mocha": "^3.4.2", "node-libs-browser": "^2.0.0", "normalize-newline": "^3.0.0", From df745c763f36e83e00449b1e6b21b5fdd8b63eb1 Mon Sep 17 00:00:00 2001 From: aorz Date: Thu, 15 Mar 2018 08:31:36 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=94=AF=E6=8C=81=20es5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/templates.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mp-compiler/templates.js b/lib/mp-compiler/templates.js index ff59787..740dfae 100644 --- a/lib/mp-compiler/templates.js +++ b/lib/mp-compiler/templates.js @@ -2,9 +2,9 @@ function genScript (name, isPage) { const prefix = isPage ? '../..' : '.' return ` - import '${prefix}/static/js/manifest' - import '${prefix}/static/js/vendor' - import '${prefix}/static/js/${name}' + require('${prefix}/static/js/manifest') + require('${prefix}/static/js/vendor') + require('${prefix}/static/js/${name}') ` } From 41343b739b17c039ecb716264d3dac29aabb098f Mon Sep 17 00:00:00 2001 From: anchengjian Date: Thu, 15 Mar 2018 22:21:08 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E8=87=AA=E5=AE=9A=E4=B9=89=20entry?= =?UTF-8?q?=20key=20=E4=B8=BA=E6=9C=80=E7=BB=88=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/loader.js | 14 ------- lib/mp-compiler/index.js | 50 ++++++++++++------------ lib/mp-compiler/templates.js | 22 ++++++----- lib/mp-compiler/util.js | 73 +++++++++++++++++++++++++++++++++--- 4 files changed, 105 insertions(+), 54 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index 49d90da..4cb2135 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -45,24 +45,10 @@ function getRawRequest (context, excludedPreLoaders) { }) } -const mpPages = [] -const mpInfo = {} - module.exports = function (content) { // for mp // 针对 entry 的 main.js 处理 page 和 app 的入口文件和配置等 const mpOptions = loaderUtils.getOptions(this) || {} - if (!mpPages.length) { - const { entry = {}} = this.options - Object.keys(entry).forEach(k => { - mpInfo[entry[k]] = k - if (k !== 'app') { - mpPages.push(`pages/${k}/${k}`) - } - }) - } - mpOptions.pages = mpPages - mpOptions.mpInfo = mpInfo if (mpOptions.checkMPEntry) { return compileMP.call(this, content, mpOptions) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 39e82f7..1ca8536 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -1,29 +1,29 @@ // for mp const compiler = require('mpvue-template-compiler') -const htmlBeautify = require('js-beautify').html -const path = require('path') const babel = require('babel-core') const { parseConfig, parseComponentsDeps } = require('./parse') const { genScript, genStyle, genPageWxml } = require('./templates') -const { cacheFileInfo, getFileInfo, getCompNameBySrc, resolveTarget, covertCCVar, cacheSlots, getSlots } = require('./util') - -const htmlBeautifyOptions = { - wrap_line_length: '80', - indent_size: 2, - preserve_newlines: true, - max_preserve_newlines: 0, - e4x: true, - unformatted: ['a', 'span', 'img', 'code', 'pre', 'sub', 'sup', 'em', 'strong', 'b', 'i', 'u', 'strike', 'big', 'small', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'] -} +const { + cacheFileInfo, + getFileInfo, + getCompNameBySrc, + resolveTarget, + covertCCVar, + cacheSlots, + getSlots, + htmlBeautify, + getBabelrc, + getPageSrc +} = require('./util') function createSlotsWxml (emitFile, slots) { cacheSlots(slots) const allSlots = getSlots() const content = Object.keys(allSlots).map(v => allSlots[v].code).join('\n') if (content.trim()) { - emitFile('components/slots.wxml', htmlBeautify(content, htmlBeautifyOptions)) + emitFile('components/slots.wxml', htmlBeautify(content)) } } @@ -47,7 +47,7 @@ function genComponentWxml (compiled, options, emitFile, emitError, emitWarning) mpTips.map(e => ` - ${e}`).join('\n') + '\n' ) } - return htmlBeautify(wxmlCodeStr, htmlBeautifyOptions) + return htmlBeautify(wxmlCodeStr) } function createWxml (emitWarning, emitError, emitFile, resourcePath, rootComponent, compiled, html) { @@ -63,7 +63,7 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone if (rootComponent) { const componentName = getCompNameBySrc(rootComponent) - wxmlContent = genPageWxml(componentName) + wxmlContent = genPageWxml(componentName, src) wxmlSrc = src } else { // TODO, 这儿传 options 进去 @@ -91,8 +91,8 @@ function compileWxml (compiled, html) { // 针对 .vue 单文件的脚本逻辑的处理 // 处理出当前单文件组件的子组件依赖 -function compileMPScript (script, optioins, moduleId) { - const babelrc = optioins.globalBabelrc ? optioins.globalBabelrc : path.resolve('./.babelrc') +function compileMPScript (script, mpOptioins, moduleId) { + const babelrc = getBabelrc(mpOptioins.globalBabelrc) const { metadata } = babel.transform(script.content, { extends: babelrc, plugins: [parseComponentsDeps] }) // metadata: importsMap, components @@ -124,7 +124,7 @@ function compileMPScript (script, optioins, moduleId) { components.isCompleted = true } - const fileInfo = resolveTarget(this.resourcePath, optioins.mpInfo) + const fileInfo = resolveTarget(this.resourcePath, this.options.entry) cacheFileInfo(this.resourcePath, fileInfo, { importsMap, components, moduleId }) return script @@ -135,16 +135,16 @@ function compileMPScript (script, optioins, moduleId) { const startPageReg = /^\^/ -function compileMP (content, optioins) { - const { resourcePath, emitError, emitFile, emitWarning, resolve, context } = this +function compileMP (content, mpOptioins) { + const { resourcePath, emitError, emitFile, emitWarning, resolve, context, options } = this - const babelrc = optioins.globalBabelrc ? optioins.globalBabelrc : path.resolve('./.babelrc') + const babelrc = getBabelrc(mpOptioins.globalBabelrc) const { metadata } = babel.transform(content, { extends: babelrc, plugins: [parseConfig] }) // metadata: config const { config, rootComponent } = metadata - const fileInfo = resolveTarget(resourcePath, optioins.mpInfo) + const fileInfo = resolveTarget(resourcePath, options.entry) cacheFileInfo(resourcePath, fileInfo) const { src, name, isApp, isPage } = fileInfo @@ -155,7 +155,7 @@ function compileMP (content, optioins) { // 只有 app 才处理 pages if (isApp) { - const pages = (configObj.pages || []).concat(optioins.pages) + const pages = Object.keys(options.entry).concat(configObj.pages).filter(v => v && v !== 'app').map(getPageSrc) // ^ 开头的放在第一个 const startPageIndex = pages.findIndex(v => startPageReg.test(v)) @@ -170,10 +170,10 @@ function compileMP (content, optioins) { } // 生成入口 js - emitFile(`${src}.js`, genScript(name, isPage)) + emitFile(`${src}.js`, genScript(name, isPage, src)) // 生成入口 wxss - emitFile(`${src}.wxss`, genStyle(name, isPage)) + emitFile(`${src}.wxss`, genStyle(name, isPage, src)) // 这儿应该异步在所有的模块都清晰后再生成 // 生成入口 wxml diff --git a/lib/mp-compiler/templates.js b/lib/mp-compiler/templates.js index 740dfae..a69c707 100644 --- a/lib/mp-compiler/templates.js +++ b/lib/mp-compiler/templates.js @@ -1,20 +1,22 @@ -function genScript (name, isPage) { - const prefix = isPage ? '../..' : '.' +const { getPathPrefix } = require('./util') + +function genScript (name, isPage, src) { + const prefix = isPage ? getPathPrefix(src) : '.' return ` - require('${prefix}/static/js/manifest') - require('${prefix}/static/js/vendor') - require('${prefix}/static/js/${name}') - ` +require('${prefix}/static/js/manifest') +require('${prefix}/static/js/vendor') +require('${prefix}/static/js/${name}') +` } -function genStyle (name, isPage) { - const prefix = isPage ? '../..' : '.' +function genStyle (name, isPage, src) { + const prefix = isPage ? getPathPrefix(src) : '.' return `@import "${prefix}/static/css/${name}.wxss";` } -function genPageWxml (templateName) { - return `