Skip to content

Commit 8c59e2b

Browse files
authored
Update packages to compile to ESM modules by default (#3112)
BREAKING INTERNAL
1 parent 13d849c commit 8c59e2b

File tree

90 files changed

+10165
-2450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+10165
-2450
lines changed

cloudbuild.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ steps:
7171
args: ['./scripts/run-build.sh', 'tfjs']
7272
waitFor: ['find-affected-packages']
7373

74+
# TODO(yassogba) re-enable after big merge
7475
# Vis.
75-
- name: 'gcr.io/cloud-builders/gcloud'
76-
entrypoint: 'bash'
77-
id: 'tfjs-vis'
78-
args: ['./scripts/run-build.sh', 'tfjs-vis']
79-
waitFor: ['find-affected-packages']
76+
# - name: 'gcr.io/cloud-builders/gcloud'
77+
# entrypoint: 'bash'
78+
# id: 'tfjs-vis'
79+
# args: ['./scripts/run-build.sh', 'tfjs-vis']
80+
# waitFor: ['find-affected-packages']
8081

8182
# WebGPU.
8283
- name: 'gcr.io/cloud-builders/gcloud'

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
"@octokit/rest": "15.2.6",
44
"@types/jasmine": "2.8.7",
55
"@types/node": "^12.7.5",
6+
"@types/shelljs": "^0.8.7",
67
"argparse": "^1.0.10",
78
"chalk": "~2.4.2",
89
"clang-format": "~1.2.4",
910
"jasmine": "3.1.0",
1011
"shelljs": "~0.8.3",
11-
"ts-node": "~4.1.0",
12+
"ts-node": "~8.8.2",
1213
"tslint": "~5.20.0",
1314
"typescript": "3.5.3"
1415
},
1516
"scripts": {
1617
"find-affected-packages": "./scripts/find-affected-packages.js",
17-
"release": "ts-node ./scripts/release.ts",
18-
"publish-npm": "ts-node ./scripts/publish-npm.ts",
19-
"release-notes": "ts-node ./scripts/release_notes/release_notes.ts",
20-
"test-release-notes": "ts-node ./scripts/release_notes/run_tests.ts"
18+
"release": "ts-node -s ./scripts/release.ts",
19+
"publish-npm": "ts-node -s ./scripts/publish-npm.ts",
20+
"release-notes": "ts-node -s ./scripts/release_notes/release_notes.ts",
21+
"test-release-notes": "ts-node -s ./scripts/release_notes/run_tests.ts"
2122
}
2223
}

scripts/package_dependencies.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"tfjs-converter": ["tfjs-core"],
55
"tfjs-layers": ["tfjs-core"],
66
"tfjs-data": ["tfjs-core", "tfjs-layers"],
7-
"tfjs": ["tfjs-core", "tfjs-converter", "tfjs-layers", "tfjs-data"],
7+
"tfjs": ["tfjs-core", "tfjs-converter", "tfjs-layers", "tfjs-data", "tfjs-backend-cpu", "tfjs-backend-webgl"],
88
"tfjs-node": ["tfjs", "tfjs-core"],
99
"tfjs-node-gpu": ["tfjs", "tfjs-core"],
1010
"tfjs-backend-wasm": ["tfjs-core"],

scripts/release_notes/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"target": "es5",
6+
}
7+
}

scripts/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"target": "es5",
6+
}
7+
}

tfjs-automl/tsconfig.json

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
{
2-
"extends": "../tsconfig",
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"moduleResolution": "node",
5+
"noImplicitAny": true,
6+
"sourceMap": true,
7+
"removeComments": false,
8+
"preserveConstEnums": true,
9+
"declaration": true,
10+
"target": "es5",
11+
"lib": [
12+
"es2015",
13+
"dom"
14+
],
15+
"outDir": "./dist",
16+
"noUnusedLocals": true,
17+
"noImplicitReturns": true,
18+
"noImplicitThis": true,
19+
"alwaysStrict": true,
20+
"noUnusedParameters": false,
21+
"pretty": true,
22+
"noFallthroughCasesInSwitch": true,
23+
"allowUnreachableCode": false
24+
},
325
"include": [
426
"src/"
527
],
6-
"exclude": [
7-
"node_modules/"
8-
],
9-
"compilerOptions": {
10-
"outDir": "./dist"
11-
}
28+
"exclude": ["node_modules/"]
1229
}

