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

Preprocessing #31

Merged
merged 8 commits into from
Dec 17, 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
.idea/
*.iml
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js: '8.0'
node_js:
- '9'
- '8'
- '6'

script:
- npm run all
35 changes: 18 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { basename, extname } = require('path');
const { compile } = require('svelte');
const { compile, preprocess } = require('svelte');
const { getOptions } = require('loader-utils');
const { statSync, utimesSync, writeFileSync } = require('fs');
const { fileSync } = require('tmp');

function sanitize(input) {
return basename(input)
.replace(extname(input), '')
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
return basename(input).
replace(extname(input), '').
replace(/[^a-zA-Z_$0-9]+/g, '_').
replace(/^_/, '').
replace(/_$/, '').
replace(/^(\d)/, '_$1');
}

function capitalize(str) {
Expand All @@ -20,34 +20,35 @@ function capitalize(str) {
module.exports = function(source, map) {
this.cacheable();

const options = getOptions(this) || {};
const options = Object.assign({}, this.options, getOptions(this));
const callback = this.async();

options.filename = this.resourcePath;
options.format = this.version === 1 ? options.format || 'cjs' : 'es';
options.shared =
options.format === 'es' && require.resolve('svelte/shared.js');
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, css, cssMap } = compile(source, options);
preprocess(source, options).then(processed => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use tabs instead of spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, thanks!

let { code, map, css, cssMap } = compile(processed.toString(), options);

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

writeFileSync(tmpobj.name, css);
const stats = statSync(tmpobj.name);
utimesSync(tmpobj.name, stats.atimeMs - 9999, stats.mtimeMs - 9999);
const { atime, mtime } = statSync(tmpobj.name);
utimesSync(tmpobj.name, atime.getTime() - 9999, mtime.getTime() - 9999);
}

this.callback(null, code, map);
} catch (err) {
callback(null, code, map);
}, err => callback(err)).catch(err => {
// wrap error to provide correct
// context when logging to console
this.callback(new Error(err.toString() + '\n' + err.frame));
}
callback(new Error(`${err.name}: ${err.toString()}`));
});
};
26 changes: 13 additions & 13 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"mocha": "^3.2.0",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0",
"svelte": "^1.6.0"
"svelte": "^1.47.2"
},
"peerDependencies": {
"svelte": "^1.0.7"
"svelte": "^1.44.0"
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/style-valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<button>+1</button>
<style type="text/scss">
button {
width: $size;
height: $size;
}
</style>
Loading