forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.js
141 lines (113 loc) · 3.67 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import spaces from '../src/utils/spaces.js';
import assert from 'assert';
import * as path from 'path';
import * as fs from 'fs';
import * as acorn from 'acorn';
import { svelte, env, setupHtmlEqual } from './helpers.js';
let showCompiledCode = false;
let compileOptions = null;
require.extensions[ '.html' ] = function ( module, filename ) {
const options = Object.assign({ filename }, compileOptions );
const { code } = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options );
if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
return module._compile( code, filename );
};
function addLineNumbers ( code ) {
return code.split( '\n' ).map( ( line, i ) => {
i = String( i + 1 );
while ( i.length < 3 ) i = ` ${i}`;
return `${i}: ${line.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) )}`;
}).join( '\n' );
}
function loadConfig ( dir ) {
try {
const resolved = require.resolve( `./generator/${dir}/_config.js` );
delete require.cache[ resolved ];
return require( resolved ).default;
} catch ( err ) {
if ( err.code === 'E_NOT_FOUND' ) {
return {};
}
throw err;
}
}
describe( 'generate', () => {
before( setupHtmlEqual );
function runTest ( dir, shared ) {
if ( dir[0] === '.' ) return;
const config = loadConfig( dir );
if ( config.solo && process.env.CI ) {
throw new Error( 'Forgot to remove `solo: true` from test' );
}
( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => {
let compiled;
showCompiledCode = config.show;
compileOptions = config.compileOptions || {};
compileOptions.shared = shared;
try {
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' );
compiled = svelte.compile( source );
} catch ( err ) {
if ( config.compileError ) {
config.compileError( err );
return;
} else {
throw err;
}
}
const { code } = compiled;
// check that no ES2015+ syntax slipped in
try {
const startIndex = code.indexOf( 'function renderMainFragment' ); // may change!
const es5 = spaces( startIndex ) + code.slice( startIndex ).replace( /export default .+/, '' );
acorn.parse( es5, { ecmaVersion: 5 });
} catch ( err ) {
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
throw err;
}
Object.keys( require.cache ).filter( x => x.endsWith( '.html' ) ).forEach( file => {
delete require.cache[ file ];
});
let SvelteComponent;
try {
SvelteComponent = require( `./generator/${dir}/main.html` ).default;
} catch ( err ) {
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
throw err;
}
return env()
.then( window => {
// Put the constructor on window for testing
window.SvelteComponent = SvelteComponent;
const target = window.document.querySelector( 'main' );
const component = new SvelteComponent({
target,
data: config.data
});
if ( config.html ) {
assert.htmlEqual( target.innerHTML, config.html );
}
if ( config.test ) {
config.test( assert, component, target, window );
} else {
component.teardown();
assert.equal( target.innerHTML, '' );
}
})
.catch( err => {
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console
throw err;
});
});
}
describe( 'inline helpers', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, null );
});
});
describe( 'shared helpers', () => {
fs.readdirSync( 'test/generator' ).forEach( dir => {
runTest( dir, path.resolve( 'shared.js' ) );
});
});
});