Skip to content

Commit

Permalink
feat!: add optional --main for build to include "main" (mikeal#8)
Browse files Browse the repository at this point in the history
Don't include "main" in package.json by default, but include it if --main is
passed.
  • Loading branch information
rvagg authored Mar 11, 2021
1 parent f5e35fe commit 729ffbf
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const run = async opts => {
pkg.dist = dist
return pkg
}
run.schema = { tests: false }
run.schema = {
tests: false,
main: false
}

export default run
9 changes: 7 additions & 2 deletions src/package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const { writeFile, mkdir, unlink, readdir, readFile, copyFile } = fs
const plugins = [preserveShebangs.preserveShebangs()]

class Package {
constructor ({ cwd, hooks, tests }) {
constructor ({ cwd, hooks, tests, main }) {
this.cwd = cwd
this.hooks = hooks || {}
this.parsed = this.parse()
this.files = new Map()
this.testFiles = new Map()
this.includeTests = tests
this.includeMain = main
}

file (url) {
Expand Down Expand Up @@ -181,7 +182,11 @@ class Package {
const json = copy(this.pkgjson)

delete json.type
json.main = `./${join('./cjs', json.main || './index.js')}`
if (this.includeMain) {
json.main = `./${join('./cjs', json.main || './index.js')}`
} else {
delete json.main
}
json.browser = {}
json.exports = {}
const _join = (...args) => './' + join(...args)
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/cjs/src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var sub = require('./sub.js');
var browser = require('./sub/browser.js');



exports.mod = sub;
exports.sub = browser;
11 changes: 11 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/cjs/src/deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var sub = require('./sub.js');
var index = require('./sub/index.js');



exports.mod = sub;
exports.sub = index;
11 changes: 11 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/cjs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var sub = require('./sub.js');
var index = require('./sub/index.js');



exports.mod = sub;
exports.sub = index;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var secondary = 'secondary';

module.exports = secondary;
5 changes: 5 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/cjs/src/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var sub = 'sub';

module.exports = sub;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var browser = 'browser';

module.exports = browser;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var index = 'import';

module.exports = index;
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-main/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type" : "module" }
6 changes: 6 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/esm/src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mod from './sub.js';
import sub from './sub/browser.js';
export {
mod,
sub
};
6 changes: 6 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/esm/src/deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mod from './sub.js';
import sub from './sub/index.js';
export {
mod,
sub
};
6 changes: 6 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/esm/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mod from './sub.js';
import sub from './sub/index.js';
export {
mod,
sub
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'secondary';
1 change: 1 addition & 0 deletions test/fixtures/pkg-kitchensink/output-main/esm/src/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'sub';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'browser';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'import';
28 changes: 28 additions & 0 deletions test/fixtures/pkg-kitchensink/output-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "pkg-kitchensink",
"version": "0.0.0",
"description": "",
"main": "./cjs/src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Mikeal Rogers <[email protected]> (https://www.mikealrogers.com/)",
"license": "(Apache-2.0 AND MIT)",
"exports": {
".": {
"browser": "./esm/src/browser.js",
"require": "./cjs/src/index.js",
"import": "./esm/src/index.js"
},
"./secondary": {
"browser": "./esm/src/secondary.js",
"require": "./cjs/src/secondary.js",
"import": "./esm/src/secondary.js"
}
},
"browser": {
".": "./cjs/src/browser.js",
"./secondary": "./cjs/src/secondary.js"
}
}
1 change: 0 additions & 1 deletion test/fixtures/pkg-kitchensink/output-notests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "pkg-kitchensink",
"version": "0.0.0",
"description": "",
"main": "./cjs/src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/pkg-kitchensink/output-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "pkg-kitchensink",
"version": "0.0.0",
"description": "",
"main": "./cjs/src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
9 changes: 9 additions & 0 deletions test/test-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ export default async test => {
}
*/
})

test('pkg-kitchensink w/ main', async test => {
const dist = pathToFileURL(await tempy.directory())
test.after(() => rmtree(fileURLToPath(dist)))
const opts = { cwd, dist, main: true }
await build(opts)
await verify(new URL('./output-main', url), dist)
await verify(dist, new URL('./output-main', url))
})
}

0 comments on commit 729ffbf

Please sign in to comment.