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

Add emitCss option #28

Merged
merged 2 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,91 @@ Configure inside your `webpack.config.js`:

Checkout [example setup](./example).

### Extracting CSS

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.

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.

```javascript
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
}),
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
...
]
...
```

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`.

### Source maps

JavaScript source maps are enabled by default, you just have to use an appropriate [webpack devtool](https://webpack.js.org/configuration/devtool/).

To enable CSS source maps, you'll need to use `emitCss` and pass the `sourceMap` option to the `css-loader`. The above config should look like this:

```javascript
module.exports = {
...
devtool: "source-map", // any "source-map"-like devtool is possible
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{ loader: 'css-loader', options: { sourceMap: true } }],
}),
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
...
]
...
};
```

This should create an additional `styles.css.map` file.

## License

Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { basename, extname } = require('path');
const { compile } = require('svelte');
const { getOptions } = require('loader-utils');
const { appendFileSync } = require('fs');
const { fileSync } = require('tmp');

function sanitize(input) {
return basename(input)
Expand All @@ -25,10 +27,19 @@ module.exports = function(source, map) {
options.shared =
options.format === 'es' && require.resolve('svelte/shared.js');

if (options.emitCss) options.css = false;

if (!options.name) options.name = capitalize(sanitize(options.filename));

try {
let { code, map } = compile(source, options);
let { code, map, css, cssMap } = compile(source, options);

if (options.emitCss && css) {
const tmpobj = fileSync({ postfix: '.css' });
css += '\n/*# sourceMappingURL=' + cssMap.toUrl() + '*/';
appendFileSync(tmpobj.name, css);
code = code + `\nrequire('${tmpobj.name}');\n`;
}

this.callback(null, code, map);
} catch (err) {
Expand Down
31 changes: 22 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: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"webpack-loader"
],
"dependencies": {
"loader-utils": "^1.1.0"
"loader-utils": "^1.1.0",
"tmp": "0.0.31"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
28 changes: 28 additions & 0 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ describe('loader', () => {
)
);
});

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

expect(code).not.to.match(/require\('.+\.css'\);/);
},
{}
)
);

it(
'should configure emitCss=true',
testLoader(
'test/fixtures/css.html',
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.match(/require\('.+\.css'\);/);
},
{ emitCss: true }
)
);
});
});
});

Expand Down