简体   繁体   English

如何通过 Webpack 捆绑 Nunjucks 文件

[英]How to bundle Nunjucks files via Webpack

I have a complete site that I want to design a build tool for it.In fact, I chose Webpack for doing that.我有一个完整的网站,我想为它设计一个构建工具。事实上,我选择了 Webpack 来做这件事。 The project structure is like this:项目结构是这样的:

在此处输入图片说明

I have nunjucks, html, css, sass and js files.我有 nunjucks、html、css、sass 和 js 文件。 I must bundle them via webpack.我必须通过 webpack 捆绑它们。 My webpack config file is here:我的 webpack 配置文件在这里:

var HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const path = require('path')
const CopyPlugin = require('copy-webpack-plugin')
const NunjucksWebpackPlugin = require('nunjucks-webpack-plugin')

module.exports = {
    entry: ['./src/index.js'],
    output: {
       filename: 'bundle.js',
       path: path.resolve(__dirname, 'dist')
        },
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist',
        writeToDisk: true
    },
   plugins: [
       new CopyPlugin([
           { from: 'public/images', to: 'images' },
           { from: 'public/fonts', to: 'fonts' },
           { from: 'src/pages/about', to: '.' }
         ]),
         new CleanWebpackPlugin(),
       // new HtmlWebpackPlugin()
        new HtmlWebpackPlugin({
           title: 'Asset Management' //title of file.html
         })
       ],
     module: {
        rules: [
           {
               test: /\.css$/,
               use: ['style-loader', 'css-loader']
            },
            {
               test: /\.s[ac]ss$/i,
               use: [
                // Creates `style` nodes from JS strings
                'style-loader',
                // Translates CSS into CommonJS
                'css-loader',
                // Compiles Sass to CSS
                'sass-loader'
            ]
          },
          {
            test: /\.(png|svg|jpg|gif)$/,
            use: ['file-loader']
          },
          {
            test: /\.(woff|woff2|eot|ttf|otf)$/,
            use: ['file-loader']
          },
          {
            test: /\.(njk|nunjucks)$/,
            loader: 'nunjucks-loader'
          },
         {
             // to auto refresh index.html and other html
             test: /\.html$/,
             loader: 'raw-loader',
             exclude: /node_modules/
         },
        {
            test: /\.html$/,
            use: [
                {
                    loader: 'html-loader',
                    options: {
                        interpolate: true
                    }
                }
            ]
        }
     ]
   }
  }

The "index.js" file also is like this: “index.js”文件也是这样的:

  import _ from 'lodash'
  import './pages/about/about_moon.scss'
  import './pages/about/about_moon.html'
  var tpl = require('./pages/home/index_moon.njk')
  var html = tpl.render({ message: 'Foo that!' })
  function component() {
  return element
    }
   document.body.appendChild(component())

I configured the "package.json" file and defined scripts to run webpack:我配置了“package.json”文件并定义了脚本来运行 webpack:

    "start": "webpack-dev-server --open",
    "build": "webpack"

The problem is when I run npm run build , the dist folder was made and it had a html file but there is nothing to show.问题是当我运行npm run buildnpm run build了 dist 文件夹并且它有一个 html 文件,但没有任何显示。 I have already had some html files and wanted to bundle all of them to "bundle.js", but I have not known how.我已经有一些 html 文件并想将它们全部捆绑到“bundle.js”中,但我不知道如何。 Would you please tell me how I can bundle this project?你能告诉我如何捆绑这个项目吗?

Thank you in advance.先感谢您。

