markdalgleish / static-site-generator-webpack-plugin

Minimal, unopinionated static site generator powered by webpack
MIT License
1.61k stars 97 forks source link

How to with HtmlWebpackPlugin? #90

Open rkmax opened 7 years ago

rkmax commented 7 years ago

my config is the following

const { join } = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  context: __dirname,
  entry  : {
    main: './src/app.js'
  },
  output : {
    path         : join(__dirname, 'dist'),
    filename     : '[name].bundle.js',
    chunkFilename: '[id]_chunk.js'
  },
  module : {
    rules: [
      { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
      { test: /\.css$/, loader: "style-loader!css-loader" },
      { test: /\.(scss|sass)$/, loaders: ['style-loader', 'css-loader', 'sass-loader'] },
      { test: /\.(png|jpg|jpeg|gif)$/, loader: 'url-loader?limit=8192' },
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({m: 'mithril'}),
    new HtmlWebpackPlugin({
      title: 'MyTitle'
    })
  ],
  devServer: {
    historyApiFallback: true
  }
};

my entry point is just rendering a simple component

import m from 'mithril';
import Landing from './landing';

import './styles/theme.scss';

m.render(document.body, m(Landing));

now when Add the static site generator at the end of plugin list always got an "Error: window is not defined" maybe I'm doing something wrong with my config

nealgriffin commented 7 years ago

I'm not sure if this'll help, but I ran into a similar issue and it was simply because I was not specifying the entry point correctly in webpack.config.js - the first argument to the plugin should be the name of your entry point, e.g. your entry point from above is: entry : { main: './src/app.js' }, therefore in your webpack config you'd want to specify: new StaticSiteGeneratorPlugin('main', some_array_of_routes, some_other_file), specifying 'main' is what I had overlooked, hope it helps.

rkmax commented 7 years ago

Still have the same error

nealgriffin commented 7 years ago

Understood. I was really interested in this, it took awhile to get the elements correct, but I did end up creating a repository that shows how to use this plugin with Mithril. I believe the root cause of the issues you are having is that mithril is expecting to be run from the browser and this plugin runs it from node's context. I walkthrough the steps I took to get the two to work together in the linked repository.

I'm not sure if it'll solve what you are ultimately trying to do, but my hope is that it is helpful.