From 69e16cfc3158847101f508e186e48ae2c799828c Mon Sep 17 00:00:00 2001 From: sushuang Date: Sun, 12 Nov 2017 16:21:18 +0800 Subject: [PATCH] Fix override methods for transform esm to cjs. --- ...el-plugin-transform-modules-commonjs-ec.js | 5 ----- build/helper.js | 3 --- src/contain/text.js | 20 ++++++++++++------- src/core/util.js | 17 ++++++++++------ src/vml/core.js | 11 +++++++--- src/vml/graphic.js | 2 +- 6 files changed, 33 insertions(+), 25 deletions(-) diff --git a/build/babel-plugin-transform-modules-commonjs-ec.js b/build/babel-plugin-transform-modules-commonjs-ec.js index eacb4cd81..bad53148d 100644 --- a/build/babel-plugin-transform-modules-commonjs-ec.js +++ b/build/babel-plugin-transform-modules-commonjs-ec.js @@ -61,11 +61,6 @@ module.exports = function ({types, template}, options) { }; }; -// FIXME -module.exports.replaceInject = function (code) { - return code.replace(/\/\* ESM2CJS_REPLACE ([^*/]+)\*\//g, '$1'); -}; - /** * Remove all imports and exports from the file, and return all metadata diff --git a/build/helper.js b/build/helper.js index 966eed15b..48cee992f 100644 --- a/build/helper.js +++ b/build/helper.js @@ -163,9 +163,6 @@ exports.prePulishSrc = function ({inputPath, outputPath, transform}) { removeDEVPlugin.recheckDEV(code); - // FIXME - code = esm2cjsPlugin.replaceInject(code); - if (transform) { code = transform({code, inputPath, outputPath}); } diff --git a/src/contain/text.js b/src/contain/text.js index 6c0b413eb..17f1fdb00 100644 --- a/src/contain/text.js +++ b/src/contain/text.js @@ -15,6 +15,13 @@ var STYLE_REG = /\{([a-zA-Z0-9_]+)\|([^}]*)\}/g; export var DEFAULT_FONT = '12px sans-serif'; +// Avoid assign to an exported variable, for transforming to cjs. +var methods = {}; + +export function $override(name, fn) { + methods[name] = fn; +} + /** * @public * @param {string} text @@ -367,7 +374,12 @@ export function getLineHeight(font) { * @param {string} font * @return {Object} width */ -export var measureText = function (text, font) { +export function measureText(text, font) { + return methods.measureText(text, font); +} + +// Avoid assign to an exported variable, for transforming to cjs. +methods.measureText = function (text, font) { var ctx = getContext(); ctx.font = font || DEFAULT_FONT; return ctx.measureText(text); @@ -666,9 +678,3 @@ export function makeFont(style) { style.fontFamily || 'sans-serif' ].join(' ') || style.textFont || style.font; } - -export var $inject = { - measureText: function (f) { - measureText = f; /* ESM2CJS_REPLACE exports.measureText = f; */ - } -}; diff --git a/src/core/util.js b/src/core/util.js index 6db6763db..b7be34566 100644 --- a/src/core/util.js +++ b/src/core/util.js @@ -36,6 +36,13 @@ var nativeSlice = arrayProto.slice; var nativeMap = arrayProto.map; var nativeReduce = arrayProto.reduce; +// Avoid assign to an exported variable, for transforming to cjs. +var methods = {}; + +export function $override(name, fn) { + methods[name] = fn; +} + /** * Those data types can be cloned: * Plain object, Array, TypedArray, number, string, null, undefined. @@ -178,6 +185,10 @@ export function defaults(target, source, overlay) { } export var createCanvas = function () { + return methods.createCanvas(); +}; + +methods.createCanvas = function () { return document.createElement('canvas'); }; @@ -606,9 +617,3 @@ export function createHashMap(obj) { } export function noop() {} - -export var $inject = { - createCanvas: function (f) { - createCanvas = f; /* ESM2CJS_REPLACE exports.createCanvas = f; */ - } -}; diff --git a/src/vml/core.js b/src/vml/core.js index f87b4fcb2..89340f0f2 100644 --- a/src/vml/core.js +++ b/src/vml/core.js @@ -8,17 +8,22 @@ var vmlInited = false; export var doc = win && win.document; -export var createNode; +export function createNode(tagName) { + return doCreateNode(tagName); +} + +// Avoid assign to an exported variable, for transforming to cjs. +var doCreateNode; if (doc && !env.canvasSupported) { try { !doc.namespaces.zrvml && doc.namespaces.add('zrvml', urn); - createNode = function (tagName) { + doCreateNode = function (tagName) { return doc.createElement(''); }; } catch (e) { - createNode = function (tagName) { + doCreateNode = function (tagName) { return doc.createElement('<' + tagName + ' xmlns="' + urn + '" class="zrvml">'); }; } diff --git a/src/vml/graphic.js b/src/vml/graphic.js index deb30b55a..1eb549d56 100644 --- a/src/vml/graphic.js +++ b/src/vml/graphic.js @@ -794,7 +794,7 @@ if (!env.canvasSupported) { var textMeasureEl; // Overwrite measure text method - textContain.$inject.measureText(function (text, textFont) { + textContain.$override('measureText', function (text, textFont) { var doc = vmlCore.doc; if (!textMeasureEl) { textMeasureEl = doc.createElement('div');