Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const { basename, extname, relative } = require('path');
const { relative } = require('path');
const { getOptions } = require('loader-utils');
const VirtualModules = require('./lib/virtual');

const hotApi = require.resolve('./lib/hot-api.js');

const { version } = require('svelte/package.json');
const major_version = +version[0];
const { compile, preprocess } = major_version >= 3
? require('svelte/compiler')
: require('svelte');
const { compile, preprocess } = require('svelte/compiler');

const pluginOptions = {
externalDependencies: true,
Expand Down Expand Up @@ -53,19 +49,6 @@ function posixify(file) {
return file.replace(/[/\\]/g, '/');
}

function sanitize(input) {
return basename(input)
.replace(extname(input), '')
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
}

function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}

function normalize(compiled) {
// svelte.compile signature changed in 1.60 — this avoids
// future deprecation warnings while preserving backwards
Expand Down Expand Up @@ -115,19 +98,11 @@ module.exports = function(source, map) {

const compileOptions = {
filename: this.resourcePath,
format: options.format || (major_version >= 3 ? 'esm' : 'es')
format: options.format || 'esm'
};

const handleWarning = warning => this.emitWarning(new Error(warning));

if (major_version >= 3) {
// TODO anything?
} else {
compileOptions.shared = options.shared || 'svelte/shared.js';
compileOptions.name = capitalize(sanitize(compileOptions.filename));
compileOptions.onwarn = options.onwarn || handleWarning;
}

for (const option in options) {
if (!pluginOptions[option]) compileOptions[option] = options[option];
}
Expand All @@ -146,13 +121,11 @@ module.exports = function(source, map) {

let { js, css, warnings } = normalize(compile(processed.toString(), compileOptions));

if (major_version >= 3) {
warnings.forEach(
options.onwarn
? warning => options.onwarn(warning, handleWarning)
: handleWarning
);
}
warnings.forEach(
options.onwarn
? warning => options.onwarn(warning, handleWarning)
: handleWarning
);

if (options.hotReload && !isProduction && !isServer) {
const hotOptions = Object.assign({}, options.hotOptions);
Expand Down