Skip to content
Permalink
Browse files

fix(gatsby-ssr): Ignored all actions during dev build.

This plugin does not currently handle removing javascript during the dev build, but it may be
something to support in the future. This change checks the NODE_ENV before doing any action
  • Loading branch information
itmayziii committed Jun 28, 2019
1 parent 9c11787 commit 2f9c26553b2b83c74dc959434d62d547a06dbaa1
Showing with 57 additions and 7 deletions.
  1. +5 −2 README.md
  2. +6 −0 package-lock.json
  3. +2 −1 package.json
  4. +38 −4 src/gatsby-ssr.spec.ts
  5. +6 −0 src/gatsby-ssr.ts
@@ -1,17 +1,20 @@
## Description

Removes all javascript files created by Gatsby from the static HTML files. This plugin is not meant to remove all javascript, but only the javascript that Gatsby is adding to the
page.
page.

A code coverage tool is not set up for this repository yet to add a nice badge for the percent covered,
but rest knowing that this plugin is currently 100% unit tested. Go ahead and run `npm run test` to see the `instanbul` code coverage output.

:warning: The Gatsby javascript is only removed from the production build `gatsby build` and not during the dev build `gatsby develop`. If you do not write any state logic or event
handlers then this should not effect you. This feature may be something this plugin wants to tackle in the future.

## How to install

`npm install gatsby-plugin-no-javascript` or `yarn add gatsby-plugin-no-javascript`

