Skip to content

Added CoffeeScript loader support #201

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

Merged
merged 2 commits into from
Feb 11, 2018
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: 4 additions & 0 deletions fixtures/js/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fun = () ->
document.getElementById("app").innerHTML = "<h1>Welcome to Your Coffee App</h1>"

fun()
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,27 @@ const publicApi = {
return this;
},

/**
* Call this if you plan on loading CoffeeScript files.
*
* Encore.enableCoffeeScriptLoader()
*
* Or, configure the coffee-loader options:
*
* Encore.enableCoffeeScriptLoader(function(coffeeScriptOptions) {
* // http://coffeescript.org/#nodejs-usage
* // coffeeScriptOptions.header = true;
* });
*
* @param {function} callback
* @return {exports}
*/
enableCoffeeScriptLoader(callback = () => {}) {
webpackConfig.enableCoffeeScriptLoader(callback);

return this;
},

/**
* Call this to enable forked type checking for TypeScript loader
* https://github.com/TypeStrong/ts-loader/blob/v2.3.0/README.md#faster-builds
Expand Down
12 changes: 12 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class WebpackConfig {
this.usePreact = false;
this.useVueLoader = false;
this.useTypeScriptLoader = false;
this.useCoffeeScriptLoader = false;
this.useForkedTypeScriptTypeChecking = false;
this.useWebpackNotifier = false;

Expand All @@ -79,6 +80,7 @@ class WebpackConfig {
this.babelConfigurationCallback = () => {};
this.vueLoaderOptionsCallback = () => {};
this.tsConfigurationCallback = () => {};
this.coffeeScriptConfigurationCallback = () => {};

// Plugins options
this.cleanWebpackPluginPaths = ['**/*'];
Expand Down Expand Up @@ -393,6 +395,16 @@ class WebpackConfig {
forkedTypeScriptTypesCheckOptionsCallback;
}

enableCoffeeScriptLoader(callback = () => {}) {
this.useCoffeeScriptLoader = true;

if (typeof callback !== 'function') {
throw new Error('Argument 1 to enableCoffeeScriptLoader() must be a callback function.');
}

this.coffeeScriptConfigurationCallback = callback;
}

enableVueLoader(vueLoaderOptionsCallback = () => {}) {
this.useVueLoader = true;

Expand Down
8 changes: 8 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const lessLoaderUtil = require('./loaders/less');
const stylusLoaderUtil = require('./loaders/stylus');
const babelLoaderUtil = require('./loaders/babel');
const tsLoaderUtil = require('./loaders/typescript');
const coffeeScriptLoaderUtil = require('./loaders/coffee-script');
const vueLoaderUtil = require('./loaders/vue');
// plugins utils
const extractTextPluginUtil = require('./plugins/extract-text');
Expand Down Expand Up @@ -209,6 +210,13 @@ class ConfigGenerator {
});
}

if (this.webpackConfig.useCoffeeScriptLoader) {
rules.push({
test: /\.coffee$/,
use: coffeeScriptLoaderUtil.getLoaders(this.webpackConfig)
});
}

this.webpackConfig.loaders.forEach((loader) => {
rules.push(loader);
});
Expand Down
5 changes: 5 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const features = {
packages: ['typescript', 'ts-loader'],
description: 'process TypeScript files'
},
coffeescript: {
method: 'enableCoffeeScriptLoader()',
packages: ['coffeescript', 'coffee-loader'],
description: 'process CoffeeScript files'
},
forkedtypecheck: {
method: 'enableForkedTypeScriptTypesChecking()',
packages: ['typescript', 'ts-loader', 'fork-ts-checker-webpack-plugin'],
Expand Down
42 changes: 42 additions & 0 deletions lib/loaders/coffee-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const loaderFeatures = require('../features');

/**
* @param {WebpackConfig} webpackConfig
* @return {Array} of loaders to use for coffeescript files
*/
module.exports = {
getLoaders(webpackConfig) {
loaderFeatures.ensurePackagesExist('coffeescript');

const options = {
sourceMap: webpackConfig.useSourceMaps,
transpile: {
presets: ['env']
}
};

// allow options to be configured
webpackConfig.coffeeScriptConfigurationCallback.apply(
options,
[options]
);

return [
{
loader: 'coffee-loader',
options: options,
},
];
}
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"babel-preset-react": "^6.23.0",
"chai": "^3.5.0",
"chai-fs": "^1.0.0",
"coffee-loader": "^0.9.0",
"coffeescript": "^2.0.2",
"eslint": "^3.19.0",
"eslint-plugin-header": "^1.0.0",
"eslint-plugin-node": "^4.2.2",
Expand Down
25 changes: 25 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,31 @@ describe('WebpackConfig object', () => {
});
});

describe('enableCoffeeScriptLoader', () => {
it('Calling method sets it', () => {
const config = createConfig();
config.enableCoffeeScriptLoader();

expect(config.useCoffeeScriptLoader).to.be.true;
});

it('Calling with callback', () => {
const config = createConfig();
const callback = () => {};
config.enableCoffeeScriptLoader(callback);

expect(config.coffeeScriptConfigurationCallback).to.equal(callback);
});

it('Calling with non-callback throws an error', () => {
const config = createConfig();

expect(() => {
config.enableCoffeeScriptLoader('FOO');
}).to.throw('must be a callback function');
});
});

describe('enableVueLoader', () => {
it('Call with no config', () => {
const config = createConfig();
Expand Down
24 changes: 24 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,30 @@ describe('The config-generator function', () => {
});
});

describe('enableCoffeeScriptLoader() adds the coffee-loader', () => {
it('without enableCoffeeScriptLoader()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');
const actualConfig = configGenerator(config);

expect(JSON.stringify(actualConfig.module.rules)).to.not.contain('coffee-loader');
});

it('enableCoffeeScriptLoader()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addEntry('main', './main');
config.enableCoffeeScriptLoader();

const actualConfig = configGenerator(config);

expect(JSON.stringify(actualConfig.module.rules)).to.contain('coffee-loader');
});
});

describe('addLoader() adds a custom loader', () => {
it('addLoader()', () => {
const config = createConfig();
Expand Down
31 changes: 31 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,37 @@ module.exports = {
}).to.throw('wrong `tsconfig` path in fork plugin configuration (should be a relative or absolute path)');
});

it('When configured, CoffeeScript is compiled', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addEntry('main', ['./js/index.coffee']);
const testCallback = () => {};
config.enableCoffeeScriptLoader(testCallback);

testSetup.runWebpack(config, (webpackAssert) => {
webpackAssert.assertOutputFileContains(
'main.js',
'return document.getElementById("app").innerHTML = "<h1>Welcome to Your Coffee App</h1>"'
);

expect(config.outputPath).to.be.a.directory().with.deep.files([
'main.js',
'manifest.json'
]);

testSetup.requestTestPage(
path.join(config.getContext(), 'www'),
[
'build/main.js'
],
(browser) => {
browser.assert.text('#app h1', 'Welcome to Your Coffee App');
done();
}
);
});
});

it('The output directory is cleaned between builds', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ describe('Public API', () => {

});

describe('enableCoffeeScriptLoader', () => {

it('must return the API object', () => {
const returnedValue = api.enableCoffeeScriptLoader();
expect(returnedValue).to.equal(api);
});

});

describe('enableForkedTypeScriptTypesChecking', () => {

it('must return the API object', () => {
Expand Down
39 changes: 39 additions & 0 deletions test/loaders/coffee-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const coffeeScriptLoader = require('../../lib/loaders/coffee-script');

function createConfig() {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.context = __dirname;
runtimeConfig.babelRcFileExists = false;

return new WebpackConfig(runtimeConfig);
}

describe('loaders/coffee-script', () => {
it('getLoaders() with callback', () => {
const config = createConfig();
config.enableSourceMaps(true);
config.enableCoffeeScriptLoader(function(options) {
options.header = true;
});

const loaders = coffeeScriptLoader.getLoaders(config);
expect(loaders[0].options).to.deep.include({
sourceMap: true,
header: true,
});
});
});
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,16 @@ code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"

coffee-loader@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/coffee-loader/-/coffee-loader-0.9.0.tgz#6deabd336062ddc6d773da4dfd16367fc7107bd6"
dependencies:
loader-utils "^1.0.2"

coffeescript@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/coffeescript/-/coffeescript-2.0.2.tgz#065702a484b46194b8c7eb08481e6d4dc38b87c9"

color-convert@^1.3.0, color-convert@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.0.tgz#1accf97dd739b983bf994d56fec8f95853641b7a"
Expand Down