Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Golding committed Apr 26, 2020
0 parents commit bb38c41
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


# This project
dist
vendor
build
tmp
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

Collection of Webpack helper functions.
101 changes: 101 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const webpack = require('webpack')

const WebpackHelpers = {
mode: () => {
// https://webpack.js.org/configuration/mode/
function setEnvironmentVariable(currentValue, defaultValue) {
const aliases = {
dev: 'development',
prod: 'production'
}
if (aliases[currentValue]) {
currentValue = aliases[currentValue]
}
if (!currentValue) {
currentValue = defaultValue
}
return currentValue
}

console.log('Current process.env.NODE_ENV:', process.env.NODE_ENV) // TODO: Logs not going through. Is this NODE_ENV even going through consider NODE_ENV might just be local to that terminal.
const mode = setEnvironmentVariable(process.env.NODE_ENV, 'production') // TODO: Use constants.
console.log('Set mode:', mode)
return mode
},
Rules: {
json: () => {
const {
delimitedArrayToReadableStringMap
} = (require('js-functions')).BaseUtility

return {
test: /messages\.json$/,
loaders: [{
loader: 'content-loader',
options: {
/**
* @param {string} data
*/
function: (data) => { // TODO: Name this as automatic way of setting keys.
/**
* @type {Object}
*/
const object = JSON.parse(data)
const keys = Object.keys(object)
const map = delimitedArrayToReadableStringMap(keys, '-')
return JSON.stringify(map)
}
}
}]
}
},
htmlString: () => {
return {
test: /\.html$/,
loaders: [
'raw-loader'
]
}
},
cssString: () => {
return {
test: /\.css$/,
loaders: [
'to-string-loader',
'css-loader'
]
}
},
image: () => {
return {
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'components/assets/images/'
// the images will be emited to dist/.../components/assets/images/ folder
}
}
}
},
Plugins: {
jquery: () => {
// Use the ProvidePlugin constructor to inject jquery implicit globals
return new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': "jquery'",
'window.$': 'jquery'
})
},
bundleAnalyzer: () => {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
// https://github.com/webpack-contrib/webpack-bundle-analyzer
return new BundleAnalyzerPlugin({
analyzerMode: 'static'
})
}
}
}

module.exports = WebpackHelpers
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "webpack-helpers",
"version": "1.0.0",
"description": "Collection of webpack helpers for easily generating config.",
"main": "index.js",
"scripts": {

},
"keywords": [],
"author": "Damien Golding",
"license": "MIT",
"dependencies": {
"js-functions": "git+https://github.com/goldingdamien/js-functions.git"
}
}

0 comments on commit bb38c41

Please sign in to comment.