Skip to content

Commit

Permalink
Adds tests for default webpack package config
Browse files Browse the repository at this point in the history
Adds a root module to the npm pacakge to make it possible to customize
the RAILS_ROOT location. Supporting this approach means ensuring that
relative paths for various Webpack configuration items resolve relative
to the given root if it exists.
  • Loading branch information
rossta committed Dec 13, 2017
1 parent d839231 commit d4ea578
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"coffeescript": ">= 1.12.7 || >= 2.x"
},
"scripts": {
"test": "jest",
"test": "RAILS_ROOT=test/test_app jest",
"lint": "eslint {package,lib}/"
},
"repository": {
Expand Down
66 changes: 66 additions & 0 deletions package/__tests__/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { resolve, join } = require('path')
const { sync } = require('glob')
const assert = require('assert')

const { ConfigList, ConfigObject } = require('../config_types')

const Environment = require('../environment')

describe('Environment', () => {
let environment;

describe('toWebpackConfig', () => {
beforeEach(() => {
environment = new Environment()
})

test('should return entry', () => {
const config = environment.toWebpackConfig()
expect(config.entry.application).toEqual(resolve('test', 'test_app', 'app', 'javascript', 'packs', 'application.js'))
})

test('should return output', () => {
const config = environment.toWebpackConfig()
expect(config.output.filename).toEqual('[name]-[chunkhash].js')
expect(config.output.chunkFilename).toEqual('[name]-[chunkhash].chunk.js')
expect(config.output.path).toEqual(resolve('test', 'test_app', 'public', 'packs-test'))
expect(config.output.publicPath).toEqual('/packs-test/')
})

test('should return default loader rules for each file in config/loaders', () => {
const config = environment.toWebpackConfig()
const rules = Object.keys(require('../rules'))
const [{ oneOf: configRules }] = config.module.rules;

expect(rules.length).toBeGreaterThan(1)
expect(configRules.length).toEqual(rules.length)
})

test('should return default plugins', () => {
const config = environment.toWebpackConfig()
expect(config.plugins.length).toEqual(4)
})

test('should return default resolveLoader', () => {
const config = environment.toWebpackConfig()
expect(config.resolveLoader.modules).toEqual([join('test', 'test_app','node_modules')])
})

test('should return default resolve.modules with additions', () => {
const config = environment.toWebpackConfig()
expect(config.resolve.modules).toEqual([
resolve('test', 'test_app', 'app', 'javascript'),
join('test', 'test_app', 'node_modules'),
resolve('test', 'test_app', 'app', 'assets'),
'/etc/yarn',
])
})

test('returns plugins property as Array', () => {
const config = environment.toWebpackConfig()

expect(config.plugins).toBeInstanceOf(Array)
expect(config.plugins).not.toBeInstanceOf(ConfigList)
})
})
})
4 changes: 2 additions & 2 deletions package/asset_host.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const config = require('./config')
const { resolve } = require('path')
const { resolveRoot } = require('./root')

const removeOuterSlashes = string =>
string.replace(/^\/*/, '').replace(/\/*$/, '')
Expand All @@ -14,7 +14,7 @@ const formatPublicPath = (host = '', path = '') => {
}

module.exports = {
path: resolve('public', config.public_output_path),
path: resolveRoot('public', config.public_output_path),
publicPath: `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1'),
publicPathWithHost: formatPublicPath(process.env.WEBPACKER_ASSET_HOST, config.public_output_path)
}
4 changes: 2 additions & 2 deletions package/config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { resolve } = require('path')
const { safeLoad } = require('js-yaml')
const { readFileSync } = require('fs')
const deepMerge = require('./utils/deep_merge')

const { resolveRoot } = require('./root')
const defaultFilePath = require.resolve('../lib/install/config/webpacker.yml')
const filePath = resolve('config', 'webpacker.yml')
const filePath = resolveRoot('config', 'webpacker.yml')

const environment = process.env.NODE_ENV || 'development'
const defaultConfig = safeLoad(readFileSync(defaultFilePath), 'utf8')[environment]
Expand Down
11 changes: 6 additions & 5 deletions package/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { ConfigList, ConfigObject } = require('./config_types')
const rules = require('./rules')
const config = require('./config')
const assetHost = require('./asset_host')
const { root, resolveRoot } = require('./root')

const getLoaderList = () => {
const result = new ConfigList()
Expand Down Expand Up @@ -43,7 +44,7 @@ const getExtensionsGlob = () => {
const getEntryObject = () => {
const result = new ConfigObject()
const glob = getExtensionsGlob()
const rootPath = join(config.source_path, config.source_entry_path)
const rootPath = resolveRoot(config.source_path, config.source_entry_path)
const paths = sync(join(rootPath, glob))
paths.forEach((path) => {
const namespace = relative(join(rootPath), dirname(path))
Expand All @@ -55,10 +56,10 @@ const getEntryObject = () => {

const getModulePaths = () => {
const result = new ConfigList()
result.append('source', resolve(config.source_path))
result.append('node_modules', 'node_modules')
result.append('source', resolveRoot(config.source_path))
result.append('node_modules', join(root, 'node_modules'))
if (config.resolved_paths) {
config.resolved_paths.forEach(path => result.append(basename(path), path))
config.resolved_paths.forEach(path => result.append(basename(path), resolveRoot(path)))
}
return result
}
Expand All @@ -77,7 +78,7 @@ const getBaseConfig = () =>
},

resolveLoader: {
modules: ['node_modules']
modules: [join(root, 'node_modules')]
},

node: {
Expand Down
11 changes: 11 additions & 0 deletions package/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { resolve, join } = require('path')
const root = process.env.RAILS_ROOT || '';
const resolveRoot = (first, ...args) => first.startsWith('/')
? join(first, ...args)
: resolve(root, first, ...args)


module.exports = {
root,
resolveRoot
}
65 changes: 65 additions & 0 deletions test/test_app/config/webpacker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
source_path: app/javascript
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker

# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths:
- app/assets
- /etc/yarn

# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

extensions:
- .coffee
- .erb
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default
compile: true

# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
disable_host_check: true
use_local_ip: false

test:
<<: *default
compile: true

# Compile test packs to a separate directory
public_output_path: packs-test

production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Cache manifest.json for performance
cache_manifest: true
21 changes: 19 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,13 @@ date-now@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"

debug@^2.2.0, debug@^2.6.8:
debug@^2.2.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351"
dependencies:
ms "0.7.2"

debug@^2.6.8:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
dependencies:
Expand Down Expand Up @@ -2296,7 +2302,18 @@ glob@^6.0.4:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@~7.1.1:
glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
version "7.1.1"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.2"
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.1, glob@^7.1.2, glob@~7.1.1:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
dependencies:
Expand Down

0 comments on commit d4ea578

Please sign in to comment.