Skip to content

Commit

Permalink
Merge pull request #178 from negezor/master
Browse files Browse the repository at this point in the history
feat: Improvements and update dependencies
  • Loading branch information
dohomi authored Nov 21, 2018
2 parents 0f591f2 + 663f1a8 commit d422ec5
Show file tree
Hide file tree
Showing 10 changed files with 6,746 additions and 5,046 deletions.
50 changes: 33 additions & 17 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const {join} = require('path')
const { join } = require('path')
const nodeExternals = require('webpack-node-externals')

const IS_LINK_RE = /^https?:\/\//
const ALLOW_GQL_FILES = ['.graphql', '.gql']

module.exports = function (moduleOptions) {
const isNuxtVersion2 = this.options.build.transpile
// Fetch `apollo` option from `nuxt.config.js`
const options = this.options.apollo || moduleOptions
// Check network interfaces valid definition
const {clientConfigs} = options
const { clientConfigs } = options
const clientConfigKeys = Object.keys(clientConfigs)

if (clientConfigKeys.length === 0) throw new Error('[Apollo module] No clientConfigs found in apollo configuration')
Expand All @@ -31,9 +32,9 @@ module.exports = function (moduleOptions) {

options.tokenName = options.tokenName || 'apollo-token'
options.tokenExpires = options.tokenExpires || 7
options.authenticationType = (options.authenticationType === undefined) ? 'Bearer' : options.authenticationType
options.authenticationType = options.authenticationType === undefined ? 'Bearer' : options.authenticationType

if (options.hasOwnProperty('errorHandler') && typeof options.errorHandler !== 'function') {
if (options.errorHandler !== undefined && typeof options.errorHandler !== 'function') {
throw new Error(`[Apollo module] errorHandler must be a function.`)
}

Expand All @@ -50,31 +51,46 @@ module.exports = function (moduleOptions) {
}

// Add graphql loader
this.extendBuild((config, {isServer}) => {
config.resolve.extensions = config.resolve.extensions.concat('.graphql', '.gql')
this.extendBuild((config, { isServer }) => {
const { resolve } = config

const gqlRules = {
test: /\.(graphql|gql)$/,
use: 'graphql-tag/loader',
exclude: /(node_modules)/
}
const hasGqlExt = resolve.extensions.some(ext => (
ALLOW_GQL_FILES.includes(ext)
))

if (options.includeNodeModules) {
delete gqlRules.exclude
if (!hasGqlExt) {
resolve.extensions = [...resolve.extensions, ...ALLOW_GQL_FILES]
}

const { rules } = config.module

const hasGqlLoader = rules.some(rule => (
rule.use === 'graphql-tag/loader'
))

if (!hasGqlLoader) {
const gqlRules = {
test: /\.(graphql|gql)$/,
use: 'graphql-tag/loader'
}

if (!options.includeNodeModules) {
gqlRules.exclude = /(node_modules)/
}

if( ! config.module.rules.find( (rule) => rule.use === 'graphql-tag/loader' ) ){
config.module.rules.push(gqlRules)
rules.push(gqlRules)
}

if (isServer) {
const apolloModuleRe = /^vue-cli-plugin-apollo/

// Adding proper way of handling whitelisting with Nuxt 2
if (isNuxtVersion2) {
this.options.build.transpile.push(/^vue-cli-plugin-apollo/)
this.options.build.transpile.push(apolloModuleRe)
} else {
config.externals = [
nodeExternals({
whitelist: [/^vue-cli-plugin-apollo/]
whitelist: [apolloModuleRe]
})
]
}
Expand Down
Loading

0 comments on commit d422ec5

Please sign in to comment.