Skip to content

Commit 5cf8a55

Browse files
authored
Merge pull request #78 from sveltejs/v3
v3 fixes
2 parents ade07c2 + a25ccf0 commit 5cf8a55

10 files changed

+84
-137
lines changed

index.js

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { basename, extname, relative } = require('path');
22
const { getOptions } = require('loader-utils');
33
const VirtualModules = require('./lib/virtual');
4-
const requireRelative = require('require-relative');
54

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

@@ -11,6 +10,20 @@ const { compile, preprocess } = major_version >= 3
1110
? require('svelte/compiler')
1211
: require('svelte');
1312

13+
const pluginOptions = {
14+
externalDependencies: true,
15+
hotReload: true,
16+
hotOptions: true,
17+
preprocess: true,
18+
emitCss: true,
19+
20+
// legacy
21+
shared: true,
22+
style: true,
23+
script: true,
24+
markup: true
25+
};
26+
1427
function makeHot(id, code, hotOptions) {
1528
const options = JSON.stringify(hotOptions);
1629
const replacement = `
@@ -62,7 +75,7 @@ function normalize(compiled) {
6275
? compiled.css
6376
: { code: compiled.css, map: compiled.cssMap };
6477

65-
return { js, css, ast: compiled.ast };
78+
return { js, css, ast: compiled.ast, warnings: compiled.warnings || compiled.stats.warnings || [] };
6679
}
6780

6881
const warned = {};
@@ -99,22 +112,27 @@ module.exports = function(source, map) {
99112
const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr');
100113
const isProduction = this.minimize || process.env.NODE_ENV === 'production';
101114

102-
options.filename = this.resourcePath;
103-
if (!('format' in options)) {
104-
options.format = major_version >= 3 ? 'esm' : 'es';
115+
const compileOptions = {
116+
filename: this.resourcePath,
117+
format: options.format || (major_version >= 3 ? 'esm' : 'es')
118+
};
119+
120+
const handleWarning = warning => this.emitWarning(new Error(warning));
121+
122+
if (major_version >= 3) {
123+
// TODO anything?
124+
} else {
125+
compileOptions.shared = options.shared || 'svelte/shared.js';
126+
compileOptions.name = capitalize(sanitize(options.filename));
127+
compileOptions.onwarn = options.onwarn || handleWarning;
105128
}
106-
if (!('shared' in options)) {
107-
const shared = (major_version >= 3 ? 'svelte/internal.js' : 'svelte/shared.js');
108-
options.shared = (options.format === 'es' || options.format === 'esm') &&
109-
requireRelative.resolve(shared, process.cwd());
129+
130+
for (const option in options) {
131+
if (!pluginOptions[option]) compileOptions[option] = options[option];
110132
}
111-
if (!('name' in options)) options.name = capitalize(sanitize(options.filename));
112-
if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning));
113-
if (options.emitCss) options.css = false;
114-
if (options.externalDependencies) options.externalDependencies.forEach(dep => this.addDependency(dep));
115133

116134
deprecatePreprocessOptions(options);
117-
options.preprocess.filename = options.filename;
135+
options.preprocess.filename = compileOptions.filename;
118136

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

126-
let { js, css } = normalize(compile(processed.toString(), options));
144+
let { js, css, warnings } = normalize(compile(processed.toString(), compileOptions));
145+
146+
if (major_version >= 3) {
147+
warnings.forEach(
148+
options.onwarn
149+
? warning => options.onwarn(warning, handleWarning)
150+
: handleWarning
151+
);
152+
}
127153

128154
if (options.hotReload && !isProduction && !isServer) {
129155
const hotOptions = Object.assign({}, options.hotOptions);
130-
const id = JSON.stringify(relative(process.cwd(), options.filename));
156+
const id = JSON.stringify(relative(process.cwd(), compileOptions.filename));
131157
js.code = makeHot(id, js.code, hotOptions);
132158
}
133159

134160
if (options.emitCss && css.code) {
135-
const cssFilepath = options.filename.replace(
161+
const cssFilepath = compileOptions.filename.replace(
136162
/\.[^/.]+$/,
137163
`.svelte.css`
138164
);

package-lock.json

+4-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
],
1717
"dependencies": {
1818
"loader-utils": "^1.1.0",
19-
"require-relative": "^0.8.7",
2019
"svelte-dev-helper": "^1.1.9"
2120
},
2221
"devDependencies": {
@@ -26,7 +25,7 @@
2625
"mocha": "^5.2.0",
2726
"sinon": "^6.1.5",
2827
"sinon-chai": "^3.2.0",
29-
"svelte": "^2.9.5"
28+
"svelte": "^3.0.0-beta.5"
3029
},
3130
"peerDependencies": {
3231
"svelte": ">1.44.0"

test/fixtures/css.html

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
<p>Count: {count}</p>
2-
<button on:click='set({ count: count + 1 })'>+1</button>
2+
<button on:click="{() => count += 1}">+1</button>
33
44
<style>
5-
button {
6-
width: 50px;
7-
height: 50px;
8-
}
5+
button {
6+
width: 50px;
7+
height: 50px;
8+
}
99
</style>
1010

1111
<script>
12-
export default {
13-
data () {
14-
return {
15-
count: 0
16-
};
17-
}
18-
};
12+
let count = 0;
1913
</script>

test/fixtures/es2015.html

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<p>{hi}</p>
22
33
<script>
4-
import { hello } from './utils';
4+
import { hello } from './utils';
55

6-
export default {
7-
data() {
8-
return {
9-
hi: hello('friend')
10-
};
11-
}
12-
};
6+
const hi = hello('friend');
137
</script>

test/fixtures/good.html

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
<p>Count: {count}</p>
2-
<button on:click='set({ count: count + 1 })'>+1</button>
3-
41
<script>
5-
export default {
6-
data () {
7-
return {
8-
count: 0
9-
};
10-
}
11-
};
12-
</script>
2+
let count = 1;
3+
</script>
4+
5+
<p>Count: {count}</p>
6+
<button on:click="{() => count += 1}">+1</button>

test/fixtures/parent.html

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
<Nested />
2-
31
<script>
4-
import Nested from './nested';
2+
import Nested from './nested';
3+
</script>
54

6-
export default {
7-
components: {
8-
Nested
9-
}
10-
};
11-
</script>
5+
<Nested />

test/fixtures/parse-error.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
<p>Count: {count}</p>{/if}
2-
<button on:click='set({ count: count + 1 })'>+1</button>
1+
<p>Count: {count}</p>{/if}

test/fixtures/validation-error.html

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
<p>Count: {count}</p>
2-
31
<script>
4-
export default {
5-
computed: {
6-
foo: 'BAR'
7-
}
8-
};
9-
</script>
2+
export default {};
3+
</script>
4+
5+
<h1>Hello world</h1>

test/loader.spec.js

+15-59
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,7 @@ describe('loader', () => {
7171
expect(err.message).to.eql(d`
7272
ParseError: Unexpected block closing tag (1:23)
7373
1: <p>Count: {count}</p>{/if}
74-
^
75-
2: <button on:click='set({ count: count + 1 })'>+1</button>`);
76-
77-
expect(code).not.to.exist;
78-
expect(map).not.to.exist;
79-
})
80-
);
81-
82-
it(
83-
'should handle wrong export',
84-
testLoader('test/fixtures/export-error.html', function(
85-
err,
86-
code,
87-
map,
88-
context
89-
) {
90-
expect(err).to.exist;
91-
92-
expect(err.message).to.eql(d`
93-
ParseError: Unexpected token (5:7)
94-
3: <script>
95-
4: export {
96-
5: foo: 'BAR'
97-
^
98-
6: };
99-
7: </script>`);
74+
^`);
10075

