-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a8f78e
commit a645e60
Showing
20 changed files
with
196 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { join } from 'node:path'; | ||
import { build } from '@rslib/core'; | ||
import { expect, test } from 'vitest'; | ||
import { getEntryJsResults } from '#shared'; | ||
import { loadConfig } from '../../../packages/core/src/config'; | ||
|
||
test('alias in js', async () => { | ||
delete process.env.NODE_ENV; | ||
|
||
const fixturePath = join(__dirname, 'js'); | ||
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts')); | ||
await build(rslibConfig); | ||
const results = await getEntryJsResults(rslibConfig); | ||
|
||
expect(results.esm).toContain('hello world'); | ||
expect(results.cjs).toContain('hello world'); | ||
}); | ||
|
||
test('alias in ts', async () => { | ||
delete process.env.NODE_ENV; | ||
|
||
const fixturePath = join(__dirname, 'ts'); | ||
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts')); | ||
await build(rslibConfig); | ||
const results = await getEntryJsResults(rslibConfig); | ||
|
||
expect(results.esm).toContain('hello world'); | ||
expect(results.cjs).toContain('hello world'); | ||
}); |
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,15 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared'; | ||
|
||
export default defineConfig({ | ||
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.js'), | ||
}, | ||
alias: { | ||
'@src': join(__dirname, 'src'), | ||
}, | ||
}, | ||
}); |
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 @@ | ||
export const a = 'hello world'; |
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,3 @@ | ||
import { a } from '@src/a'; | ||
|
||
console.info(a); |
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,15 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared'; | ||
|
||
export default defineConfig({ | ||
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.ts'), | ||
}, | ||
alias: { | ||
'@src': join(__dirname, 'src'), | ||
}, | ||
}, | ||
}); |
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,2 @@ | ||
export const a = 'hello world'; | ||
export type A = string; |
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,5 @@ | ||
import { a } from '@src/a'; | ||
|
||
console.info(a); | ||
|
||
export type { A } from './a'; |
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,10 @@ | ||
{ | ||
"extends": "@rslib/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"paths": { | ||
"@src/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["src"] | ||
} |
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 |
---|---|---|
@@ -1,47 +1,33 @@ | ||
import { join } from 'node:path'; | ||
import { type RslibConfig, build } from '@rslib/core'; | ||
import { build } from '@rslib/core'; | ||
import { expect, test } from 'vitest'; | ||
import { globContentJSON } from '#helper'; | ||
import { getEntryJsResults } from '#shared'; | ||
import { loadConfig } from '../../../packages/core/src/config'; | ||
|
||
test('define', async () => { | ||
test('define in js', async () => { | ||
delete process.env.NODE_ENV; | ||
|
||
const rslibConfig: RslibConfig = { | ||
lib: [ | ||
{ | ||
format: 'esm', | ||
output: { | ||
distPath: { | ||
root: join(__dirname, './dist/esm'), | ||
}, | ||
}, | ||
}, | ||
{ | ||
format: 'cjs', | ||
output: { | ||
distPath: { | ||
root: join(__dirname, './dist/cjs'), | ||
}, | ||
}, | ||
}, | ||
], | ||
source: { | ||
entry: { | ||
main: join(__dirname, './js/src/index.js'), | ||
}, | ||
define: { | ||
VERSION: JSON.stringify('1.0.0'), | ||
}, | ||
}, | ||
}; | ||
const fixturePath = join(__dirname, 'js'); | ||
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts')); | ||
await build(rslibConfig); | ||
const results = await getEntryJsResults(rslibConfig); | ||
|
||
const instance = await build(rslibConfig); | ||
expect(results.esm).not.toContain('console.info(VERSION)'); | ||
expect(results.esm).toContain('1.0.0'); | ||
expect(results.cjs).not.toContain('console.info(VERSION)'); | ||
expect(results.cjs).toContain('1.0.0'); | ||
}); | ||
|
||
test('define in ts', async () => { | ||
delete process.env.NODE_ENV; | ||
|
||
const results = await globContentJSON(instance[0]!.context.distPath, { | ||
absolute: true, | ||
ignore: ['/**/*.map'], | ||
}); | ||
const fixturePath = join(__dirname, 'ts'); | ||
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts')); | ||
await build(rslibConfig); | ||
const results = await getEntryJsResults(rslibConfig); | ||
|
||
const entryJs = Object.keys(results).find((file) => file.endsWith('.js')); | ||
expect(results[entryJs!]).not.toContain('console.info(VERSION)'); | ||
expect(results.esm).not.toContain('console.info(VERSION)'); | ||
expect(results.esm).toContain('1.0.0'); | ||
expect(results.cjs).not.toContain('console.info(VERSION)'); | ||
expect(results.cjs).toContain('1.0.0'); | ||
}); |
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,15 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared'; | ||
|
||
export default defineConfig({ | ||
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.js'), | ||
}, | ||
define: { | ||
VERSION: JSON.stringify('1.0.0'), | ||
}, | ||
}, | ||
}); |
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,15 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared'; | ||
|
||
export default defineConfig({ | ||
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.ts'), | ||
}, | ||
define: { | ||
VERSION: JSON.stringify('1.0.0'), | ||
}, | ||
}, | ||
}); |
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 @@ | ||
declare const VERSION: number; |
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 @@ | ||
console.info(VERSION); |
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,44 @@ | ||
import { join } from 'node:path'; | ||
import type { LibConfig, RslibConfig } from '@rslib/core'; | ||
import { globContentJSON } from '#helper'; | ||
|
||
export function generateBundleEsmConfig(cwd: string): LibConfig { | ||
return { | ||
format: 'esm', | ||
output: { | ||
distPath: { | ||
root: join(cwd, './dist/esm'), | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export function generateBundleCjsConfig(cwd: string): LibConfig { | ||
return { | ||
format: 'cjs', | ||
output: { | ||
distPath: { | ||
root: join(cwd, './dist/cjs'), | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export async function getEntryJsResults(rslibConfig: RslibConfig) { | ||
const results: Record<string, string> = {}; | ||
|
||
for (const libConfig of rslibConfig.lib) { | ||
const result = await globContentJSON(libConfig?.output?.distPath?.root!, { | ||
absolute: true, | ||
ignore: ['/**/*.map'], | ||
}); | ||
|
||
const entryJs = Object.keys(result).find((file) => file.endsWith('.js')); | ||
|
||
if (entryJs) { | ||
results[libConfig.format!] = result[entryJs]!; | ||
} | ||
} | ||
|
||
return results; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.