Skip to content

Commit

Permalink
Merge branch 'main' into bin
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 3, 2023
2 parents aaf6c03 + 0179926 commit 5d0b1c7
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using th
- Output (`-o <file>`): Specify output filename.
- Format (`-f <format>`): Set output format (default: `'esm'`).
- External (`--external <dep,>`): Specifying extra external dependencies, by default it is the list of `dependencies` and `peerDependencies` from `package.json`. Values are separate by comma.
- Target (`--target <target>`): Set ECMAScript target (default: `'es2016'`).
- Target (`--target <target>`): Set ECMAScript target (default: `'es2015'`).
- Runtime (`--runtime <runtime>`): Set build runtime (default: `'browser'`).
- Environment (`--env <env,>`): Define environment variables. (default: `NODE_ENV`, separate by comma)
- Working Directory (`--cwd <cwd>`): Set current working directory where containing `package.json`.
Expand Down Expand Up @@ -308,7 +308,7 @@ await bundle(path.resolve('./src/index.ts'), {
sourcemap: false, // Boolean
external: [], // string[]
format: 'esm', // 'esm' | 'cjs'
target: 'es2016', // ES syntax target
target: 'es2015', // ES syntax target
runtime: 'nodejs', // 'browser' | 'nodejs'
cwd: process.cwd(), // string
})
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@
"license": "MIT",
"dependencies": {
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-esm-shim": "^0.1.5",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-wasm": "^6.1.3",
"@rollup/pluginutils": "^5.0.4",
"@swc/core": "^1.3.93",
"@swc/core": "^1.3.99",
"@swc/helpers": "^0.5.0",
"arg": "^5.0.2",
"pretty-bytes": "^5.6.0",
"publint": "~0.2.2",
"rollup": "^3.28.1",
"rollup-plugin-dts": "^6.0.1",
"rollup-plugin-swc3": "^0.10.3",
"rollup-swc-preserve-directives": "^0.5.0",
"rollup-preserve-directives": "^1.0.0",
"tslib": "^2.5.0"
},
"peerDependencies": {
Expand Down
120 changes: 68 additions & 52 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import esmShim from '@rollup/plugin-esm-shim'
import { sizeCollector } from './plugins/size-plugin'
import { inlineCss } from './plugins/inline-css'
import swcPreserveDirectivePlugin from 'rollup-swc-preserve-directives'
import preserveDirectives from 'rollup-preserve-directives'
import {
getTypings,
getExportPaths,
Expand Down Expand Up @@ -170,7 +171,7 @@ async function buildInputConfig(
: [
...commonPlugins,
inlineCss({ exclude: /node_modules/ }),
swcPreserveDirectivePlugin(),
preserveDirectives(),
replace({
values: getBuildEnv(options.env || []),
preventAssignment: true,
Expand All @@ -190,6 +191,7 @@ async function buildInputConfig(
tsconfig: tsConfigPath,
...swcOptions,
}),
esmShim()
]
).filter(isNotNull<Plugin>)

Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Options:
-h, --help output usage information
--external <mod> specify an external dependency, separate by comma
--no-external do not bundle external dependencies
--target <target> js features target: swc target es versions. default: es2016
--target <target> js features target: swc target es versions. default: es2015
--runtime <runtime> build runtime (nodejs, browser). default: browser
--env <env> inlined process env variables, separate by comma. default: NODE_ENV
--cwd <cwd> specify current working directory
Expand Down
23 changes: 23 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,29 @@ const testCases: {
expect(await fs.readFile(distFiles[0], 'utf-8')).toContain(
'#!/usr/bin/env node',
)
}
},
{
name: 'esm-shims',
args: [],
async expected(dir) {
const shimsCode = [
'const __filename = cjsUrl.fileURLToPath(import.meta.url)',
'const __dirname = cjsPath.dirname(__filename)',
'const require = cjsModule.createRequire(import.meta.url)',
]
const esmOutput = await fs.readFile(join(dir, './dist/index.mjs'), 'utf-8')
const cjsOutput = await fs.readFile(join(dir, './dist/index.cjs'), 'utf-8')
expect(
shimsCode.every((code) => esmOutput.includes(code)),
).toBe(true)
expect(
shimsCode.map((code) => cjsOutput.includes(code)),
).toEqual([false, false, false])
// for import.meta.url, should use pathToFileURL + URL polyfill
expect(cjsOutput).toContain('pathToFileURL')
expect(cjsOutput).toContain('new URL')
expect(cjsOutput).not.toContain('import.meta.url')
},
},
]
Expand Down
8 changes: 8 additions & 0 deletions test/integration/esm-shims/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}
3 changes: 3 additions & 0 deletions test/integration/esm-shims/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(__dirname)
console.log(__filename)
console.log(import.meta.url)
2 changes: 1 addition & 1 deletion test/typing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('types', () => {
sourcemap: false,
external: [],
format: 'esm',
target: 'es2016',
target: 'es2015',
runtime: 'nodejs',
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var l;const o={bar:"hello"};console.log("bar",null==o?void 0:o.bar);const a=null!=(l=o.bar)?l:"default";console.log("name",a);
var l;const o={bar:"hello"};console.log("bar",null==o?void 0:o.bar),console.log("name",null!=(l=o.bar)?l:"default");
2 changes: 1 addition & 1 deletion test/unit/module/__snapshot__/module.min.js.snapshot
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class s{f(){return 1}}const e=new class extends s{get x(){return super.f()}constructor(){super()}};console.log("main",e.x);
class e{f(){return 1}}console.log("main",new class extends e{get x(){return super.f()}constructor(){super()}}().x);

0 comments on commit 5d0b1c7

Please sign in to comment.