Skip to content

Commit 7c54d92

Browse files
authored
Merge pull request #4 from Polyconseil/master
Add options to control the output format
2 parents d226f99 + 372529e commit 7c54d92

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
const { compile } = require('svelte');
2+
const { parseQuery } = require('loader-utils');
23

34
module.exports = function(source, map) {
45
this.cacheable();
56

67
const filename = this.resourcePath;
8+
const query = parseQuery(this.query);
79

810
try {
911
let { code, map } = compile(source, {
1012
// name: CamelCase component name
1113
filename: filename,
12-
format: 'es',
14+
format: query.format || 'es',
15+
name: query.name,
1316
onerror: (err) => {
1417
console.log(err.message);
1518
this.emitError(err.message);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"chai": "^3.5.0",
1919
"eslint": "^3.11.1",
2020
"eslint-plugin-mocha": "^4.7.0",
21+
"loader-utils": "^0.2.16",
2122
"mocha": "^3.2.0",
2223
"sinon": "^1.17.6",
2324
"sinon-chai": "^2.8.0",

test/loader.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const loader = require('../');
1515

1616
describe('loader', function() {
1717

18-
function testLoader(fileName, callback) {
18+
function testLoader(fileName, callback, query) {
1919

2020
return function() {
2121

@@ -29,6 +29,7 @@ describe('loader', function() {
2929
cacheable: cacheableSpy,
3030
callback: callbackSpy,
3131
filename: fileName,
32+
query: query,
3233
}, fileContents, null);
3334

3435
expect(callbackSpy).to.have.been.called;
@@ -83,9 +84,25 @@ describe('loader', function() {
8384
})
8485
);
8586

87+
88+
it('should compile Component to CJS if requested',
89+
testLoader('test/fixtures/good.html', function(err, code, map) {
90+
expect(err).not.to.exist;
91+
expect(code).to.contain('module.exports = SvelteComponent;');
92+
}, { format: 'cjs' })
93+
);
94+
95+
96+
it('should compile Component to UMD if requested',
97+
testLoader('test/fixtures/good.html', function(err, code, map) {
98+
expect(err).not.to.exist;
99+
expect(code).to.contain('(global.FooComponent = factory());');
100+
}, { format: 'umd', name: 'FooComponent' })
101+
);
102+
86103
});
87104

88105

89106
function readFile(path) {
90107
return readFileSync(path, 'utf-8');
91-
}
108+
}

0 commit comments

Comments
 (0)