bunchee 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,7 +61,10 @@ Using pure ESM package?
61
61
  }
62
62
  ```
63
63
 
64
- Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using these package managers.
64
+ Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using these package managers. The output format will based on the exports condition and also the file extension. Given an example:
65
+
66
+ * It's CommonJS for `require` and ESM for `import` based on the exports condition.
67
+ * It's CommonJS for `.js` and ESM for `.mjs` based on the extension regardless the exports condition. Then for export condition like "node" you could choose the format with your extension.
65
68
 
66
69
  ## Configuration
67
70
 
@@ -121,7 +124,6 @@ bunchee --env=ENV1,ENV2,ENV3
121
124
 
122
125
  Replace `ENV1`, `ENV2`, and `ENV3` with the names of the environment variables you want to include in your bundled code. These environment variables will be inlined during the bundling process.
123
126
 
124
-
125
127
  ### Entry Files Convention
126
128
 
127
129
  While `exports` field is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command.
@@ -161,6 +163,35 @@ Then you need to add two entry files `index.ts` and `lite.ts` in project root di
161
163
  |- package.json
162
164
  ```
163
165
 
166
+ It will also look up for `index.<ext>` file under the directory having the name of the export path. For example, if you have `"./lite": "./dist/lite.js"` in exports field, then it will look up for `./lite/index.js` as the entry file as well.
167
+
168
+ ### Special Exports Conventions
169
+
170
+ For exports condition like `react-native`, `react-server` and `edge-light` as they're special platforms, they could have different exports or different code conditions. In this case bunchee provides an override input source file convention if you want to build them as different code bundle.
171
+
172
+ For instance:
173
+
174
+ ```json
175
+ {
176
+ "exports": {
177
+ "react-server": "./dist/react-server.mjs",
178
+ "edge-light": "./dist/edge-light.mjs",
179
+ "import": "./dist/index.mjs"
180
+ }
181
+ }
182
+ ```
183
+
184
+ You can use `index.<export-type>.<ext>` to override the input source file for specific export name. Or using `<export-path>/index.<export-type>.<ext>` also works. Such as:
185
+
186
+ ```
187
+ |- src/
188
+ |- index/.ts
189
+ |- index.react-server.ts
190
+ |- index.edge-light.ts
191
+ ```
192
+
193
+ This will match the export name `"react-server"` and `"edge-light"` then use the corresponding input source file to build the bundle.
194
+
164
195
  #### Package lint
165
196
 
166
197
  `bunchee` has support for checking the package bundles are matched with package exports configuration.
package/dist/cli.js CHANGED
@@ -97,7 +97,7 @@ function _fileExists() {
97
97
  return _fileExists.apply(this, arguments);
98
98
  }
99
99
 
100
- var version = "3.4.0";
100
+ var version = "3.5.0";
101
101
 
