Skip to content

Commit

Permalink
Add eslint-loader to prevent attempted use of native Node.js APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
detroitenglish committed Aug 31, 2018
1 parent 77d131d commit b007605
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
20 changes: 15 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
/* eslint-env node */

module.exports = {
const dev = !process.env.WORKER_ACTION

const config = {
env: {
es6: true,
},
parser: 'babel-eslint',
plugins: ['prettier', 'babel'],
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 9,
parser: 'babel-eslint',
},
rules: {
'no-undef': 'error',
},
}

if (dev) {
config.plugins = ['babel', 'prettier']
config.extends = ['eslint:recommended', 'plugin:prettier/recommended']
config.rules = {
'prettier/prettier': 'error',
'no-console': 'off',
'require-await': 'error',
},
}
}

module.exports = config
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
},
"homepage": "https://github.com/detroitenglish/cloudflare-worker-webpack-boilerplate",
"scripts": {
"lint": "./node_modules/.bin/eslint webpack.config.js",
"lint": "./node_modules/.bin/eslint src webpack.config.js",
"precommit": "./node_modules/.bin/eslint src webpack.config.js",
"build": "cross-env WORKER_ACTION=build webpack",
"deploy": "cross-env WORKER_ACTION=deploy webpack --display none",
"build:example": "cross-env EXAMPLE_WORKER=1 WORKER_ACTION=build webpack",
"deploy:example": "cross-env EXAMPLE_WORKER=1 WORKER_ACTION=deploy webpack --display none"
"build": "cross-env WORKER_ACTION=build webpack --bail",
"deploy": "cross-env WORKER_ACTION=deploy webpack --display none --bail",
"build:example": "cross-env EXAMPLE_WORKER=1 WORKER_ACTION=build webpack --bail",
"deploy:example": "cross-env EXAMPLE_WORKER=1 WORKER_ACTION=deploy webpack --display none --bail"
},
"devDependencies": {
"babel-eslint": "^9.0.0",
"eslint": "^5.4.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-prettier": "^2.6.2",
"husky": "^0.14.3",
"prettier": "^1.14.2"
Expand All @@ -33,12 +30,16 @@
"@babel/core": "^7.0.0",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-eslint": "^9.0.0",
"babel-loader": "^8.0.0",
"clean-webpack-plugin": "^0.1.19",
"cloudflare-worker-webpack-plugin": "^1.2.3",
"colors": "^1.3.2",
"cross-env": "^5.2.0",
"dotenv": "^6.0.0",
"eslint": "^5.4.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-babel": "^5.1.0",
"lodash.sample": "^4.2.1",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0"
Expand Down
53 changes: 41 additions & 12 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,69 @@ const webpack = require('webpack')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CloudflareWorkerPlugin = require('cloudflare-worker-webpack-plugin')

if (!require('fs').existsSync(__dirname + '/.env'))
throw new Error(`'.env' configuration file not found`)

const isExample = !!process.env.EXAMPLE_WORKER
const useColors = !process.env.NO_COLORS
const useEmoji = !process.env.NO_EMOJI
const stfu = !process.env.NO_VERBOSE
const printOutput = !process.env.NO_VERBOSE

const entry = __dirname + `/src/${isExample ? `example.worker` : `worker`}.js`

const filename = `bundled-${isExample ? `example-worker` : `worker`}.js`

let exampleGreeting
if (isExample) exampleGreeting = process.env.EXAMPLE_GREETING || 'Aloha'

startupText()

module.exports = {
entry: __dirname + `/src/${isExample ? `example.worker` : `worker`}.js`,
entry,

output: {
path: __dirname + '/dist',
filename: `bundled-${isExample ? `example-worker` : `worker`}.js`,
filename,
},
// This lets Webpack know the context in which our script will run
target: 'webworker',
// This lets Webpack know that we mean business

// Let Webpack know we mean business
mode: 'production',
// This runs all JS through Babel to ensure compatibility with the Cloudflare Worker (i.e. Chrome) runtime

// Let Webpack know the context in which our script will run
target: 'webworker',

module: {
rules: [
/*
This causes scripts attempting to use Node's built-in APIs or global objects
to fail, e.g. `Buffer` and `require('crypto')`. Install and require() shim
modules manually if you wish to use these features.
*/
{
include: entry,
loader: 'eslint-loader',
options: {
failOnError: true,
pre: true,
},
},
/*
This runs all JS through Babel to ensure compatibility with the
Cloudflare Worker (i.e. latest Chrome) runtime.
*/
{
test: /\.js$/,
loader: 'babel-loader',
},
],
},

// Prevent Webpack from getting angry if we bundle a large script
performance: {
hints: false,
},

// This prevents Webpack from trying to shim any Node.js APIs
// Prevent Webpack from shimming Node features and bloating our Worker scripts
node: false,

optimization: {
Expand All @@ -49,7 +77,7 @@ module.exports = {
},

plugins: [
// First, remove any previous builds in the dist folder
// Remove any previous builds in the dist folder
new CleanWebpackPlugin(`dist/*`, {
root: __dirname,
verbose: false,
Expand All @@ -64,16 +92,17 @@ module.exports = {
INJECTED_VARIABLE: JSON.stringify(exampleGreeting),
}),

// This plugin deploys our worker script to Cloudflare,
// and manages route matching patterns
/*
This deploys our worker script to Cloudflare and manages route patterns
*/
new CloudflareWorkerPlugin(
process.env.CLOUDFLARE_AUTH_EMAIL,
process.env.CLOUDFLARE_AUTH_KEY,
{
zone: process.env.CLOUDFLARE_ZONE_ID,
site: process.env.CLOUDFLARE_SITE_NAME,
pattern: process.env.ROUTE_PATTERN,
verbose: stfu,
verbose: printOutput,
colors: useColors,
emoji: useEmoji,
enabled: process.env.WORKER_ACTION === 'deploy',
Expand Down

0 comments on commit b007605

Please sign in to comment.