2022-12-28 01:27:12 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*
|
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
const {
|
2022-12-28 01:27:12 -08:00
|
|
|
argv,
|
2022-12-30 14:29:42 -08:00
|
|
|
cleanTask,
|
2022-12-28 01:27:12 -08:00
|
|
|
logger,
|
|
|
|
jestTask,
|
|
|
|
option,
|
|
|
|
parallel,
|
|
|
|
series,
|
|
|
|
spawn,
|
|
|
|
task,
|
2023-05-04 08:11:04 -07:00
|
|
|
tscTask,
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
} = require('just-scripts');
|
2022-12-28 01:27:12 -08:00
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
const {readFile, writeFile} = require('fs/promises');
|
2023-05-09 22:21:01 -07:00
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
const glob = require('glob');
|
|
|
|
const path = require('path');
|
|
|
|
const which = require('which');
|
2022-12-28 01:27:12 -08:00
|
|
|
|
|
|
|
const node = process.execPath;
|
|
|
|
|
2023-05-09 15:35:42 -07:00
|
|
|
option('fix');
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-05-09 15:35:42 -07:00
|
|
|
task('clean', cleanTask({paths: ['build', 'dist']}));
|
2022-12-30 14:29:42 -08:00
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
function defineFlavor(flavor, env) {
|
2023-05-09 15:35:42 -07:00
|
|
|
task(`cmake-build:${flavor}`, cmakeBuildTask({targets: [flavor]}));
|
2023-05-04 08:11:04 -07:00
|
|
|
task(
|
|
|
|
`jest:${flavor}`,
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
jestTask({
|
|
|
|
config: path.join(__dirname, 'jest.config.js'),
|
|
|
|
nodeArgs: ['--experimental-vm-modules'],
|
2023-10-19 18:36:31 -07:00
|
|
|
env,
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
}),
|
2023-05-04 08:11:04 -07:00
|
|
|
);
|
2022-12-28 01:27:12 -08:00
|
|
|
task(
|
|
|
|
`test:${flavor}`,
|
2023-05-09 15:35:42 -07:00
|
|
|
series(emcmakeGenerateTask(), `cmake-build:${flavor}`, `jest:${flavor}`),
|
2022-12-28 01:27:12 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
defineFlavor('web');
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-07-12 15:43:10 -07:00
|
|
|
task('build', series(emcmakeGenerateTask(), cmakeBuildTask()));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-10-19 18:36:31 -07:00
|
|
|
task('test', series(emcmakeGenerateTask(), 'cmake-build:web', 'jest:web'));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
|
|
|
task(
|
2023-05-09 15:35:42 -07:00
|
|
|
'benchmark',
|
2023-07-12 15:43:10 -07:00
|
|
|
series(
|
|
|
|
emcmakeGenerateTask(),
|
2023-10-19 05:04:43 -07:00
|
|
|
cmakeBuildTask({targets: ['web']}),
|
2023-07-12 15:43:10 -07:00
|
|
|
runBenchTask(),
|
|
|
|
),
|
2022-12-28 01:27:12 -08:00
|
|
|
);
|
|
|
|
|
2023-07-17 14:27:32 -07:00
|
|
|
task('clang-format', clangFormatTask({fix: argv().fix}));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-05-09 22:21:01 -07:00
|
|
|
task('prepack-package-json', async () => {
|
|
|
|
const packageJsonPath = path.join(__dirname, 'package.json');
|
|
|
|
const packageJsonContents = await readFile(packageJsonPath);
|
|
|
|
const packageJson = JSON.parse(packageJsonContents.toString('utf-8'));
|
|
|
|
|
|
|
|
recursiveReplace(packageJson, /(.\/src\/.*)\.ts/, '$1.js');
|
|
|
|
await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
|
|
});
|
|
|
|
|
2023-05-09 15:35:42 -07:00
|
|
|
task(
|
2023-05-09 22:21:01 -07:00
|
|
|
'prepack',
|
|
|
|
series(
|
|
|
|
parallel('build', tscTask({emitDeclarationOnly: true})),
|
2023-05-09 15:35:42 -07:00
|
|
|
babelTransformTask({dir: 'src'}),
|
2023-05-09 22:21:01 -07:00
|
|
|
'prepack-package-json',
|
2023-05-09 15:35:42 -07:00
|
|
|
),
|
|
|
|
);
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-10-19 18:36:31 -07:00
|
|
|
function recursiveReplace(obj, pattern, replacement) {
|
2023-05-09 22:21:01 -07:00
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
obj[key] = value.replace(pattern, replacement);
|
|
|
|
} else if (typeof value === 'object' && value != null) {
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
recursiveReplace(value, pattern, replacement);
|
2023-05-09 22:21:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
function babelTransformTask(opts) {
|
2023-05-09 15:35:42 -07:00
|
|
|
return () => {
|
|
|
|
const args = [
|
|
|
|
opts.dir,
|
|
|
|
'--source-maps',
|
|
|
|
'--out-dir',
|
|
|
|
opts.dir,
|
|
|
|
'--extensions',
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
'.js,.cjs,.mjs,.ts,.cts,.mts',
|
2023-05-09 15:35:42 -07:00
|
|
|
];
|
|
|
|
logger.info(`Transforming "${path.resolve(opts.dir)}"`);
|
|
|
|
|
|
|
|
return spawn(node, [require.resolve('@babel/cli/bin/babel'), ...args], {
|
|
|
|
cwd: __dirname,
|
|
|
|
});
|
2022-12-28 01:27:12 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function runBenchTask() {
|
|
|
|
return () => {
|
2023-05-09 15:35:42 -07:00
|
|
|
const files = glob.sync('./tests/Benchmarks/**/*');
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-05-10 17:12:10 -07:00
|
|
|
const args = [
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
'--loader=babel-register-esm',
|
2023-05-10 17:12:10 -07:00
|
|
|
'./tests/bin/run-bench.ts',
|
|
|
|
...files,
|
|
|
|
];
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
logger.info(['node', ...args].join(' '));
|
2023-05-10 17:12:10 -07:00
|
|
|
|
2023-10-19 18:36:31 -07:00
|
|
|
return spawn(node, args, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
});
|
2022-12-28 01:27:12 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function emcmakeGenerateTask() {
|
|
|
|
return () => {
|
2023-05-09 15:35:42 -07:00
|
|
|
const emcmake = which.sync('emcmake');
|
|
|
|
const ninja = which.sync('ninja', {nothrow: true});
|
2022-12-28 01:27:12 -08:00
|
|
|
const args = [
|
2023-05-09 15:35:42 -07:00
|
|
|
'cmake',
|
|
|
|
'-S',
|
|
|
|
'.',
|
|
|
|
'-B',
|
|
|
|
'build',
|
|
|
|
...(ninja ? ['-G', 'Ninja'] : []),
|
2022-12-28 01:27:12 -08:00
|
|
|
];
|
2023-05-09 15:35:42 -07:00
|
|
|
logger.info(['emcmake', ...args].join(' '));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-06-23 14:08:12 -07:00
|
|
|
return spawn(emcmake, args, {stdio: 'inherit'});
|
2022-12-28 01:27:12 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
function cmakeBuildTask(opts) {
|
2022-12-28 01:27:12 -08:00
|
|
|
return () => {
|
2023-05-09 15:35:42 -07:00
|
|
|
const cmake = which.sync('cmake');
|
2022-12-28 01:27:12 -08:00
|
|
|
const args = [
|
2023-05-09 15:35:42 -07:00
|
|
|
'--build',
|
|
|
|
'build',
|
|
|
|
...(opts?.targets ? ['--target', ...opts.targets] : []),
|
2022-12-28 01:27:12 -08:00
|
|
|
];
|
2023-05-09 15:35:42 -07:00
|
|
|
logger.info(['cmake', ...args].join(' '));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-05-09 15:35:42 -07:00
|
|
|
return spawn(cmake, args, {stdio: 'inherit'});
|
2022-12-28 01:27:12 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
Consolidate JavaScript Flavors
Fixes https://github.com/facebook/yoga/issues/1417
This dramatically simplifies the matrix of Node vs web, ASM vs WASM, sync vs async compilation, or CommonJS vs ES Modules. We have one variant, using without, with ESModule top-level await to do async compilation. Web/node share the same binary, and we base64 encode the WASM into a wrapper JS file for compatibility with Node and bundlers.
After this change we target:
This has some downsides, like requiring an environment with top level await, but also has upsides, like a consistent, sync looking API compatible with older Yoga, and mitigating TypeScript issues with package exports and typings resolution.
## Test Plan
1. `yarn test`
2. `yarn lint`
3. `yarn tsc`
4. `yarn build` website-next
5. Locally test website
5. Examine package artifact created by GitHub
2023-10-19 04:59:10 -07:00
|
|
|
function clangFormatTask(opts) {
|
2022-12-28 01:27:12 -08:00
|
|
|
return () => {
|
|
|
|
const args = [
|
2023-05-09 15:35:42 -07:00
|
|
|
...(opts?.fix ? ['-i'] : ['--dry-run', '--Werror']),
|
|
|
|
...glob.sync('**/*.{h,hh,hpp,c,cpp,cc,m,mm}'),
|
2022-12-28 01:27:12 -08:00
|
|
|
];
|
2023-05-09 15:35:42 -07:00
|
|
|
logger.info(['clang-format', ...args].join(' '));
|
2022-12-28 01:27:12 -08:00
|
|
|
|
2023-05-09 15:35:42 -07:00
|
|
|
return spawn(node, [require.resolve('clang-format'), ...args], {
|
|
|
|
stdio: 'inherit',
|
2022-12-28 01:27:12 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|