forked from jaredpalmer/tsdx
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for dts.config.ts (#132)
* feat: add support for dts.config.ts attempts to load dts.config.ts if it is available, throws an error if unable to load it. if dts.config.ts isn't available, moves on to check for dts.config.js. enables type declarations in tsconfig.json so that people can import types correctly. this commit does not include documentation about how to use dts.config.ts as type exports should probably be revisited. closes #128 * test: use native typescript syntax for dts.config.ts * docs: add docs on dts.config.ts
- Loading branch information
1 parent
4768b77
commit a5b4365
Showing
14 changed files
with
254 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as fs from 'fs-extra'; | ||
import * as shell from 'shelljs'; | ||
|
||
import * as util from '../utils/fixture'; | ||
import { execWithCache } from '../utils/shell'; | ||
|
||
shell.config.silent = false; | ||
|
||
const testDir = 'integration'; | ||
const fixtureName = 'build-withConfigTs'; | ||
const stageName = `stage-integration-${fixtureName}`; | ||
|
||
describe('integration :: dts build :: dts-config.ts', () => { | ||
beforeAll(() => { | ||
util.teardownStage(stageName); | ||
util.setupStageWithFixture(testDir, stageName, fixtureName); | ||
}); | ||
|
||
it('should create a CSS file in the dist/ directory', () => { | ||
const output = execWithCache('node ../dist/index.js build'); | ||
|
||
// TODO: this is kind of subpar naming, rollup-plugin-postcss just names it | ||
// the same as the output file, but with the .css extension | ||
expect(shell.test('-f', 'dist/build-withconfigts.cjs.development.css')); | ||
|
||
expect(output.code).toBe(0); | ||
}); | ||
|
||
it('should autoprefix and minify the CSS file', async () => { | ||
const output = execWithCache('node ../dist/index.js build'); | ||
|
||
const cssText = await fs.readFile( | ||
'./dist/build-withconfigts.cjs.development.css' | ||
); | ||
|
||
// autoprefixed and minifed output | ||
expect( | ||
cssText.includes('.test::-moz-placeholder{color:"blue"}') | ||
).toBeTruthy(); | ||
|
||
expect(output.code).toBe(0); | ||
}); | ||
|
||
it('should compile files into a dist directory', () => { | ||
const output = execWithCache('node ../dist/index.js build'); | ||
|
||
expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); | ||
expect( | ||
shell.test('-f', 'dist/build-withconfigts.cjs.development.js') | ||
).toBeTruthy(); | ||
expect( | ||
shell.test('-f', 'dist/build-withconfigts.cjs.production.min.js') | ||
).toBeTruthy(); | ||
expect(shell.test('-f', 'dist/build-withconfigts.esm.js')).toBeTruthy(); | ||
|
||
expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); | ||
|
||
expect(output.code).toBe(0); | ||
}); | ||
|
||
afterAll(() => { | ||
util.teardownStage(stageName); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
test/integration/fixtures/build-withConfigTs/dts.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { DtsOptions, RollupOptions } from '../src'; | ||
import autoprefixer from 'autoprefixer'; | ||
import cssnano from 'cssnano'; | ||
import postcss from 'rollup-plugin-postcss'; | ||
|
||
// This is necessary due to how typechecking works with the @types/cssnano | ||
// package. If you remove the `if` check below and attempt to add the cssnano | ||
// processor directly, you run into issues with type stack depth. | ||
const getPlugins = () => { | ||
const plugins: any[] = [autoprefixer()]; | ||
const cssnanoProcessor = cssnano({ preset: 'default' }); | ||
if ('version' in cssnanoProcessor) { | ||
plugins.push(cssnanoProcessor); | ||
} | ||
return plugins; | ||
}; | ||
|
||
export default { | ||
rollup(config: RollupOptions, options: DtsOptions) { | ||
config?.plugins?.push( | ||
postcss({ | ||
plugins: getPlugins(), | ||
inject: false, | ||
// only write out CSS for the first bundle (avoids pointless extra files): | ||
extract: !!options.writeMeta, | ||
}) | ||
); | ||
return config; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"scripts": { | ||
"build": "dts build" | ||
}, | ||
"name": "build-withconfigts", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* ::placeholder should be autoprefixed, and everything minified */ | ||
.test::placeholder { | ||
color: 'blue'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import './index.css'; | ||
|
||
export const sum = (a: number, b: number) => { | ||
if ('development' === process.env.NODE_ENV) { | ||
console.log('dev only output'); | ||
} | ||
return a + b; | ||
}; |
25 changes: 25 additions & 0 deletions
25
test/integration/fixtures/build-withConfigTs/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"module": "ESNext", | ||
"lib": ["dom", "esnext"], | ||
"declaration": true, | ||
"sourceMap": true, | ||
"rootDir": "./src", | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"moduleResolution": "node", | ||
"jsx": "react", | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"paths": { | ||
"dts-cli": ["../src"] | ||
} | ||
}, | ||
"include": ["src", "types"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.