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 |
|
6 | A 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
|
22 | npm install --dev @pixel2html/scripts-frontend
|
23 | ```
|
24 |
|
25 |
|
26 | ```sh
|
27 | npx scripts-frontend development
|
28 | ```
|
29 |
|
30 | :fire:
|
31 |
|
32 | We 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 |
|
47 | That 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 |
|
51 | If 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.
|
52 | But if you're coming from scratch this is what you need to do:
|
53 |
|
54 |
|
55 | ## Node
|
56 |
|
57 | Create an `index.js` with the following:
|
58 |
|
59 | ```js
|
60 | const { compiler } = require('@pixel2html/scripts-frontend')
|
61 |
|
62 | // Options are development, production or debug
|
63 | const mode = 'development'
|
64 | console.log(`Compiling in ${mode} mode...`)
|
65 |
|
66 | compiler(mode)
|
67 | .then(() => console.log(`All done!!`))
|
68 | .catch(e => console.log(e))
|
69 | ```
|
70 |
|
71 | You can now run `node index` to get your JS compiled.
|
72 |
|
73 |
|
74 | ## Gulp
|
75 |
|
76 | ```js
|
77 | const gulp = require('gulp')
|
78 | const { compiler } = require('@pixel2html/scripts-frontend')
|
79 |
|
80 | gulp.task('start', () => compiler('development'))
|
81 | gulp.task('build', () => compiler('production'))
|
82 |
|
83 | gulp.task('default', gulp.series('start'))
|
84 | ```
|
85 |
|
86 | ## Customizing the underlying config
|
87 |
|
88 | To 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
|
97 | module.exports = function(config, webpack) {
|
98 | // tweak away esketit
|
99 |
|
100 | // Always return the config
|
101 | return config
|
102 | }
|
103 | ```
|
104 |
|
105 | ## Shopify
|
106 |
|
107 | Since 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
|
112 | const { getShopifyPlugins } = require('@pixel2html/scripts-generator')
|
113 |
|
114 | module.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 |
|
125 | We also supplied a fully opinionated Shopify config which you can setup like this:
|
126 |
|
127 | `scripts.config.js`
|
128 |
|
129 | ```js
|
130 | const { createShopifyConfig } = require('@pixel2html/scripts-frontend')
|
131 |
|
132 | module.exports = config => createShopifyConfig(config)
|
133 | ```
|
134 |
|
135 | :fire:
|
136 |
|
137 | This is compliant with the way our Shopify Generator works meaning your starting point is:
|
138 |
|
139 | `src/scripts/index.js`
|
140 |
|
141 | and the bundle outputs to:
|
142 |
|
143 | `.deploy/assets/main.js.liquid`
|
144 |
|
145 | While splitting vendor code from node_modules to:
|
146 |
|
147 | `.deploy/assets/vendor.js.liquid`
|
148 |
|
149 | The liquid extension is used for sourcemaps support via Shopify Cache busting situation for assets.
|
150 |
|
151 | Check the examples folder for some reasonable examples.
|
152 |
|
153 | ## PRs Welcome!
|
154 |
|
155 | Open an issue so we can discuss about it, then file a PR. :heart_eyes:
|
156 |
|
157 | Love,
|
158 |
|
159 | [Pixel2HTML](https://pixel2html.com/)
|