Skip to content

Commit

Permalink
udpate configFile and set default sourceType to script
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed May 1, 2018
1 parent ce74217 commit 6063ec6
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Reason for Filename to be babel.config.js
*
* https://github.com/facebook/jest/issues/6053#issuecomment-383632515
* https://github.com/babel/babel/pull/7784
* https://github.com/facebook/jest/issues/6053#issuecomment-383632515
*/

module.exports = {
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function babelMinify(
minified = true,
inputSourceMap,
sourceMaps = false,
sourceType = "script",

// to override the default babelCore used
babel = babelCore,
Expand All @@ -21,10 +22,12 @@ module.exports = function babelMinify(
) {
return babel.transformSync(input, {
babelrc: false,
configFile: false,
presets: [[minifyPreset, options]],
comments: false,
inputSourceMap,
sourceMaps,
minified
minified,
sourceType
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const simplify = require("../../babel-plugin-minify-simplify/src/index");
const thePlugin = require("test-transform")(deadcode);

function transformWithSimplify(code) {
return babel.transform(code, {
plugins: [deadcode, simplify]
return babel.transformSync(code, {
plugins: [deadcode, simplify],
sourceType: "script"
}).code;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

// #issue55, #issue57
(function () {
(function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ describe("type-constructors-plugin", () => {
`
);
expect(
babel.transform(source, {
plugins: [[plugin, { [names[i]]: false }]]
babel.transformSync(source, {
plugins: [[plugin, { [names[i]]: false }]],
sourceType: "script"
}).code
).toBe(source);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gulp-babel-minify/__tests__/gulp-babili-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ describe("gulp-babel-minify", () => {
const babel = Object.assign({}, babelCore);

let usedTransform = false;
Object.defineProperty(babel, "transform", {
Object.defineProperty(babel, "transformSync", {
get() {
usedTransform = true;
return babelCore.transform;
return babelCore.transformSync;
}
});

Expand Down
7 changes: 5 additions & 2 deletions packages/gulp-babel-minify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function gulpBabelMinify(
{
babel = babelCore,
minifyPreset = babelPresetMinify,
comments = /preserve|licen(s|c)e/
comments = /preserve|licen(s|c)e/,
sourceType = "script"
} = {}
) {
return through2.obj(function(file, enc, callback) {
Expand All @@ -35,6 +36,8 @@ function gulpBabelMinify(
babelrc: false,
ast: false,

sourceType,

/* preset */
presets: [[minifyPreset, minifyOpts]],

Expand Down Expand Up @@ -73,7 +76,7 @@ function transform({ babel, input, babelOpts }) {
try {
return {
success: true,
result: babel.transform(input, babelOpts)
result: babel.transformSync(input, babelOpts)
};
} catch (e) {
return {
Expand Down
16 changes: 13 additions & 3 deletions utils/test-runner/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ function testRunner(dir) {
options = JSON.parse(await fs.readFile(optionsFile));
}

let babelOpts = {};
let babelOpts = {
// set the default sourcetype to be script
sourceType: "script"
};

if (await fs.isFile(babelOptionsFile)) {
babelOpts = JSON.parse(await fs.readFile(babelOptionsFile));
Object.assign(
babelOpts,
JSON.parse(await fs.readFile(babelOptionsFile))
);
}

const currentPlugin = pathJoin(pkgDir, "src/index.js");
Expand All @@ -71,7 +78,10 @@ function testRunner(dir) {
babelOpts.plugins = [[currentPlugin, options]];
}

const actualTransformed = babel.transform(actual, babelOpts).code;
// don't consider the project's babel.config.js
babelOpts.configFile = false;

const actualTransformed = babel.transformSync(actual, babelOpts).code;

if (!(await fs.isFile(expectedFile))) {
await fs.writeFile(expectedFile, actualTransformed);
Expand Down
21 changes: 19 additions & 2 deletions utils/test-transform/src/test-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const babel = require("@babel/core");
const unpad = require("unpad");

function _transform(source, options) {
return babel.transform(unpad(source), options).code.trim();
return babel.transformSync(unpad(source), options).code.trim();
}

function makeTester(
Expand All @@ -15,19 +15,23 @@ function makeTester(
if (!Array.isArray(plugins)) {
plugins = [plugins];
}

const thePlugin = (name, source, expected = source, babelOpts) => {
if (typeof expected === "object") {
babelOpts = expected;
expected = source;
}

const { stack } = new Error();
const options = Object.assign(
{ plugins, sourceType: "script" },
{ plugins, sourceType: "script", configFile: false },
opts,
babelOpts
);

test(name, () => {
const transformed = transform(source, options);

try {
check({
transformed,
Expand All @@ -42,14 +46,21 @@ function makeTester(
}
});
};

thePlugin.skip = name => test.skip(name);

if (excludeKeys.indexOf("inEachLine") === -1) {
thePlugin.inEachLine = makeTester(
plugins,
opts,
{
test,
transform(source, options) {
options.configFile = false;
options.sourceType = hop(options, "sourceType")
? options.sourceType
: "script";

return unpad(source)
.split("\n")
.map(line => _transform(line, options))
Expand All @@ -60,6 +71,7 @@ function makeTester(
excludeKeys.concat("inEachLine")
);
}

if (excludeKeys.indexOf("only") === -1) {
thePlugin.only = makeTester(
plugins,
Expand All @@ -72,6 +84,7 @@ function makeTester(
excludeKeys.concat("only")
);
}

return thePlugin;
}

Expand All @@ -89,3 +102,7 @@ exports.snapshot = (plugins, opts) =>
expect({ _source: source, expected: transformed }).toMatchSnapshot();
}
});

function hop(o, key) {
return Object.prototype.hasOwnProperty.call(o, key);
}

0 comments on commit 6063ec6

Please sign in to comment.