diff --git a/docs/webpack.md b/docs/webpack.md index 6480c11ab..b34c55814 100644 --- a/docs/webpack.md +++ b/docs/webpack.md @@ -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) ``` diff --git a/package/__tests__/environment.js b/package/__tests__/environment.js index 83e5b9d58..c5cb81c1a 100644 --- a/package/__tests__/environment.js +++ b/package/__tests__/environment.js @@ -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(() => { @@ -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', () => { @@ -63,7 +60,7 @@ describe('Environment', () => { resolve('app', 'javascript'), 'node_modules', 'app/assets', - '/etc/yarn', + '/etc/yarn' ]) }) diff --git a/package/asset_host.js b/package/asset_host.js deleted file mode 100644 index d66ca5228..000000000 --- a/package/asset_host.js +++ /dev/null @@ -1,20 +0,0 @@ -const config = require('./config') -const { resolve } = require('path') - -const removeOuterSlashes = string => - string.replace(/^\/*/, '').replace(/\/*$/, '') - -const formatPublicPath = (host = '', path = '') => { - let formattedHost = removeOuterSlashes(host) - if (formattedHost && !/^http/i.test(formattedHost)) { - formattedHost = `//${formattedHost}` - } - const formattedPath = removeOuterSlashes(path) - return `${formattedHost}/${formattedPath}/` -} - -module.exports = { - path: resolve('public', config.public_output_path), - publicPath: `/${config.public_output_path}/`.replace(/([^:]\/)\/+/g, '$1'), - publicPathWithHost: formatPublicPath(process.env.WEBPACKER_ASSET_HOST, config.public_output_path) -} diff --git a/package/config.js b/package/config.js index ab111cff5..da81d1855 100644 --- a/package/config.js +++ b/package/config.js @@ -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 diff --git a/package/environment.js b/package/environment.js index c7044e37f..0658c0dbc 100644 --- a/package/environment.js +++ b/package/environment.js @@ -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() @@ -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 } @@ -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: { diff --git a/package/environments/development.js b/package/environments/development.js index 41cc31483..512eed723 100644 --- a/package/environments/development.js +++ b/package/environments/development.js @@ -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() { @@ -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 }, diff --git a/package/index.js b/package/index.js index 63d6662d7..289853a12 100644 --- a/package/index.js +++ b/package/index.js @@ -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 = () => { @@ -17,5 +16,5 @@ const createEnvironment = () => { const environment = createEnvironment() module.exports = { - environment, config, assetHost, loaders, Environment + environment, config, loaders, Environment } diff --git a/package/rules/file.js b/package/rules/file.js index c101b99c5..bd7690a06 100644 --- a/package/rules/file.js +++ b/package/rules/file.js @@ -1,6 +1,5 @@ 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)?$/, @@ -8,8 +7,7 @@ module.exports = { loader: 'file-loader', options: { name: '[path][name]-[hash].[ext]', - context: join(source_path), - publicPath: assetHost.publicPathWithHost + context: join(source_path) } }] } diff --git a/package/utils/__tests__/objectify.js b/package/utils/__tests__/objectify.js index 2c64c6248..dfe3030bc 100644 --- a/package/utils/__tests__/objectify.js +++ b/package/utils/__tests__/objectify.js @@ -1,5 +1,4 @@ /* global test expect */ -console.log('objectify test cwd', process.cwd()); const objectify = require('../objectify') diff --git a/yarn.lock b/yarn.lock index ec6d92774..0a88b7e55 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3539,6 +3539,10 @@ mixin-object@^2.0.1: dependencies: minimist "0.0.8" +ms@0.7.2: + version "0.7.2" + resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"