Skip to content

Commit

Permalink
fix: add ability to pass functions in config
Browse files Browse the repository at this point in the history
  • Loading branch information
orlov-vo committed Jan 11, 2021
1 parent 4595812 commit 8e52663
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 34 deletions.
46 changes: 12 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ const { Transformer } = require('@parcel/plugin');
const { default: SourceMap } = require('@parcel/source-map');
const { relativeUrl } = require('@parcel/utils');
const { compile, preprocess } = require('svelte/compiler.js');
const { load, preSerialize, postDeserialize } = require('./loadConfig');

Object.defineProperty(exports, '__esModule', { value: true });

const CONFIG_FILES = ['.svelterc', 'svelte.config.js'];
const CONFIG_PACKAGE_KEY = 'svelte';

function generateName(input) {
let name = path
.basename(input)
Expand Down Expand Up @@ -40,51 +38,31 @@ async function handleError(sourceFileName, func) {
}
}

async function getConfig(config) {
const packageKey = CONFIG_PACKAGE_KEY;
const data = await config.getConfig(CONFIG_FILES, { packageKey });

return data && data.contents ? data.contents : {};
}

exports.default = new Transformer({
async loadConfig({ config, options }) {
const customOptions = await getConfig(config);

if (customOptions.compiler) {
console.error(
'The "compiler" option in .svelterc is deprecated, use "compilerOptions" instead',
);
customOptions.compilerOptions =
customOptions.compilerOptions || customOptions.compiler;
}

const compilerOptions = {
css: false,
...customOptions.compilerOptions,
loadConfig({ config, options, logger }) {
return load({ config, options, logger });
},

dev: options.mode !== 'production',
};
const preprocess = customOptions.preprocess;
preSerializeConfig({ config }) {
return preSerialize(config);
},

config.setResult({
compilerOptions,
preprocess,
});
postDeserializeConfig({ config, options }) {
return postDeserialize(config, options);
},

async transform({ asset, config, options }) {
let code = await asset.getCode();
const sourceFileName = relativeUrl(options.projectRoot, asset.filePath);
const compilerOptions = {
...config.compilerOptions,
...config.raw.compilerOptions,
filename: sourceFileName,
name: generateName(sourceFileName),
};

if (config.preprocess) {
if (config.hydrated.preprocess) {
const preprocessed = await handleError(sourceFileName, () =>
preprocess(code, config.preprocess, compilerOptions),
preprocess(code, config.hydrated.preprocess, compilerOptions),
);
code = preprocessed.toString();
}
Expand Down
87 changes: 87 additions & 0 deletions loadConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const path = require('path');

Object.defineProperty(exports, '__esModule', { value: true });

const CONFIG_FILES = ['.svelterc', 'svelte.config.js'];
const CONFIG_PACKAGE_KEY = 'svelte';

async function getConfigFile(config) {
const packageKey = CONFIG_PACKAGE_KEY;
const file = await config.getConfig(CONFIG_FILES, { packageKey });

return file || null;
}

async function configHydrator(configFile, config, options) {
config.setResult({
raw: configFile,
hydrated: {
preprocess: configFile.preprocess,
},
});
}

exports.load = async function load({ config, options, logger }) {
const configFile = await getConfigFile(config);
if (!configFile) return;

const { contents } = configFile;
const isDynamic = path.extname(configFile.filePath) === '.js';

if (typeof contents !== 'object') {
throw new Error('Svelte config should be an object.');
}

if (isDynamic) {
if (!contents.preprocess) {
logger.warn({
message:
'WARNING: Using a JavaScript Svelte config file means losing out on caching features of Parcel. Use a .svelterc(.json) file whenever possible.',
});
}

config.shouldInvalidateOnStartup();

if (contents.preprocess) {
config.shouldReload();
}
}

if (contents.compiler) {
logger.warn({
message:
'WARNING: The "compiler" option in .svelterc is deprecated, use "compilerOptions" instead',
});
contents.compilerOptions = contents.compilerOptions || contents.compiler;
}

return configHydrator(
{
compilerOptions: {
css: false,
...contents.compilerOptions,

dev: options.mode !== 'production',
},
preprocess: contents.preprocess,
},
config,
options,
);
};

exports.preSerialize = function preSerialize(config) {
if (!config.result) return;

// Ensure we don't pass preprocess functions to the serializer
if (config.result.raw.preprocess) {
config.result.raw = {};
}

// This gets re-hydrated in Deserialize, so never store this
config.result.hydrated = {};
};

exports.postDeserialize = function postDeserialize(config, options) {
return configHydrator(config.result.raw, config, options);
};

0 comments on commit 8e52663

Please sign in to comment.