Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Use require.context for hot reload of ream entry
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Jun 5, 2018
1 parent bd4ee22 commit 5688c70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
27 changes: 16 additions & 11 deletions app/create-app-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ const pathToId = file => {
return path.basename(slash(file)).replace(/\W/g, '_')
}

module.exports = api => {
let entryExists = false
if (api.options.entry) {
try {
require.resolve(api.resolveBaseDir(api.options.entry))
entryExists = true
} catch (err) {}
}
const escapeRegexp = s => s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')

module.exports = api => {
const enhanceAppFiles = [...api.enhanceAppFiles].map((filepath, index) => ({
id: `${pathToId(filepath)}_${index}`,
filepath: slash(filepath)
Expand All @@ -33,7 +27,7 @@ module.exports = api => {
import Vue from 'vue'
import Meta from 'vue-meta'
import Router from 'vue-router'
import { getRequireDefault } from '#app/utils'
import { getRequireDefault, importContextModule } from '#app/utils'
Vue.config.productionTip = false
Expand All @@ -47,8 +41,19 @@ module.exports = api => {
})
const _entry = ${
entryExists ? `getRequireDefault(require('#app-entry'))` : `{}`
}
api.options.entry
? `
getRequireDefault(
importContextModule(
require.context(
${JSON.stringify(api.baseDir)},
false,
/^${escapeRegexp('./' + api.options.entry)}$/
),
${JSON.stringify('./' + api.options.entry)}
)`
: `undefined`
}) || {}
const enhanceApp = getRequireDefault(require('#app/enhance-app'))
Expand Down
12 changes: 12 additions & 0 deletions app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export const getRequireDefault = obj => {
return obj && obj.hasOwnProperty('default') ? obj.default : obj
}

export const importContextModule = (context, path) => {
let moduleExists = false
try {
context.resolve(path)
moduleExists = true
} catch (err) {}

if (moduleExists) {
return context(path)
}
}

export const setInitialData = vm => {
const { getInitialData, initialDataKey } = vm.$options

Expand Down
7 changes: 0 additions & 7 deletions lib/webpack/webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ const resolveModules = config => {
}

module.exports = (api, config, type) => {
if (api.options.entry) {
config.resolve.alias.set(
'#app-entry$',
api.resolveBaseDir(api.options.entry)
)
}

config.resolve.alias
.set('#create-app$', api.resolveOutDir('create-app.js'))
.set('#base', api.baseDir)
Expand Down

0 comments on commit 5688c70

Please sign in to comment.