Skip to content

Commit

Permalink
build-system: use esbuild for compiling AMP Server (new server). (#33247
Browse files Browse the repository at this point in the history
)

* build-system: use esbuild for compiling AMP Server (new server).

* make type checking work again

* address justin feedback

* CONFIG_PATH

* Promise --> Promise<void>

* update message for typecheck

* make globby.sync happy.

* nit
  • Loading branch information
samouri authored Mar 16, 2021
1 parent 40d1933 commit 68021ad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
48 changes: 25 additions & 23 deletions build-system/server/typescript-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,51 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const pathModule = require('path');
const esbuild = require('esbuild');
const globby = require('globby');
const path = require('path');
const {cyan, green} = require('kleur/colors');
const {endBuildStep} = require('../tasks/helpers');
const {exec} = require('../common/exec');
const {log} = require('../common/logging');

const SERVER_TRANSFORM_PATH = 'build-system/server/new-server/transforms';

/**
* @return {string}
*/
function getBuildCmd() {
switch (process.platform) {
case 'win32':
return `node .\\node_modules\\typescript\\lib\\tsc.js -p ${SERVER_TRANSFORM_PATH.split(
'/'
).join(pathModule.sep)}${pathModule.sep}tsconfig.json`;

default:
return `./node_modules/typescript/bin/tsc -p ${SERVER_TRANSFORM_PATH}/tsconfig.json`;
}
}
const CONFIG_PATH = `${SERVER_TRANSFORM_PATH}/tsconfig.json`;

/**
* Builds the new server by converting typescript transforms to JS
* @return {void}
* @return {Promise<void>}
*/
function buildNewServer() {
async function buildNewServer() {
log(
green('Building'),
cyan('AMP Server'),
green('at'),
cyan(`${SERVER_TRANSFORM_PATH}/dist`) + green('...')
);
const result = exec(getBuildCmd(), {'stdio': ['inherit', 'inherit', 'pipe']});
const entryPoints = globby.sync(`${SERVER_TRANSFORM_PATH}/**/*.ts`);
const startTime = Date.now();
await esbuild.build({
entryPoints,
outdir: path.join(SERVER_TRANSFORM_PATH, 'dist'),
bundle: false,
tsconfig: CONFIG_PATH,
format: 'cjs',
});
endBuildStep('Built', 'AMP Server', startTime);
}

function typecheckNewServer() {
const cmd = `npx -p typescript tsc --noEmit -p ${CONFIG_PATH}`;
const result = exec(cmd, {'stdio': ['inherit', 'inherit', 'pipe']});

if (result.status != 0) {
const err = new Error('Could not build AMP Server');
// @ts-ignore
err.showStack = false;
throw err;
throw new Error(`Typechecking AMP Server failed.`);
}
}

module.exports = {
buildNewServer,
typecheckNewServer,
SERVER_TRANSFORM_PATH,
};
4 changes: 4 additions & 0 deletions build-system/tasks/check-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {compileCss} = require('./css');
const {extensions, maybeInitializeExtensions} = require('./extension-helpers');
const {log} = require('../common/logging');
const {maybeUpdatePackages} = require('./update-packages');
const {typecheckNewServer} = require('../server/typescript-compile');

/**
* Dedicated type check path.
Expand All @@ -37,6 +38,9 @@ async function checkTypes() {
process.env.NODE_ENV = 'production';
cleanupBuildDir();
maybeInitializeExtensions();

typecheckNewServer();

const compileSrcs = [
'src/amp.js',
'src/amp-shadow.js',
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/coverage-map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function generateMap() {
*/
async function coverageMap() {
installPackages(__dirname);
buildNewServer();
await buildNewServer();

if (!argv.nobuild) {
await dist();
Expand Down
4 changes: 2 additions & 2 deletions build-system/tasks/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async function startServer(
serverOptions = {},
modeOptions = {}
) {
buildNewServer();
await buildNewServer();
if (serverOptions.lazyBuild) {
lazyBuild = serverOptions.lazyBuild;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ async function stopServer() {
async function restartServer() {
stopServer();
try {
buildNewServer();
await buildNewServer();
} catch {
log(red('ERROR:'), 'Could not rebuild', cyan('AMP Server'));
return;
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/server-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async function runTest(inputFile) {
* Tests for AMP server custom transforms. Entry point for `gulp server-tests`.
*/
async function serverTests() {
buildNewServer();
await buildNewServer();
const inputFiles = globby.sync(inputPaths);
for (const inputFile of inputFiles) {
await runTest(inputFile);
Expand Down

0 comments on commit 68021ad

Please sign in to comment.