Skip to content

Commit

Permalink
feat(plugins): allow to import separate plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S committed Jul 19, 2018
1 parent 6cf8700 commit eb81c39
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ module.exports = {
]
],
plugins: [
process.env.PLUGINS && 'transform-es2015-modules-strip',
'@babel/proposal-object-rest-spread'
].filter(Boolean),
],
env: {
test: {
plugins: [ 'istanbul' ]
Expand Down
81 changes: 81 additions & 0 deletions build/build-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*!
* Script to build our plugins to use them separately.
* Copyright 2018 The Bootstrap Authors
* Copyright 2018 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/

'use strict'

const rollup = require('rollup')
const path = require('path')
const babel = require('rollup-plugin-babel')
const TEST = process.env.NODE_ENV === 'test'

const plugins = [
babel({
exclude: 'node_modules/**', // Only transpile our source code
externalHelpersWhitelist: [ // Include only required helpers
'defineProperties',
'createClass',
'inheritsLoose',
'defineProperty',
'objectSpread'
]
})
]

const format = 'umd'
const rootPath = !TEST ? '../js/dist/' : '../js/coverage/dist/'
const bsPlugins = {
Alert: path.resolve(__dirname, '../js/src/alert.js'),
Button: path.resolve(__dirname, '../js/src/button.js'),
Carousel: path.resolve(__dirname, '../js/src/carousel.js'),
Collapse: path.resolve(__dirname, '../js/src/collapse.js'),
Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
Modal: path.resolve(__dirname, '../js/src/modal.js'),
Popover: path.resolve(__dirname, '../js/src/popover.js'),
ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
Tab: path.resolve(__dirname, '../js/src/tab.js'),
Tooltip: path.resolve(__dirname, '../js/src/tooltip.js'),
Util: path.resolve(__dirname, '../js/src/util.js')
}

Object.keys(bsPlugins)
.forEach((pluginKey) => {
console.log(`Building ${pluginKey} plugin...`)

const external = ['jquery', 'popper.js']
const globals = {
jquery: 'jQuery', // Ensure we use jQuery which is always available even in noConflict mode
'popper.js': 'Popper'
}

// Do not bundle Util in plugins
if (pluginKey !== 'Util') {
external.push(bsPlugins.Util)
globals[bsPlugins.Util] = 'Util'
}

// Do not bundle Tooltip in Popover
if (pluginKey === 'Popover') {
external.push(bsPlugins.Tooltip)
globals[bsPlugins.Tooltip] = 'Tooltip'
}

rollup.rollup({
input: bsPlugins[pluginKey],
plugins,
external
}).then((bundle) => {
bundle.write({
format,
name: pluginKey,
sourcemap: true,
globals,
file: path.resolve(__dirname, `${rootPath}${pluginKey.toLowerCase()}.js`)
})
.then(() => console.log(`Building ${pluginKey} plugin... Done !`))
.catch((err) => console.error(`${pluginKey}: ${err}`))
})
})
34 changes: 21 additions & 13 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"js-compile": "npm-run-all --parallel js-compile-* --sequential js-copy",
"js-compile-standalone": "rollup --environment BUNDLE:false --config build/rollup.config.js --sourcemap",
"js-compile-bundle": "rollup --environment BUNDLE:true --config build/rollup.config.js --sourcemap",
"js-compile-plugins": "cross-env PLUGINS=true babel js/src/ --out-dir js/dist/ --source-maps",
"js-compile-plugins-coverage": "cross-env PLUGINS=true NODE_ENV=test babel js/src/ --out-dir js/coverage/dist/ --source-maps",
"js-compile-plugins": "node build/build-plugins.js",
"js-compile-plugins-coverage": "cross-env NODE_ENV=test node build/build-plugins.js",
"js-minify": "npm-run-all --parallel js-minify-*",
"js-minify-standalone": "uglifyjs --compress typeofs=false --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.js.map,includeSources,url=bootstrap.min.js.map\" --output dist/js/bootstrap.min.js dist/js/bootstrap.js",
"js-minify-bundle": "uglifyjs --compress typeofs=false --mangle --comments \"/^!/\" --source-map \"content=dist/js/bootstrap.bundle.js.map,includeSources,url=bootstrap.bundle.min.js.map\" --output dist/js/bootstrap.bundle.min.js dist/js/bootstrap.bundle.js",
Expand Down Expand Up @@ -103,7 +103,6 @@
"autoprefixer": "^8.6.5",
"babel-eslint": "^8.2.5",
"babel-plugin-istanbul": "^4.1.6",
"babel-plugin-transform-es2015-modules-strip": "^0.1.1",
"broken-link-checker": "^0.7.8",
"bundlesize": "^0.15.3",
"clean-css-cli": "^4.1.11",
Expand Down
4 changes: 3 additions & 1 deletion site/docs/4.1/getting-started/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ toc: true

## Individual or compiled

Plugins can be included individually (using Bootstrap's individual `*.js` files), or all at once using `bootstrap.js` or the minified `bootstrap.min.js` (don't include both).
Plugins can be included individually (using Bootstrap's individual `js/dist/*.js`), or all at once using `bootstrap.js` or the minified `bootstrap.min.js` (don't include both).

If you use a bundler (Webpack, Rollup...), you can use `/js/dist/*.js` files which are UMD ready.

## Dependencies

Expand Down
7 changes: 1 addition & 6 deletions site/docs/4.1/getting-started/webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ Alternatively, you may **import plugins individually** as needed:

{% highlight js %}
import 'bootstrap/js/dist/util';
import 'bootstrap/js/dist/dropdown';
import 'bootstrap/js/dist/alert';
...
{% endhighlight %}

Bootstrap is dependent on [jQuery](https://jquery.com/) and [Popper](https://popper.js.org/),
these are defined as `peerDependencies`, this means that you will have to make sure to add both of them
to your `package.json` using `npm install --save jquery popper.js`.

{% capture callout %}
Notice that if you chose to **import plugins individually**, you must also install [exports-loader](https://github.com/webpack-contrib/exports-loader)
{% endcapture %}
{% include callout.html content=callout type="warning" %}

## Importing Styles

### Importing Precompiled Sass
Expand Down

0 comments on commit eb81c39

Please sign in to comment.