Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: added suggestion for rollup config #9201

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
build(rollup): added suggestion for rollup config
for the gatsby package
  • Loading branch information
travi committed Oct 18, 2018
commit 7405585f5a98231c3391dda18d8425300f9dcde8
4 changes: 2 additions & 2 deletions .babel-preset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const r = m => require.resolve(m)

function preset(context, options = {}) {
const { browser = false, debug = false } = options
const { browser = false, debug = false, modules = 'commonjs' } = options
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the output of the transpilation during the rollup build needs to preserve the es-module syntax so that rollup can properly do the rest of its processing. the default retains the previous behavior, but this allows overriding how the modules are handled.

const { NODE_ENV, BABEL_ENV } = process.env

const PRODUCTION = (BABEL_ENV || NODE_ENV) === "production"
Expand Down Expand Up @@ -31,7 +31,7 @@ function preset(context, options = {}) {
debug: !!debug,
useBuiltIns: "entry",
shippedProposals: true,
modules: "commonjs",
modules,
},
browser ? browserConfig : nodeConfig
),
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@
"cross-env": "^5.1.4",
"lerna": "^2.9.0",
"nyc": "^7.0.0",
"rimraf": "^2.6.1"
"rimraf": "^2.6.1",
"rollup": "^0.66.6",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤘

