Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Simply create a `jest.config.js` file in the root of your project, and extend th
```js
// ./jest.config.js
module.exports = {
preset: "@stoplight/scripts"
preset: "@stoplight/scripts",
};
```

Expand Down Expand Up @@ -187,3 +187,17 @@ Simply add a `release` property to your `package.json` file. For example:
// ... props
}
```

## Rollup

By default all `dependencies` and `peerDependencies` declared in your `package.json` will be treated as external deps during `sl-scripts bundle`. If you would like to always bundle a dep, list those deps in your `package.json` file like so:

```json
{
"name": "your-package",
"version": "0.0.0",
"rollup": {
"bundleDeps": ["dep-1", "dep-2"]
}
}
```
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"/bin",
"/dist",
"/templates",
"/vendor",
"/npm-shrinkwrap.json",
"/oclif.manifest.json",
"release.js",
Expand Down Expand Up @@ -93,6 +94,8 @@
"@oclif/plugin-help": "2.2.3",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/pluginutils": "4.x",
"@semantic-release/commit-analyzer": "8.0.1",
"@semantic-release/git": "9.0.0",
"@semantic-release/github": "7.0.3",
Expand All @@ -102,10 +105,12 @@
"commitizen": "4.0.3",
"cz-conventional-changelog": "3.1.x",
"esm": "^3.2.25",
"estree-walker": "2.x",
"find-up": "^4.1.0",
"husky": "4.2.3",
"inquirer": "7.0.4",
"lint-staged": "10.0.7",
"magic-string": "0.25.x",
"rimraf": "3.0.2",
"rollup": "^2.47.0",
"rollup-plugin-terser": "^7.0.2",
Expand Down
18 changes: 16 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const commonjs = require('@rollup/plugin-commonjs');
const json = require('@rollup/plugin-json');
const path = require('path');
const fs = require('fs');
const { nodeResolve } = require('@rollup/plugin-node-resolve');

const { optimizeLodashImports } = require('./vendor/rollup-plugin-optimize-lodash-imports/rollup-plugin/dist/index.js');

const BASE_PATH = process.cwd();

Expand All @@ -23,6 +26,9 @@ const plugins = () =>
tsconfig: getConfigFile('tsconfig.build.json'),
useTsconfigDeclarationDir: true,
}),
nodeResolve(),
commonjs(),
optimizeLodashImports(),
process.env.MINIFY ? terser() : null,
].filter(Boolean);

Expand All @@ -31,7 +37,15 @@ const dependencies = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.peerDependencies || {}),
];
const external = module => dependencies.some(dep => module === dep || module.startsWith(`${dep}/`));
const rollupConfig = packageJson.rollup || {};
const alwaysInclude = rollupConfig.bundleDeps || [];

const external = module => {
const isDefaultExternal = dependencies.some(dep => module === dep || module.startsWith(`${dep}/`));
const forceBundle = alwaysInclude.some(dep => module === dep || module.startsWith(`${dep}/`));

return isDefaultExternal && !forceBundle;
};

module.exports = [
{
Expand All @@ -41,7 +55,7 @@ module.exports = [
dir: path.resolve(BASE_PATH, 'dist'),
format: 'cjs',
},
plugins: [json(), commonjs(), ...plugins()],
plugins: [json(), ...plugins()],
external
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Plugin } from "rollup";
import { FilterPattern } from "@rollup/pluginutils";
export declare type OptimizeLodashOptions = {
/**
* A minimatch pattern, or array of patterns, of files that should be
* processed by this plugin (if omitted, all files are included by default)
*/
include?: FilterPattern;
/**
* Files that should be excluded, if `include` is otherwise too permissive.
*/
exclude?: FilterPattern;
/**
* Changes *all* lodash imports (but not lodash/fp imports!) to 'lodash-es' imports.
* Don't use this for CommonJS outputs, the plugin will error should you do so.
*/
useLodashEs?: true;
};
/**
* Converts lodash imports to be specific, enabling better tree-shaking:
*
* `import { isNil } from "lodash";` -> `import { isNil } from "lodash/isNil";`
*
* Note that only specific named imports are supported, unlike babel-plugin-lodash. For example,
* this plugin will print a warning for this import and make no changes to the import:
*
* `import _ from "lodash";`
*
* Optionally, set `useLodashEs` to true and `lodash` imports will be converted to `lodash-es`
* imports. Note that it's up to user to include the `lodash-es` module and ensure the output
* is set to some form of `es` (other output formats will error). An example:
*
* `import { isNil } from "lodash";` -> `import { isNil } from "lodash-es";`
*
* @param include files/globs to include with this plugin (optional)
* @param exclude files/globs to exclude from this plugin (optional)
* @param useLodashEs set `true` to convert imports to use "lodash-es" (optional; default false)
*/
export declare function optimizeLodashImports({ include, exclude, useLodashEs, }?: OptimizeLodashOptions): Plugin & Required<Pick<Plugin, "transform">>;
//# sourceMappingURL=index.d.ts.map

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"name": "@optimize-lodash/rollup-plugin",
"version": "2.1.0",
"description": "Rewrite lodash imports with Rollup for improved tree-shaking.",
"keywords": [
"lodash",
"rollup",
"rollup-plugin",
"optimize",
"minify"
],
"homepage": "https://github.com/kyle-johnson/rollup-plugin-optimize-lodash-imports/tree/main/packages/rollup-plugin",
"repository": {
"type": "git",
"url": "https://github.com/kyle-johnson/rollup-plugin-optimize-lodash-imports.git"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"author": "Kyle Johnson",
"license": "MIT",
"engines": {
"node": ">= 12"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"type-check": "tsc --noEmit",
"lint": "eslint .",
"test": "jest",
"test:ci": "jest --coverage --ci",
"build": "rm -rf dist && tsc -p tsconfig.dist.json",
"format": "prettier --write .",
"format:check": "prettier --check .",
"depcheck": "depcheck"
},
"jest": {
"coverageDirectory": "coverage",
"testEnvironment": "node",
"preset": "ts-jest",
"globals": {
"ts-jest": {}
},
"testTimeout": 10000
},
"peerDependencies": {
"rollup": "2.x"
},
"devDependencies": {
"@rollup/plugin-commonjs": "21.0.1",
"@rollup/plugin-node-resolve": "13.0.6",
"@tsconfig/node12": "1.0.9",
"@types/estree": "0.0.50",
"@types/jest": "27.0.3",
"@types/lodash": "4.14.177",
"@types/node": "12.20.37",
"@typescript-eslint/eslint-plugin": "5.4.0",
"@typescript-eslint/parser": "5.4.0",
"depcheck": "1.4.2",
"eslint": "8.2.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-jest": "25.2.4",
"eslint-plugin-unicorn": "38.0.1",
"jest": "27.3.1",
"lodash": "4.17.21",
"prettier": "2.4.1",
"rollup": "2.59.0",
"rollup-plugin-terser": "7.0.2",
"ts-jest": "27.0.7",
"typescript": "4.3.5"
},
"dependencies": {
"@optimize-lodash/transform": "workspace:2.x",
"@rollup/pluginutils": "4.x"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { BaseNode, ImportDeclaration, ImportSpecifier, Program } from "estree";
export declare function isImportDeclaration(node: BaseNode): node is ImportDeclaration;
export declare function isProgram(node: BaseNode): node is Program;
export declare function isImportSpecifierArray(items: ImportDeclaration["specifiers"]): items is Array<ImportSpecifier>;
//# sourceMappingURL=guards.d.ts.map

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Node } from "acorn";
import { SourceMap } from "magic-string";
declare module "estree" {
interface BaseNodeWithoutComments {
start: number;
end: number;
}
}
export declare type UNCHANGED = null;
export declare const UNCHANGED: null;
export interface CodeWithSourcemap {
code: string;
map: SourceMap;
}
export declare type ParseFunction = (code: string) => Node;
export declare type WarnFunction = (message: string) => void;
export declare function transform({ code, id, parse, warn, useLodashEs, }: {
code: string;
id: string;
parse: ParseFunction;
warn?: WarnFunction;
useLodashEs?: true;
}): CodeWithSourcemap | UNCHANGED;
//# sourceMappingURL=index.d.ts.map

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

Loading