-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support js autoExtension (#28)
- Loading branch information
1 parent
2c2743e
commit 5f38246
Showing
19 changed files
with
235 additions
and
49 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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { expect, test } from 'vitest'; | ||
import { buildAndGetResults } from '#shared'; | ||
import { buildAndGetEntryJsResults } from '#shared'; | ||
|
||
test('alias should work', async () => { | ||
const fixturePath = __dirname; | ||
const { entries } = await buildAndGetResults(fixturePath); | ||
const { contents } = await buildAndGetEntryJsResults(fixturePath); | ||
|
||
expect(entries.esm).toContain('hello world'); | ||
expect(entries.cjs).toContain('hello world'); | ||
expect(contents.esm).toContain('hello world'); | ||
expect(contents.cjs).toContain('hello world'); | ||
|
||
// simple artifacts check | ||
expect(entries.esm).toMatchSnapshot(); | ||
expect(entries.cjs).toMatchSnapshot(); | ||
expect(contents.esm).toMatchSnapshot(); | ||
expect(contents.cjs).toMatchSnapshot(); | ||
}); |
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,17 @@ | ||
import { extname, join } from 'node:path'; | ||
import { expect, test } from 'vitest'; | ||
import { buildAndGetEntryJsResults } from '#shared'; | ||
|
||
test('autoExtension generate .mjs in build artifacts with esm format when type is commonjs', async () => { | ||
const fixturePath = join(__dirname, 'type-commonjs'); | ||
const { files } = await buildAndGetEntryJsResults(fixturePath); | ||
expect(extname(files.esm!)).toEqual('.mjs'); | ||
expect(extname(files.cjs!)).toEqual('.js'); | ||
}); | ||
|
||
test('autoExtension generate .cjs in build artifacts with cjs format when type is module', async () => { | ||
const fixturePath = join(__dirname, 'type-module'); | ||
const { files } = await buildAndGetEntryJsResults(fixturePath); | ||
expect(extname(files.esm!)).toEqual('.js'); | ||
expect(extname(files.cjs!)).toEqual('.cjs'); | ||
}); |
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 @@ | ||
{ | ||
"name": "auto-extension-commonjs-test", | ||
"version": "1.0.0", | ||
"private": true | ||
} |
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,22 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { | ||
generateBundleCjsConfig, | ||
generateBundleEsmConfig, | ||
} from '../../../scripts/shared'; | ||
|
||
export default defineConfig({ | ||
lib: [ | ||
generateBundleEsmConfig(__dirname, { | ||
autoExtension: true, | ||
}), | ||
generateBundleCjsConfig(__dirname, { | ||
autoExtension: true, | ||
}), | ||
], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.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 @@ | ||
export const a = 1; |
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 './common'; | ||
|
||
console.log(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,6 @@ | ||
{ | ||
"name": "auto-extension-module-test", | ||
"version": "1.0.0", | ||
"private": true, | ||
"type": "module" | ||
} |
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,22 @@ | ||
import { join } from 'node:path'; | ||
import { defineConfig } from '@rslib/core'; | ||
import { | ||
generateBundleCjsConfig, | ||
generateBundleEsmConfig, | ||
} from '../../../scripts/shared'; | ||
|
||
export default defineConfig({ | ||
lib: [ | ||
generateBundleEsmConfig(__dirname, { | ||
autoExtension: true, | ||
}), | ||
generateBundleCjsConfig(__dirname, { | ||
autoExtension: true, | ||
}), | ||
], | ||
source: { | ||
entry: { | ||
main: join(__dirname, 'src/index.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 @@ | ||
export const a = 1; |
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 './common'; | ||
|
||
console.log(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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { expect, test } from 'vitest'; | ||
import { buildAndGetResults } from '#shared'; | ||
import { buildAndGetEntryJsResults } from '#shared'; | ||
|
||
test('define should work', async () => { | ||
const fixturePath = __dirname; | ||
const { entries } = await buildAndGetResults(fixturePath); | ||
const { contents } = await buildAndGetEntryJsResults(fixturePath); | ||
|
||
expect(entries.esm).not.toContain('console.info(VERSION)'); | ||
expect(entries.esm).toContain('1.0.0'); | ||
expect(contents.esm).not.toContain('console.info(VERSION)'); | ||
expect(contents.esm).toContain('1.0.0'); | ||
|
||
expect(entries.cjs).not.toContain('console.info(VERSION)'); | ||
expect(entries.cjs).toContain('1.0.0'); | ||
expect(contents.cjs).not.toContain('console.info(VERSION)'); | ||
expect(contents.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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { join } from 'node:path'; | ||
import { expect, test } from 'vitest'; | ||
import { buildAndGetResults } from '#shared'; | ||
import { buildAndGetEntryJsResults } from '#shared'; | ||
|
||
test('should fail when platform is not "node"', async () => { | ||
const fixturePath = join(__dirname); | ||
const build = buildAndGetResults(fixturePath); | ||
const build = buildAndGetEntryJsResults(fixturePath); | ||
await expect(build).rejects.toThrowError('Rspack build failed!'); | ||
}); |
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,24 +1,24 @@ | ||
import { join } from 'node:path'; | ||
import { expect, test } from 'vitest'; | ||
import { buildAndGetResults } from '#shared'; | ||
import { buildAndGetEntryJsResults } from '#shared'; | ||
|
||
test('auto externalize Node.js built-in modules when platform is "node"', async () => { | ||
const fixturePath = join(__dirname); | ||
const { entries } = await buildAndGetResults(fixturePath); | ||
const { contents } = await buildAndGetEntryJsResults(fixturePath); | ||
|
||
for (const external of [ | ||
'import * as __WEBPACK_EXTERNAL_MODULE_fs__ from "fs"', | ||
'import * as __WEBPACK_EXTERNAL_MODULE_node_assert__ from "node:assert"', | ||
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"', | ||
]) { | ||
expect(entries.esm).toContain(external); | ||
expect(contents.esm).toContain(external); | ||
} | ||
|
||
for (const external of [ | ||
'var external_fs_namespaceObject = require("fs");', | ||
'var external_node_assert_namespaceObject = require("node:assert");', | ||
'var external_react_namespaceObject = require("react");', | ||
]) { | ||
expect(entries.cjs).toContain(external); | ||
expect(contents.cjs).toContain(external); | ||
} | ||
}); |
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
Oops, something went wrong.