with-webpack.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ---
  2. title: With webpack
  3. layout: documentation
  4. doc-tab: customize
  5. doc-subtab: webpack
  6. breadcrumb:
  7. - home
  8. - documentation
  9. - customize
  10. - customize-webpack
  11. ---
  12. {% capture dependencies %}
  13. npm install bulma --save-dev
  14. npm install css-loader --save-dev
  15. npm install extract-text-webpack-plugin@next --save-dev
  16. npm install node-sass --save-dev
  17. npm install sass-loader --save-dev
  18. npm install style-loader --save-dev
  19. npm install webpack --save-dev
  20. npm install webpack-cli --save-dev
  21. {% endcapture %}
  22. {% capture package %}
  23. {
  24. "name": "mybulma",
  25. "version": "1.0.0",
  26. "main": "webpack.config.js",
  27. "license": "MIT",
  28. "devDependencies": {
  29. "bulma": "^0.7.1",
  30. "css-loader": "^1.0.0",
  31. "extract-text-webpack-plugin": "^4.0.0-beta.0",
  32. "node-sass": "^4.9.2",
  33. "sass-loader": "^7.0.3",
  34. "style-loader": "^0.21.0",
  35. "webpack": "^4.16.0",
  36. "webpack-cli": "^3.0.8"
  37. }
  38. }
  39. {% endcapture %}
  40. {% capture step_2 %}
  41. <div class="content">
  42. <p>
  43. Install the packages required to parse and build your CSS:
  44. </p>
  45. </div>
  46. {% highlight bash %}{{ dependencies }}{% endhighlight %}
  47. <div class="content">
  48. <p>
  49. Your <code>package.json</code> should look like this at this point.
  50. </p>
  51. </div>
  52. {% highlight bash %}{{ package }}{% endhighlight %}
  53. {% endcapture %}
  54. {% capture config %}
  55. const path = require('path');
  56. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  57. module.exports = {
  58. entry: './src/index.js',
  59. output: {
  60. path: path.resolve(__dirname, 'dist'),
  61. filename: 'js/bundle.js'
  62. },
  63. module: {
  64. rules: [{
  65. test: /\.scss$/,
  66. use: ExtractTextPlugin.extract({
  67. fallback: 'style-loader',
  68. use: [
  69. 'css-loader',
  70. 'sass-loader'
  71. ]
  72. })
  73. }]
  74. },
  75. plugins: [
  76. new ExtractTextPlugin('css/mystyles.css'),
  77. ]
  78. };
  79. {% endcapture %}
  80. {% capture step_3 %}
  81. <div class="content">
  82. <p>
  83. Create a <code>webpack.config.js</code> file:
  84. </p>
  85. </div>
  86. <div class="highlight-full">
  87. {% highlight js %}{{ config }}{% endhighlight %}
  88. </div>
  89. <div class="content">
  90. <p>
  91. This setup takes the <code>src</code> folder as input, and outputs in the <code>dist</code> folder.
  92. </p>
  93. </div>
  94. {% endcapture %}
  95. {% capture require_css %}
  96. require('./mystyles.scss');
  97. {% endcapture %}
  98. {% capture step_4 %}
  99. <div class="content">
  100. <p>
  101. Create a <code>src</code> folder in which you add a file called <code>index.js</code> with the following content:
  102. </p>
  103. </div>
  104. {% highlight js %}{{ require_css }}{% endhighlight %}
  105. {% endcapture %}
  106. {% capture step_6 %}
  107. <div class="content">
  108. <p>
  109. Create a <code>dist</code> folder in which you add a <code>css</code> folder, and a <code>js</code> folder. Leave these last two folders empty. Their content will be generated by the webpack build.
  110. </p>
  111. </div>
  112. {% endcapture %}
  113. {% capture webpack_script %}
  114. "scripts": {
  115. "build": "webpack --mode production"
  116. },
  117. {% endcapture %}
  118. {% capture npm_build %}
  119. npm run build
  120. {% endcapture %}
  121. {% capture npm_build_success %}
  122. Rendering Complete, saving .css file...
  123. Wrote CSS to /path/to/mybulma/css/mystyles.css
  124. {% endcapture %}
  125. {% capture step_8 %}
  126. <div class="content">
  127. <p>
  128. In <code>package.json</code>, add the following:
  129. </p>
  130. </div>
  131. {% highlight html %}{{ webpack_script }}{% endhighlight %}
  132. <div class="content">
  133. <p>
  134. To test it out, go in your <strong>terminal</strong> and run the following command:
  135. </p>
  136. </div>
  137. {% highlight bash %}{{ npm_build }}{% endhighlight %}
  138. {% endcapture %}
  139. {% include steps/create-package.html
  140. number="1"
  141. entry="webpack.config.js"
  142. %}
  143. <hr>
  144. {% include components/step.html
  145. title="2. Install the dev dependencies"
  146. content=step_2
  147. %}
  148. <hr>
  149. {% include components/step.html
  150. title="3. Create a webpack config"
  151. content=step_3
  152. %}
  153. <hr>
  154. {% include components/step.html
  155. title="4. Create a <code>src</code> folder"
  156. content=step_4
  157. %}
  158. <hr>
  159. {% include steps/create-sass-file.html
  160. number="5"
  161. path='~bulma/bulma'
  162. bis=true
  163. %}
  164. <hr>
  165. {% include components/step.html
  166. title="6. Create a <code>dist</code> folder"
  167. content=step_6
  168. %}
  169. <hr>
  170. {% include steps/create-html-page.html
  171. number="7"
  172. dist=true
  173. %}
  174. <hr>
  175. {% include components/step.html
  176. title="8. Add node scripts to build your bundle"
  177. content=step_8
  178. %}
  179. <hr>
  180. {% include steps/add-custom-styles.html
  181. number="9"
  182. build=true
  183. %}