Skip to content

Commit 2cb8a47

Browse files
shreeveCompuIves
authored andcommitted
CoffeeScript support for vue-cli and parcel templates (#1467)
* CoffeeScript support for vue-cli template This is based off the work done by @zephraph on #443 and is not quite yet working. * match syntax * Update icons.js * match syntax * invoke babel from the preset, not from the coffeescript compiler * use more consistent syntax * use babel as the last stage of <style lang="coffee"> blocks in .vue files * doesn't seem to work here, I had to add a postLoader for coffee * move coffeescript transpiler higher up the food chain * simpler coffeescript -> babel -> js transpilation * add to contributors file * re-enable the babel transpiler after coffeescript * doesn't seem like this is needed? * add coffeescript support to parcel
1 parent 37dec71 commit 2cb8a47

File tree

11 files changed

+128
-2
lines changed

11 files changed

+128
-2
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,15 @@
699699
"code"
700700
]
701701
},
702+
{
703+
"login": "shreeve",
704+
"name": "Steve Shreeve",
705+
"avatar_url": "https://avatars2.githubusercontent.com/u/142875?s=460&v=4",
706+
"profile": "https://github.com/shreeve",
707+
"contributions": [
708+
"code"
709+
]
710+
},
702711
],
703712
"repoType": "github"
704713
}

packages/app/src/sandbox/eval/presets/parcel/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@ import vueStyleLoader from '../../transpilers/vue/style-loader';
1414
import cssLoader from '../../transpilers/vue/css-loader';
1515
import htmlTranspiler from './transpilers/html-transpiler';
1616
import pugTranspiler from '../../transpilers/pug';
17+
import coffeeTranspiler from '../../transpilers/coffee';
18+
import noopTranspiler from '../../transpilers/noop';
1719

1820
import Preset from '../';
1921

