Skip to content

Commit

Permalink
Remove hardcoded public path (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravtiwari authored Dec 15, 2017
1 parent 6bccc41 commit ad812fa
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 53 deletions.
4 changes: 2 additions & 2 deletions docs/webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ If you need access to configs within Webpacker's configuration,
you can import them like so:

```js
const { config, asset_host } = require('@rails/webpacker')
const { config } = require('@rails/webpacker')

console.log(asset_host.publicPathWithHost)
console.log(config.output_path)
console.log(config.source_path)
```

Expand Down
31 changes: 14 additions & 17 deletions package/__tests__/environment.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/* global test expect, describe */
/* global test expect, describe, afterAll, beforeEach */

// environment.js expects to find config/webpacker.yml and resolved modules from
// the root of a Rails project
const cwd = process.cwd();
const chdirApp = () => process.chdir('test/test_app')
const chdirCwd = () => process.chdir(cwd)
chdirApp();

const { resolve, join } = require('path')
const { sync } = require('glob')
const assert = require('assert')

const { ConfigList, ConfigObject } = require('../config_types')
const chdirApp = () => process.chdir('test/test_app')
const chdirCwd = () => process.chdir(process.cwd())
chdirApp()

const { resolve } = require('path')
const rules = require('../rules')
const { ConfigList } = require('../config_types')
const Environment = require('../environment')

describe('Environment', () => {
afterAll(chdirCwd);
afterAll(chdirCwd)

let environment;
let environment

describe('toWebpackConfig', () => {
beforeEach(() => {
Expand All @@ -40,11 +37,11 @@ describe('Environment', () => {

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;
const defaultRules = Object.keys(rules)
const configRules = config.module.rules

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

test('should return default plugins', () => {
Expand All @@ -63,7 +60,7 @@ describe('Environment', () => {
resolve('app', 'javascript'),
'node_modules',
'app/assets',
'/etc/yarn',
'/etc/yarn'
])
})

Expand Down
20 changes: 0 additions & 20 deletions package/asset_host.js

This file was deleted.

3 changes: 3 additions & 0 deletions package/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ if (config.dev_server) {
})
}

config.outputPath = resolve('public', config.public_output_path)
config.publicPath = `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1')

module.exports = config
7 changes: 3 additions & 4 deletions package/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const { ConfigList, ConfigObject } = require('./config_types')
const rules = require('./rules')
const config = require('./config')
const assetHost = require('./asset_host')

const getLoaderList = () => {
const result = new ConfigList()
Expand All @@ -28,7 +27,7 @@ const getPluginList = () => {
result.append('Environment', new webpack.EnvironmentPlugin(JSON.parse(JSON.stringify(process.env))))
result.append('CaseSensitivePaths', new CaseSensitivePathsPlugin())
result.append('ExtractText', new ExtractTextPlugin('[name]-[contenthash].css'))
result.append('Manifest', new ManifestPlugin({ publicPath: assetHost.publicPath, writeToFileEmit: true }))
result.append('Manifest', new ManifestPlugin({ publicPath: config.publicPath, writeToFileEmit: true }))
return result
}

Expand Down Expand Up @@ -68,8 +67,8 @@ const getBaseConfig = () =>
output: {
filename: '[name]-[chunkhash].js',
chunkFilename: '[name]-[chunkhash].chunk.js',
path: assetHost.path,
publicPath: assetHost.publicPath
path: config.outputPath,
publicPath: config.publicPath
},

resolve: {
Expand Down
7 changes: 3 additions & 4 deletions package/environments/development.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const webpack = require('webpack')
const Environment = require('../environment')
const { dev_server: devServer } = require('../config')
const assetHost = require('../asset_host')
const { dev_server: devServer, outputPath: contentBase, publicPath } = require('../config')

module.exports = class extends Environment {
constructor() {
Expand All @@ -27,11 +26,11 @@ module.exports = class extends Environment {
port: devServer.port,
https: devServer.https,
hot: devServer.hmr,
contentBase: assetHost.path,
contentBase,
inline: devServer.inline,
useLocalIp: devServer.use_local_ip,
public: devServer.public,
publicPath: assetHost.publicPath,
publicPath,
historyApiFallback: {
disableDotRule: true
},
Expand Down
3 changes: 1 addition & 2 deletions package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { resolve } = require('path')
const { existsSync } = require('fs')
const Environment = require('./environment')
const config = require('./config')
const assetHost = require('./asset_host')
const loaders = require('./rules')

const createEnvironment = () => {
Expand All @@ -17,5 +16,5 @@ const createEnvironment = () => {
const environment = createEnvironment()

module.exports = {
environment, config, assetHost, loaders, Environment
environment, config, loaders, Environment
}
4 changes: 1 addition & 3 deletions package/rules/file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const { join } = require('path')
const { source_path } = require('../config')
const assetHost = require('../asset_host')

module.exports = {
exclude: /\.(js|jsx|coffee|ts|tsx|vue|elm|scss|sass|css|html|json)?(\.erb)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[path][name]-[hash].[ext]',
context: join(source_path),
publicPath: assetHost.publicPathWithHost
context: join(source_path)
}
}]
}
1 change: 0 additions & 1 deletion package/utils/__tests__/objectify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global test expect */
console.log('objectify test cwd', process.cwd());

const objectify = require('../objectify')

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,10 @@ mixin-object@^2.0.1:
dependencies:
minimist "0.0.8"

[email protected]:
version "0.7.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit ad812fa

Please sign in to comment.