Skip to content

Commit 0c900f7

Browse files
non25nergmada
andauthored
Remove deprecated code and make README updates (#158)
Co-authored-by: Adam Green <[email protected]>
1 parent 244fd9f commit 0c900f7

File tree

4 files changed

+51
-110
lines changed

4 files changed

+51
-110
lines changed

README.md

+46-46
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ Webpack's [`resolve.mainFields`](https://webpack.js.org/configuration/resolve/#r
6161

6262
If your Svelte components contain `<style>` tags, by default the compiler will add JavaScript that injects those styles into the page when the component is rendered. That's not ideal, because it adds weight to your JavaScript, prevents styles from being fetched in parallel with your code, and can even cause CSP violations.
6363

64-
A better option is to extract the CSS into a separate file. Using the `emitCss` option as shown below would cause a virtual CSS file to be emitted for each Svelte component. The resulting file is then imported by the component, thus following the standard Webpack compilation flow. Add [ExtractTextPlugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) to the mix to output the css to a separate file.
64+
A better option is to extract the CSS into a separate file. Using the `emitCss` option as shown below would cause a virtual CSS file to be emitted for each Svelte component. The resulting file is then imported by the component, thus following the standard Webpack compilation flow. Add [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to the mix to output the css to a separate file.
6565

6666
```javascript
67+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
68+
const mode = process.env.NODE_ENV || 'development';
69+
const prod = mode === 'production';
6770
...
6871
module: {
6972
rules: [
@@ -80,23 +83,49 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
8083
},
8184
{
8285
test: /\.css$/,
83-
use: ExtractTextPlugin.extract({
84-
fallback: 'style-loader',
85-
use: 'css-loader',
86-
}),
86+
use: [
87+
prod ? MiniCssExtractPlugin.loader :'style-loader',
88+
{
89+
loader: 'css-loader',
90+
options: {
91+
url: false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
92+
}
93+
}
94+
]
8795
},
8896
...
8997
]
9098
},
9199
...
92100
plugins: [
93-
new ExtractTextPlugin('styles.css'),
101+
new MiniCssExtractPlugin('styles.css'),
94102
...
95103
]
96104
...
97105
```
98106

99-
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use `css: false`.
107+
Note that the configuration shown above switches off `MiniCssExtractPlugin` in development mode in favour of using CSS javascript injection. This is recommended by `MiniCssExtractPlugin` because it does not support hot reloading.
108+
109+
`prod` indicates, that `NODE_ENV=production` has been set from `package.json` or manually (`NODE_ENV=production npx webpack`) for production builds. We can rely on that to make dynamic adjustments to the config.
110+
111+
Additionally, if you're using multiple entrypoints, you may wish to change `new MiniCssExtractPlugin('styles.css')` for `new MiniCssExtractPlugin('[name].css')` to generate one CSS file per entrypoint.
112+
113+
Warning: in production, if you have set `sideEffects: false` in your `package.json`, `MiniCssExtractPlugin` has a tendency to drop CSS, regardless of whether it's included in your svelte components.
114+
115+
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use
116+
117+
```javascript
118+
...
119+
use: {
120+
loader: 'svelte-loader',
121+
options: {
122+
compilerOptions: {
123+
css: false
124+
}
125+
},
126+
},
127+
...
128+
```
100129

101130
### Source maps
102131

@@ -112,29 +141,24 @@ module.exports = {
112141
module: {
113142
rules: [
114143
...
115-
{
116-
test: /\.(html|svelte)$/,
117-
exclude: /node_modules/,
118-
use: {
119-
loader: 'svelte-loader',
120-
options: {
121-
emitCss: true,
122-
},
123-
},
124-
},
125144
{
126145
test: /\.css$/,
127-
use: ExtractTextPlugin.extract({
128-
fallback: 'style-loader',
129-
use: [{ loader: 'css-loader', options: { sourceMap: true } }],
130-
}),
146+
use: [
147+
prod ? MiniCssExtractPlugin.loader :'style-loader',
148+
{
149+
loader: 'css-loader',
150+
options: {
151+
sourceMap: true
152+
}
153+
}
154+
]
131155
},
132156
...
133157
]
134158
},
135159
...
136160
plugins: [
137-
new ExtractTextPlugin('styles.css'),
161+
new MiniCssExtractPlugin('styles.css'),
138162
...
139163
]
140164
...
@@ -231,30 +255,6 @@ module.exports = {
231255
}
232256
```
233257

234-
#### External Dependencies
235-
236-
If you rely on any external dependencies (files required in a preprocessor for example) you might want to watch these files for changes and re-run svelte compile.
237-
238-
Webpack allows [loader dependencies](https://webpack.js.org/contribute/writing-a-loader/#loader-dependencies) to trigger a recompile. svelte-loader exposes this API via `options.externalDependencies`.
239-
For example:
240-
241-
```js
242-
...
243-
const variables = path.resolve('./variables.js');
244-
...
245-
{
246-
test: /\.(html|svelte)$/,
247-
use: [
248-
{
249-
loader: 'svelte-loader',
250-
options: {
251-
externalDependencies: [variables]
252-
}
253-
}
254-
]
255-
}
256-
```
257-
258258
## License
259259

260260
MIT

index.js

+4-34
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,13 @@ function posixify(file) {
77
return file.replace(/[/\\]/g, '/');
88
}
99

10-
function normalize(compiled) {
11-
// svelte.compile signature changed in 1.60 — this avoids
12-
// future deprecation warnings while preserving backwards
13-
// compatibility
14-
const js = compiled.js || { code: compiled.code, map: compiled.map };
15-
16-
const css = compiled.css && typeof compiled.css === 'object'
17-
? compiled.css
18-
: { code: compiled.css, map: compiled.cssMap };
19-
20-
return { js, css, ast: compiled.ast, warnings: compiled.warnings || compiled.stats.warnings || [] };
21-
}
22-
23-
const warned = {};
24-
function deprecatePreprocessOptions(options) {
25-
const preprocessOptions = {};
26-
27-
['markup', 'style', 'script'].forEach(kind => {
28-
if (options[kind]) {
29-
if (!warned[kind]) {
30-
console.warn(`[svelte-loader] DEPRECATION: options.${kind} is now options.preprocess.${kind}`);
31-
warned[kind] = true;
32-
}
33-
preprocessOptions[kind] = options[kind];
34-
}
35-
});
36-
37-
options.preprocess = options.preprocess || preprocessOptions;
38-
}
39-
4010
const virtualModules = new Map();
4111
let index = 0;
4212

4313
module.exports = function(source, map) {
4414
this.cacheable();
4515

46-
const options = Object.assign({}, getOptions(this));
16+
const options = { ...getOptions(this) };
4717
const callback = this.async();
4818

4919
if (options.cssPath) {
@@ -65,7 +35,7 @@ module.exports = function(source, map) {
6535

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

68-
deprecatePreprocessOptions(options);
38+
options.preprocess = options.preprocess || {};
6939
options.preprocess.filename = compileOptions.filename;
7040

7141
preprocess(source, options.preprocess).then(processed => {
@@ -76,7 +46,7 @@ module.exports = function(source, map) {
7646
}
7747

7848
const compiled = compile(processed.toString(), compileOptions);
79-
let { js, css, warnings } = normalize(compiled);
49+
let { js, css, warnings } = compiled;
8050

8151
warnings.forEach(
8252
options.onwarn
@@ -85,7 +55,7 @@ module.exports = function(source, map) {
8555
);
8656

8757
if (options.hotReload && !isProduction && !isServer) {
88-
const hotOptions = Object.assign({}, options.hotOptions);
58+
const hotOptions = { ...options.hotOptions };
8959
const id = JSON.stringify(relative(process.cwd(), compileOptions.filename));
9060
js.code = makeHot(id, js.code, hotOptions, compiled, source, compileOptions);
9161
}

lib/hot-api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ export const applyHmr = makeApplyHmr(args => {
9999
accept,
100100
};
101101

102-
return Object.assign({}, args, { hot });
102+
return { ...args, hot };
103103
});

test/loader.spec.js

-29
Original file line numberDiff line numberDiff line change
@@ -270,35 +270,6 @@ describe('loader', () => {
270270
});
271271
});
272272

273-
describe('deprecations', () => {
274-
it('should warn on options.style', done => {
275-
const { warn } = console;
276-
const warnings = [];
277-
278-
console.warn = msg => {
279-
warnings.push(msg);
280-
};
281-
282-
testLoader(
283-
'test/fixtures/style-valid.html',
284-
(err, code, map) => {
285-
expect(code).to.contain('50px');
286-
expect(warnings).to.deep.equal([
287-
'[svelte-loader] DEPRECATION: options.style is now options.preprocess.style'
288-
]);
289-
console.warn = warn;
290-
},
291-
{
292-
style: ({ content }) => {
293-
return {
294-
code: content.replace(/\$size/gi, '50px')
295-
};
296-
}
297-
}
298-
)(done);
299-
});
300-
});
301-
302273
describe('hotReload', () => {
303274
it(
304275
'should configure hotReload=false (default)',

0 commit comments

Comments
 (0)