Skip to content

Update tsickle and typescript #5

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"testEnvironment": "node"
},
"peerDependencies": {
"typescript": "~3.3.1",
"typescript": "^3.7.4",
"webpack": "^4.29.1"
},
"devDependencies": {
Expand All @@ -26,6 +26,7 @@
"@babel/preset-typescript": "^7.1.0",
"@babel/runtime": "^7.3.1",
"@types/fs-extra": "^5.0.5",
"@types/json-schema": "^7.0.4",
"@types/loader-utils": "^1.1.3",
"@types/node": "^11.11.3",
"babel-jest": "^24.1.0",
Expand All @@ -40,14 +41,16 @@
"eslint-plugin-standard": "^4.0.0",
"google-closure-compiler": "^20181231.0.0-webpack-beta",
"jest": "^24.1.0",
"json-utils": "^0.1.0",
"memory-fs": "^0.4.1",
"schema-utils": "^2.6.4",
"source-map": "^0.6.1",
"ts-loader": "^5.3.3",
"typescript": "~3.3.1",
"typescript": "~3.7.4",
"webpack": "^4.29.1"
},
"dependencies": {
"fs-extra": "^7.0.1",
"tsickle": "^0.34.3"
"tsickle": "^0.38.0"
}
}
}
21 changes: 13 additions & 8 deletions src/fix-output.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/**
* Fix some common issues with the tsickle output
* @param code {string} the transformed typescript code
* @param {string} code the transformed typescript code
* @return {string} transformed code
*/
export const fixCode = (code: string): string => {
return code
.replace(
/(?:const|var)\s*.*tsickle_forward_declare_.*\s*=\s*goog\.forwardDeclare.*/g,
""
)
.replace(/goog\.require.*/g, "")
.replace(/tsickle_forward_declare_\d\./g, "");
.replace(
/(?:const|var)\s*.*tsickle_.*\s*=\s*goog\.requireType.*/g,
""
)
.replace(
/(?:const|var)\s*.*tsickle_forward_declare_.*\s*=\s*goog\.forwardDeclare.*/g,
""
)
.replace(/goog\.require.*/g, "")
.replace(/tsickle_forward_declare_\d\./g, "")
.replace(/\/\/# sourceMappingURL=.*\.js\.map/g, "");
};

/**
* Fix some issues with the tsickle extern file definition specific
* to typescript-in-webpack
* @param extern {string} the extern definition file content
* @param {string} extern the extern definition file content
* @return {string} transformed code
*/
export const fixExtern = (extern: string | null): string => {
Expand Down
81 changes: 48 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import fs from "fs-extra";
import * as path from "path";
import { getOptions, OptionObject } from "loader-utils";
import validateOptions = require("schema-utils");
import tsickle = require("tsickle");
import ts from "typescript";
import { EOL } from "os";
import webpack = require("webpack");
import { fixCode, fixExtern } from "./fix-output";
import { jsToTS, tsToJS } from "./path-utils";
import {RawSourceMap} from "source-map";
import validateOptions from 'schema-utils';
import {JSONSchema7} from "schema-utils/declarations/validate";

const LOADER_NAME = "tsickle-loader";
const DEFAULT_EXTERN_DIR = "dist/externs";
const EXTERNS_FILE_NAME = "externs.js";
const DEFAULT_CONFIG_FILE = "tsconfig.json";

const optionsSchema = {
type: "object",
const optionsSchema: JSONSchema7 = {
type: 'object',
properties: {
tsconfig: {
anyOf: [
{
type: "string"
type: 'string'
},
{
type: "boolean"
type: 'boolean'
}
]
},
externDir: {
type: "string"
type: 'string'
}
}
};
Expand All @@ -42,7 +43,7 @@ interface RealOptions extends OptionObject {

const setup = (loaderCTX: webpack.loader.LoaderContext): RealOptions => {
const options = getOptions(loaderCTX);
validateOptions(optionsSchema, options, LOADER_NAME);
validateOptions(optionsSchema, options, {name: LOADER_NAME});

const externDir =
options.externDir != null ? options.externDir : DEFAULT_EXTERN_DIR;
Expand All @@ -64,7 +65,7 @@ const setup = (loaderCTX: webpack.loader.LoaderContext): RealOptions => {
const compilerConfig = ts.parseJsonConfigFileContent(
compilerConfigFile.config,
ts.sys,
".",
path.dirname(tsconfig || ''),
{},
tsconfig
);
Expand Down Expand Up @@ -140,35 +141,49 @@ const tsickleLoader: webpack.loader.Loader = function(
handleDiagnostics(this, [warning], diagnosticsHost, "warning")
};

const jsFiles = new Map<string, string>();

const output = tsickle.emitWithTsickle(
program,
tsickleHost,
compilerHost,
options,
undefined,
(path: string, contents: string) => jsFiles.set(path, contents)
let transpiledSources: string[] = [];
let transpiledSourceMaps: string[] = [];

const output = tsickle.emit(
program,
tsickleHost,
(jsFileName: string, contents: string, _writeByteOrderMark: boolean, _onError, tsSourceFiles) => {
for (const source of tsSourceFiles ?? []) {
if (source.fileName === sourceFileName) {
if (jsFileName.endsWith('.map')) {
transpiledSourceMaps.push(contents);
} else {
transpiledSources.push(contents);
}
}
}
},
program.getSourceFile(sourceFileName)
);

const sourceFileAsJs = tsToJS(sourceFileName);
for (const [path, source] of jsFiles) {
if (sourceFileAsJs.indexOf(path) === -1) {
continue;
}

const tsPathName = jsToTS(path);
const extern = output.externs[tsPathName];
if (extern != null) {
fs.appendFileSync(externFile, fixExtern(extern));
}
if (transpiledSources.length !== 1) {
this.emitError(
Error(`missing compiled result for source file: ${sourceFileName}`)
);
return;
}
if (this.sourceMap && transpiledSourceMaps.length !== 1) {
this.emitError(
Error(`tsconfig must specify sourceMap: "true" when sourcemaps are enabled!`)
);
return;
}

return fixCode(source);
const extern = output.externs[sourceFileName];
if (extern != null) {
fs.appendFileSync(externFile, fixExtern(extern));
}

this.emitError(
Error(`missing compiled result for source file: ${sourceFileName}`)
);
let sourceMap: RawSourceMap|undefined = undefined;
if (this.sourceMap) {
sourceMap = JSON.parse(transpiledSourceMaps[0]);
}
this.callback(null, fixCode(transpiledSources[0]), sourceMap);
};

export default tsickleLoader;
8 changes: 0 additions & 8 deletions src/path-utils.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/schema-utils.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('It will correctly collapse unnecessary modules (tree shaking)', async () =
})

expect(output).toContain('myRealExport')
})
}, 10000);

test('will work with closure compiler plugin', async () => {
const externDir = path.resolve(__dirname, 'tmp', 'externs-' + Math.floor(Math.random() * 10))
Expand Down
Loading