From 372529e6ee4c68e140a3e21ca4cfce3877e20abc Mon Sep 17 00:00:00 2001 From: Victor Perron Date: Thu, 8 Dec 2016 10:00:22 +0100 Subject: [PATCH] Add options to control the output format --- index.js | 5 ++++- package.json | 1 + test/loader.spec.js | 21 +++++++++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f2dce60b..10872dda 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,18 @@ const { compile } = require('svelte'); +const { parseQuery } = require('loader-utils'); module.exports = function(source, map) { this.cacheable(); const filename = this.resourcePath; + const query = parseQuery(this.query); try { let { code, map } = compile(source, { // name: CamelCase component name filename: filename, - format: 'es', + format: query.format || 'es', + name: query.name, onerror: (err) => { console.log(err.message); this.emitError(err.message); diff --git a/package.json b/package.json index dee577d2..5f9434bb 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "chai": "^3.5.0", "eslint": "^3.11.1", "eslint-plugin-mocha": "^4.7.0", + "loader-utils": "^0.2.16", "mocha": "^3.2.0", "sinon": "^1.17.6", "sinon-chai": "^2.8.0", diff --git a/test/loader.spec.js b/test/loader.spec.js index c521412a..fc0b0c2e 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -15,7 +15,7 @@ const loader = require('../'); describe('loader', function() { - function testLoader(fileName, callback) { + function testLoader(fileName, callback, query) { return function() { @@ -29,6 +29,7 @@ describe('loader', function() { cacheable: cacheableSpy, callback: callbackSpy, filename: fileName, + query: query, }, fileContents, null); expect(callbackSpy).to.have.been.called; @@ -83,9 +84,25 @@ describe('loader', function() { }) ); + + 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 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' }) + ); + }); function readFile(path) { return readFileSync(path, 'utf-8'); -} \ No newline at end of file +}