:warning: This plugin should be included last in your `gatsby-config.js` as it relies on [onPreRenderHTML](https://www.gatsbyjs.org/docs/ssr-apis/#onPreRenderHTML)
`replaceHeadComponents` and `replacePostBodyComponents`. :warning:
`replaceHeadComponents` and `replacePostBodyComponents`.

## Available options

Some generated files are not rendered by default. Learn more.

@@ -7,7 +7,7 @@
"build": "npx tsc --project tsconfig.json",
"watch": "npx tsc --project tsconfig.json --watch",
"package": "npm run build && npx standard-version",
"test": "npm run build && npx nyc --exclude-after-remap=false jasmine --config=jasmine.json",
"test": "npm run build && npx nyc --exclude-after-remap=false --exclude=\"**/*.spec.js\" jasmine --config=jasmine.json",
"lint": "npx tslint --project tsconfig.json --config tslint.json",
"lint:fix": "npx tslint --fix --project tsconfig.json --config tslint.json",
"commit": "npx git-cz",
@@ -36,6 +36,7 @@
"homepage": "https://github.com/itmayziii/gatsby-plugin-no-javascript#readme",
"devDependencies": {
"@types/jasmine": "^3.3.13",
"@types/node": "^12.0.10",
"@types/react": "^16.8.22",
"commitizen": "^3.1.1",
"cz-conventional-changelog": "^2.1.0",
@@ -4,6 +4,10 @@ import { headComponentsData, postBodyComponentsData, scriptsData } from './fake-

describe('gatsby-ssr.js', () => {

beforeEach(() => {
process.env.NODE_ENV = 'production' // Testing Gatsby production builds by default.
})

describe('onRenderBody', () => {
it('throws an error when no scripts are passed in', function () {
expect(() => { onRenderBody({}) }).toThrow(new Error('gatsby-plugin-no-javascript: Gatsby removed an internal detail that this plugin relied upon, please submit this issue to https://www.github.com/itmayziii/gatsby-plugin-no-javascript.'))
@@ -39,7 +43,12 @@ describe('gatsby-ssr.js', () => {
return []
}

onPreRenderHTML({ getHeadComponents, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, { exclude: /webpack-runtime/ })
onPreRenderHTML({
getHeadComponents,
replaceHeadComponents: replaceHeadComponentsSpy,
getPostBodyComponents,
replacePostBodyComponents: replacePostBodyComponentsSpy
}, { exclude: /webpack-runtime/ })
expect(replaceHeadComponentsSpy).toHaveBeenCalledWith([headComponentsData[13]])
})

@@ -58,13 +67,13 @@ describe('gatsby-ssr.js', () => {

it('should remove preload scripts from the head that are called out by Gatsby during onRenderBody', () => {
onRenderBody({ scripts: scriptsData })

function getHeadComponents () {
return [
headComponentsData[0], headComponentsData[1], headComponentsData[8], headComponentsData[9], headComponentsData[10], headComponentsData[11],
headComponentsData[12], headComponentsData[13], headComponentsData[14]
]
}

function getPostBodyComponents () {
return []
}
@@ -86,9 +95,11 @@ describe('gatsby-ssr.js', () => {
key: 'styles-module2.css'
}
]

function getHeadComponents () {
return []
}

function getPostBodyComponents () {
return fakeBodyComponents
}
@@ -106,7 +117,12 @@ describe('gatsby-ssr.js', () => {
return [postBodyComponentsData[3]]
}

onPreRenderHTML({ getHeadComponents, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, { exclude: /webpack-runtime/ })
onPreRenderHTML({
getHeadComponents,
replaceHeadComponents: replaceHeadComponentsSpy,
getPostBodyComponents,
replacePostBodyComponents: replacePostBodyComponentsSpy
}, { exclude: /webpack-runtime/ })
expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith([postBodyComponentsData[3]])
})

@@ -125,7 +141,6 @@ describe('gatsby-ssr.js', () => {

it('should remove preload scripts from the body that are called out by Gatsby during onRenderBody', () => {
onRenderBody({ scripts: scriptsData })

function getHeadComponents () {
return []
}
@@ -138,5 +153,24 @@ describe('gatsby-ssr.js', () => {
expect(replacePostBodyComponentsSpy.calls.argsFor(0)[0].length).toEqual(1)
expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith([postBodyComponentsData[0]])
})

it('should not remove anything during a non production build', () => {
const oldEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'development'
onRenderBody({})
function getHeadComponents () {
return headComponentsData
}
function getPostBodyComponents () {
return postBodyComponentsData
}

onPreRenderHTML({ getHeadComponents, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {})

expect(replaceHeadComponentsSpy).toHaveBeenCalledTimes(0)
expect(replacePostBodyComponentsSpy).toHaveBeenCalledTimes(0)

process.env.NODE_ENV = oldEnv
})
})
})
@@ -28,6 +28,9 @@ let pageScripts: Script[]
* the head and post body. We will be relying on this undocumented variable until it does not work anymore as the alternative is to read the webpack.stats.json file and parse it ourselves.
*/
export function onRenderBody ({ scripts }: OnRenderBodyArgs) {
if (process.env.NODE_ENV !== 'production') { // During a gatsby development build (gatsby develop) we do nothing.
return
}
// TODO maybe we should not even wait and see if Gatsby removes this internal "script" variable and code around the issue if the variable is not there.
if (!scripts) {
throw new Error('gatsby-plugin-no-javascript: Gatsby removed an internal detail that this plugin relied upon, please submit this issue to https://www.github.com/itmayziii/gatsby-plugin-no-javascript.')
@@ -37,6 +40,9 @@ export function onRenderBody ({ scripts }: OnRenderBodyArgs) {

// Here we rely on the fact that onPreRenderHTML is called after onRenderBody so we have access to the scripts Gatsby inserted into the HTML.
export function onPreRenderHTML ({ getHeadComponents, replaceHeadComponents, getPostBodyComponents, replacePostBodyComponents }: OnPreRenderHTMLArgs, pluginOptions: PluginOptions) {
if (process.env.NODE_ENV !== 'production') { // During a gatsby development build (gatsby develop) we do nothing.
return
}
replaceHeadComponents(getHeadComponentsNoJS(getHeadComponents(), pluginOptions))
replacePostBodyComponents(getPostBodyComponentsNoJS(getPostBodyComponents(), pluginOptions))
}

0 comments on commit 2f9c265

Please sign in to comment.
You can’t perform that action at this time.