Problem solved.问题解决了。 I changed the Webpack.config.js file to this:我将Webpack.config.js文件更改为:

  const path = require('path')
  var HtmlWebpackPlugin = require('html-webpack-plugin')
  var UglifyJSPlugin = require('uglifyjs-webpack-plugin')
  const CopyPlugin = require('copy-webpack-plugin')
  const { CleanWebpackPlugin } = require('clean-webpack-plugin')
  const BrowserSyncPlugin = require('browser-sync-webpack-plugin')
  module.exports = {
       entry: ['./src/index.js', './script.js'],
       output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
      },
    plugins: [
    // HtmlWebpackPluginConfig
      new HtmlWebpackPlugin({
          filename: 'index.html',
           inject: 'head',
           template: './index.njk'
         }),
      new CleanWebpackPlugin(),
      new CopyPlugin([
        { from: 'public/images', to: 'images' },
        { from: 'public/fonts', to: 'fonts' }
      ])
     ],
    module: {
    rules: [
          {
            test: /\.exec\.js$/,
            use: ['script-loader']
          },
          {
            test: /\.css$/i,
            use: ['style-loader', 'css-loader']
          },
          {
            test: /\.(scss)$/,
            use: [
                {
                 // Adds CSS to the DOM by injecting a `<style>` tag
                    loader: 'style-loader'
                },
                {
                    // Interprets `@import` and `url()` like `import/require()` and will resolve them
                    loader: 'css-loader'
                },
                {
                    // Loader for webpack to process CSS with PostCSS
                    loader: 'postcss-loader',
                    options: {
                        plugins: function() {
                            return [require('autoprefixer')]
                        }
                    }
                },
                {
                    // Loads a SASS/SCSS file and compiles it to CSS
                    loader: 'sass-loader'
                }
              ]
            },
          {
            // HTML LOADER
            // Super important: We need to test for the html
            // as well as the nunjucks files
            test: /\.html$|njk|nunjucks/,
            use: [
                'html-loader',
                {
                    loader: 'nunjucks-html-loader',
                    options: {
                        // Other super important. This will be the base
                        // directory in which webpack is going to find
                        // the layout and any other file index.njk is calling.
                        //  searchPaths: [...returnEntries('./src/pages/**/')]
                        // Use the one below if you want to use a single path.
                        // searchPaths: ['./']
                    }
                }
            ]
        }
       ]
     }
    }

Also, I wrote the script.js file like this, since, function names were changed and they could not be run after bundling.另外,我写了这样的script.js文件,因为函数名被更改了,捆绑后无法运行。

   document.getElementById('body').onload = function() {
   console.log('Document loaded')
   var menu = localStorage.getItem('menu')
   if (menu === 'opened') 
     document.getElementById('navigation').classList.add('opened')
   }
  document.getElementById('menu-button').onclick = function() {
  // localstorage used to define global variable
  var menu = localStorage.getItem('menu')
  localStorage.setItem('menu', menu === 'closed' ? 'opened' : 'closed')
  document.getElementById('navigation').classList.toggle('opened')
  }
  // Window.onLoad = onLoad // global variable in js

The index.js was used to import other files and it was like this: index.js用于导入其他文件,它是这样的:

   import _ from 'lodash'
   require('../index.njk')
   require('../base.html')
   require('../style.css')

This is the Json file:这是 Json 文件:

   {
   "name": "menu_moon",
   "version": "1.0.0",
   "description": "",
   "private": true,
   "dependencies": {
   "browser-sync": "^2.26.7",
   "extract-text-webpack-plugin": "^3.0.2",
   "fast-glob": "^3.1.1",
   "fs-extra": "^8.1.0",
   "g": "^2.0.1",
   "html-loader": "^0.5.5",
   "i": "^0.3.6",
   "lodash": "^4.17.15",
   "mkdirp": "^0.5.1",
   "nunjucks": "^3.2.0",
   "nunjucks-html-loader": "^1.1.0",
   "nunjucks-isomorphic-loader": "^2.0.2",
   "nunjucks-loader": "^3.0.0",
   "raw-loader": "^4.0.0"
},
   "devDependencies": {
      "browser-sync-webpack-plugin": "^2.2.2",
      "clean-webpack-plugin": "^3.0.0",
      "copy-webpack-plugin": "^5.0.5",
      "css-loader": "^3.2.1",
      "file-loader": "^5.0.2",
      "html-webpack-plugin": "^3.2.0",
      "node-sass": "^4.13.0",
      "nunjucks-webpack-plugin": "^5.0.0",
      "sass-loader": "^8.0.0",
      "script-loader": "^0.7.2",
      "style-loader": "^1.0.1",
      "uglifyjs-webpack-plugin": "^2.2.0",
      "webpack": "^4.41.2",
      "webpack-cli": "^3.3.10",
      "webpack-dev-server": "^3.9.0"
       },
    "scripts": {
      "moon-start": "browser-sync start --server --files './**/*.*'",
      "moon-build": "node build_product_moon.js",
      "start": "webpack-dev-server --open",
      "build": "webpack"
      },
    "author": "",
    "license": "ISC"
    }

I hope it was useful for others.我希望它对其他人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM