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 5 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
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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');
Expand All @@ -20,7 +20,8 @@ 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';
Expand All @@ -31,8 +32,8 @@ module.exports = function(source, map) {

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' });
Expand All @@ -44,10 +45,10 @@ module.exports = function(source, map) {
utimesSync(tmpobj.name, stats.atimeMs - 9999, stats.mtimeMs - 9999);
}

this.callback(null, code, map);
Copy link
Contributor

Choose a reason for hiding this comment

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

Formating: use tabs instead of spaces.

} catch (err) {
// wrap error to provide correct
// context when logging to console
this.callback(new Error(err.toString() + '\n' + err.frame));
}
callback(null, code, map);
}, err => callback(err)).catch(err => {
// wrap error to provide correct
// context when logging to console
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>
119 changes: 100 additions & 19 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ function d([str]) {

describe('loader', () => {
function testLoader(fileName, callback, query, version = 2) {
return () => {
return (done) => {
function cb() {
try {
callback(...[].slice.call(arguments));
} catch (err) {
expect(callbackSpy).to.have.been.called;
return done(err);
}
expect(callbackSpy).to.have.been.called;
done();
}

const fileContents = readFile(fileName);
const cacheableSpy = spy(() => {});
const callbackSpy = spy(callback);

loader.call(
{
cacheable: cacheableSpy,
callback: callbackSpy,
resourcePath: fileName,
version,
query
},
fileContents,
null
);

expect(callbackSpy).to.have.been.called;
const cacheableSpy = spy(function() { });

const callbackSpy = spy(cb);

loader.call({
cacheable: cacheableSpy,
async: () => callbackSpy,
resourcePath: fileName,
version,
query,
}, fileContents, null);

expect(cacheableSpy).to.have.been.called;
};
}
Expand All @@ -57,7 +65,7 @@ describe('loader', () => {
expect(err).to.exist;

expect(err.message).to.eql(d`
ParseError: Expected }}}
ParseError: Expected }}} (1:18)
1: <p>Count: {{{count}}</p>
^
2: <button on:click='set({ count: count + 1 })'>+1</button>`
Expand All @@ -79,7 +87,7 @@ describe('loader', () => {
expect(err).to.exist;

expect(err.message).to.eql(d`
ParseError: Unexpected token
ParseError: Unexpected token (5:7)
3: <script>
4: export {
5: foo: 'BAR'
Expand All @@ -104,7 +112,7 @@ describe('loader', () => {
expect(err).to.exist;

expect(err.message).to.eql(d`
ValidationError: Computed properties can be function expressions or arrow function expressions
ValidationError: Computed properties can be function expressions or arrow function expressions (6:11)
4: export default {
5: computed: {
6: foo: 'BAR'
Expand Down Expand Up @@ -253,6 +261,79 @@ describe('loader', () => {
)
);
});

describe('preprocess', () => {
it('should preprocess successfully', (done) => {
function callback(err, code, map) {
expect(err).not.to.exist;
expect(code).to.exist;
expect(code).to.contain('button{width:50px;height:50px}');
expect(map).to.exist;
}

function cb() {
try {
callback(...[].slice.call(arguments));
} catch (err) {
expect(callbackSpy).to.have.been.called;
return done(err);
}
expect(callbackSpy).to.have.been.called;
done();
}

const fileContents = readFileSync('test/fixtures/style-valid.html',
'utf-8');
const cacheableSpy = spy(() => {
});
const callbackSpy = spy(cb);
const options = {
style: ({ content }) => {
return {
code: content.replace(/\$size/gi, '50px'),
};
},
};

loader.call(
{
cacheable: cacheableSpy,
async: () => callbackSpy,
resourcePath: 'test/fixtures/style-valid.html',
options,
},
fileContents,
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix formating; use tabs instead of spaces.

null
);

expect(cacheableSpy).to.have.been.called;
});

it('should not preprocess successfully', () => {
const fileContents = readFileSync('test/fixtures/style-valid.html', 'utf-8');
const cacheableSpy = spy(() => {
});
const options = {
style: () => {
throw new Error('Error while preprocessing');
},
};

loader.call(
{
cacheable: cacheableSpy,
async: () => (err) => {
expect(err).to.exist;
},
resourcePath: 'test/fixtures/style-valid.html',
options,
},
fileContents,
null
);

});
});
});
});

Expand Down