10176
expect(code).not.to.exist;
10277
expect(map).not.to.exist;
@@ -113,14 +88,13 @@ describe('loader', () => {
11388
) {
11489
expect(err).to.exist;
11590

116-
expect(err.message).to.eql(d`
117-
ValidationError: Computed properties can be function expressions or arrow function expressions (6:11)
118-
4: export default {
119-
5: computed: {
120-
6: foo: 'BAR'
121-
^
122-
7: }
123-
8: };`);
91+
expect(err.message.trim()).to.eql(d`
92+
ValidationError: A component cannot have a default export (2:1)
93+
1: <script>
94+
2: export default {};
95+
^
96+
3: </script>
97+
4:`);
12498

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

140114
// es2015 statements remain
141-
expect(code).to.contain(`import { hello } from './utils';`);
142-
expect(code).to.contain('data() {');
115+
expect(code).to.contain(`import { hello } from "./utils";`);
143116
})
144117
);
145118

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

151124
// es2015 statements remain
152-
expect(code).to.contain(`import Nested from './nested';`);
125+
expect(code).to.contain(`import Nested from "./nested";`);
153126

154127
expect(code).to.exist;
155128
expect(map).to.exist;
@@ -180,33 +153,18 @@ describe('loader', () => {
180153
);
181154
});
182155

183-
describe('shared', () => {
184-
it(
185-
'should configure shared=false (default)',
186-
testLoader(
187-
'test/fixtures/good.html',
188-
function(err, code, map) {
189-
expect(err).not.to.exist;
190-
191-
expect(code).not.to.contain('import {');
192-
expect(code).not.to.contain('svelte/shared.js');
193-
},
194-
{ shared: false },
195-
1
196-
)
197-
);
198-
156+
describe('sveltePath', () => {
199157
it(
200-
'should configure shared=true',
158+
'should configure sveltePath',
201159
testLoader(
202160
'test/fixtures/good.html',
203161
function(err, code, map) {
204162
expect(err).not.to.exist;
205163

206164
expect(code).to.contain('import {');
207-
expect(code).to.contain('svelte/shared.js');
165+
expect(code).to.contain('custom-svelte/internal');
208166
},
209-
{ shared: true }
167+
{ sveltePath: 'custom-svelte' }
210168
)
211169
);
212170
});
@@ -230,9 +188,7 @@ describe('loader', () => {
230188
function(err, code, map) {
231189
expect(err).not.to.exist;
232190

233-
expect(code).to.contain(
234-
'.render = function(state, options = {}) {'
235-
);
191+
expect(code).to.contain('create_ssr_component');
236192
},
237193
{ generate: 'ssr' }
238194
)

0 commit comments

Comments
 (0)