Skip to content

Commit

Permalink
compile ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 5, 2019
1 parent 7fa0680 commit 049ff59
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 51 deletions.
16 changes: 15 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

module.exports = {
babelrcRoots: ['examples/*'],
overrides: [
{
presets: ['@babel/preset-flow'],
test: '**/*.js',
},
{
plugins: [
require.resolve(
'./scripts/babel-plugin-jest-replace-ts-export-assignment.js',
),
],
presets: ['@babel/preset-typescript'],
test: /\.tsx?$/,
},
],
plugins: [
['@babel/plugin-transform-modules-commonjs', {allowTopLevelThis: true}],
'@babel/plugin-transform-strict-mode',
Expand All @@ -14,6 +29,5 @@ module.exports = {
targets: {node: 6},
},
],
'@babel/preset-flow',
],
};
8 changes: 1 addition & 7 deletions e2e/coverage-remapping/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
"transform": {
"^.+\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
},
"testRegex": "/__tests__/.*\\.(ts|tsx|js)$",
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
"testEnvironment": "node"
},
"dependencies": {
"typescript": "^1.8.10"
Expand Down
4 changes: 0 additions & 4 deletions e2e/stack-trace-source-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"^.+\\.(ts)$": "<rootDir>/preprocessor.js"
},
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
"js"
],
"testRegex": "fails"
},
"dependencies": {
Expand Down
12 changes: 2 additions & 10 deletions e2e/typescript-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
"transform": {
"^.+\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
},
"testMatch": [
"**/__tests__/*.+(ts|tsx|js)"
],
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
"testEnvironment": "node"
},
"dependencies": {
"typescript": "^1.8.10"
"typescript": "^3.3.1"
}
}
8 changes: 4 additions & 4 deletions e2e/typescript-coverage/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# yarn lockfile v1


typescript@^1.8.10:
version "1.8.10"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-1.8.10.tgz#b475d6e0dff0bf50f296e5ca6ef9fbb5c7320f1e"
integrity sha1-tHXW4N/wv1DyluXKbvn7tccyDx4=
typescript@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.1.tgz#6de14e1db4b8a006ac535e482c8ba018c55f750b"
integrity sha512-cTmIDFW7O0IHbn1DPYjkiebHxwtCMU+eTy30ZtJNBPF9j2O1ITu5XH2YnBeVRKWHqF+3JQwWJv0Q0aUgX8W7IA==
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ module.exports = {
'/e2e/__tests__/iterator-to-null-test.js',
],
transform: {
'^.+\\.js$': '<rootDir>/packages/babel-jest',
'^.+\\.[jt]sx?$': '<rootDir>/packages/babel-jest',
},
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"private": true,
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/core": "^7.2.2",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/plugin-transform-strict-mode": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"@babel/register": "^7.0.0",
"@types/babel__core": "^7.0.0",
"@types/babel__generator": "^7.0.0",
"@types/babel__template": "^7.0.0",
"ansi-regex": "^4.0.0",
"ansi-styles": "^3.2.0",
"babel-eslint": "^9.0.0",
Expand Down Expand Up @@ -60,7 +64,7 @@
"slash": "^2.0.0",
"string-length": "^2.0.0",
"strip-ansi": "^5.0.0",
"typescript": "^3.0.3",
"typescript": "^3.3.1",
"webpack": "^4.28.4"
},
"scripts": {
Expand Down
26 changes: 26 additions & 0 deletions scripts/babel-plugin-jest-replace-ts-export-assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// Replace `export =` with `module.exports` which allows us to keep CJS semantics

module.exports = ({template}) => {
const moduleExportsDeclaration = template(`
module.exports = ASSIGNMENT;
`);
return {
name: 'jest-replace-ts-export-assignment',
visitor: {
TSExportAssignment(path) {
path.replaceWith(
moduleExportsDeclaration({ASSIGNMENT: path.node.expression})
);
},
},
};
};
5 changes: 4 additions & 1 deletion scripts/browserBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ const webpack = require('webpack');
const camelCase = require('camelcase');
const rimraf = require('rimraf');

const transformOptions = require('../babel.config.js');

const babelEs5Options = {
// Dont load other config files
babelrc: false,
configFile: false,
overrides: transformOptions.overrides,
plugins: ['@babel/plugin-transform-strict-mode'],
presets: [
[
Expand All @@ -27,7 +30,6 @@ const babelEs5Options = {
targets: 'IE 11',
},
],
'@babel/preset-flow',
],
};

Expand Down Expand Up @@ -63,6 +65,7 @@ function browserBuild(pkgName, entryPath, destination) {
'../packages/expect/build/fakeChalk.js'
),
},
extensions: ['.js', '.json', '.ts'],
},
node: {
fs: 'empty',
Expand Down
40 changes: 28 additions & 12 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* node ./scripts/build.js
* node ./scripts/build.js /users/123/jest/packages/jest-111/src/111.js
*
* NOTE: this script is node@4 compatible
* NOTE: this script is node@6 compatible
*/

'use strict';
Expand All @@ -24,6 +24,7 @@ const fs = require('fs');
const path = require('path');
const glob = require('glob');
const mkdirp = require('mkdirp');
const execa = require('execa');

const babel = require('@babel/core');
const chalk = require('chalk');
Expand All @@ -38,13 +39,13 @@ const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const BUILD_ES5_DIR = 'build-es5';
const JS_FILES_PATTERN = '**/*.js';
const TS_FILES_PATTERN = '**/*.ts';
const IGNORE_PATTERN = '**/__{tests,mocks}__/**';
const PACKAGES_DIR = path.resolve(__dirname, '../packages');

const INLINE_REQUIRE_BLACKLIST = /packages\/expect|(jest-(circus|diff|get-type|jasmine2|matcher-utils|message-util|regex-util|snapshot))|pretty-format\//;

const transformOptions = require('../babel.config.js');
transformOptions.babelrc = false;

const prettierConfig = prettier.resolveConfig.sync(__filename);
prettierConfig.trailingComma = 'none';
Expand Down Expand Up @@ -73,19 +74,18 @@ function getBuildPath(file, buildFolder) {
const pkgSrcPath = path.resolve(PACKAGES_DIR, pkgName, SRC_DIR);
const pkgBuildPath = path.resolve(PACKAGES_DIR, pkgName, buildFolder);
const relativeToSrcPath = path.relative(pkgSrcPath, file);
return path.resolve(pkgBuildPath, relativeToSrcPath);
return path.resolve(pkgBuildPath, relativeToSrcPath).replace(/\.ts$/, '.js');
}

function buildNodePackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {
nodir: true,
});
const files = glob.sync(pattern, {nodir: true});

