|
| 1 | +const { basename, extname } = require('path'); |
1 | 2 | const { compile } = require('svelte');
|
2 | 3 | const { getOptions } = require('loader-utils');
|
3 | 4 |
|
| 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 | + |
4 | 18 | module.exports = function(source, map) {
|
5 | 19 | this.cacheable();
|
6 | 20 |
|
7 | 21 | 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)); |
9 | 29 |
|
10 | 30 | 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); |
19 | 32 |
|
20 | 33 | this.callback(null, code, map);
|
21 | 34 | } catch (err) {
|
|
0 commit comments