|
| 1 | +import assert from 'assert'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import {parse} from 'acorn'; |
| 4 | +import {addLineNumbers, env, normalizeHtml, svelte} from '../helpers.js'; |
| 5 | + |
| 6 | +function tryRequire(file) { |
| 7 | + try { |
| 8 | + const mod = require(file); |
| 9 | + return mod.default || mod; |
| 10 | + } catch (err) { |
| 11 | + if (err.code !== 'MODULE_NOT_FOUND') throw err; |
| 12 | + return null; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function normalizeWarning(warning) { |
| 17 | + warning.frame = warning.frame.replace(/^\n/, ''). |
| 18 | + replace(/^\t+/gm, ''). |
| 19 | + replace(/\s+$/gm, ''); |
| 20 | + delete warning.filename; |
| 21 | + delete warning.toString; |
| 22 | + return warning; |
| 23 | +} |
| 24 | + |
| 25 | +function checkCodeIsValid(code) { |
| 26 | + try { |
| 27 | + parse(code); |
| 28 | + } catch (err) { |
| 29 | + console.error(addLineNumbers(code)); |
| 30 | + throw new Error(err.message); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +describe('preprocess', () => { |
| 35 | + fs.readdirSync('test/preprocess/samples').forEach(dir => { |
| 36 | + if (dir[0] === '.') return; |
| 37 | + |
| 38 | + // add .solo to a sample directory name to only run that test |
| 39 | + const solo = /\.solo/.test(dir); |
| 40 | + const skip = /\.skip/.test(dir); |
| 41 | + |
| 42 | + if (solo && process.env.CI) { |
| 43 | + throw new Error('Forgot to remove `solo: true` from test'); |
| 44 | + } |
| 45 | + |
| 46 | + (solo ? it.only : skip ? it.skip : it)(dir, () => { |
| 47 | + const config = tryRequire(`./samples/${dir}/_config.js`) || {}; |
| 48 | + const input = fs.existsSync(`test/preprocess/samples/${dir}/input.pug`) ? |
| 49 | + fs.readFileSync(`test/preprocess/samples/${dir}/input.pug`, |
| 50 | + 'utf-8').replace(/\s+$/, '') : |
| 51 | + fs.readFileSync(`test/preprocess/samples/${dir}/input.html`, |
| 52 | + 'utf-8').replace(/\s+$/, ''); |
| 53 | + |
| 54 | + svelte.preprocess(input, config). |
| 55 | + then(processed => processed.toString()). |
| 56 | + then(processed => { |
| 57 | + |
| 58 | + const expectedWarnings = (config.warnings || []).map( |
| 59 | + normalizeWarning); |
| 60 | + const domWarnings = []; |
| 61 | + const ssrWarnings = []; |
| 62 | + |
| 63 | + const dom = svelte.compile( |
| 64 | + processed, |
| 65 | + Object.assign(config, { |
| 66 | + format: 'iife', |
| 67 | + name: 'SvelteComponent', |
| 68 | + onwarn: warning => { |
| 69 | + domWarnings.push(warning); |
| 70 | + }, |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + const ssr = svelte.compile( |
| 75 | + processed, |
| 76 | + Object.assign(config, { |
| 77 | + format: 'iife', |
| 78 | + generate: 'ssr', |
| 79 | + name: 'SvelteComponent', |
| 80 | + onwarn: warning => { |
| 81 | + ssrWarnings.push(warning); |
| 82 | + }, |
| 83 | + }) |
| 84 | + ); |
| 85 | + |
| 86 | + // check the code is valid |
| 87 | + checkCodeIsValid(dom.code); |
| 88 | + checkCodeIsValid(ssr.code); |
| 89 | + |
| 90 | + assert.equal(dom.css, ssr.css); |
| 91 | + |
| 92 | + assert.deepEqual( |
| 93 | + domWarnings.map(normalizeWarning), |
| 94 | + ssrWarnings.map(normalizeWarning) |
| 95 | + ); |
| 96 | + assert.deepEqual(domWarnings.map(normalizeWarning), expectedWarnings); |
| 97 | + |
| 98 | + const expected = { |
| 99 | + html: read(`test/preprocess/samples/${dir}/expected.html`), |
| 100 | + css: read(`test/preprocess/samples/${dir}/expected.css`), |
| 101 | + }; |
| 102 | + |
| 103 | + if (expected.css !== null) { |
| 104 | + fs.writeFileSync(`test/preprocess/samples/${dir}/_actual.css`, |
| 105 | + dom.css); |
| 106 | + assert.equal(dom.css.replace(/svelte-\d+/g, 'svelte-xyz'), |
| 107 | + expected.css); |
| 108 | + } |
| 109 | + |
| 110 | + // verify that the right elements have scoping selectors |
| 111 | + if (expected.html !== null) { |
| 112 | + const window = env(); |
| 113 | + |
| 114 | + // dom |
| 115 | + try { |
| 116 | + const Component = eval( |
| 117 | + `(function () { ${dom.code}; return SvelteComponent; }())` |
| 118 | + ); |
| 119 | + const target = window.document.querySelector('main'); |
| 120 | + |
| 121 | + new Component({target, data: config.data}); |
| 122 | + const html = target.innerHTML; |
| 123 | + |
| 124 | + fs.writeFileSync(`test/preprocess/samples/${dir}/_actual.html`, |
| 125 | + html); |
| 126 | + |
| 127 | + assert.equal( |
| 128 | + normalizeHtml(window, |
| 129 | + html.replace(/svelte-\d+/g, 'svelte-xyz')), |
| 130 | + normalizeHtml(window, expected.html) |
| 131 | + ); |
| 132 | + } catch (err) { |
| 133 | + console.log(dom.code); |
| 134 | + throw err; |
| 135 | + } |
| 136 | + |
| 137 | + // ssr |
| 138 | + try { |
| 139 | + const component = eval( |
| 140 | + `(function () { ${ssr.code}; return SvelteComponent; }())` |
| 141 | + ); |
| 142 | + |
| 143 | + assert.equal( |
| 144 | + normalizeHtml( |
| 145 | + window, |
| 146 | + component.render(config.data). |
| 147 | + replace(/svelte-\d+/g, 'svelte-xyz') |
| 148 | + ), |
| 149 | + normalizeHtml(window, expected.html) |
| 150 | + ); |
| 151 | + } catch (err) { |
| 152 | + console.log(ssr.code); |
| 153 | + throw err; |
| 154 | + } |
| 155 | + } |
| 156 | + }).catch(error => { |
| 157 | + throw error; |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +function read(file) { |
| 164 | + try { |
| 165 | + return fs.readFileSync(file, 'utf-8'); |
| 166 | + } catch (err) { |
| 167 | + return null; |
| 168 | + } |
| 169 | +} |
0 commit comments