Skip to content

Commit

Permalink
Fix override methods for transform esm to cjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
100pah committed Nov 12, 2017
1 parent c9b4963 commit 69e16cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
5 changes: 0 additions & 5 deletions build/babel-plugin-transform-modules-commonjs-ec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions build/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down
20 changes: 13 additions & 7 deletions src/contain/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; */
}
};
17 changes: 11 additions & 6 deletions src/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -178,6 +185,10 @@ export function defaults(target, source, overlay) {
}

export var createCanvas = function () {
return methods.createCanvas();
};

methods.createCanvas = function () {
return document.createElement('canvas');
};

Expand Down Expand Up @@ -606,9 +617,3 @@ export function createHashMap(obj) {
}

export function noop() {}

export var $inject = {
createCanvas: function (f) {
createCanvas = f; /* ESM2CJS_REPLACE exports.createCanvas = f; */
}
};
11 changes: 8 additions & 3 deletions src/vml/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<zrvml:' + tagName + ' class="zrvml">');
};
}
catch (e) {
createNode = function (tagName) {
doCreateNode = function (tagName) {
return doc.createElement('<' + tagName + ' xmlns="' + urn + '" class="zrvml">');
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/vml/graphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 69e16cf

Please sign in to comment.