From 9c27dd0bd134b903e8d30f5d8921b6d6ef7a25e4 Mon Sep 17 00:00:00 2001 From: aorz Date: Sat, 10 Mar 2018 22:03:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B7=AF=E5=BE=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/loader.js | 3 +- lib/mp-compiler/index.js | 6 +- lib/mp-compiler/templates.js | 22 +- lib/mp-compiler/util.js | 18 +- yarn.lock | 5413 ++++++++++++++++++++++++++++++++++ 5 files changed, 5446 insertions(+), 16 deletions(-) create mode 100644 yarn.lock diff --git a/lib/loader.js b/lib/loader.js index 49d90da..56b78f7 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -52,12 +52,13 @@ 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}`) + mpPages.push(k) } }) } diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 1b77394..47c21c7 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -62,7 +62,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 进去 @@ -166,10 +166,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 ff59787..fd7f12b 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 ` - import '${prefix}/static/js/manifest' - import '${prefix}/static/js/vendor' - import '${prefix}/static/js/${name}' + import '${prefix}static/js/manifest' + import '${prefix}static/js/vendor' + import '${prefix}static/js/${name}' ` } -function genStyle (name, isPage) { - const prefix = isPage ? '../..' : '.' - return `@import "${prefix}/static/css/${name}.wxss";` +function genStyle (name, isPage, src) { + const prefix = isPage ? getPathPrefix(src) : './' + return `@import "${prefix}static/css/${name}.wxss";` } -function genPageWxml (templateName) { - return `