# parcel

***

**1. Basic Parcel Configuration**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {});

bundler.bundle();
```

**2. Using a Custom Output Path**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {
  outFile: 'dist/index.html',
});

bundler.bundle();
```

**3. Serving Static Files**

```js
const express = require('express');
const { ParcelMiddleware } = require('parcel-bundler');

const app = express();
app.use(ParcelMiddleware({
  publicUrl: '/',
  outDir: './dist',
}));

app.listen(3000);
```

**4. Including CSS and JS**

```html
<html>
<head>
  <link rel="stylesheet" href="/dist/index.css">
</head>
<body>
  <script src="/dist/index.js"></script>
</body>
</html>
```

**5. Using Tailwind.css with Parcel**

```js
const { ParcelConfig } = require('parcel-bundler');
const tailwindcss = require('tailwindcss');
const postcss = require('postcss');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('css', async function (source, options) {
  const result = await postcss([tailwindcss()]).process(source);
  return result.css;
});
```

**6. Building a Production Bundle**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {
  env: 'production',
});

bundler.bundle();
```

**7. Using a Polyfill**

```js
const { ParcelConfig } = require('parcel-bundler');
const autoprefixer = require('autoprefixer');
const postcss = require('postcss');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('css', async function (source, options) {
  const result = await postcss([autoprefixer()]).process(source);
  return result.css;
});
```

**8. Bundling SVGs**

```html
<img src="./icon.svg">
```

**9. Using Babel**

```js
const { ParcelConfig } = require('parcel-bundler');
const babelCore = require('@babel/core');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('js', async function (source, options) {
  const result = await babelCore.transformAsync(source, { presets: ['@babel/preset-env'] });
  return result.code;
});
```

**10. Using TypeScript**

```js
const { ParcelConfig } = require('parcel-bundler');
const ts = require('typescript');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('ts', async function (source, options) {
  const result = ts.transpile(source);
  return result;
});
```

**11. Bundling JSON**

```html
<script>
  const data = require('./data.json');
</script>
```

**12. Using a Custom Plugin**

```js
const { ParcelBundler, transformers } = require('parcel-bundler');

const MyPlugin = {
  apply(bundler) {
    bundler.addAssetType('.my-extension', transformers.raw);
  },
};

const bundler = new ParcelBundler('index.html', { plugins: [MyPlugin] });
```

**13. Watching for File Changes**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', { watch: true });

bundler.bundle();
```

**14. Using a Custom Cache Directory**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', { cacheDir: './my-cache' });

bundler.bundle();
```

**15. Using a Custom Resolver**

```js
const { ParcelConfig } = require('parcel-bundler');
const { NodeResolver } = require('@parcel/node-resolver');

const parcelConfig = new ParcelConfig();

parcelConfig.resolvers.unshift(new NodeResolver({ root: './node_modules' }));
```

**16. Using a Custom Query String**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {
  query: 'development=true',
});

bundler.bundle();
```

**17. Using a Custom URL Prefix**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {
  publicUrl: '//cdn.example.com',
});

bundler.bundle();
```

**18. Using a Custom Source Map**

```js
const { ParcelBundler } = require('parcel-bundler');

const bundler = new ParcelBundler('index.html', {
  sourceMaps: true,
});

bundler.bundle();
```

**19. Using a Custom Transform**

```js
const { ParcelConfig } = require('parcel-bundler');
const { Transform } = require('stream');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('js', async function (source, options) {
  return new Transform({
    transform(chunk, encoding, callback) {
      callback(null, chunk.toString().toUpperCase());
    },
  });
});
```

**20. Using a Custom Webpack Loader**

```js
const { ParcelConfig } = require('parcel-bundler');
const { WebpackLoader } = require('@parcel/webpack-loader');

const parcelConfig = new ParcelConfig();

parcelConfig.addTransformer('js', new WebpackLoader({
  loaders: [
    {
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env']

```
