Skip to content

Commit

Permalink
ts 4 & webpack 5
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Jan 15, 2021
1 parent efcdda2 commit 51b1482
Show file tree
Hide file tree
Showing 7 changed files with 871 additions and 2,497 deletions.
18 changes: 2 additions & 16 deletions dist/index.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions dist/index.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0

THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.

See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */

/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"devDependencies": {
"@types/concat-stream": "^1.6.0",
"@types/glob": "^7.1.1",
"@types/node": "^13.7.7",
"@types/node": "^14.14.21",
"@types/webpack": "^4.41.26",
"@types/yarnpkg__lockfile": "^1.1.3",
"hard-source-webpack-plugin": "^0.13.1",
"ts-loader": "^6.2.2",
"typescript": "^3.8.3",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
"ts-loader": "^8.0.14",
"ts-node": "^9.1.1",
"typescript": "^4.1.3",
"webpack": "^5.14.0",
"webpack-cli": "^4.3.1"
},
"dependencies": {
"@actions/core": "^1.2.3",
Expand Down
111 changes: 54 additions & 57 deletions src/langSvc/createHost.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import * as _ts from 'typescript'
import * as fs from 'fs'
import * as path from 'path'
import { FileEntry } from '../types'
import { libDTS } from '../gen/libDTS'
import * as _ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import { FileEntry } from '../types';
import { libDTS } from '../gen/libDTS';

const libDTSRegexp = /^lib\..*\.d\.ts$/
const libDTSRegexp = /^lib\..*\.d\.ts$/;

export const createHost = (fileNames: string[], compilerOptions: _ts.CompilerOptions, fileEntry: FileEntry, ts: typeof _ts): _ts.LanguageServiceHost => {
const getCurrentVersion = (fileName: string) => fileEntry.has(fileName) ? fileEntry.get(fileName)!.version : 0
const getTextFromSnapshot = (snapshot: _ts.IScriptSnapshot) => snapshot.getText(0, snapshot.getLength())
export const createHost = (
fileNames: string[],
compilerOptions: _ts.CompilerOptions,
fileEntry: FileEntry,
ts: typeof _ts
): _ts.LanguageServiceHost => {
const getCurrentVersion = (fileName: string) => (fileEntry.has(fileName) ? fileEntry.get(fileName)!.version : 0);
const getTextFromSnapshot = (snapshot: _ts.IScriptSnapshot) => snapshot.getText(0, snapshot.getLength());

const readFile = (fileName: string, encoding: string | undefined = 'utf8') => {
const readFile = (fileName: string, encoding: BufferEncoding = 'utf8') => {
if (libDTSRegexp.test(fileName)) {
return libDTS[fileName].content
return libDTS[fileName].content;
}

fileName = path.normalize(fileName);
Expand All @@ -21,87 +26,79 @@ export const createHost = (fileNames: string[], compilerOptions: _ts.CompilerOpt
} catch (e) {
return undefined;
}
}
};

const readFileWithFallback = (
filePath: string,
encoding?: string | undefined
) => {
return ts.sys.readFile(filePath, encoding) || readFile(filePath, encoding)
}
const readFileWithFallback = (filePath: string, encoding?: BufferEncoding) => {
return ts.sys.readFile(filePath, encoding) || readFile(filePath, encoding);
};

const moduleResolutionHost: _ts.ModuleResolutionHost = {
fileExists: fileName => {
return ts.sys.fileExists(fileName) || readFile(fileName) !== undefined
fileExists: (fileName) => {
return ts.sys.fileExists(fileName) || readFile(fileName) !== undefined;
},
readFile: fileName => {
readFile: (fileName) => {
if (fileEntry.has(fileName)) {
const snapshot = fileEntry.get(fileName)!.scriptSnapshot
return getTextFromSnapshot(snapshot)
const snapshot = fileEntry.get(fileName)!.scriptSnapshot;
return getTextFromSnapshot(snapshot);
}
return readFileWithFallback(fileName)
return readFileWithFallback(fileName);
},
realpath: ts.sys.realpath,
directoryExists: ts.sys.directoryExists,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getDirectories: ts.sys.getDirectories
}
getDirectories: ts.sys.getDirectories,
};

const host: _ts.LanguageServiceHost = {
getScriptFileNames: () => fileNames,
getScriptVersion: fileName => getCurrentVersion(fileName) + '',
getScriptSnapshot: fileName => {
getScriptVersion: (fileName) => getCurrentVersion(fileName) + '',
getScriptSnapshot: (fileName) => {
if (fileEntry.has(fileName)) {
return fileEntry.get(fileName)!.scriptSnapshot
return fileEntry.get(fileName)!.scriptSnapshot;
} else {
const isLibDTS = libDTSRegexp.test(fileName)
const isLibDTS = libDTSRegexp.test(fileName);
if (!isLibDTS && !fs.existsSync(fileName)) {
return undefined
return undefined;
}
const content = isLibDTS ? libDTS[fileName].content : fs.readFileSync(fileName).toString()
const content = isLibDTS ? libDTS[fileName].content : fs.readFileSync(fileName).toString();

const scriptSnapshot = ts.ScriptSnapshot.fromString(content)
fileEntry.set(fileName, { version: 0, scriptSnapshot })
return scriptSnapshot
const scriptSnapshot = ts.ScriptSnapshot.fromString(content);
fileEntry.set(fileName, { version: 0, scriptSnapshot });
return scriptSnapshot;
}
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => compilerOptions,
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
resolveModuleNames: (moduleNames, containingFile, _, __, options) => {
const ret: (_ts.ResolvedModule | undefined)[] = moduleNames.map(name => {
if (/\.vue$/.test(name)) {
const resolved: _ts.ResolvedModule = {
resolvedFileName: normalize(path.resolve(path.dirname(containingFile), name))
}
return resolved
}
const ret: (_ts.ResolvedModule | undefined)[] = moduleNames.map((name) => {
if (/\.vue$/.test(name)) {
const resolved: _ts.ResolvedModule = {
resolvedFileName: normalize(path.resolve(path.dirname(containingFile), name)),
};
return resolved;
}

const { resolvedModule } = ts.resolveModuleName(
name,
containingFile,
options,
moduleResolutionHost
);
return resolvedModule
const { resolvedModule } = ts.resolveModuleName(name, containingFile, options, moduleResolutionHost);
return resolvedModule;
});
return ret;
},
fileExists: moduleResolutionHost.fileExists,
readFile: moduleResolutionHost.readFile,
readDirectory: ts.sys.readDirectory,
getDirectories: ts.sys.getDirectories,
realpath: moduleResolutionHost.realpath
}
realpath: moduleResolutionHost.realpath,
};

return host
}
return host;
};

// .ts suffix is needed since the compiler skips compile
// if the file name seems to be not supported types
function normalize (fileName: string): string {
function normalize(fileName: string): string {
if (/\.vue$/.test(fileName)) {
return fileName + '.ts'
return fileName + '.ts';
}
return fileName
return fileName;
}
29 changes: 0 additions & 29 deletions webpack.config.js

This file was deleted.

26 changes: 26 additions & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path from 'path';
import webpack from 'webpack';

module.exports = {
mode: 'production',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
target: 'node',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
} as webpack.Configuration;
Loading

0 comments on commit 51b1482

Please sign in to comment.