Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 877001d

Browse files
committed
Switch to a local mime-type lookup table
- Fixes detection of CJSX - Fixes detection of TSX - Update require-hook to support multiple extensions per mimetype, fixes support for `.es6` JS files and `.litcoffee` files read via require.
1 parent 064bdb1 commit 877001d

File tree

9 files changed

+57
-44
lines changed

9 files changed

+57
-44
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"node": ">= 5.0"
3232
},
3333
"dependencies": {
34-
"@paulcbetts/mime-types": "^2.1.10",
3534
"btoa": "^1.1.2",
3635
"debug-electron": "0.0.1",
3736
"lru-cache": "^4.0.1",

src/compiler-host.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import mimeTypes from '@paulcbetts/mime-types';
21
import fs from 'fs';
32
import zlib from 'zlib';
43
import path from 'path';
54
import {pfs, pzlib} from './promise';
65

6+
import mimeTypes from './mime-types';
77
import {forAllFiles, forAllFilesSync} from './for-all-files';
88
import CompileCache from './compile-cache';
99
import FileChangedCache from './file-change-cache';
1010
import ReadOnlyCompiler from './read-only-compiler';
1111

1212
const d = require('debug-electron')('electron-compile:compiler-host');
1313

14-
require('./rig-mime-types').init();
15-
1614
// This isn't even my
1715
const finalForms = {
1816
'text/javascript': true,

src/mime-types.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import path from 'path';
2+
3+
const MimeTypesToExtensions = {
4+
'application/javascript': ['js', 'es6'],
5+
'text/less': ['less'],
6+
'text/stylus': ['stylus'],
7+
'text/jsx': ['jsx'],
8+
'text/cjsx': ['cjsx'],
9+
'text/coffeescript': ['coffee', 'litcoffee'],
10+
'text/typescript': ['ts'],
11+
'text/tsx': ['tsx'],
12+
'text/cson': ['cson'],
13+
'text/html': ['html', 'htm'],
14+
'text/jade': ['jade'],
15+
'text/plain': ['txt'],
16+
'image/svg+xml': ['svg'],
17+
};
18+
19+
const ExtensionsToMimeTypes = {};
20+
for (const mimetype of Object.keys(MimeTypesToExtensions)) {
21+
for (const ext of MimeTypesToExtensions[mimetype]) {
22+
ExtensionsToMimeTypes[ext] = mimetype;
23+
}
24+
}
25+
26+
class MimeTypes {
27+
lookup(filepath) {
28+
const ext = path.extname(filepath);
29+
return ExtensionsToMimeTypes[ext.slice(1)] || false;
30+
}
31+
32+
extension(mimeType) {
33+
return this.extensions(mimeType)[0];
34+
}
35+
36+
extensions(mimeType) {
37+
return MimeTypesToExtensions[mimeType] || [];
38+
}
39+
40+
}
41+
export default new MimeTypes();

src/protocol-hook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import url from 'url';
22
import fs from 'fs';
3-
import mime from '@paulcbetts/mime-types';
3+
import mime from './mime-types';
44

55
const magicWords = "__magic__file__to__help__electron__compile.js";
66

src/require-hook.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import mimeTypes from '@paulcbetts/mime-types';
1+
import mimeTypes from './mime-types';
22

33
/**
4-
* Initializes the node.js hook that allows us to intercept files loaded by
5-
* node.js and rewrite them. This method along with {@link initializeProtocolHook}
6-
* are the top-level methods that electron-compile actually uses to intercept
4+
* Initializes the node.js hook that allows us to intercept files loaded by
5+
* node.js and rewrite them. This method along with {@link initializeProtocolHook}
6+
* are the top-level methods that electron-compile actually uses to intercept
77
* code that Electron loads.
8-
*
8+
*
99
* @param {CompilerHost} compilerHost The compiler host to use for compilation.
10-
*/
10+
*/
1111
export default function registerRequireExtension(compilerHost) {
1212
Object.keys(compilerHost.compilersByMimeType).forEach((mimeType) => {
13-
let ext = mimeTypes.extension(mimeType);
14-
15-
require.extensions[`.${ext}`] = (module, filename) => {
16-
let {code} = compilerHost.compileSync(filename);
17-
module._compile(code, filename);
18-
};
13+
mimeTypes.extensions(mimeType).forEach((ext) => {
14+
require.extensions[`.${ext}`] = (module, filename) => {
15+
let {code} = compilerHost.compileSync(filename);
16+
module._compile(code, filename);
17+
};
18+
});
1919
});
2020
}

src/rig-mime-types.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/compiler-host.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path';
44
import fs from 'fs';
55
import rimraf from 'rimraf';
66
import mkdirp from 'mkdirp';
7-
import mimeTypes from '@paulcbetts/mime-types';
7+
import mimeTypes from '../mime-types';
88
import FileChangeCache from '../src/file-change-cache';
99
import CompilerHost from '../src/compiler-host';
1010

test/compiler-valid-invalid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import './support';
33
import pify from 'pify';
44
import fs from 'fs';
55
import path from 'path';
6-
import mimeTypes from '@paulcbetts/mime-types';
6+
import mimeTypes from '../mime-types';
77

88
const pfs = pify(fs);
99

test/support.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ global.AssertionError = chai.AssertionError;
1313
global.Assertion = chai.Assertion;
1414
global.assert = chai.assert;
1515

16-
require('../src/rig-mime-types').init();
17-
1816
global.compilersByMimeType = allCompilerClasses.reduce((acc,x) => {
1917
acc = acc || {};
2018

0 commit comments

Comments
 (0)