2022
export default function initialize() {
2123
const parcelPreset = new Preset(
2224
'parcel',
23-
['js', 'jsx', 'ts', 'tsx', 'json', 'less', 'scss', 'sass', 'styl', 'css'],
25+
[
26+
'js',
27+
'jsx',
28+
'ts',
29+
'tsx',
30+
'json',
31+
'less',
32+
'scss',
33+
'sass',
34+
'styl',
35+
'css',
36+
'vue',
37+
],
2438
{},
2539
{
2640
htmlDisabled: true,
@@ -34,6 +48,11 @@ export default function initialize() {
3448
}
3549
);
3650

51+
parcelPreset.registerTranspiler(module => /\.coffee$/.test(module.path), [
52+
{ transpiler: coffeeTranspiler },
53+
{ transpiler: babelTranspiler },
54+
]);
55+
3756
parcelPreset.registerTranspiler(module => /\.jsx?$/.test(module.path), [
3857
{
3958
transpiler: babelTranspiler,
@@ -121,5 +140,9 @@ export default function initialize() {
121140

122141
parcelPreset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]);
123142

143+
parcelPreset.registerTranspiler(() => false, [
144+
{ transpiler: noopTranspiler },
145+
]);
146+
124147
return parcelPreset;
125148
}

packages/app/src/sandbox/eval/presets/vue-cli/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import vueStyleLoader from '../../transpilers/vue/style-loader';
2020
import cssLoader from '../../transpilers/vue/css-loader';
2121
import base64Transpiler from '../../transpilers/base64';
2222
import pugTranspiler from '../../transpilers/pug';
23+
import coffeeTranspiler from '../../transpilers/coffee';
2324

2425
import Preset from '../';
2526

@@ -127,6 +128,10 @@ export default function initialize() {
127128
vuePreset.registerTranspiler(module => /\.vue$/.test(module.path), [
128129
{ transpiler: vueTranspiler },
129130
]);
131+
vuePreset.registerTranspiler(module => /\.coffee$/.test(module.path), [
132+
{ transpiler: coffeeTranspiler },
133+
{ transpiler: babelTranspiler },
134+
]);
130135

131136
registerStyleTranspilers();
132137

@@ -148,7 +153,7 @@ export default function initialize() {
148153
{ transpiler: noopTranspiler },
149154
]);
150155
vuePreset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]);
151-
vuePreset.registerTranspiler(m => m.path.endsWith('pug'), [
156+
vuePreset.registerTranspiler(module => /\.pug$/.test(module.path), [
152157
{ transpiler: pugTranspiler },
153158
]);
154159

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { buildWorkerError } from '../utils/worker-error-handler';
2+
3+
self.importScripts(
4+
`${process.env.CODESANDBOX_HOST || ''}/static/js/coffeescript.2.3.2.js`
5+
);
6+
self.postMessage('ready');
7+
8+
self.addEventListener('message', event => {
9+
const { code, path } = event.data;
10+
11+
try {
12+
const compiled = CoffeeScript.compile(code, {
13+
filename: path,
14+
sourceFiles: [path],
15+
bare: true,
16+
literate: false,
17+
inlineMap: true,
18+
sourceMap: false,
19+
});
20+
return self.postMessage({
21+
type: 'result',
22+
transpiledCode: compiled,
23+
});
24+
} catch (err) {
25+
return self.postMessage({
26+
type: 'error',
27+
error: buildWorkerError(err),
28+
});
29+
}
30+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @flow
2+
import CoffeeWorker from 'worker-loader?publicPath=/&name=coffee-transpiler.[hash:8].worker.js!./coffee-worker.js';
3+
4+
import WorkerTranspiler from '../worker-transpiler';
5+
import { type LoaderContext } from '../../transpiled-module';
6+
7+
class CoffeeTranspiler extends WorkerTranspiler {
8+
worker: Worker;
9+
10+
constructor() {
11+
super('coffee-loader', CoffeeWorker, 1);
12+
13+
this.cacheable = false;
14+
}
15+
16+
doTranspilation(code: string, loaderContext: LoaderContext) {
17+
return new Promise((resolve, reject) => {
18+
const path = loaderContext.path;
19+
20+
this.queueTask(
21+
{
22+
code,
23+
path,
24+
},
25+
loaderContext._module.getId(),
26+
loaderContext,
27+
(err, data) => {
28+
if (err) {
29+
loaderContext.emitError(err);
30+
31+
return reject(err);
32+
}
33+
34+
return resolve(data);
35+
}
36+
);
37+
});
38+
}
39+
}
40+
41+
const transpiler = new CoffeeTranspiler();
42+
43+
export { CoffeeTranspiler };
44+
45+
export default transpiler;

packages/app/src/sandbox/eval/transpilers/vue/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export default function(content: string, loaderContext: LoaderContext) {
122122
ts: ['ts-loader'],
123123
typescript: ['ts-loader'],
124124
pug: ['pug-loader'],
125+
coffee: ['babel-loader', 'coffee-loader'],
125126
};
126127

127128
const loaders = Object.assign({}, defaultLoaders, codeSandboxLoaders);

packages/app/static/js/coffeescript.2.3.2.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
927 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Loading

packages/homepage/src/screens/home/Frameworks/icons.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import babelSvg from './babel.png';
77
import cssSvg from './css.png';
88
import htmlSvg from './html.png';
99
import pugSvg from './pug.png';
10+
import coffeescriptSvg from './coffeescript.png';
1011
import imageSvg from './image.png';
1112
import lessSvg from './less.png';
1213
import scssSvg from './scss.png';
@@ -41,5 +42,6 @@ export const stylus = { svg: stylusSvg, title: 'stylus', extension: 'styl' };
4142
export const image = { svg: imageSvg, title: 'image', extension: 'png' };
4243
export const html = { svg: htmlSvg, title: 'html', extension: 'html' };
4344
export const pug = { svg: pugSvg, title: 'pug', extension: 'pug' };
45+
export const coffee = { svg: coffeescriptSvg, title: 'coffeescript', extension: 'coffee' };
4446
export const cssGlobal = { svg: cssSvg, title: 'css', extension: 'css' };
4547
export const vue = { svg: vueSvg, title: 'vue', extension: 'vue' };

0 commit comments

Comments
 (0)