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

CoffeeScript support for vue-cli and parcel templates #1467

Merged
merged 14 commits into from
Jan 20, 2019
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,15 @@
"code"
]
},
{
"login": "shreeve",
"name": "Steve Shreeve",
"avatar_url": "https://avatars2.githubusercontent.com/u/142875?s=460&v=4",
"profile": "https://github.com/shreeve",
"contributions": [
"code"
]
},
],
"repoType": "github"
}
25 changes: 24 additions & 1 deletion packages/app/src/sandbox/eval/presets/parcel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ import vueStyleLoader from '../../transpilers/vue/style-loader';
import cssLoader from '../../transpilers/vue/css-loader';
import htmlTranspiler from './transpilers/html-transpiler';
import pugTranspiler from '../../transpilers/pug';
import coffeeTranspiler from '../../transpilers/coffee';
import noopTranspiler from '../../transpilers/noop';

import Preset from '../';

export default function initialize() {
const parcelPreset = new Preset(
'parcel',
['js', 'jsx', 'ts', 'tsx', 'json', 'less', 'scss', 'sass', 'styl', 'css'],
[
'js',
'jsx',
'ts',
'tsx',
'json',
'less',
'scss',
'sass',
'styl',
'css',
'vue',
],
{},
{
htmlDisabled: true,
Expand All @@ -34,6 +48,11 @@ export default function initialize() {
}
);

parcelPreset.registerTranspiler(module => /\.coffee$/.test(module.path), [
{ transpiler: coffeeTranspiler },
{ transpiler: babelTranspiler },
]);

parcelPreset.registerTranspiler(module => /\.jsx?$/.test(module.path), [
{
transpiler: babelTranspiler,
Expand Down Expand Up @@ -121,5 +140,9 @@ export default function initialize() {

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

parcelPreset.registerTranspiler(() => false, [
{ transpiler: noopTranspiler },
]);

return parcelPreset;
}
7 changes: 6 additions & 1 deletion packages/app/src/sandbox/eval/presets/vue-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import vueStyleLoader from '../../transpilers/vue/style-loader';
import cssLoader from '../../transpilers/vue/css-loader';
import base64Transpiler from '../../transpilers/base64';
import pugTranspiler from '../../transpilers/pug';
import coffeeTranspiler from '../../transpilers/coffee';

import Preset from '../';

Expand Down Expand Up @@ -127,6 +128,10 @@ export default function initialize() {
vuePreset.registerTranspiler(module => /\.vue$/.test(module.path), [
{ transpiler: vueTranspiler },
]);
vuePreset.registerTranspiler(module => /\.coffee$/.test(module.path), [
{ transpiler: coffeeTranspiler },
{ transpiler: babelTranspiler },
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally added this coffee transpiler to the end of the file, but realized that it was not being executed since the level or declaration determines its priority and it was lower than the raw-loader.


registerStyleTranspilers();

Expand All @@ -148,7 +153,7 @@ export default function initialize() {
{ transpiler: noopTranspiler },
]);
vuePreset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]);
vuePreset.registerTranspiler(m => m.path.endsWith('pug'), [
vuePreset.registerTranspiler(module => /\.pug$/.test(module.path), [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the pug section also, just so all sections in this file use the same syntax.

{ transpiler: pugTranspiler },
]);

Expand Down
30 changes: 30 additions & 0 deletions packages/app/src/sandbox/eval/transpilers/coffee/coffee-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { buildWorkerError } from '../utils/worker-error-handler';

self.importScripts(
`${process.env.CODESANDBOX_HOST || ''}/static/js/coffeescript.2.3.2.js`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be better to pull this from a CDN instead of including this always?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think it doesn't really matter where we get it from. CDN works too, might be a bit faster even, but not noticeably I think.

);
self.postMessage('ready');

self.addEventListener('message', event => {
const { code, path } = event.data;

try {
const compiled = CoffeeScript.compile(code, {
filename: path,
sourceFiles: [path],
bare: true,
literate: false,
inlineMap: true,
sourceMap: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the transpile: ... entry here.

});
return self.postMessage({
type: 'result',
transpiledCode: compiled,
});
} catch (err) {
return self.postMessage({
type: 'error',
error: buildWorkerError(err),
});
}
});
45 changes: 45 additions & 0 deletions packages/app/src/sandbox/eval/transpilers/coffee/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @flow
import CoffeeWorker from 'worker-loader?publicPath=/&name=coffee-transpiler.[hash:8].worker.js!./coffee-worker.js';

import WorkerTranspiler from '../worker-transpiler';
import { type LoaderContext } from '../../transpiled-module';

class CoffeeTranspiler extends WorkerTranspiler {
worker: Worker;

constructor() {
super('coffee-loader', CoffeeWorker, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this 1 mean the number of concurrent workers? Should we change it or keep it at 1?

Copy link
Member

Choose a reason for hiding this comment

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

Yep! I think 1 is enough for the amount of coffee files in a project (since node_modules doesn't have coffee files it should be a low amount)


this.cacheable = false;
}

doTranspilation(code: string, loaderContext: LoaderContext) {
return new Promise((resolve, reject) => {
const path = loaderContext.path;

this.queueTask(
{
code,
path,
},
loaderContext._module.getId(),
loaderContext,
(err, data) => {
if (err) {
loaderContext.emitError(err);

return reject(err);
}

return resolve(data);
}
);
});
}
}

const transpiler = new CoffeeTranspiler();

export { CoffeeTranspiler };

export default transpiler;
1 change: 1 addition & 0 deletions packages/app/src/sandbox/eval/transpilers/vue/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default function(content: string, loaderContext: LoaderContext) {
ts: ['ts-loader'],
typescript: ['ts-loader'],
pug: ['pug-loader'],
coffee: ['babel-loader', 'coffee-loader'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why is the 'babel-loader' needed to make this work?

Copy link
Member

Choose a reason for hiding this comment

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

babel-loader also checks all the require statements and adds them to the dependency graph. So this is required to make import statements work properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, wow. I'm glad I asked! :-)

};

const loaders = Object.assign({}, defaultLoaders, codeSandboxLoaders);
Expand Down
8 changes: 8 additions & 0 deletions packages/app/static/js/coffeescript.2.3.2.js

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/homepage/src/screens/home/Frameworks/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import babelSvg from './babel.png';
import cssSvg from './css.png';
import htmlSvg from './html.png';
import pugSvg from './pug.png';
import coffeescriptSvg from './coffeescript.png';
import imageSvg from './image.png';
import lessSvg from './less.png';
import scssSvg from './scss.png';
Expand Down Expand Up @@ -41,5 +42,6 @@ export const stylus = { svg: stylusSvg, title: 'stylus', extension: 'styl' };
export const image = { svg: imageSvg, title: 'image', extension: 'png' };
export const html = { svg: htmlSvg, title: 'html', extension: 'html' };
export const pug = { svg: pugSvg, title: 'pug', extension: 'pug' };
export const coffee = { svg: coffeescriptSvg, title: 'coffeescript', extension: 'coffee' };
export const cssGlobal = { svg: cssSvg, title: 'css', extension: 'css' };
export const vue = { svg: vueSvg, title: 'vue', extension: 'vue' };
2 changes: 2 additions & 0 deletions packages/homepage/src/screens/home/Frameworks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import FileType, {
image,
html,
pug,
coffee,
cssGlobal,
vue,
} from './icons';
Expand Down Expand Up @@ -230,6 +231,7 @@ const TEMPLATE_SUPPORT = {
less,
stylus,
pug,
coffee,
cssGlobal,
image,
],
Expand Down