Skip to content

Commit 493c4bb

Browse files
committed
pass through all options, override format and shared (#17)
1 parent b10c59a commit 493c4bb

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

index.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1+
const { basename, extname } = require('path');
12
const { compile } = require('svelte');
23
const { getOptions } = require('loader-utils');
34

5+
function sanitize(input) {
6+
return basename(input)
7+
.replace(extname(input), '')
8+
.replace(/[^a-zA-Z_$0-9]+/g, '_')
9+
.replace(/^_/, '')
10+
.replace(/_$/, '')
11+
.replace(/^(\d)/, '_$1');
12+
}
13+
14+
function capitalize(str) {
15+
return str[0].toUpperCase() + str.slice(1);
16+
}
17+
418
module.exports = function(source, map) {
519
this.cacheable();
620

721
const filename = this.filename;
8-
const options = getOptions(this) || {};
22+
const options = Object.assign(getOptions(this) || {}, {
23+
filename,
24+
format: 'es',
25+
shared: require.resolve('svelte/shared.js')
26+
});
27+
28+
if (!options.name) options.name = capitalize(sanitize(filename));
929

1030
try {
11-
let { code, map } = compile(source, {
12-
filename: filename,
13-
generate: options.generate,
14-
format: 'es',
15-
shared: require.resolve('svelte/shared.js'),
16-
name: options.name,
17-
css: options.css !== false,
18-
});
31+
let { code, map } = compile(source, options);
1932

2033
this.callback(null, code, map);
2134
} catch (err) {

test/loader.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('loader', function() {
171171
testLoader('test/fixtures/good.html', function(err, code, map) {
172172
expect(err).not.to.exist;
173173

174-
expect(code).not.to.contain('SvelteComponent.render = function ( root, options ) {');
174+
expect(code).not.to.contain('.render = function ( root, options ) {');
175175
})
176176
);
177177

@@ -180,7 +180,7 @@ describe('loader', function() {
180180
testLoader('test/fixtures/good.html', function(err, code, map) {
181181
expect(err).not.to.exist;
182182

183-
expect(code).to.contain('SvelteComponent.render = function ( root, options ) {');
183+
expect(code).to.contain('.render = function ( root, options ) {');
184184
}, { generate: 'ssr' })
185185
);
186186

0 commit comments

Comments
 (0)