UNPKG

3.94 kBMarkdownView Raw
1# @pixel2html/scripts-frontend
2
3[![npm version](https://badge.fury.io/js/%40pixel2html%2Fscripts-frontend.svg)](https://badge.fury.io/js/%40pixel2html%2Fscripts-frontend)
4[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
5
6A highly shareable and customizable webpack config.
7
8## Features
9
10- Dynamic Imports (Code Splitting)
11- ES2017+
12- JSX
13- Linting via `@pixel2html/eslint-config`
14- Parsing all js (ours and vendor, vendor is cached though)
15- Sourcemaps for debugging
16- Tree-Shaking
17- Extend and customize the underlying config
18
19## Getting Started FAST!
20
21```sh
22npm install --dev @pixel2html/scripts-frontend
23```
24
25
26```sh
27npx scripts-frontend development
28```
29
30:fire:
31
32We have a set of opinions towards how the files should look like for it to be a zero-config situation.
33
34```
35# Your project root
36.
37├── dist
38│   └── assets
39│   └── js
40│   └── main.js # Compiled file
41└── src
42 └── assets
43 └── js
44 └── index.js # Your starting point
45```
46
47That is to be compliant with the defaults we were using on our `generator-frontend` package, however this is easy to customize.
48
49## Frontend Generator
50
51If you're using our [Frontend Generator](http://npm.im/@pixel2html/generator-frontend) or our [Shopify Generator](http://npm.im/@pixel2html/generator-shopify) then you don't have to to anything since they already use this package.
52But if you're coming from scratch this is what you need to do:
53
54
55## Node
56
57Create an `index.js` with the following:
58
59```js
60const { compiler } = require('@pixel2html/scripts-frontend')
61
62// Options are development, production or debug
63const mode = 'development'
64console.log(`Compiling in ${mode} mode...`)
65
66compiler(mode)
67 .then(() => console.log(`All done!!`))
68 .catch(e => console.log(e))
69```
70
71You can now run `node index` to get your JS compiled.
72
73
74## Gulp
75
76```js
77const gulp = require('gulp')
78const { compiler } = require('@pixel2html/scripts-frontend')
79
80gulp.task('start', () => compiler('development'))
81gulp.task('build', () => compiler('production'))
82
83gulp.task('default', gulp.series('start'))
84```
85
86## Customizing the underlying config
87
88To customize your setup you need to create a `scripts.config.js` file at the root of your project that file will be a function that takes 2 parameters:
89
90- config (the default config) [See Webpack Config](https://webpack.js.org/configuration/)
91- webpack (a webpack instance so you can use plugins and whatever)
92
93
94`scripts.config.js`
95
96```js
97module.exports = function(config, webpack) {
98 // tweak away esketit
99
100 // Always return the config
101 return config
102}
103```
104
105## Shopify
106
107Since we do quite a bit of Shopify ourselves we added some opinionated list of shopify plugins which you can access like this:
108
109`scripts.config.js`
110
111```js
112const { getShopifyPlugins } = require('@pixel2html/scripts-generator')
113
114module.exports = (config, webpack) => {
115 config.plugins = getShopifyPlugins()
116
117 // So the sourcemaps work on Shopify
118 config.output.filename = '[name].js.liquid'
119 return config
120}
121```
122
123## Shopify Generator
124
125We also supplied a fully opinionated Shopify config which you can setup like this:
126
127`scripts.config.js`
128
129```js
130const { createShopifyConfig } = require('@pixel2html/scripts-frontend')
131
132module.exports = config => createShopifyConfig(config)
133```
134
135:fire:
136
137This is compliant with the way our Shopify Generator works meaning your starting point is:
138
139`src/scripts/index.js`
140
141and the bundle outputs to:
142
143`.deploy/assets/main.js.liquid`
144
145While splitting vendor code from node_modules to:
146
147`.deploy/assets/vendor.js.liquid`
148
149The liquid extension is used for sourcemaps support via Shopify Cache busting situation for assets.
150
151Check the examples folder for some reasonable examples.
152
153## PRs Welcome!
154
155Open an issue so we can discuss about it, then file a PR. :heart_eyes:
156
157Love,
158
159[Pixel2HTML](https://pixel2html.com/)