From b6177a385e91eaca96f89325c380934f364f6030 Mon Sep 17 00:00:00 2001 From: atanasster Date: Wed, 12 Feb 2020 03:19:40 -0500 Subject: [PATCH] fix: generate esm modules for storybook addons --- integrations/storybook/rollup.config.js | 87 ++++++++++++++++++++++++- integrations/storybook/tsconfig.json | 9 +-- package.json | 2 +- yarn.lock | 2 +- 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/integrations/storybook/rollup.config.js b/integrations/storybook/rollup.config.js index 21f331a21..903c72700 100644 --- a/integrations/storybook/rollup.config.js +++ b/integrations/storybook/rollup.config.js @@ -1,4 +1,89 @@ -import { config } from 'docz-rollup'; +const { omit } = require('lodash/fp'); +const typescript = require('rollup-plugin-typescript2'); +const babel = require('rollup-plugin-babel'); +const json = require('rollup-plugin-json'); +const commonjs = require('rollup-plugin-commonjs'); +const nodeResolve = require('rollup-plugin-node-resolve'); +const alias = require('rollup-plugin-alias'); +const progress = require('rollup-plugin-progress'); + +const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs']; +const omitOpts = omit([ + 'alias', + 'external', + 'output', + 'plugins', + 'runtimeHelpers', + 'filename', +]); + +const defaultExternal = id => + !id.startsWith('\0') && + !id.startsWith('~') && + !id.startsWith('.') && + !id.startsWith(process.platform === 'win32' ? process.cwd() : '/'); + +const createOutput = (dir = 'dist', defaultOpts) => { + const opts = omitOpts(defaultOpts); + const { + alias: moduleAlias, + external, + output, + plugins = [], + runtimeHelpers, + filename, + } = defaultOpts; + + const defaultPlugins = [ + Object.keys(moduleAlias || {}).length > 0 && + alias({ + ...moduleAlias, + resolve: EXTENSIONS, + }), + nodeResolve({ + mainFields: ['module', 'main'], + browser: true, + }), + commonjs({ + include: /\/node_modules\//, + }), + json(), + typescript({ + typescript: require('typescript'), + rollupCommonJSResolveHack: true, + }), + babel({ + runtimeHelpers, + extensions: EXTENSIONS, + exclude: 'node_modules/**', + }), + progress({ + clearLine: false, + }), + ]; + + const outputs = [ + { + dir, + format: 'esm', + chunkFileNames: filename ? `${filename}.js` : `[name].js`, + entryFileNames: filename ? `${filename}.js` : `[name].js`, + ...output, + }, + ]; + + return { + ...opts, + external: external || defaultExternal, + plugins: defaultPlugins.filter(Boolean).concat(plugins), + output: outputs, + }; +}; + +const config = opts => { + const inputs = Array.isArray(opts) ? opts : [opts]; + return inputs.map(({ dest: dir, ...opts }) => createOutput(dir, opts)); +}; export default config({ input: [ diff --git a/integrations/storybook/tsconfig.json b/integrations/storybook/tsconfig.json index 25ce695d2..0fc3660db 100644 --- a/integrations/storybook/tsconfig.json +++ b/integrations/storybook/tsconfig.json @@ -2,12 +2,13 @@ "extends": "../../tsconfig.json", "compilerOptions": { "module": "esnext", - "outDir": "dist", - "rootDir": "src", "declaration": true, - "types": ["node", "jest"], + "sourceMap": false, + "outDir": "./dist", + "rootDir": "./src", + "baseUrl": "./", "typeRoots": ["../../node_modules/@types", "node_modules/@types"] }, - "include": ["src/**/*", "src/typings.d.ts"], + "include": ["src/**/*"], "exclude": ["node_modules/**"] } \ No newline at end of file diff --git a/package.json b/package.json index 612c2d9d3..78dc3a4f7 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "lint-staged": "^9.2.0", "npm-run-all": "^4.1.5", "prettier": "^1.18.2", - "rollup": "^1.17.0", + "rollup": "^1.31.0", "storybook-addon-deps": "^1.0.11", "storybook-dark-mode": "^0.3.0", "trash-cli": "^3.0.0", diff --git a/yarn.lock b/yarn.lock index a13524555..4185de543 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14235,7 +14235,7 @@ rollup-pluginutils@^2.0.1, rollup-pluginutils@^2.5.0, rollup-pluginutils@^2.8.1: dependencies: estree-walker "^0.6.1" -rollup@^1.17.0: +rollup@^1.17.0, rollup@^1.31.0: version "1.31.0" resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.31.0.tgz#e2a87212e96aa7850f3eb53fdd02cf89f2d2fe9a" integrity sha512-9C6ovSyNeEwvuRuUUmsTpJcXac1AwSL1a3x+O5lpmQKZqi5mmrjauLeqIjvREC+yNRR8fPdzByojDng+af3nVw==