Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to transfer compilation errors into runtime. #208

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ describe('htmlbars-inline-precompile', function() {
`);
});

it('avoids a build time error when passed `insertRuntimeErrors`', function() {
precompile = () => {
throw new Error('NOOOOOOOOOOOOOOOOOOOOOO');
};

let transformed = transform(
`import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('hello', { insertRuntimeErrors: true });`
);

expect(transformed).toMatchInlineSnapshot(`
"var compiled = function () {
throw new Error(\\"NOOOOOOOOOOOOOOOOOOOOOO\\");
}();"
`);
});

it('escapes any */ included in the template string', function() {
let transformed = transform(stripIndent`
import hbs from 'htmlbars-inline-precompile';
Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module.exports = function(babel) {
let t = babel.types;

const runtimeErrorIIFE = babel.template(
`(function() {\n throw new Error('ERROR_MESSAGE');\n})();`
);

function buildExpression(value) {
switch (typeof value) {
case 'string':
Expand Down Expand Up @@ -88,7 +92,18 @@ module.exports = function(babel) {
function compileTemplate(precompile, template, _options) {
let options = Object.assign({ contents: template }, _options);

let precompileResult = precompile(template, options);
let precompileResult;

if (options.insertRuntimeErrors) {
try {
precompileResult = precompile(template, options);
} catch (error) {
return runtimeErrorIIFE({ ERROR_MESSAGE: error.message });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume babel is taking care of escaping for us, is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, will test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, the error.message itself is already a string, and any valid string will work (including any escaping needed internally).

}
} else {
precompileResult = precompile(template, options);
}

let precompiled = JSON.parse(precompileResult);

let templateExpression = buildExpression(precompiled);
Expand Down