Skip to content

Commit

Permalink
run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jun 12, 2017
1 parent 9194fdf commit cc44739
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 61 deletions.
1 change: 0 additions & 1 deletion .eslintrc
Expand Up @@ -6,7 +6,6 @@
"semi": [ 2, "always" ],
"keyword-spacing": [ 2, { "before": true, "after": true } ],
"space-before-blocks": [ 2, "always" ],
"space-before-function-paren": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": [ 0 ]
},
Expand Down
7 changes: 1 addition & 6 deletions rollup.config.js
Expand Up @@ -2,12 +2,7 @@ import buble from 'rollup-plugin-buble';

export default {
entry: 'src/index.js',
external: [
'path',
'rollup-pluginutils',
'svelte',
'require-relative'
],
external: ['path', 'rollup-pluginutils', 'svelte', 'require-relative'],
plugins: [
buble({
target: { node: 4 }
Expand Down
117 changes: 64 additions & 53 deletions src/index.js
Expand Up @@ -4,17 +4,18 @@ import relative from 'require-relative';
import { compile } from 'svelte';
import { createFilter } from 'rollup-pluginutils';

function sanitize ( input ) {
return path.basename( input )
.replace( path.extname( input ), '' )
.replace( /[^a-zA-Z_$0-9]+/g, '_' )
.replace( /^_/, '' )
.replace( /_$/, '' )
.replace( /^(\d)/, '_$1' );
function sanitize(input) {
return path
.basename(input)
.replace(path.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 capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}

const pluginOptions = {
Expand All @@ -23,117 +24,127 @@ const pluginOptions = {
extensions: true
};

function tryRequire ( id ) {
function tryRequire(id) {
try {
return require( id );
} catch ( err ) {
return require(id);
} catch (err) {
return null;
}
}

function exists ( file ) {
function exists(file) {
try {
fs.statSync( file );
fs.statSync(file);
return true;
} catch ( err ) {
if ( err.code === 'ENOENT' ) return false;
} catch (err) {
if (err.code === 'ENOENT') return false;
throw err;
}
}

export default function svelte ( options = {} ) {
const filter = createFilter( options.include, options.exclude );
export default function svelte(options = {}) {
const filter = createFilter(options.include, options.exclude);

const extensions = options.extensions || [ '.html', '.svelte' ];
const extensions = options.extensions || ['.html', '.svelte'];

const fixedOptions = {};

Object.keys( options ).forEach( key => {
Object.keys(options).forEach(key => {
// add all options except include, exclude, extensions
if ( pluginOptions[ key ] ) return;
fixedOptions[ key ] = options[ key ];
if (pluginOptions[key]) return;
fixedOptions[key] = options[key];
});

fixedOptions.format = 'es';
fixedOptions.shared = require.resolve( 'svelte/shared.js' );
fixedOptions.shared = require.resolve('svelte/shared.js');
fixedOptions.onerror = err => {
let message = ( err.loc ? `(${err.loc.line}:${err.loc.column}) ` : '' ) + err.message;
if ( err.frame ) message += `\n${err.frame}`;
let message =
(err.loc ? `(${err.loc.line}:${err.loc.column}) ` : '') + err.message;
if (err.frame) message += `\n${err.frame}`;

const err2 = new Error( message );
const err2 = new Error(message);
err2.stack = err.stack;

throw err2;
};

// handle CSS extraction
if ( 'css' in options ) {
if ( typeof options.css !== 'function' && typeof options.css !== 'boolean' ) {
throw new Error( 'options.css must be a boolean or a function' );
if ('css' in options) {
if (typeof options.css !== 'function' && typeof options.css !== 'boolean') {
throw new Error('options.css must be a boolean or a function');
}
}

let css = options.css && typeof options.css === 'function' ? options.css : null;
let css = options.css && typeof options.css === 'function'
? options.css
: null;
const cssLookup = new Map();

if ( css ) {
if (css) {
fixedOptions.css = false;
}

return {
name: 'svelte',

resolveId ( importee, importer ) {
if ( !importer || path.isAbsolute( importee ) || importee[0] === '.' ) return null;
resolveId(importee, importer) {
if (!importer || path.isAbsolute(importee) || importee[0] === '.')
return null;

// if this is a bare import, see if there's a valid pkg.svelte
const parts = importee.split('/');
let name = parts.shift();
if ( name[0] === '@' ) name += `/${parts.shift()}`;
if (name[0] === '@') name += `/${parts.shift()}`;

const resolved = relative.resolve( `${name}/package.json`, path.dirname( importer ) );
const pkg = tryRequire( resolved );
if ( !pkg ) return null;
const resolved = relative.resolve(
`${name}/package.json`,
path.dirname(importer)
);
const pkg = tryRequire(resolved);
if (!pkg) return null;

const dir = path.dirname(resolved);

if ( parts.length === 0 ) {
if (parts.length === 0) {
// use pkg.svelte
if ( pkg.svelte ) {
if (pkg.svelte) {
return path.resolve(dir, pkg.svelte);
}
} else {
if ( pkg['svelte.root'] ) {
if (pkg['svelte.root']) {
const sub = path.resolve(dir, pkg['svelte.root'], parts.join('/'));
if ( exists( sub ) ) return sub;
if (exists(sub)) return sub;
}
}
},

transform ( code, id ) {
if ( !filter( id ) ) return null;
if ( !~extensions.indexOf( path.extname( id ) ) ) return null;
transform(code, id) {
if (!filter(id)) return null;
if (!~extensions.indexOf(path.extname(id))) return null;

const compiled = compile( code, Object.assign( {}, fixedOptions, {
name: capitalize( sanitize( id ) ),
filename: id
}));
const compiled = compile(
code,
Object.assign({}, fixedOptions, {
name: capitalize(sanitize(id)),
filename: id
})
);

if ( css ) cssLookup.set( id, compiled.css );
if (css) cssLookup.set(id, compiled.css);

return compiled;
},

ongenerate () {
if ( css ) {
ongenerate() {
if (css) {
// write out CSS file. TODO would be nice if there was a
// a more idiomatic way to do this in Rollup
let result = '';
for ( let chunk of cssLookup.values() ) {
for (let chunk of cssLookup.values()) {
result += chunk || '';
}

css( result );
css(result);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -19,4 +19,4 @@ describe('rollup-plugin-svelte', () => {
path.resolve('test/node_modules/widgets/src/Foo.html')
);
});
});
});

0 comments on commit cc44739

Please sign in to comment.