Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can this be used with a react app? #59

Closed
npomfret opened this issue Jan 3, 2019 · 8 comments
Closed

can this be used with a react app? #59

npomfret opened this issue Jan 3, 2019 · 8 comments

Comments

@npomfret
Copy link

npomfret commented Jan 3, 2019

I'm very new to this, apologies if this is a silly question but I'm quite lost.

I want to create a react app in a single html file. Is it possible?

@SovietFrontier
Copy link

You will just have to mess with the settings a bit to make the compiler pick up .jsx files. If you're just using html, css, js it should work fine. The final compilation is in js anyways. Try it out! Tell us how it works.

@npomfret
Copy link
Author

npomfret commented Jan 3, 2019

Thanks. I'm very new to this stuff and am not sure how to get it working from a basic react app (created using yarn create react-app my-app). Do you know of a detailed example that would help?

@SovietFrontier
Copy link

I can leave you my webpack.config if that will help? I am not sure exactly because we do not use React. I had to do a lot of trial and error to get it to work because there wasn't very many examples of how I use it.

@npomfret
Copy link
Author

npomfret commented Jan 3, 2019

Thanks, if you could gist it that might be helpful. I think this is a react specific problem though.

@SovietFrontier
Copy link

SovietFrontier commented Jan 3, 2019

https://gist.github.com/SovietFrontier/d6d88147affd6a69e7baa97ce32dee79
Have at it, and good luck! Post your results here! I would keep up with these issues more but I am not the author.

@npomfret
Copy link
Author

npomfret commented Jan 4, 2019

Think I got it working. For future reference (using react 16.7.0 & yarn 1.10.1)...

Create a react app:

npx create-react-app my-app
cd my-app
yarn build

All good? Cool, run eject so that you have access to the webpack config:

yarn eject
rm -rf build node_modules
yarn build

Add this project and update webpack:

yarn add html-webpack-plugin@4.0.0-beta.4
yarn add html-webpack-inline-source-plugin@1.0.0-beta.2

Edit config/webpack.config.js:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); <--- add this
...
plugins: [
  new HtmlWebpackPlugin(
    Object.assign(
      ...
      isEnvProduction
        ? {
            inlineSource: '.(js|css)$', <-- add this
            minify: {
              ...
  ),
  ...
  isEnvProduction &&
    shouldInlineRuntimeChunk &&
    new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin), <--- add this
    new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
    ...

Run rm -rf build && yarn build && serve -s build - the index.html you load should now contain all the js and css stuff inline. Note that the static files are still created in the build dir, but they aren't used by the page.

@desjob
Copy link

desjob commented Jul 1, 2019

Think I got it working. For future reference (using react 16.7.0 & yarn 1.10.1)...

Create a react app:

npx create-react-app my-app
cd my-app
yarn build

All good? Cool, run eject so that you have access to the webpack config:

yarn eject
rm -rf build node_modules
yarn build

Add this project and update webpack:

yarn add html-webpack-plugin@4.0.0-beta.4
yarn add html-webpack-inline-source-plugin@1.0.0-beta.2

Edit config/webpack.config.js:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); <--- add this
...
plugins: [
  new HtmlWebpackPlugin(
    Object.assign(
      ...
      isEnvProduction
        ? {
            inlineSource: '.(js|css)$', <-- add this
            minify: {
              ...
  ),
  ...
  isEnvProduction &&
    shouldInlineRuntimeChunk &&
    new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin), <--- add this
    new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
    ...

Run rm -rf build && yarn build && serve -s build - the index.html you load should now contain all the js and css stuff inline. Note that the static files are still created in the build dir, but they aren't used by the page.

thanks so much for posting this! works like a charm!

@neokeld
Copy link

neokeld commented Aug 11, 2019

Thx a lot ! It also works with yarn add html-webpack-plugin@next instead of yarn add html-webpack-plugin@4.0.0-beta.4
To know more, this is related : jantimon/html-webpack-plugin#1068

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants