Skip to content

Commit

Permalink
fix tests for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 19, 2019
1 parent ade07c2 commit f919ccf
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 129 deletions.
49 changes: 34 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ const { compile, preprocess } = major_version >= 3
? require('svelte/compiler')
: require('svelte');

const pluginOptions = {
externalDependencies: true,
hotReload: true,
hotOptions: true,
preprocess: true,
emitCss: true,

// legacy
shared: true,
style: true,
script: true,
markup: true
};

function makeHot(id, code, hotOptions) {
const options = JSON.stringify(hotOptions);
const replacement = `
Expand Down Expand Up @@ -99,22 +113,27 @@ module.exports = function(source, map) {
const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr');
const isProduction = this.minimize || process.env.NODE_ENV === 'production';

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

if (major_version >= 3) {
// TODO anything?
} else {
compileOptions.shared = options.shared || 'svelte/shared.js';
compileOptions.name = capitalize(sanitize(options.filename));
}
if (!('shared' in options)) {
const shared = (major_version >= 3 ? 'svelte/internal.js' : 'svelte/shared.js');
options.shared = (options.format === 'es' || options.format === 'esm') &&
requireRelative.resolve(shared, process.cwd());

for (const option in options) {
if (!pluginOptions[option]) compileOptions[option] = options[option];
}
if (!('name' in options)) options.name = capitalize(sanitize(options.filename));
if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning));
if (options.emitCss) options.css = false;
if (options.externalDependencies) options.externalDependencies.forEach(dep => this.addDependency(dep));

// if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning));


deprecatePreprocessOptions(options);
options.preprocess.filename = options.filename;
options.preprocess.filename = compileOptions.filename;

preprocess(source, options.preprocess).then(processed => {
if (processed.dependencies && this.addDependency) {
Expand All @@ -123,16 +142,16 @@ module.exports = function(source, map) {
}
}

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

if (options.hotReload && !isProduction && !isServer) {
const hotOptions = Object.assign({}, options.hotOptions);
const id = JSON.stringify(relative(process.cwd(), options.filename));
const id = JSON.stringify(relative(process.cwd(), compileOptions.filename));
js.code = makeHot(id, js.code, hotOptions);
}

if (options.emitCss && css.code) {
const cssFilepath = options.filename.replace(
const cssFilepath = compileOptions.filename.replace(
/\.[^/.]+$/,
`.svelte.css`
);
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"mocha": "^5.2.0",
"sinon": "^6.1.5",
"sinon-chai": "^3.2.0",
"svelte": "^2.9.5"
"svelte": "^3.0.0-beta.5"
},
"peerDependencies": {
"svelte": ">1.44.0"
Expand Down
18 changes: 6 additions & 12 deletions test/fixtures/css.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<p>Count: {count}</p>
<button on:click='set({ count: count + 1 })'>+1</button>
<button on:click="{() => count += 1}">+1</button>
<style>
button {
width: 50px;
height: 50px;
}
button {
width: 50px;
height: 50px;
}
</style>

<script>
export default {
data () {
return {
count: 0
};
}
};
let count = 0;
</script>
10 changes: 2 additions & 8 deletions test/fixtures/es2015.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<p>{hi}</p>
<script>
import { hello } from './utils';
import { hello } from './utils';

export default {
data() {
return {
hi: hello('friend')
};
}
};
const hi = hello('friend');
</script>
16 changes: 5 additions & 11 deletions test/fixtures/good.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<p>Count: {count}</p>
<button on:click='set({ count: count + 1 })'>+1</button>
<script>
export default {
data () {
return {
count: 0
};
}
};
</script>
let count = 1;
</script>

<p>Count: {count}</p>
<button on:click="{() => count += 1}">+1</button>
12 changes: 3 additions & 9 deletions test/fixtures/parent.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<Nested />

<script>
import Nested from './nested';
import Nested from './nested';
</script>

export default {
components: {
Nested
}
};
</script>
<Nested />
3 changes: 1 addition & 2 deletions test/fixtures/parse-error.html
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<p>Count: {count}</p>{/if}
<button on:click='set({ count: count + 1 })'>+1</button>
<p>Count: {count}</p>{/if}
12 changes: 4 additions & 8 deletions test/fixtures/validation-error.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<p>Count: {count}</p>

<script>
export default {
computed: {
foo: 'BAR'
}
};
</script>
export default {};
</script>

<h1>Hello world</h1>
74 changes: 15 additions & 59 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,7 @@ describe('loader', () => {
expect(err.message).to.eql(d`
ParseError: Unexpected block closing tag (1:23)
1: <p>Count: {count}</p>{/if}
^
2: <button on:click='set({ count: count + 1 })'>+1</button>`);

expect(code).not.to.exist;
expect(map).not.to.exist;
})
);

it(
'should handle wrong export',
testLoader('test/fixtures/export-error.html', function(
err,
code,
map,
context
) {
expect(err).to.exist;

expect(err.message).to.eql(d`
ParseError: Unexpected token (5:7)
3: <script>
4: export {
5: foo: 'BAR'
^
6: };
7: </script>`);
^`);

expect(code).not.to.exist;
expect(map).not.to.exist;
Expand All @@ -113,14 +88,13 @@ describe('loader', () => {
) {
expect(err).to.exist;

expect(err.message).to.eql(d`
ValidationError: Computed properties can be function expressions or arrow function expressions (6:11)
4: export default {
5: computed: {
6: foo: 'BAR'
^
7: }
8: };`);
expect(err.message.trim()).to.eql(d`
ValidationError: A component cannot have a default export (2:1)
1: <script>
2: export default {};
^
3: </script>
4:`);

expect(code).not.to.exist;
expect(map).not.to.exist;
Expand All @@ -138,8 +112,7 @@ describe('loader', () => {
expect(map).to.exist;

// es2015 statements remain
expect(code).to.contain(`import { hello } from './utils';`);
expect(code).to.contain('data() {');
expect(code).to.contain(`import { hello } from "./utils";`);
})
);

Expand All @@ -149,7 +122,7 @@ describe('loader', () => {
expect(err).not.to.exist;

// es2015 statements remain
expect(code).to.contain(`import Nested from './nested';`);
expect(code).to.contain(`import Nested from "./nested";`);

expect(code).to.exist;
expect(map).to.exist;
Expand Down Expand Up @@ -180,33 +153,18 @@ describe('loader', () => {
);
});

describe('shared', () => {
it(
'should configure shared=false (default)',
testLoader(
'test/fixtures/good.html',
function(err, code, map) {
expect(err).not.to.exist;

expect(code).not.to.contain('import {');
expect(code).not.to.contain('svelte/shared.js');
},
{ shared: false },
1
)
);

describe('sveltePath', () => {
it(
'should configure shared=true',
'should configure sveltePath',
testLoader(
'test/fixtures/good.html',
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.contain('import {');
expect(code).to.contain('svelte/shared.js');
expect(code).to.contain('custom-svelte/internal');
},
{ shared: true }
{ sveltePath: 'custom-svelte' }
)
);
});
Expand All @@ -230,9 +188,7 @@ describe('loader', () => {
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.contain(
'.render = function(state, options = {}) {'
);
expect(code).to.contain('create_ssr_component');
},
{ generate: 'ssr' }
)
Expand Down

0 comments on commit f919ccf

Please sign in to comment.