Skip to content

Commit

Permalink
Send Babel 6 to client
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Apr 5, 2018
1 parent 54ba34c commit 326b090
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 73 deletions.
109 changes: 51 additions & 58 deletions app/plugins/hbs-plugin.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,72 @@
// Based on version 0.2.3

export default function(babel, _options) {
var options = _options || {};

function htmlbarsInlineCompilerPlugin(babel) {
var t = babel.types;

var replaceNodeWithPrecompiledTemplate = function(node, template) {
var compiledTemplateString = "Ember.HTMLBars.compile(`" + template + "`)";

// Prefer calling replaceWithSourceString if it is present.
// this prevents a deprecation warning in Babel 5.6.7+.
//
// TODO: delete the fallback once we only support babel >= 5.6.7.
if (node.replaceWithSourceString) {
node.replaceWithSourceString(compiledTemplateString);
} else {
return compiledTemplateString;
}
};
let t = babel.types;

return {
visitor: {
ImportDeclaration: function(path, state) {
let node = path.node;
if (t.isLiteral(node.source, { value: "htmlbars-inline-precompile" })) {
let first = node.specifiers && node.specifiers[0];
if (!t.isImportDefaultSpecifier(first)) {
let input = state.file.code;
let usedImportStatement = input.slice(node.start, node.end);
let msg = `Only \`import hbs from 'htmlbars-inline-precompile'\` is supported. You used: \`${usedImportStatement}\``;
throw path.buildCodeFrameError(msg);
}

return new babel.Transformer('htmlbars-inline-precompile', {
ImportDeclaration: function(node, parent, scope, file) {
if (t.isLiteral(node.source, { value: "htmlbars-inline-precompile" })) {
var first = node.specifiers && node.specifiers[0];
if (t.isImportDefaultSpecifier(first)) {
file.importSpecifier = first.local.name;
} else {
var input = file.code;
var usedImportStatement = input.slice(node.start, node.end);
var msg = "Only `import hbs from 'htmlbars-inline-precompile'` is supported. You used: `" + usedImportStatement + "`";
throw file.errorWithNode(node, msg);
state.importId = state.importId || path.scope.generateUidIdentifierBasedOnNode(path.node.id);
path.scope.rename(first.local.name, state.importId.name);
path.remove();
}
},

// Prefer calling dangerouslyRemove instead of remove (if present) to
// suppress a deprecation warning.
//
// TODO: delete the fallback once we only support babel >= 5.5.0.
if (typeof this.dangerouslyRemove === 'function') {
this.dangerouslyRemove();
} else {
this.remove();
TaggedTemplateExpression(path, state) {
if (!state.importId) { return; }

let tagPath = path.get('tag');
if (tagPath.node.name !== state.importId.name) {
return;
}
}
},

CallExpression: function(node, parent, scope, file) {
if (t.isIdentifier(node.callee, { name: file.importSpecifier })) {
var argumentErrorMsg = "hbs should be invoked with a single argument: the template string";
if (node.arguments.length !== 1) {
throw file.errorWithNode(node, argumentErrorMsg);

if (path.node.quasi.expressions.length) {
throw path.buildCodeFrameError("placeholders inside a tagged template string are not supported");
}

var template = node.arguments[0].value;
if (typeof template !== "string") {
throw file.errorWithNode(node, argumentErrorMsg);
let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join('');
let compiledTemplateString = "Ember.HTMLBars.compile(`" + template + "`)";

path.replaceWithSourceString(compiledTemplateString);
},

CallExpression(path, state) {
if (!state.importId) { return; }

let calleePath = path.get('callee');
if (calleePath.node.name !== state.importId.name) {
return;
}

return replaceNodeWithPrecompiledTemplate(this, template);
}
},
let argumentErrorMsg = "hbs should be invoked with a single argument: the template string";
if (path.node.arguments.length !== 1) {
throw path.buildCodeFrameError(argumentErrorMsg);
}

TaggedTemplateExpression: function(node, parent, scope, file) {
if (t.isIdentifier(node.tag, { name: file.importSpecifier })) {
if (node.quasi.expressions.length) {
throw file.errorWithNode(node, "placeholders inside a tagged template string are not supported");
let template = path.node.arguments[0].value;
if (typeof template !== "string") {
throw path.buildCodeFrameError(argumentErrorMsg);
}

var template = node.quasi.quasis.map(function(quasi) {
return quasi.value.cooked;
}).join("");
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`;

return replaceNodeWithPrecompiledTemplate(this, template);
}
path.replaceWithSourceString(compiledTemplateString);
},
}
});
};
}

// used by broccoli-babel-transpiler to bust the cache when
Expand Down
9 changes: 7 additions & 2 deletions app/services/ember-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,15 @@ export default Ember.Service.extend({
*/
function babelOpts(moduleName) {
return {
modules: 'amdStrict',
moduleIds: true,
moduleId: moduleName,
plugins: [ hbsPlugin ]
plugins: [
['transform-es2015-modules-amd', {
loose: true,
noInterop: false
}],
hbsPlugin
]
};
}

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"Faker": "~3.1.0",
"file-saver": "1.3.4",
"jstree": "~3.3.0",
"babel": "^5.8.38"
"babel-standalone": "^6.19.0"
},
"resolutions": {
"codemirror": "~5.19.0",
Expand Down
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function(environment) {
}

if (environment === 'test') {
ENV.locationType = 'auto';
ENV.locationType = 'none';

// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
Expand Down
4 changes: 2 additions & 2 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module.exports = function(defaults) {
}

app.import('vendor/ember/ember-template-compiler.js');
app.import('bower_components/babel/browser.js');
app.import('bower_components/babel-standalone/babel.js');
app.import('vendor/shims/babel.js');
app.import('vendor/shims/path.js');
app.import('bower_components/file-saver/FileSaver.js');
Expand All @@ -110,7 +110,7 @@ module.exports = function(defaults) {
}
});
testLoaderTree = babelTranspiler(testLoaderTree, {
modules:'amdStrict',
modules: 'amdStrict',
moduleIds:true
});

Expand Down
18 changes: 10 additions & 8 deletions mirage/factories/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ export default Mirage.Factory.extend({
},
//owner,
forks: [],
history: [
{
"user": null,
"version": "921e8958fe32b5a1b724fa6754d0dd904cfa9e62",
"committed_at": "2015-07-23T22:49:45Z",
"url": `https://api.github.com/gists/${this.id}/921e8958fe32b5a1b724fa6754d0dd904cfa9e62`
}
]
history() {
return [
{
"user": null,
"version": "921e8958fe32b5a1b724fa6754d0dd904cfa9e62",
"committed_at": "2015-07-23T22:49:45Z",
"url": `https://api.github.com/gists/${this.id}/921e8958fe32b5a1b724fa6754d0dd904cfa9e62`
}
];
}
});

/* Sample
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/addons-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ test('Ember Data works as an addon', function (assert) {
filename: "models.app.js",
content: `import Ember from "ember";
import DS from "ember-data";
console.log(DS);
debugger;
export default DS.Model.extend({
appName: DS.attr('string')
});`
Expand Down Expand Up @@ -115,6 +117,8 @@ test('Ember Data works as an addon', function (assert) {
andThen(function() {
const outputDiv = 'div';

debugger;

assert.equal(outputContents(outputDiv), 'Welcome to Ember Twiddle');
});
});
2 changes: 1 addition & 1 deletion vendor/shims/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function vendorModule() {
'use strict';

return { 'default': self['babel'] };
return { 'default': self['Babel'] };
}

define('babel-core', [], vendorModule);
Expand Down

0 comments on commit 326b090

Please sign in to comment.