32
loading...
This website collects cookies to deliver better user experience
/dist
bundle.js
[styles.css]
/src
index.js
/styles
styles.scss
index.html
package.json
[*.config.js]
package.json
...
"scripts": {
"build": "rm -rf dist && webpack --mode development"
}
...
Note that we'll be adding the command rm -rf dist to every build of every bundler. What this does is removing the dist folder every time a new build script is executed.
webpack.config.js
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve("dist")
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: "/node-modules/",
use: "babel-loader"
},
{
test: /\.html$/,
use: "html-loader"
},
{
test: /\.(scss|sass)$/,
use: ["style-loader", "css-loader", "sass-loader"]
}
]
}
}
style-loader
, css-loader
and sass-loader
for the styles, html-loader
for the HTML files and babel-loader
for ES6.style-loader
to bundle the styles, instead of a plugin to minify CSS and generate a new file (MiniCSSExtractPlugin
), the styles are injected into the HTML file inside a <script>
tag, so the only output file is bundle.js
, which needs to be added to index.html
.package.json
...
"scripts": {
"build": "rm -rf dist && rollup src/index.js --file dist/bundle.js"
}
...
...
"scripts": {
"build": "rm -rf dist && rollup -c"
}
...
rollup.config.js
import babel from "@rollup/plugin-babel";
import scss from "rollup-plugin-scss";
export default {
input: "./src/index.js",
output: {
file: "./dist/bundle.js",
format: "cjs",
},
plugins: [
babel({ exclude: "node_modules/**" }),
scss({ output: "styles.css" }),
]
}
@rollup/plugin-babel
and rollup-plugin-scss
.rollup-plugin-css-only
that works in the exact same way as the plugin we're using for SCSS.bundle.js
and styles.css
. It's necessary to import the original styles files in the entry point index.js
for the bundler to be able to find the file (there's no other place where we can reference it).package.json
package.json
and the build command can get a little bit tedious if we don't have the concepts clear.package.json
...
"scripts": {
"build": "rm -rf dist && browserify -o dist/bundle.js src/index.js"
}
...
...
"scripts": {
"build": "rm -rf dist && browserify -t [browserify-css --output dist/styles.css] -o dist/bundle.js src/index.js"
}
...
...
"browserify": {
"browserify-css": {
"autoInject": true,
"minify": true,
"rootDir": "."
}
}
...
Babelify
and scssify
....
"scripts": {
"build": "rm -rf dist && browserify -t [ scssify --output dist/styles.css ] -t [ babelify --presets [ @babel/preset-env ] ] -o dist/bundle.js src/index.js"
}
...
...
"browserify": {
"transform": [
[ "babelify", {
"presets": [
"@babel/preset-env"
]
}
],
[ "scssify", { "autoInject": true } ]
]
}
...
...
"scripts": {
"build": "rm -rf dist && browserify -o dist/bundle.js src/index.js"
}
...
bundle.js
file and, only if we set an output file for the styles in the plugin that takes care of them, we'll get a styles.css file. Otherwise, the styles will be injected at the bottom of the <head>
element in the HTML file inside a <script>
element.browserify-css
:...
[ "browserify-css", {
"autoInject": false,
"minify": true,
"rootDir": ".",
"output": "dist/styles.css"
}
]
...
...
[ "browserify-css", {
"autoInject": true,
"minify": true,
"rootDir": "."
}
]
...
<script>
tag in the head of index.html
.esbuild.config.js
and we'll execute it from the build script by running the command node
.package.json
...
"scripts": {
"build": "rm -rf dist && esbuild --bundle src/index.js --outfile=dist/bundle.js"
}
...
...
"scripts": {
"build": "rm -rf dist && esbuild --bundle src/index.js --outfile=dist/bundle.js && esbuild --bundle styles/styles.css --outfile=dist/bundle.css"
}
...
esbuild.config.js
import esbuild from 'esbuild';
import { sassPlugin } from "esbuild-sass-plugin";
import babel from 'esbuild-plugin-babel';
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/bundle.js',
plugins: [sassPlugin(), babel()],
}).catch(() => process.exit(1));
"scripts": {
"build": "rm -rf dist && node esbuild.config.js"
}
package.json
if you're using ES Modules (import) to load your plugins into the config file:...
"type": "module"
...
esbuild-sass-plugin
for SASS/SCSS and esbuild-plugin-babel
for Babel. Both of them work with import, so no extra problems.package.json
...
"scripts": {
"build": "rm -rf dist && rm -rf && parcel build src/index.js --no-scope-hoist --no-source-maps"
}
...
--no-scope-hoist
to avoid odd behaviors regarding require
when running js scripts and --no-source-maps
to avoid the creation of sourcemaps. Otherwise, Parcel will create one for every bundle file by default.package.json
, like this:...
"main": "dist/bundle.js"
...
npm init
)..sassrc
and will contain the following code inside:{
"includePaths": ["node_modules"]
}
@parcel/transformer-sass
as a dependency and will create a bundle.css
file in the same specified directory for the bundle, and that's all the configuration. Pretty cool, right? .babelrc
config file to specify the needed presets (let's say we want to get it ready for using React in the future):{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
@parcel/transformer-babel
and will do the job for us.@babel/preset-env
, @babel/preset-react
and all the dependencies needed by React.