Skip to content

Commit b84f023

Browse files
committed
init git repository
0 parents  commit b84f023

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
npm-debug.log*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Vladislav Orlov <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# parcel-transformer-svelte
2+
3+
A Parcel 2 transformer for Svelte 3.
4+
5+
## Installation
6+
7+
```bash
8+
npm install parcel-transformer-svelte -D
9+
```
10+
11+
After this you should configure in `.parcelrc` the transformation for Svelte files:
12+
13+
```json
14+
{
15+
"extends": ["@parcel/config-default"],
16+
"transforms": {
17+
"*.svelte": ["parcel-transformer-svelte"]
18+
}
19+
}
20+
```
21+
22+
## Configuration
23+
24+
You can change Svelte options though a `.svelterc`, `svelte.config.js` file or `svelte` field
25+
in `package.json`.
26+
27+
For documentation on which options you can use look at the official
28+
[svelte docs](https://github.com/sveltejs/svelte).
29+
30+
```js
31+
// Options used by svelte.compile
32+
compiler: {
33+
...
34+
},
35+
// Preprocessors for svelte.preprocess
36+
preprocessors: {
37+
...
38+
}
39+
```
40+
41+
## License
42+
43+
MIT License

index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const path = require('path');
2+
const { Transformer } = require('@parcel/plugin');
3+
const { default: SourceMap } = require('@parcel/source-map');
4+
const { relativeUrl } = require('@parcel/utils');
5+
const { compile, preprocess } = require('svelte/compiler.js');
6+
7+
Object.defineProperty(exports, '__esModule', { value: true });
8+
9+
function generateName(input) {
10+
let name = path
11+
.basename(input)
12+
.replace(path.extname(input), '')
13+
.replace(/[^a-zA-Z_$0-9]+/g, '_')
14+
.replace(/^_/, '')
15+
.replace(/_$/, '')
16+
.replace(/^(\d)/, '_$1');
17+
18+
name = name[0].toUpperCase() + name.slice(1);
19+
}
20+
21+
exports.default = new Transformer({
22+
async getConfig({ asset, options }) {
23+
const sourceFileName = relativeUrl(options.projectRoot, asset.filePath);
24+
25+
const customOptions =
26+
(await asset.getConfig(['.svelterc', 'svelte.config.js'], {
27+
packageKey: 'svelte'
28+
})) || {};
29+
30+
const compiler = {
31+
css: false,
32+
...customOptions.compiler,
33+
filename: sourceFileName,
34+
name: generateName(sourceFileName),
35+
dev: options.mode !== 'production'
36+
};
37+
const preprocessors = customOptions.preprocessors;
38+
39+
return { compiler, preprocessors };
40+
},
41+
42+
async transform({ asset, config }) {
43+
let code = await asset.getCode();
44+
45+
if (config.preprocessors) {
46+
const preprocessed = await preprocess(
47+
code,
48+
config.preprocessors,
49+
config.compiler
50+
);
51+
code = preprocessed.toString();
52+
}
53+
54+
const { js, css } = compile(code, config.compiler);
55+
56+
// FIXME: source map should be set in different way ?
57+
if (false && options.sourceMaps) {
58+
asset.setMap(SourceMap.fromRawSourceMap(js.map));
59+
}
60+
61+
return [
62+
{
63+
type: 'js',
64+
code: js.code
65+
},
66+
css && {
67+
type: 'css',
68+
code: css.code
69+
}
70+
].filter(Boolean);
71+
}
72+
});

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "parcel-transformer-svelte",
3+
"version": "1.0.0",
4+
"description": "Parcel 2 transformer for Svelte 3",
5+
"main": "index.js",
6+
"author": "Vladislav Orlov <[email protected]>",
7+
"repository": "github:orlov-vo/parcel-transformer-svelte",
8+
"license": "MIT",
9+
"keywords": [
10+
"parcel",
11+
"transformer",
12+
"svelte"
13+
],
14+
"engines": {
15+
"node": ">=8",
16+
"parcel": "^2.0.0-alpha.2"
17+
},
18+
"peerDependencies": {
19+
"@parcel/plugin": "^2.0.0-alpha.2",
20+
"svelte": "^3.0.0"
21+
}
22+
}

0 commit comments

Comments
 (0)