Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 fixes #78

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { basename, extname, relative } = require('path');
const { getOptions } = require('loader-utils');
const VirtualModules = require('./lib/virtual');
const requireRelative = require('require-relative');

const hotApi = require.resolve('./lib/hot-api.js');

@@ -11,6 +10,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 = `
@@ -62,7 +75,7 @@ function normalize(compiled) {
? compiled.css
: { code: compiled.css, map: compiled.cssMap };

return { js, css, ast: compiled.ast };
return { js, css, ast: compiled.ast, warnings: compiled.warnings || compiled.stats.warnings || [] };
}

const warned = {};
@@ -99,22 +112,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')
};

const handleWarning = warning => this.emitWarning(new Error(warning));

if (major_version >= 3) {
// TODO anything?
} else {
compileOptions.shared = options.shared || 'svelte/shared.js';
compileOptions.name = capitalize(sanitize(options.filename));
compileOptions.onwarn = options.onwarn || handleWarning;
}
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));

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

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

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

if (major_version >= 3) {
warnings.forEach(
options.onwarn
? warning => options.onwarn(warning, handleWarning)
: handleWarning
);
}

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`
);
13 changes: 4 additions & 9 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@
],
"dependencies": {
"loader-utils": "^1.1.0",
"require-relative": "^0.8.7",
"svelte-dev-helper": "^1.1.9"
},
"devDependencies": {
@@ -26,7 +25,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"
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
@@ -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;
@@ -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;
@@ -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";`);
})
);

@@ -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;
@@ -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' }
)
);
});
@@ -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' }
)