Skip to content

Commit

Permalink
feat(bundle): support package exports (#74)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: support for package exports might cause unintended side effects, so to e safe we're marking this as a major. but hopefully it does not cause issues

* feat(bundle): support package exports
* chore: make js a commonjs by default
* chore: emit TS declarations
  • Loading branch information
P0lip authored May 13, 2021
1 parent 4fa0d4f commit de1cb96
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 121 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ USAGE
$ sl-scripts bundle
OPTIONS
--minify minify output using terser
--verbose moar logs
EXAMPLE
Expand Down
1 change: 0 additions & 1 deletion oclif.manifest.json

This file was deleted.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
},
"lint-staged": {
"*.{ts,tsx}$": [
"yarn lint.fix",
"git add"
"yarn lint.fix"
]
},
"husky": {
Expand All @@ -92,7 +91,8 @@
"@oclif/command": "1.5.19",
"@oclif/config": "1.14.0",
"@oclif/plugin-help": "2.2.3",
"@rollup/plugin-typescript": "^3.0.0",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.1.0",
"@semantic-release/commit-analyzer": "8.0.1",
"@semantic-release/git": "9.0.0",
"@semantic-release/github": "7.0.3",
Expand All @@ -107,11 +107,12 @@
"inquirer": "7.0.4",
"lint-staged": "10.0.7",
"rimraf": "3.0.2",
"rollup": "^1.31.1",
"rollup-plugin-terser": "^5.2.0",
"rollup": "^2.47.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"semantic-release": "17.0.3",
"shelljs": "0.8.x",
"tslib": "1.10.0"
"tslib": "^2.2.0"
},
"devDependencies": {
"@oclif/dev-cli": "1.22.2",
Expand Down
47 changes: 30 additions & 17 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const typescript = require('@rollup/plugin-typescript');
'use strict';
const typescript = require('rollup-plugin-typescript2');
const { terser } = require('rollup-plugin-terser');
const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const path = require('path');
const fs = require('fs');

const BASE_PATH = process.cwd();
const getConfigFile = (name) => {

const getConfigFile = name => {
const filePath = path.resolve(BASE_PATH, name);
if (fs.existsSync(path.resolve(BASE_PATH, name))) {
return filePath;
Expand All @@ -13,23 +17,32 @@ const getConfigFile = (name) => {
return path.resolve(BASE_PATH, 'node_modules', '@stoplight', 'scripts', name);
};

module.exports = {
input: path.resolve(BASE_PATH, 'src/index.ts'),
plugins: [
const plugins = () =>
[
typescript({
tsconfig: getConfigFile('tsconfig.build.json'),
include: ['src/**/*.{ts,tsx}'],
useTsconfigDeclarationDir: true,
}),
terser(),
],
output: [
{
file: path.resolve(BASE_PATH, 'dist/index.cjs.js'),
format: 'cjs'
process.env.MINIFY ? terser() : null,
].filter(Boolean);

module.exports = [
{
input: path.resolve(BASE_PATH, 'src/index.ts'),
output: {
entryFileNames: '[name].js',
dir: path.resolve(BASE_PATH, 'dist'),
format: 'cjs',
},
{
file: path.resolve(BASE_PATH, 'dist/index.es.js'),
format: 'esm'
plugins: [json(), commonjs(), ...plugins()],
},
{
input: path.resolve(BASE_PATH, 'src/index.ts'),
output: {
entryFileNames: '[name].mjs',
dir: path.resolve(BASE_PATH, 'dist'),
format: 'esm',
},
],
};
plugins: plugins(),
},
];
1 change: 1 addition & 0 deletions src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class BuildCommand extends Command {
'dependencies',
'dependenciesMeta',
'pkg',
'type',
]);

releasePkg.main = 'index.js';
Expand Down
37 changes: 33 additions & 4 deletions src/commands/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flags as flagHelpers } from '@oclif/command';
import { buildCommand, getConfigFilePath } from '../../utils';
import BuildCommand from '../build';

Expand All @@ -8,22 +9,50 @@ export default class BundleCommand extends BuildCommand {

public static examples = [`$ sl-scripts bundle`];

public static flags = {
minify: flagHelpers.boolean({
description: 'minify output using terser',
required: false,
default: false,
}),
verbose: flagHelpers.boolean({
description: 'moar logs',
required: false,
}),
};

protected get commands() {
const parsed = this.parse(BundleCommand);

return [
buildCommand(`rollup --config ${getConfigFilePath('rollup.config.js')}`, {
rawArgs: parsed.raw,
rawArgs: parsed.raw.map(rawArg => {
if (rawArg.type === 'flag' && rawArg.flag === 'minify') {
return {
type: 'arg',
input: '--environment MINIFY',
};
}

return rawArg;
}),
flags: Object.keys(BundleCommand.flags),
}),
buildCommand(`tsc --declaration --emitDeclarationOnly -p ${getConfigFilePath('tsconfig.build.json')}`),
buildCommand(`tsc -p tsconfig.build.json --emitDeclarationOnly --declaration --declarationDir dist`),
];
}

protected preparePackageJson() {
const pkg = super.preparePackageJson();
pkg.main = 'index.cjs.js';
pkg.module = 'index.es.js';
Object.assign(pkg, {
type: 'commonjs',
main: './index.js',
module: './index.mjs',
exports: {
require: './index.js',
import: './index.mjs',
},
});
return pkg;
}
}
Loading

0 comments on commit de1cb96

Please sign in to comment.