Skip to content

Commit

Permalink
chore(monocdk): support barrel imports (#6996)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elad Ben-Israel authored Mar 25, 2020
1 parent c94ce62 commit 53f82f0
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/@monocdk-experiment/assert/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ done

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


# symlink the full staged monocdk from the staging directory to node_modules
rm -fr node_modules/monocdk-experiment
mkdir -p node_modules
ln -s $PWD/../../monocdk-experiment/staging node_modules/monocdk-experiment
3 changes: 2 additions & 1 deletion packages/@monocdk-experiment/assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "./clone.sh && cdk-build",
"build": "cdk-build",
"watch": "cdk-watch",
"lint": "cdk-lint",
"test": "cdk-test",
Expand All @@ -15,6 +15,7 @@
"build+test": "npm run build && npm test"
},
"cdk-build": {
"pre": [ "./clone.sh" ],
"eslint": {
"disable": true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/monocdk-experiment/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
*.d.ts
!deps.js
!gen.js
src/
staging/
6 changes: 3 additions & 3 deletions packages/monocdk-experiment/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ cd ${scriptdir}
mkdir -p dist/js
cp ${tarball} dist/js

# copying src/ so this module will also work as a local dependency (e.g. for modules under @monocdk-experiment/*).
rm -fr src
rsync -av ${unpacked}/src/ src/
# so this module will also work as a local dependency (e.g. for modules under @monocdk-experiment/*).
rm -fr staging
mv ${unpacked} staging
2 changes: 0 additions & 2 deletions packages/monocdk-experiment/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ for (const dir of modules) {
continue;
}



if (!exists) {
console.error(`missing dependency: ${meta.name}`);
errors = true;
Expand Down
10 changes: 4 additions & 6 deletions packages/monocdk-experiment/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const exclude_files = [
async function main() {
const outdir = path.join(await fs.mkdtemp(path.join(os.tmpdir(), 'monocdk-')), 'package');

const srcdir = path.join(outdir, 'src');

console.error(`generating monocdk at ${outdir}`);
const reexports = [];

Expand Down Expand Up @@ -89,7 +87,7 @@ async function main() {

const basename = path.basename(moduledir);
const files = await fs.readdir(moduledir);
const targetdir = path.join(srcdir, basename);
const targetdir = path.join(outdir, basename);
for (const file of files) {
const source = path.join(moduledir, file);

Expand Down Expand Up @@ -150,12 +148,12 @@ async function main() {
}
}

await fs.writeFile(path.join(srcdir, 'index.ts'), reexports.join('\n'));
await fs.writeFile(path.join(outdir, 'index.ts'), reexports.join('\n'));

console.error(`rewriting "import" statements...`);
const sourceFiles = await findSources(srcdir);
const sourceFiles = await findSources(outdir);
for (const source of sourceFiles) {
await rewriteImports(srcdir, source);
await rewriteImports(outdir, source);
}

// copy tsconfig.json and .npmignore
Expand Down
4 changes: 2 additions & 2 deletions packages/monocdk-experiment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "monocdk-experiment",
"version": "0.0.0",
"description": "An experiment to bundle the entire CDK into a single module",
"main": "src/index.js",
"types": "src/index.d.ts",
"main": "index.js",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/aws/aws-cdk.git"
Expand Down

0 comments on commit 53f82f0

Please sign in to comment.