tfjs-backend-cpu/karma.conf.js

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ module.exports = function(config) {
5050
config.set({
5151
frameworks: ['jasmine', 'karma-typescript'],
5252
files: [
53-
'src/setup_test.ts',
5453
{pattern: 'src/**/*.ts'},
5554
],
5655
preprocessors: {'**/*.ts': ['karma-typescript']},

tfjs-backend-cpu/package.json

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"version": "0.0.0",
44
"description": "Vanilla JavaScript backend for TensorFlow.js",
55
"private": false,
6-
"main": "dist/index.js",
6+
"type": "module",
7+
"main": "dist/tf-backend-cpu.node.js",
78
"jsdelivr": "dist/tf-backend-cpu.min.js",
89
"unpkg": "dist/tf-backend-cpu.min.js",
910
"types": "dist/index.d.ts",
10-
"jsnext:main": "dist/tf-backend-cpu.esm.js",
11-
"module": "dist/tf-backend-cpu.esm.js",
11+
"jsnext:main": "dist/index.js",
12+
"module": "dist/index.js",
1213
"engines": {
1314
"yarn": ">= 1.3.2"
1415
},
@@ -36,15 +37,17 @@
3637
"rollup": "~2.3.2",
3738
"rollup-plugin-terser": "~5.3.0",
3839
"rollup-plugin-visualizer": "~3.3.2",
39-
"ts-node": "~7.0.0",
40+
"ts-node": "~8.8.2",
4041
"tslint": "~5.11.0",
4142
"tslint-no-circular-imports": "~0.5.0",
4243
"typescript": "3.5.3",
4344
"yalc": "~1.0.0-pre.21"
4445
},
4546
"scripts": {
46-
"build-ci": "tsc",
47-
"build": "tsc",
47+
"build-ci": "tsc && yarn bundle-ci",
48+
"build": "tsc && yarn bundle",
49+
"bundle": "rollup -c",
50+
"bundle-ci": "rollup -c --ci",
4851
"build-core": "cd ../tfjs-core && yarn && yarn build",
4952
"build-core-ci": "cd ../tfjs-core && yarn && yarn build-ci",
5053
"build-deps": "yarn build-core && yarn build",
@@ -54,8 +57,8 @@
5457
"publish-local": "rimraf dist/ && yarn build && rollup -c && yalc push",
5558
"publish-npm": "npm publish",
5659
"lint": "tslint -p . -t verbose",
57-
"test": "yarn && yarn build-deps && ts-node run_tests.ts",
58-
"test-ci": "ts-node run_tests.ts"
60+
"test": "yarn && yarn build-deps && ts-node --project tsconfig.test.json run_tests.ts",
61+
"test-ci": "ts-node --project tsconfig.test.json run_tests.ts"
5962
},
6063
"dependencies": {
6164
"@types/seedrandom": "2.4.27",

tfjs-backend-cpu/rollup.config.js

+78-36
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,30 @@ const PREAMBLE = `/**
3838
* =============================================================================
3939
*/`;
4040

41-
function config({plugins = [], output = {}, external = [], visualize = false}) {
41+
function config({
42+
plugins = [],
43+
output = {},
44+
external = [],
45+
visualize = false,
46+
tsCompilerOptions = {}
47+
}) {
4248
if (visualize) {
4349
const filename = output.file + '.html';
44-
plugins.push(visualizer({
45-
sourcemap: true,
46-
filename,
47-
template: 'sunburst',
48-
gzipSize: true,
49-
}));
50+
plugins.push(visualizer(
51+
{sourcemap: true, filename, template: 'sunburst', gzipSize: true}));
5052
console.log(`Will output a bundle visualization in ${filename}`);
5153
}
54+
55+
const defaultTsOptions = {
56+
include: ['src/**/*.ts'],
57+
module: 'ES2015',
58+
};
59+
const tsoptions = Object.assign({}, defaultTsOptions, tsCompilerOptions);
60+
5261
return {
5362
input: 'src/index.ts',
5463
plugins: [
55-
typescript({
56-
include: ['src/**/*.ts'],
57-
module: 'ES2015',
58-
}),
59-
resolve(),
64+
typescript(tsoptions), resolve(),
6065
// Polyfill require() from dependencies.
6166
commonjs({
6267
ignore: ['crypto'],
@@ -91,39 +96,76 @@ function config({plugins = [], output = {}, external = [], visualize = false}) {
9196
module.exports = cmdOptions => {
9297
const bundles = [];
9398

94-
if (!cmdOptions.ci) {
95-
// tf-backend-cpu.js
96-
bundles.push(config({
97-
output: {
98-
format: 'umd',
99-
name: 'tf',
100-
extend: true,
101-
file: 'dist/tf-backend-cpu.js',
102-
}
103-
}));
104-
}
99+
const terserPlugin = terser({output: {preamble: PREAMBLE, comments: false}});
100+
const name = 'tf';
101+
const extend = true;
102+
const browserFormat = 'umd';
103+
const fileName = 'tf-backend-cpu';
105104

106-
// tf-backend-cpu.min.js
105+
// Node
107106
bundles.push(config({
108-
plugins: [terser({output: {preamble: PREAMBLE, comments: false}})],
109107
output: {
110-
format: 'umd',
111-
name: 'tf',
112-
extend: true,
113-
file: 'dist/tf-backend-cpu.min.js',
108+
format: 'cjs',
109+
name,
110+
extend,
111+
file: `dist/${fileName}.node.js`,
112+
freeze: false
114113
},
115-
visualize: cmdOptions.visualize
114+
tsCompilerOptions: {target: 'es5'}
116115
}));
117116

118-
if (!cmdOptions.ci) {
119-
// tf-backend-cpu.esm.js
117+
if (cmdOptions.ci || cmdOptions.npm) {
118+
// Browser default minified (ES5)
120119
bundles.push(config({
121-
plugins: [terser({output: {preamble: PREAMBLE, comments: false}})],
120+
plugins: [terserPlugin],
122121
output: {
123-
format: 'es',
124-
file: 'dist/tf-backend-cpu.esm.js',
125-
}
122+
format: browserFormat,
123+
name,
124+
extend,
125+
file: `dist/${fileName}.min.js`,
126+
freeze: false
127+
},
128+
tsCompilerOptions: {target: 'es5'},
129+
visualize: cmdOptions.visualize
130+
}));
131+
}
132+
133+
if (cmdOptions.npm) {
134+
// Browser default unminified (ES5)
135+
bundles.push(config({
136+
output: {
137+
format: browserFormat,
138+
name,
139+
extend,
140+
file: `dist/${fileName}.js`,
141+
freeze: false
142+
},
143+
tsCompilerOptions: {target: 'es5'}
144+
}));
145+
146+
// Browser ES2017
147+
bundles.push(config({
148+
output: {
149+
format: browserFormat,
150+
name,
151+
extend,
152+
file: `dist/${fileName}.es2017.js`
153+
},
154+
tsCompilerOptions: {target: 'es2017'}
155+
}));
156+
157+
// Browser ES2017 minified
158+
bundles.push(config({
159+
plugins: [terserPlugin],
160+
output: {
161+
format: browserFormat,
162+
name,
163+
extend,
164+
file: `dist/${fileName}.es2017.min.js`
165+
},
166+
tsCompilerOptions: {target: 'es2017'}
126167
}));
127168
}
169+
128170
return bundles;
129171
};

tfjs-backend-cpu/run_tests.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* =============================================================================
1616
*/
1717

18+
// Register the backend.
19+
import './src/index';
1820
// tslint:disable-next-line: no-imports-from-dist
1921
import {setTestEnvs, setupTestFilters, TestFilter} from '@tensorflow/tfjs-core/dist/jasmine_util';
2022

@@ -30,11 +32,11 @@ process.on('unhandledRejection', e => {
3032

3133
setTestEnvs([{name: 'cpu', backendName: 'cpu', isDataSync: true}]);
3234

33-
const coreTests = 'node_modules/@tensorflow/tfjs-core/dist/**/*_test.js';
35+
const coreTests = 'node_modules/@tensorflow/tfjs-core/dist/tests.js';
3436
const cpuTests = 'src/**/*_test.ts';
3537

3638
const runner = new jasmineCtor();
37-
runner.loadConfig({spec_files: [coreTests, cpuTests], random: false});
39+
runner.loadConfig({spec_files: [cpuTests, coreTests], random: false});
3840

3941
// customInclude takes higher priority then TEST_FILTERS, only when
4042
// customInclude return false will TEST_FILTERS be considered.

tfjs-backend-cpu/scripts/build-npm.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ yarn rimraf dist/
2020
yarn
2121

2222
yarn build
23-
yarn rollup -c --visualize
23+
yarn rollup -c --visualize --npm
2424

2525
echo "Stored standalone library at dist/tf-backend-cpu(.min).js"

tfjs-backend-cpu/src/setup_test.ts

-37
This file was deleted.

tfjs-backend-cpu/src/shared.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google Inc. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
// Shared kernel impls for use in other backends.
19+
export {transposeImpl} from './kernels/Transpose_impl';

0 commit comments

Comments
 (0)