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

Commit 2d18567

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 2d18567

File tree

9 files changed

+60
-101
lines changed

9 files changed

+60
-101
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: 4 additions & 60 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,
@@ -267,7 +265,6 @@ export default class CompilerHost {
267265

268266
if (hashInfo.isInNodeModules) {
269267
let code = hashInfo.sourceCode || await pfs.readFile(filePath, 'utf8');
270-
code = await CompilerHost.fixNodeModulesSourceMapping(code, filePath, this.fileChangeCache.appRoot);
271268
return { code, mimeType: type };
272269
}
273270

@@ -306,7 +303,9 @@ export default class CompilerHost {
306303
};
307304
}
308305

309-
let ctx = {};
306+
let ctx = {
307+
appRoot: this.appRoot,
308+
};
310309
let code = hashInfo.sourceCode || await pfs.readFile(filePath, 'utf8');
311310

312311
if (!(await compiler.shouldCompileFile(code, ctx))) {
@@ -491,7 +490,6 @@ export default class CompilerHost {
491490

492491
if (hashInfo.isInNodeModules) {
493492
let code = hashInfo.sourceCode || fs.readFileSync(filePath, 'utf8');
494-
code = CompilerHost.fixNodeModulesSourceMappingSync(code, filePath, this.fileChangeCache.appRoot);
495493
return { code, mimeType: type };
496494
}
497495

@@ -602,58 +600,4 @@ export default class CompilerHost {
602600
static shouldPassthrough(hashInfo) {
603601
return hashInfo.isMinified || hashInfo.isInNodeModules || hashInfo.hasSourceMap || hashInfo.isFileBinary;
604602
}
605-
606-
/**
607-
* Look at the code of a node modules and see the sourceMapping path.
608-
* If there is any, check the path and try to fix it with and
609-
* root relative path.
610-
* @private
611-
*/
612-
static async fixNodeModulesSourceMapping(sourceCode, sourcePath, appRoot) {
613-
let regexSourceMapping = /\/\/#.*sourceMappingURL=(?!data:)([^"'].*)/i;
614-
let sourceMappingCheck = sourceCode.match(regexSourceMapping);
615-
616-
if (sourceMappingCheck && sourceMappingCheck[1] && sourceMappingCheck[1] !== ''){
617-
let sourceMapPath = sourceMappingCheck[1];
618-
619-
try {
620-
await pfs.stat(sourceMapPath);
621-
} catch (error) {
622-
let normRoot = path.normalize(appRoot);
623-
let absPathToModule = path.dirname(sourcePath.replace(normRoot, '').substring(1));
624-
let newMapPath = path.join(absPathToModule, sourceMapPath);
625-
626-
return sourceCode.replace(regexSourceMapping, `//# sourceMappingURL=${newMapPath}`);
627-
}
628-
}
629-
630-
return sourceCode;
631-
}
632-
633-
/**
634-
* Look at the code of a node modules and see the sourceMapping path.
635-
* If there is any, check the path and try to fix it with and
636-
* root relative path.
637-
* @private
638-
*/
639-
static fixNodeModulesSourceMappingSync(sourceCode, sourcePath, appRoot) {
640-
let regexSourceMapping = /\/\/#.*sourceMappingURL=(?!data:)([^"'].*)/i;
641-
let sourceMappingCheck = sourceCode.match(regexSourceMapping);
642-
643-
if (sourceMappingCheck && sourceMappingCheck[1] && sourceMappingCheck[1] !== ''){
644-
let sourceMapPath = sourceMappingCheck[1];
645-
646-
try {
647-
fs.statSync(sourceMapPath);
648-
} catch (error) {
649-
let normRoot = path.normalize(appRoot);
650-
let absPathToModule = path.dirname(sourcePath.replace(normRoot, '').substring(1));
651-
let newMapPath = path.join(absPathToModule, sourceMapPath);
652-
653-
return sourceCode.replace(regexSourceMapping, `//# sourceMappingURL=${newMapPath}`);
654-
}
655-
}
656-
657-
return sourceCode;
658-
}
659603
}

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)