"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-babel": "^4.0.3"
},
"engines": {
"node": ">6.0.0"
Expand Down Expand Up @@ -179,6 +182,7 @@
"build:rawfiles": "copyfiles -u 1 src/internal-plugins/**/raw_* dist",
"build:cjs": "babel cache-dir --out-dir cache-dir/commonjs --ignore **/__tests__",
"build:src": "babel src --out-dir dist --source-maps --ignore **/gatsby-cli.js,**/raw_*,**/__tests__",
"build:rollup": "rollup -c",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is integrated, the build:cjs script above could be removed

"clean-test-bundles": "find test/ -type f -name bundle.js* -exec rm -rf {} +",
"prepare": "cross-env NODE_ENV=production npm run build",
"test-coverage": "node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
Expand Down
18 changes: 18 additions & 0 deletions packages/gatsby/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import babel from 'rollup-plugin-babel';
import autoExternal from 'rollup-plugin-auto-external';

export default {
input: 'cache-dir/gatsby-browser-entry.js',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail, unfortunately. We want the files to remain unbundled so that we can conditionally require certain files.

For instance, if we take cache-dir/public-page-renderer we can see the various conditional require statements.

Bundling this for the browser honestly just wouldn't work, I don't think, because it'll trace those dependencies, exclude conditional requires that we'd want in certain phases, and then create a (smaller, but not as functional) bundle file.

Copy link
Contributor Author

@travi travi Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair point. i'm not familiar enough with the approach, so this is some of the detail i was hoping to tease out. there could possibly still be a way to accomplish as a bundle (or multiple), but that could be more of an investment than its worth at this point.

maybe the better approach would be simple transpilation then, similar to what was done for the common js version, but with modules set to false for @babel/env.

plugins: [
autoExternal(),
babel({
presets: [[require(`path`).join(__dirname, `..`, `..`, `.babel-preset.js`), {modules: false}]],
runtimeHelpers: true,
exclude: ['./node_modules/**']
})
],
output: [
{file: 'lib/index.cjs.js', format: 'cjs', sourcemap: true},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the files produced here are the ones that should be referenced as main and module in the package.json.

the directory and file names can certainly be adjusted to fit your conventions, but i started with these to demonstrate the mechanics to start with.

{file: 'lib/index.es.js', format: 'es', sourcemap: true}
]
};
51 changes: 51 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4197,6 +4197,13 @@ builtins@^1.0.3:
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"
integrity sha1-y5T662HIaWRR2zZTThQi+U8K7og=

builtins@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/builtins/-/builtins-2.0.0.tgz#018999641e11252188652dbb2db01ad386fcdc46"
integrity sha512-8srrxpDx3a950BHYcbse+xMjupHHECvQYnShkoPz2ZLhTBrk/HQO6nWMh4o4ui8YYp2ourGVYXlGqFm+UYQwmA==
dependencies:
semver "^5.4.1"

byline@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/byline/-/byline-5.0.0.tgz#741c5216468eadc457b03410118ad77de8c1ddb1"
Expand Down Expand Up @@ -7347,6 +7354,11 @@ estree-walker@^0.2.1:
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.2.1.tgz#bdafe8095383d8414d5dc2ecf4c9173b6db9412e"
integrity sha1-va/oCVOD2EFNXcLs9MkXO225QS4=

estree-walker@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.5.2.tgz#d3850be7529c9580d815600b53126515e146dd39"
integrity sha512-XpCnW/AE10ws/kDAs37cngSkvgIR8aN3G0MS85m7dUpuK2EREo9VJ00uvw6Dg/hXEpfsE1I1TvJOJr+Z+TL+ig==

esutils@^2.0.0, esutils@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
Expand Down Expand Up @@ -16337,6 +16349,24 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
hash-base "^3.0.0"
inherits "^2.0.1"

rollup-plugin-auto-external@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-auto-external/-/rollup-plugin-auto-external-2.0.0.tgz#98fd137d66c1cbe0f4e245b31560a72dbde896aa"
integrity sha512-HQM3ZkZYfSam1uoZtAB9sK26EiAsfs1phrkf91c/YX+S07wugyRXSigBxrIwiLr5EPPilKYmoMxsrnlGBsXnuQ==
dependencies:
builtins "^2.0.0"
read-pkg "^3.0.0"
safe-resolve "^1.0.0"
semver "^5.5.0"

rollup-plugin-babel@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-4.0.3.tgz#8282b0e22233160d679e9c7631342e848422fb02"
integrity sha512-/PP0MgbPQyRywI4zRIJim6ySjTcOLo4kQbEbROqp9kOR3kHC3FeU++QpBDZhS2BcHtJTVZMVbBV46flbBN5zxQ==
dependencies:
"@babel/helper-module-imports" "^7.0.0"
rollup-pluginutils "^2.3.0"

rollup-plugin-hypothetical@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-hypothetical/-/rollup-plugin-hypothetical-2.1.0.tgz#7fec9a865ed7d0eac14ca6ee2b2c4088acdb1955"
Expand All @@ -16361,6 +16391,14 @@ rollup-pluginutils@^1.3.1:
estree-walker "^0.2.1"
minimatch "^3.0.2"

rollup-pluginutils@^2.3.0:
version "2.3.3"
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.3.3.tgz#3aad9b1eb3e7fe8262820818840bf091e5ae6794"
integrity sha512-2XZwja7b6P5q4RZ5FhyX1+f46xi1Z3qBKigLRZ6VTZjwbN0K1IFGMlwm06Uu0Emcre2Z63l77nq/pzn+KxIEoA==
dependencies:
estree-walker "^0.5.2"
micromatch "^2.3.11"

rollup@^0.59.4:
version "0.59.4"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.59.4.tgz#6f80f7017c22667ff1bf3e62adf8624a44cc44aa"
Expand All @@ -16369,6 +16407,14 @@ rollup@^0.59.4:
"@types/estree" "0.0.39"
"@types/node" "*"

rollup@^0.66.6:
version "0.66.6"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.66.6.tgz#ce7d6185beb7acea644ce220c25e71ae03275482"
integrity sha512-J7/SWanrcb83vfIHqa8+aVVGzy457GcjA6GVZEnD0x2u4OnOd0Q1pCrEoNe8yLwM6z6LZP02zBT2uW0yh5TqOw==
dependencies:
"@types/estree" "0.0.39"
"@types/node" "*"

rss@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/rss/-/rss-1.2.2.tgz#50a1698876138133a74f9a05d2bdc8db8d27a921"
Expand Down Expand Up @@ -16444,6 +16490,11 @@ safe-regex@^1.1.0:
dependencies:
ret "~0.1.10"

safe-resolve@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/safe-resolve/-/safe-resolve-1.0.0.tgz#fe34f8d29d7a3becfd249d0aa8a799b5c3cf6559"
integrity sha1-/jT40p16O+z9JJ0KqKeZtcPPZVk=

"safer-buffer@>= 2.1.2 < 3", safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
Expand Down