102
102
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
103
103
  try {
package/dist/index.js CHANGED
@@ -6,11 +6,11 @@ var rollup = require('rollup');
6
6
  var pluginWasm = require('@rollup/plugin-wasm');
7
7
  var rollupPluginSwc3 = require('rollup-plugin-swc3');
8
8
  var commonjs = require('@rollup/plugin-commonjs');
9
- var shebang = require('rollup-plugin-preserve-shebang');
10
9
  var json = require('@rollup/plugin-json');
11
10
  var pluginNodeResolve = require('@rollup/plugin-node-resolve');
12
11
  var replace = require('@rollup/plugin-replace');
13
12
  var prettyBytes = require('pretty-bytes');
13
+ var MagicString = require('magic-string');
14
14
  var module$1 = require('module');
15
15
 
16
16
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -18,10 +18,10 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
18
18
  var fs__default = /*#__PURE__*/_interopDefault(fs);
19
19
  var path__default = /*#__PURE__*/_interopDefault(path);
20
20
  var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
21
- var shebang__default = /*#__PURE__*/_interopDefault(shebang);
22
21
  var json__default = /*#__PURE__*/_interopDefault(json);
23
22
  var replace__default = /*#__PURE__*/_interopDefault(replace);
24
23
  var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
24
+ var MagicString__default = /*#__PURE__*/_interopDefault(MagicString);
25
25
 
26
26
  function chunkSizeCollector() {
27
27
  const sizes = new Map();
@@ -61,6 +61,38 @@ function chunkSizeCollector() {
61
61
  };
62
62
  }
63
63
 
64
+ function preserveDirectivePlugin() {
65
+ const directives = new Set();
66
+ return {
67
+ name: 'use-directive',
68
+ transform (code, id) {
69
+ const regex = /^(?:['"]use[^'"]+['"][^\n]*|#![^\n]*)/gm;
70
+ const replacedCode = code.replace(regex, (match)=>{
71
+ // replace double quotes with single quotes
72
+ directives.add(match.replace(/["]/g, "'"));
73
+ return '';
74
+ });
75
+ return {
76
+ code: replacedCode,
77
+ map: null
78
+ };
79
+ },
80
+ renderChunk (code, _, { sourcemap }) {
81
+ if (!directives.size) return null;
82
+ const s = new MagicString__default.default(code);
83
+ s.prepend(`${[
84
+ ...directives
85
+ ].join('\n')}\n`);
86
+ return {
87
+ code: s.toString(),
88
+ map: sourcemap ? s.generateMap({
89
+ hires: true
90
+ }) : null
91
+ };
92
+ }
93
+ };
94
+ }
95
+
64
96
  function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
65
97
  try {
66
98
  var info = gen[key](arg);
@@ -518,7 +550,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
518
550
  const hasSpecifiedTsTarget = Boolean((tsCompilerOptions == null ? void 0 : tsCompilerOptions.target) && tsConfigPath);
519
551
  const sizePlugin = sizeCollector.plugin(cwd);
520
552
  const commonPlugins = [
521
- shebang__default.default(),
553
+ preserveDirectivePlugin(),
522
554
  sizePlugin
523
555
  ];
524
556
  const plugins = (dts ? [
@@ -665,16 +697,21 @@ function _buildEntryConfig() {
665
697
  Object.keys(exportPaths).forEach(/*#__PURE__*/ _async_to_generator$2(function*(entryExport) {
666
698
  // TODO: improve the source detection
667
699
  const exportCond = exportPaths[entryExport];
668
- const hasEdgeLight = !!exportCond['edge-light'];
669
- const hasReactServer = !!exportCond['react-server'];
670
700
  const buildConfigs = [
671
701
  createBuildConfig('', exportCond) // default config
672
702
  ];
673
- if (hasEdgeLight) {
674
- buildConfigs.push(createBuildConfig('edge-light', exportCond));
675
- }
676
- if (hasReactServer) {
677
- buildConfigs.push(createBuildConfig('react-server', exportCond));
703
+ // For dts job, only build the default config.
704
+ // For assets job, build all configs.
705
+ if (!dts) {
706
+ if (exportCond['edge-light']) {
707
+ buildConfigs.push(createBuildConfig('edge-light', exportCond));
708
+ }
709
+ if (exportCond['react-server']) {
710
+ buildConfigs.push(createBuildConfig('react-server', exportCond));
711
+ }
712
+ if (exportCond['react-native']) {
713
+ buildConfigs.push(createBuildConfig('react-native', exportCond));
714
+ }
678
715
  }
679
716
  function createBuildConfig(exportType, exportCondRef) {
680
717
  return _createBuildConfig.apply(this, arguments);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": {
6
6
  "bunchee": "./dist/cli.js"
@@ -53,16 +53,16 @@
53
53
  "@swc/core": "1.3.46",
54
54
  "@swc/helpers": "0.5.0",
55
55
  "arg": "5.0.2",
56
+ "magic-string": "0.30.0",
56
57
  "pretty-bytes": "5.6.0",
57
58
  "publint": "0.1.11",
58
59
  "rollup": "3.20.2",
59
60
  "rollup-plugin-dts": "5.3.0",
60
- "rollup-plugin-preserve-shebang": "1.0.1",
61
61
  "rollup-plugin-swc3": "0.8.1",
62
62
  "tslib": "2.5.0"
63
63
  },
64
64
  "peerDependencies": {
65
- "typescript": ">=4.1.0 <5.0.0 || >=5.0.0 <6.0.0"
65
+ "typescript": "^4.1 || ^5.0"
66
66
  },
67
67
  "peerDependenciesMeta": {
68
68
  "typescript": {