Skip to content

Commit

Permalink
(feat): add --preserveModules flag
Browse files Browse the repository at this point in the history
- this works the same way as Rollup's preserveModules option, leaving
  the module structure and not bundling any files together
  - format, env, and minify are used as directories instead of filename
    suffixes since the filename can't be changed without configuring
    output.entryFileNames (possibly a future improvement?)
  • Loading branch information
agilgur5 committed Mar 4, 2020
1 parent 3a6d42f commit 97c7e41
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ Options
--onFailure Run a command on a failed build
--noClean Don't clean the dist folder
--transpileOnly Skip type checking
--preserveModules Preserve module structure; do not bundle
-h, --help Displays this message
Examples
Expand All @@ -423,6 +424,7 @@ Examples
$ tsdx watch --onSuccess "echo Successful build!"
$ tsdx watch --onFailure "echo The build failed!"
$ tsdx watch --transpileOnly
$ tsdx watch --preserveModules
```
### `tsdx build`
Expand All @@ -442,6 +444,7 @@ Options
--extractErrors Opt-in to extracting invariant error codes
--tsconfig Specify your custom tsconfig path (default <root-folder>/tsconfig.json)
--transpileOnly Skip type checking
--preserveModules Preserve module structure; do not bundle
-h, --help Displays this message
Examples
Expand All @@ -452,6 +455,7 @@ Examples
$ tsdx build --extractErrors
$ tsdx build --tsconfig ./tsconfig.foo.json
$ tsdx build --transpileOnly
$ tsdx build --preserveModules
```
### `tsdx test`
Expand Down
17 changes: 15 additions & 2 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ export async function createRollupConfig(
.filter(Boolean)
.join('.');

const outputDir = [
`${paths.appDist}/${opts.format}`,
opts.env,
shouldMinify ? 'min' : '',
]
.filter(Boolean)
.join('/');

const output = opts.preserveModules
? { dir: outputDir }
: { file: outputName };

let tsconfigJSON;
try {
tsconfigJSON = await fs.readJSON(opts.tsconfig || paths.tsconfigJson);
Expand Down Expand Up @@ -79,10 +91,11 @@ export async function createRollupConfig(
// Punchline....Don't use getters and setters
propertyReadSideEffects: false,
},
preserveModules: opts.preserveModules,
// Establish Rollup output
output: {
// Set filenames of the consumer's package
file: outputName,
// Set filenames or directories of the consumer's package
...output,
// Pass through the file format
format: opts.format,
// Do not let Rollup call Object.freeze() on namespace import objects
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ prog
.example('build --transpileOnly')
.option('--extractErrors', 'Extract invariant errors to ./errors/codes.json.')
.example('build --extractErrors')
.option('--preserveModules', 'Preserve module structure; do not bundle')
.example('watch --preserveModules')
.action(async (dirtyOpts: WatchOpts) => {
const opts = await normalizeOpts(dirtyOpts);
const buildConfigs = await createBuildConfigs(opts);
Expand Down Expand Up @@ -393,6 +395,8 @@ prog
.example(
'build --extractErrors=https://reactjs.org/docs/error-decoder.html?invariant='
)
.option('--preserveModules', 'Preserve module structure; do not bundle')
.example('build --preserveModules')
.action(async (dirtyOpts: BuildOpts) => {
const opts = await normalizeOpts(dirtyOpts);
const buildConfigs = await createBuildConfigs(opts);
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
interface SharedOpts {
// Should preserve source structure? If true, files will not be bundled
preserveModules: false;
// JS target
target: 'node' | 'browser';
// Path to tsconfig file
Expand Down
26 changes: 26 additions & 0 deletions test/tests/tsdx-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ describe('tsdx build', () => {
expect(code).toBe(0);
});

it('should preserve modules after compilation', () => {
util.setupStageWithFixture(stageName, 'build-default');

const output = shell.exec(
'node ../dist/index.js build --format esm,cjs --preserveModules'
);

// ESM build
expect(shell.test('-f', 'dist/esm/index.js')).toBeTruthy();
expect(shell.test('-f', 'dist/esm/foo.js')).toBeTruthy();
expect(shell.test('-f', 'dist/esm/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'dist/esm/foo.d.ts')).toBeTruthy();
// CJS dev build
expect(shell.test('-f', 'dist/cjs/development/index.js')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/development/foo.js')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/development/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/development/foo.d.ts')).toBeTruthy();
// CJS prod build
expect(shell.test('-f', 'dist/cjs/production/min/index.js')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/production/min/foo.js')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/production/min/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'dist/cjs/production/min/foo.d.ts')).toBeTruthy();

expect(output.code).toBe(0);
});

afterEach(() => {
util.teardownStage(stageName);
});
Expand Down

0 comments on commit 97c7e41

Please sign in to comment.