Skip to content

Commit 8819f71

Browse files
authored
Merge pull request #51 from Turbo87/node-4
Remove Babel transpilation step
2 parents 828f786 + 3f902db commit 8819f71

File tree

8 files changed

+87
-961
lines changed

8 files changed

+87
-961
lines changed

.babelrc

Lines changed: 0 additions & 9 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"devDependencies": {
3-
"babel-cli": "^6.22.2",
43
"babel-core": "^6.22.1",
54
"babel-preset-env": "^1.2.2",
65
"babel-preset-latest": "^6.24.0",
@@ -12,19 +11,17 @@
1211
"name": "babel-plugin-debug-macros",
1312
"version": "0.1.11",
1413
"description": "Debug macros and feature flag stripping",
15-
"main": "dist/index.js",
14+
"main": "src/index.js",
1615
"files": [
17-
"dist/",
18-
"!dist/tests"
16+
"src/",
17+
"!src/tests"
1918
],
2019
"repository": {
2120
"type": "git",
2221
"url": "https://github.com/chadhietala/babel-debug-macros"
2322
},
2423
"scripts": {
25-
"build": "babel src --out-dir dist",
26-
"prepublish": "babel src --out-dir dist",
27-
"test": "babel src --out-dir dist && mocha dist/tests/**-test.js"
24+
"test": "mocha src/tests/**-test.js"
2825
},
2926
"keywords": [
3027
"babel",

src/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import Macros from './lib/utils/macros';
2-
import { readFileSync } from 'fs';
3-
import { dirname, join } from 'path';
4-
import { normalizeOptions } from './lib/utils/normalize-options';
1+
'use strict';
2+
3+
const Macros = require('./lib/utils/macros');
4+
const normalizeOptions = require('./lib/utils/normalize-options').normalizeOptions;
55

66
function macros(babel) {
7-
const { types: t } = babel;
7+
const t = babel.types;
8+
89
let macroBuilder;
910
let options;
1011

@@ -23,11 +24,9 @@ function macros(babel) {
2324
if (item.isImportDeclaration()) {
2425
let importPath = item.node.source.value;
2526

26-
let {
27-
featureSources,
28-
debugTools: { debugToolsImport },
29-
envFlags: { envFlagsImport, flags }
30-
} = options;
27+
let featureSources = options.featureSources;
28+
let debugToolsImport = options.debugTools.debugToolsImport;
29+
let envFlagsImport = options.envFlags.envFlagsImport;
3130

3231
let isFeaturesImport = featureSources.indexOf(importPath) > -1;
3332

@@ -57,4 +56,4 @@ macros.baseDir = function() {
5756
return dirname(__dirname);
5857
}
5958

60-
export default macros;
59+
module.exports = macros;

src/lib/utils/builder.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export default class Builder {
1+
'use strict';
2+
3+
module.exports = class Builder {
24
constructor(t, options) {
35
this.t = t;
46
this.module = options.module;
@@ -82,21 +84,20 @@ export default class Builder {
8284
_createMacroExpression(path, _options) {
8385
let options = _options || {};
8486

85-
let { t, module, global } = this;
87+
let t = this.t;
8688
let expression = path.node.expression;
87-
let { callee, arguments: args } = expression;
88-
let callExpression;
89+
let callee = expression.callee;
90+
let args = expression.arguments;
8991

9092
if (options.validate) {
9193
options.validate(expression, args);
9294
}
9395

94-
if (module || global) {
95-
if (global) {
96-
callExpression = this._createGlobalExternalHelper(callee, args, global);
97-
} else {
98-
callExpression = expression;
99-
}
96+
let callExpression;
97+
if (this.module) {
98+
callExpression = expression;
99+
} else if (this.global) {
100+
callExpression = this._createGlobalExternalHelper(callee, args, this.global);
100101
} else if (options.buildConsoleAPI) {
101102
callExpression = options.buildConsoleAPI(expression, args);
102103
} else {
@@ -145,13 +146,13 @@ export default class Builder {
145146
predicate: (expression, args) => args[1],
146147

147148
buildConsoleAPI: (expression, args) => {
148-
let [message] = args;
149+
let message = args[0];
149150

150151
return this._createConsoleAPI(this.t.identifier('warn'), [message]);
151152
},
152153

153154
validate: (expression, args) => {
154-
let [ , , meta ] = args;
155+
let meta = args[2];
155156

156157
if (meta && meta.properties && !meta.properties.some( prop => prop.key.name === 'id')) {
157158
throw new ReferenceError(`deprecate's meta information requires an "id" field.`);
@@ -168,10 +169,12 @@ export default class Builder {
168169
* Performs the actually expansion of macros
169170
*/
170171
expandMacros(debugFlag) {
171-
let { t } = this;
172+
let t = this.t;
172173
let flag = t.booleanLiteral(debugFlag);
173174
for (let i = 0; i < this.expressions.length; i++) {
174-
let [exp, logicalExp] = this.expressions[i];
175+
let expression = this.expressions[i];
176+
let exp = expression[0];
177+
let logicalExp = expression[1];
175178
exp.replaceWith(t.parenthesizedExpression(logicalExp(flag)));
176179
}
177180
}
@@ -181,17 +184,17 @@ export default class Builder {
181184
}
182185

183186
_createGlobalExternalHelper(identifier, args, ns) {
184-
let { t } = this;
187+
let t = this.t;
185188
return t.callExpression(t.memberExpression(t.identifier(ns), identifier), args);
186189
}
187190

188191
_createConsoleAPI(identifier, args) {
189-
let { t } = this;
192+
let t = this.t;
190193
return t.callExpression(t.memberExpression(t.identifier('console'), identifier), args);
191194
}
192195

193196
_buildLogicalExpressions(identifiers, callExpression) {
194-
let { t } = this;
197+
let t = this.t;
195198

196199
return (debugIdentifier) => {
197200
identifiers.unshift(debugIdentifier);

src/lib/utils/macros.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const DEBUG = 'DEBUG';
1+
'use strict';
22

3-
import Builder from './builder';
3+
const Builder = require('./builder');
44

5+
const DEBUG = 'DEBUG';
56
const SUPPORTED_MACROS = ['assert', 'deprecate', 'warn', 'log'];
67

7-
export default class Macros {
8+
module.exports = class Macros {
89
constructor(t, options) {
910
this.localDebugBindings = [];
1011
this.envFlagBindings = [];
@@ -31,8 +32,6 @@ export default class Macros {
3132
*/
3233
expand(path) {
3334
let debugBinding = path.scope.getBinding(DEBUG);
34-
let { builder, envFlags } = this;
35-
3635

3736
if (this._hasDebugModule(debugBinding)) {
3837
debugBinding.path.parentPath.remove();
@@ -41,12 +40,12 @@ export default class Macros {
4140
this._inlineFeatureFlags(path);
4241
this._inlineSvelteFlags(path);
4342
this._inlineEnvFlags(path)
44-
this.builder.expandMacros(envFlags.DEBUG);
43+
this.builder.expandMacros(this.envFlags.DEBUG);
4544
this._cleanImports(path);
4645
}
4746

4847
_inlineFeatureFlags(path) {
49-
let { envFlags, builder, featureFlags, featuresMap } = this;
48+
let featuresMap = this.featuresMap;
5049

5150
if (this.envFlags.DEBUG) { return; }
5251
Object.keys(featuresMap).forEach((source) => {
@@ -56,7 +55,7 @@ export default class Macros {
5655

5756
if (binding && flagValue !== null) {
5857
binding.referencePaths.forEach(referencePath => {
59-
referencePath.replaceWith(builder.t.booleanLiteral(flagValue));
58+
referencePath.replaceWith(this.builder.t.booleanLiteral(flagValue));
6059
});
6160

6261
if (binding.path.parentPath.isImportDeclaration()) {
@@ -68,38 +67,42 @@ export default class Macros {
6867
}
6968

7069
_inlineEnvFlags(path) {
71-
let { envFlags, builder } = this;
70+
let envFlags = this.envFlags;
71+
7272
Object.keys(envFlags).forEach(flag => {
7373
let binding = path.scope.getBinding(flag);
7474
if (binding &&
7575
binding.path.isImportSpecifier() &&
7676
binding.path.parent.source.value === this.envFlagsSource) {
7777

78-
binding.referencePaths.forEach(p => p.replaceWith(builder.t.booleanLiteral(envFlags[flag])));
78+
binding.referencePaths.forEach(p => p.replaceWith(this.builder.t.booleanLiteral(envFlags[flag])));
7979
}
8080
});
8181
}
8282

8383
_inlineSvelteFlags(path) {
84-
let { svelteMap, envFlags, builder } = this;
84+
let svelteMap = this.svelteMap;
85+
let envFlags = this.envFlags;
86+
let builder = this.builder;
87+
8588
let sources = Object.keys(svelteMap);
8689
sources.forEach((source) => {
8790
Object.keys(svelteMap[source]).forEach((flag) => {
8891
let binding = path.scope.getBinding(flag);
8992
if (binding !== undefined) {
9093
binding.referencePaths.forEach((p) => {
94+
let t = builder.t;
9195
if (envFlags.DEBUG) {
9296
if (svelteMap[source][flag] === false) {
93-
let { t } = builder;
9497
if (!p.parentPath.isIfStatement()) { return; }
9598
let consequent = p.parentPath.get('consequent');
96-
consequent.unshiftContainer('body', builder.t.throwStatement(
99+
consequent.unshiftContainer('body', t.throwStatement(
97100
t.newExpression(t.identifier('Error'), [t.stringLiteral(`You indicated you don't have any deprecations, however you are relying on ${flag}.`)])
98101
));
99102
}
100103
} else {
101104
if (p.parentPath.isIfStatement()) {
102-
p.replaceWith(builder.t.booleanLiteral(svelteMap[source][flag]));
105+
p.replaceWith(t.booleanLiteral(svelteMap[source][flag]));
103106
}
104107
}
105108
});
@@ -130,8 +133,8 @@ export default class Macros {
130133
*/
131134
build(path) {
132135
let expression = path.node.expression;
133-
let { builder, localDebugBindings } = this;
134-
if (builder.t.isCallExpression(expression) && localDebugBindings.some((b) => b.node.name === expression.callee.name)) {
136+
137+
if (this.builder.t.isCallExpression(expression) && this.localDebugBindings.some((b) => b.node.name === expression.callee.name)) {
135138
let imported = path.scope.getBinding(expression.callee.name).path.node.imported.name;
136139
this.builder[`${imported}`](path);
137140
}
@@ -152,31 +155,23 @@ export default class Macros {
152155
}
153156

154157
_detectForeignFeatureFlag(specifiers, source) {
155-
let { featuresMap } = this;
156158
specifiers.forEach((specifier) => {
157-
if (specifier.imported && featuresMap[source][specifier.imported.name] !== null) {
159+
if (specifier.imported && this.featuresMap[source][specifier.imported.name] !== null) {
158160
throw new Error(`Imported ${specifier.imported.name} from ${source} which is not a supported flag.`);
159161
}
160162
});
161163
}
162164

163165
_cleanImports(path) {
164-
let {
165-
debugHelpers,
166-
builder,
167-
featureFlags,
168-
featureSources
169-
} = this;
170-
171166
let body = path.get('body');
172167

173168
if (!this.envFlags.DEBUG) {
174169
for (let i = 0; i < body.length; i++) {
175170
let decl = body[i];
176171

177-
if (builder.t.isImportDeclaration(decl)) {
172+
if (this.builder.t.isImportDeclaration(decl)) {
178173
let source = decl.node.source.value;
179-
if (featureSources.indexOf(source) > -1) {
174+
if (this.featureSources.indexOf(source) > -1) {
180175
if (decl.node.specifiers.length > 0) {
181176
this._detectForeignFeatureFlag(decl.node.specifiers, source);
182177
} else {
@@ -188,7 +183,7 @@ export default class Macros {
188183
}
189184
}
190185

191-
if (!debugHelpers.module) {
186+
if (!this.debugHelpers.module) {
192187
if (this.localDebugBindings.length > 0) {
193188
this.localDebugBindings[0].parentPath.parentPath
194189
let importPath = this.localDebugBindings[0].findParent(p => p.isImportDeclaration());

src/lib/utils/normalize-options.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { satisfies } from 'semver';
1+
'use strict';
22

3-
export function normalizeOptions(options) {
4-
let {
5-
features = [],
6-
debugTools,
7-
envFlags,
8-
externalizeHelpers,
9-
svelte
10-
} = options;
3+
const satisfies = require('semver').satisfies;
4+
5+
function normalizeOptions(options) {
6+
let features = options.features || [];
7+
let debugTools = options.debugTools;
8+
let envFlags = options.envFlags;
9+
let externalizeHelpers = options.externalizeHelpers;
10+
let svelte = options.svelte;
1111

1212
let featureSources = [];
1313
let featuresMap = {};
@@ -52,10 +52,8 @@ export function normalizeOptions(options) {
5252
throw new Error('You must specify `debugTools.source`');
5353
}
5454

55-
let {
56-
source: debugToolsImport,
57-
assertPredicateIndex
58-
} = debugTools;
55+
let debugToolsImport = debugTools.source;
56+
let assertPredicateIndex = debugTools.assertPredicateIndex;
5957

6058
let envFlagsImport;
6159
let _envFlags = {};
@@ -87,3 +85,7 @@ export function normalizeOptions(options) {
8785
}
8886
};
8987
}
88+
89+
module.exports = {
90+
normalizeOptions,
91+
};

src/tests/debug-tools-test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import DebugToolsPlugin from '../index';
2-
import { transform } from 'babel-core';
3-
import { expect } from 'chai';
4-
import { file } from 'chai-files';
5-
import { lstatSync, writeFileSync } from 'fs';
1+
'use strict';
2+
3+
const DebugToolsPlugin = require('../index');
4+
const transform = require('babel-core').transform;
5+
const expect = require('chai').expect;
6+
const file = require('chai-files').file;
7+
const lstatSync = require('fs').lstatSync;
68

79
const presets = [["latest", {
810
"es2015": false,

0 commit comments

Comments
 (0)