From 07c375f82e1a441f199470c6972de269b8cf5f53 Mon Sep 17 00:00:00 2001 From: Cameron Ackerman Date: Mon, 11 May 2020 10:18:00 -0700 Subject: [PATCH] Bugfix/fix bundles (#3673) * fix index.js main bundles to be commonjs instead of modules * Change rm -rf to rimraf to be cross os compatible * add rimraf as dependency Co-authored-by: damareyoh --- config/rollup/rollup.config.js | 36 +++++++++++++++++++++++++--------- package.json | 3 ++- yarn.lock | 7 +++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/config/rollup/rollup.config.js b/config/rollup/rollup.config.js index 5a89d3bd3..33506493d 100644 --- a/config/rollup/rollup.config.js +++ b/config/rollup/rollup.config.js @@ -22,6 +22,7 @@ function configure(pkg, env, target) { const isProd = env === 'production' const isUmd = target === 'umd' const isModule = target === 'module' + const isCommonJs = target === 'cjs' const input = `packages/${pkg.name}/src/index.ts` const deps = [] .concat(pkg.dependencies ? Object.keys(pkg.dependencies) : []) @@ -103,7 +104,7 @@ function configure(pkg, env, target) { ], modules: false, targets: { - esmodules: true, + esmodules: isModule, }, }, ], @@ -116,7 +117,7 @@ function configure(pkg, env, target) { ? {} : { regenerator: false, - useESModules: true, + useESModules: isModule, }, ], '@babel/plugin-proposal-class-properties', @@ -147,6 +148,28 @@ function configure(pkg, env, target) { } } + if (isCommonJs) { + return { + plugins, + input, + onwarn, + output: [ + { + file: `packages/${pkg.name}/${pkg.main}`, + format: 'cjs', + exports: 'named', + sourcemap: true, + }, + ], + // We need to explicitly state which modules are external, meaning that + // they are present at runtime. In the case of non-UMD configs, this means + // all non-Slate packages. + external: id => { + return !!deps.find(dep => dep === id || id.startsWith(`${dep}/`)) + }, + } + } + if (isModule) { return { plugins, @@ -157,13 +180,7 @@ function configure(pkg, env, target) { file: `packages/${pkg.name}/${pkg.module}`, format: 'es', sourcemap: true, - }, - { - file: `packages/${pkg.name}/${pkg.main}`, - format: 'cjs', - exports: 'named', - sourcemap: true, - }, + } ], // We need to explicitly state which modules are external, meaning that // they are present at runtime. In the case of non-UMD configs, this means @@ -182,6 +199,7 @@ function configure(pkg, env, target) { function factory(pkg, options = {}) { const isProd = process.env.NODE_ENV === 'production' return [ + configure(pkg, 'development', 'cjs', options), configure(pkg, 'development', 'module', options), isProd && configure(pkg, 'development', 'umd', options), isProd && configure(pkg, 'production', 'umd', options), diff --git a/package.json b/package.json index 944ff330a..42dcd5af4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "yarn build:rollup && yarn build:next", "build:next": "cd ./site && next build && next export", "build:rollup": "rollup --config ./config/rollup/rollup.config.js", - "clean": "rm -rf ./node_modules ./packages/*/{dist,lib,node_modules} ./site/{.next,out}", + "clean": "rimraf ./node_modules ./packages/*/{dist,lib,node_modules} ./site/{.next,out}", "fix": "yarn fix:prettier && yarn fix:eslint", "fix:eslint": "yarn lint:eslint --fix", "fix:prettier": "yarn lint:prettier --write", @@ -69,6 +69,7 @@ "react-error-boundary": "^1.2.5", "react-router-dom": "^5.1.2", "react-values": "^0.3.0", + "rimraf": "^3.0.2", "rollup": "^1.27.5", "rollup-plugin-babel": "^4.3.3", "rollup-plugin-commonjs": "^10.1.0", diff --git a/yarn.lock b/yarn.lock index 1c8cf3ab3..773ee17a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9629,6 +9629,13 @@ rimraf@2.6.3: dependencies: glob "^7.1.3" +rimraf@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a" + integrity sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA== + dependencies: + glob "^7.1.3" + ripemd160@^2.0.0, ripemd160@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"