Created
March 4, 2019 14:55
-
-
Save phryneas/f9a1e3bd42e900d5eb3dc7c7a3446532 to your computer and use it in GitHub Desktop.
Docz-Mdx-Check-Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { resolve } from 'path'; | |
export default { | |
modifyBundlerConfig: config => { | |
const orig = config.module.rules[2]; | |
const origUse = orig.use; | |
orig.use = [ | |
origUse[0], | |
{ | |
loader: resolve(__dirname, 'typeCheckLoader.js'), | |
options: { tsconfig: './tsconfig.mdx.json', basePath: __dirname } | |
}, | |
origUse[1] | |
]; | |
return config; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var __assign = (this && this.__assign) || function () { | |
__assign = Object.assign || function(t) { | |
for (var s, i = 1, n = arguments.length; i < n; i++) { | |
s = arguments[i]; | |
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | |
t[p] = s[p]; | |
} | |
return t; | |
}; | |
return __assign.apply(this, arguments); | |
}; | |
exports.__esModule = true; | |
var loaderUtils = require("loader-utils"); | |
var ts = require("typescript"); | |
function createCompilerHost(options, fakeFileName, fakeContents) { | |
return __assign({}, ts.createCompilerHost(options), { getSourceFile: function (fileName, languageVersion, _onError) { | |
var sourceText = fileName === fakeFileName ? fakeContents : ts.sys.readFile(fileName); | |
return sourceText !== undefined ? ts.createSourceFile(fileName, sourceText, languageVersion) : undefined; | |
}, | |
fileExists: function (fileName) { | |
return fileName === fakeFileName || ts.sys.fileExists(fileName); | |
} }); | |
} | |
function cleanUp(source) { | |
return source; | |
} | |
module.exports = function loader(input) { | |
var options = loaderUtils.getOptions(this); //{ tsconfig: './tsconfig.mdx.json', basePath: __dirname }; | |
var rawFileName = this.resourcePath; | |
var fakeFileName = rawFileName + '.tsx'; | |
var configFile = ts.readConfigFile(options.tsconfig, ts.sys.readFile); | |
var config = ts.parseJsonConfigFileContent(configFile.config, ts.sys, options.basePath); | |
ts.readJsonConfigFile; | |
//console.log(config); | |
config.options.noEmit = true; | |
var contents = cleanUp(input); | |
var compilerHost = createCompilerHost(config.options, fakeFileName, contents); | |
// Create a program from inputs | |
var program = ts.createProgram([fakeFileName], config.options, compilerHost); | |
var emitResult = program.emit(); | |
var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); | |
var diagnostics = allDiagnostics.filter(function (diagnostic) { | |
var messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
for (var _i = 0, _a = [ | |
/MDXContent/, | |
/'layoutProps' is declared but its value is never read./, | |
/Property 'components' does not exist on type 'Readonly<{ children\?: ReactNode; }> & Readonly<{}>'./, | |
/'props' is declared but its value is never read./ | |
]; _i < _a.length; _i++) { | |
var test_1 = _a[_i]; | |
if (messageText.toString().match(test_1)) { | |
return false; | |
} | |
} | |
return !diagnostic.reportsUnnecessary; | |
}); | |
var message = ''; | |
for (var _i = 0, diagnostics_1 = diagnostics; _i < diagnostics_1.length; _i++) { | |
var diagnostic = diagnostics_1[_i]; | |
message += '\n\n' + decodeURIComponent(contents) + '\n\n'; | |
var messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
if (diagnostic.file) { | |
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; | |
message += diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + messageText; | |
} | |
else { | |
message += "" + messageText; | |
} | |
} | |
if (diagnostics.length > 0) { | |
throw new Error('type-checking failed.\n\n' + message); | |
} | |
// console.log(decodeURIComponent(contents)); | |
// process.exit(1); | |
return input; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as loaderUtils from 'loader-utils'; | |
import * as ts from 'typescript'; | |
function createCompilerHost(options: ts.CompilerOptions, fakeFileName: string, fakeContents: string): ts.CompilerHost { | |
return { | |
...ts.createCompilerHost(options), | |
getSourceFile(fileName: string, languageVersion: ts.ScriptTarget, _onError?: (message: string) => void) { | |
const sourceText = fileName === fakeFileName ? fakeContents : ts.sys.readFile(fileName); | |
return sourceText !== undefined ? ts.createSourceFile(fileName, sourceText, languageVersion) : undefined; | |
}, | |
fileExists(fileName: string): boolean { | |
return fileName === fakeFileName || ts.sys.fileExists(fileName); | |
} | |
}; | |
} | |
function cleanUp(source: string): string { | |
return source; | |
} | |
module.exports = function loader(this: any, input: string) { | |
const options = loaderUtils.getOptions(this); //{ tsconfig: './tsconfig.mdx.json', basePath: __dirname }; | |
const rawFileName = this.resourcePath; | |
const fakeFileName = rawFileName + '.tsx'; | |
const configFile = ts.readConfigFile(options.tsconfig, ts.sys.readFile); | |
const config = ts.parseJsonConfigFileContent(configFile.config, ts.sys, options.basePath); | |
ts.readJsonConfigFile; | |
//console.log(config); | |
config.options.noEmit = true; | |
const contents = cleanUp(input); | |
const compilerHost = createCompilerHost(config.options, fakeFileName, contents); | |
// Create a program from inputs | |
var program = ts.createProgram([fakeFileName], config.options, compilerHost); | |
let emitResult = program.emit(); | |
let allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); | |
const diagnostics = allDiagnostics.filter(diagnostic => { | |
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
for (const test of [ | |
/MDXContent/, | |
/'layoutProps' is declared but its value is never read./, | |
/Property 'components' does not exist on type 'Readonly<{ children\?: ReactNode; }> & Readonly<{}>'./, | |
/'props' is declared but its value is never read./ | |
]) { | |
if (messageText.toString().match(test)) { | |
return false; | |
} | |
} | |
return !diagnostic.reportsUnnecessary; | |
}); | |
let message = ''; | |
for (const diagnostic of diagnostics) { | |
message += '\n\n' + decodeURIComponent(contents) + '\n\n'; | |
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
if (diagnostic.file) { | |
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!); | |
message += `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${messageText}`; | |
} else { | |
message += `${messageText}`; | |
} | |
} | |
if (diagnostics.length > 0) { | |
throw new Error('type-checking failed.\n\n' + message); | |
} | |
// console.log(decodeURIComponent(contents)); | |
// process.exit(1); | |
return input; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment