Skip to content

Commit 53f82f0

Browse files
author
Elad Ben-Israel
authored
chore(monocdk): support barrel imports (#6996)
chore(monocdk): support barrel imports (#6996) This change enables an alternative syntax for importing submodules from monocdk: import * as s3 from 'monocdk-experiment/aws-s3'; In addition to the currently supported syntax: import { aws_s3 as s3 } from 'monocdk-experiment'; The new syntax will make it easy to migrate from existing CDK codebase and also makes it easier to namespace submodule imports by consumers. To that end, we hoisted the submodules from `src` to be direct subdirectories of the package (otherwise, the barrel import would have been `monocdk-experiment/src/aws-s3`). Credits: @CaerusKaru
1 parent c94ce62 commit 53f82f0

File tree

7 files changed

+16
-16
lines changed

7 files changed

+16
-16
lines changed

packages/@monocdk-experiment/assert/clone.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ done
1515

1616
npx rewrite-imports {lib,test}/*.ts jest.ts
1717

18-
18+
# symlink the full staged monocdk from the staging directory to node_modules
19+
rm -fr node_modules/monocdk-experiment
20+
mkdir -p node_modules
21+
ln -s $PWD/../../monocdk-experiment/staging node_modules/monocdk-experiment

packages/@monocdk-experiment/assert/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"scripts": {
8-
"build": "./clone.sh && cdk-build",
8+
"build": "cdk-build",
99
"watch": "cdk-watch",
1010
"lint": "cdk-lint",
1111
"test": "cdk-test",
@@ -15,6 +15,7 @@
1515
"build+test": "npm run build && npm test"
1616
},
1717
"cdk-build": {
18+
"pre": [ "./clone.sh" ],
1819
"eslint": {
1920
"disable": true
2021
},

packages/monocdk-experiment/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
*.d.ts
33
!deps.js
44
!gen.js
5-
src/
5+
staging/

packages/monocdk-experiment/build.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ cd ${scriptdir}
3333
mkdir -p dist/js
3434
cp ${tarball} dist/js
3535

36-
# copying src/ so this module will also work as a local dependency (e.g. for modules under @monocdk-experiment/*).
37-
rm -fr src
38-
rsync -av ${unpacked}/src/ src/
36+
# so this module will also work as a local dependency (e.g. for modules under @monocdk-experiment/*).
37+
rm -fr staging
38+
mv ${unpacked} staging

packages/monocdk-experiment/deps.js

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ for (const dir of modules) {
3131
continue;
3232
}
3333

34-
35-
3634
if (!exists) {
3735
console.error(`missing dependency: ${meta.name}`);
3836
errors = true;

packages/monocdk-experiment/gen.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ const exclude_files = [
3434
async function main() {
3535
const outdir = path.join(await fs.mkdtemp(path.join(os.tmpdir(), 'monocdk-')), 'package');
3636

37-
const srcdir = path.join(outdir, 'src');
38-
3937
console.error(`generating monocdk at ${outdir}`);
4038
const reexports = [];
4139

@@ -89,7 +87,7 @@ async function main() {
8987

9088
const basename = path.basename(moduledir);
9189
const files = await fs.readdir(moduledir);
92-
const targetdir = path.join(srcdir, basename);
90+
const targetdir = path.join(outdir, basename);
9391
for (const file of files) {
9492
const source = path.join(moduledir, file);
9593

@@ -150,12 +148,12 @@ async function main() {
150148
}
151149
}
152150

153-
await fs.writeFile(path.join(srcdir, 'index.ts'), reexports.join('\n'));
151+
await fs.writeFile(path.join(outdir, 'index.ts'), reexports.join('\n'));
154152

155153
console.error(`rewriting "import" statements...`);
156-
const sourceFiles = await findSources(srcdir);
154+
const sourceFiles = await findSources(outdir);
157155
for (const source of sourceFiles) {
158-
await rewriteImports(srcdir, source);
156+
await rewriteImports(outdir, source);
159157
}
160158

161159
// copy tsconfig.json and .npmignore

packages/monocdk-experiment/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "monocdk-experiment",
33
"version": "0.0.0",
44
"description": "An experiment to bundle the entire CDK into a single module",
5-
"main": "src/index.js",
6-
"types": "src/index.d.ts",
5+
"main": "index.js",
6+
"types": "index.d.ts",
77
"repository": {
88
"type": "git",
99
"url": "https://github.com/aws/aws-cdk.git"

0 commit comments

Comments
 (0)