process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));

files.forEach(file => buildFile(file, true));

process.stdout.write(`${OK}\n`);
}

Expand All @@ -104,11 +104,13 @@ function buildBrowserPackage(p) {
`browser field for ${pkgJsonPath} should start with "${BUILD_ES5_DIR}"`
);
}
browserBuild(
p.split('/').pop(),
path.resolve(srcDir, 'index.js'),
path.resolve(p, browser)
)
let indexFile = path.resolve(srcDir, 'index.js');

if (!fs.existsSync(indexFile)) {
indexFile = indexFile.replace(/\.js$/, '.ts');
}

browserBuild(p.split('/').pop(), indexFile, path.resolve(p, browser))
.then(() => {
process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));
process.stdout.write(`${OK}\n`);
Expand All @@ -134,7 +136,10 @@ function buildFile(file, silent) {
}

mkdirp.sync(path.dirname(destPath), '777');
if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
if (
!micromatch.isMatch(file, JS_FILES_PATTERN) &&
!micromatch.isMatch(file, TS_FILES_PATTERN)
) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
silent ||
process.stdout.write(
Expand Down Expand Up @@ -186,10 +191,21 @@ function buildFile(file, silent) {

const files = process.argv.slice(2);

function compileTypes(packages) {
const packageWithTs = packages.filter(p =>
fs.existsSync(path.resolve(p, 'tsconfig.json'))
);

execa.sync('tsc', ['-b', ...packageWithTs]);
}

if (files.length) {
files.forEach(buildFile);
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Typechecking \n'));
compileTypes(packages);
process.stdout.write(`${OK}\n\n`);
process.stdout.write(chalk.inverse(' Building packages \n'));
packages.forEach(buildNodePackage);
process.stdout.write('\n');
Expand Down
12 changes: 10 additions & 2 deletions scripts/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const fs = require('fs');
const {execSync} = require('child_process');
const path = require('path');
const chalk = require('chalk');
const execa = require('execa');
const getPackages = require('./getPackages');

const BUILD_CMD = `node ${path.resolve(__dirname, './build.js')}`;
Expand All @@ -27,11 +28,12 @@ const exists = filename => {
};
const rebuild = filename => filesToBuild.set(filename, true);

getPackages().forEach(p => {
const packages = getPackages();
packages.forEach(p => {
const srcDir = path.resolve(p, 'src');
try {
fs.accessSync(srcDir, fs.F_OK);
fs.watch(path.resolve(p, 'src'), {recursive: true}, (event, filename) => {
fs.watch(srcDir, {recursive: true}, (event, filename) => {
const filePath = path.resolve(srcDir, filename);

if ((event === 'change' || event === 'rename') && exists(filePath)) {
Expand All @@ -55,6 +57,12 @@ getPackages().forEach(p => {
}
});

const packageWithTs = packages.filter(p =>
fs.existsSync(path.resolve(p, 'tsconfig.json'))
);

execa('tsc', ['-b', ...packageWithTs, '--watch']);

setInterval(() => {
const files = Array.from(filesToBuild.keys());
if (files.length) {
Expand Down
26 changes: 26 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": ["dom", "es2017"],
"declaration": true,
"declarationMap": true,
"composite": true,
"emitDeclarationOnly": true,
// "importHelpers": true,
// "isolatedModules": true,

"strict": true,

/* Additional Checks */
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,

/* Module Resolution Options */
"moduleResolution": "node",
"esModuleInterop": true
},
"exclude": ["**/__tests__/**/*", "**/build/**/*", "**/build-es5/**/*"]
}

0 comments on commit 049ff59

Please sign in to comment.