diff --git a/index.js b/index.js index 10872dda..e5af8f03 100644 --- a/index.js +++ b/index.js @@ -9,22 +9,15 @@ module.exports = function(source, map) { try { let { code, map } = compile(source, { - // name: CamelCase component name filename: filename, format: query.format || 'es', - name: query.name, - onerror: (err) => { - console.log(err.message); - this.emitError(err.message); - }, - onwarn: (warn) => { - console.log(warn.message); - this.emitWarn(warn.message); - } + name: query.name }); this.callback(null, code, map); } catch (err) { - this.callback(err); + // wrap error to provide correct + // context when logging to console + this.callback(new Error(err.toString())); } }; diff --git a/test/fixtures/bad.html b/test/fixtures/bad.html deleted file mode 100644 index 4481171c..00000000 --- a/test/fixtures/bad.html +++ /dev/null @@ -1,2 +0,0 @@ -

Count: {{count}}

- \ No newline at end of file diff --git a/test/fixtures/export-error.html b/test/fixtures/export-error.html new file mode 100644 index 00000000..52d97ad8 --- /dev/null +++ b/test/fixtures/export-error.html @@ -0,0 +1,7 @@ +

Count: {{count}}

+ + \ No newline at end of file diff --git a/test/fixtures/parse-error.html b/test/fixtures/parse-error.html new file mode 100644 index 00000000..eaa6d49a --- /dev/null +++ b/test/fixtures/parse-error.html @@ -0,0 +1,2 @@ +

Count: {{{count}}

+ \ No newline at end of file diff --git a/test/fixtures/validation-error.html b/test/fixtures/validation-error.html new file mode 100644 index 00000000..e152e795 --- /dev/null +++ b/test/fixtures/validation-error.html @@ -0,0 +1,9 @@ +

Count: {{count}}

+ + \ No newline at end of file diff --git a/test/loader.spec.js b/test/loader.spec.js index fc0b0c2e..4b34f42c 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -19,17 +19,17 @@ describe('loader', function() { return function() { + const fileContents = readFile(fileName); + const cacheableSpy = spy(function() { }); const callbackSpy = spy(callback); - const fileContents = readFile(fileName); - loader.call({ cacheable: cacheableSpy, callback: callbackSpy, filename: fileName, - query: query, + query, }, fileContents, null); expect(callbackSpy).to.have.been.called; @@ -38,7 +38,7 @@ describe('loader', function() { } - it('should compile good', + it('should compile', testLoader('test/fixtures/good.html', function(err, code, map) { expect(err).not.to.exist; @@ -48,57 +48,119 @@ describe('loader', function() { ); - it('should compile bad', - testLoader('test/fixtures/bad.html', function(err, code, map) { - expect(err).to.exist; + describe('error handling', function() { - expect(code).not.to.exist; - expect(map).not.to.exist; - }) - ); + it('should handle parse error', + testLoader('test/fixtures/parse-error.html', function(err, code, map, context) { + expect(err).to.exist; - it('should compile with import / ES2015 features', - testLoader('test/fixtures/es2015.html', function(err, code, map) { - expect(err).not.to.exist; + expect(err.message).to.eql( + 'Expected }}} (1:18)\n' + + '1:

Count: {{{count}}

\n' + + ' ^\n' + + '2: ' + ); - expect(code).to.exist; - expect(map).to.exist; + expect(code).not.to.exist; + expect(map).not.to.exist; + }) + ); - // es2015 statements remain - expect(code).to.contain('import { hello } from \'./utils\';'); - expect(code).to.contain('data() {'); - }) - ); + it('should handle wrong export', + testLoader('test/fixtures/export-error.html', function(err, code, map, context) { - it('should compile Component with with nesting', - testLoader('test/fixtures/parent.html', function(err, code, map) { - expect(err).not.to.exist; + expect(err).to.exist; - // es2015 statements remain - expect(code).to.contain('import Nested from \'./nested\';'); + expect(err.message).to.eql( + 'Unexpected token (5:7)\n' + + '3: ' + ); - expect(code).to.exist; - expect(map).to.exist; - }) - ); + expect(code).not.to.exist; + expect(map).not.to.exist; + }) + ); - it('should compile Component to CJS if requested', - testLoader('test/fixtures/good.html', function(err, code, map) { - expect(err).not.to.exist; - expect(code).to.contain('module.exports = SvelteComponent;'); - }, { format: 'cjs' }) - ); + it('should validation error', + testLoader('test/fixtures/validation-error.html', function(err, code, map, context) { + expect(err).to.exist; - it('should compile Component to UMD if requested', - testLoader('test/fixtures/good.html', function(err, code, map) { - expect(err).not.to.exist; - expect(code).to.contain('(global.FooComponent = factory());'); - }, { format: 'umd', name: 'FooComponent' }) - ); + expect(err.message).to.eql( + 'Computed properties can be function expressions or arrow function expressions (6:11)\n' + + '4: export default {\n' + + '5: computed: {\n' + + '6: foo: \'BAR\'\n' + + ' ^\n' + + '7: }\n' + + '8: };' + ); + + expect(code).not.to.exist; + expect(map).not.to.exist; + }) + ); + + }); + + + describe('ES2015 features', function() { + + it('should keep imports / methods', + testLoader('test/fixtures/es2015.html', function(err, code, map) { + expect(err).not.to.exist; + + expect(code).to.exist; + expect(map).to.exist; + + // es2015 statements remain + expect(code).to.contain('import { hello } from \'./utils\';'); + expect(code).to.contain('data() {'); + }) + ); + + + it('should keep nested Component import', + testLoader('test/fixtures/parent.html', function(err, code, map) { + expect(err).not.to.exist; + + // es2015 statements remain + expect(code).to.contain('import Nested from \'./nested\';'); + + expect(code).to.exist; + expect(map).to.exist; + }) + ); + + }); + + + describe('configuration via query', function() { + + it('should configure CommonJS output', + testLoader('test/fixtures/good.html', function(err, code, map) { + expect(err).not.to.exist; + expect(code).to.contain('module.exports = SvelteComponent;'); + }, { format: 'cjs' }) + ); + + + it('should configure named UMD output', + testLoader('test/fixtures/good.html', function(err, code, map) { + expect(err).not.to.exist; + expect(code).to.contain('(global.FooComponent = factory());'); + }, { format: 'umd', name: 'FooComponent' }) + ); + + }); });