Skip to content

Commit

Permalink
Improve webpack compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed May 20, 2023
1 parent 1c34b86 commit 0e3da30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default class LiveServer {

// enable CORS
if (cors) {
const myModule = (await import(/*webpackIgnore: true*/ 'cors')).default
const myModule = (await import('cors')).default
app.use(myModule({ credentials: true }))
// app.use(require('cors')({ credentials: true }))
}
Expand Down Expand Up @@ -334,10 +334,10 @@ export default class LiveServer {
if (typeof mw === 'string') {
const ext = path.extname(mw).toLocaleLowerCase()
if (ext !== '.js') {
mw = (await import(/*webpackIgnore: true*/ path.join(__dirname, 'middleware', `${mw}.js`))).default
mw = (await import(path.join(__dirname, 'middleware', `${mw}.js`))).default
// mw = require(path.join(__dirname, 'middleware', `${mw}.js`)).default
} else {
mw = (await import(/*webpackIgnore: true*/ mw)).default
mw = (await import(mw)).default
// mw = require(mw)
}
if (typeof mw !== 'function') message.error(`middleware ${mw} does not return a function`, null, false)
Expand Down Expand Up @@ -388,7 +388,7 @@ export default class LiveServer {
via: true
}

const { proxyMiddleware } = await import(/*webpackIgnore: true*/ './middleware/proxy.js')
const { proxyMiddleware } = await import('./middleware/proxy.js')
// const { proxyMiddleware } = require('./middleware/proxy')
// @ts-expect-error
app.use(ROUTE, proxyMiddleware(proxyOpts, injectBody || false))
Expand Down Expand Up @@ -427,7 +427,7 @@ export default class LiveServer {
let httpsConfig = _https as Certificate

if (typeof _https === 'string') {
httpsConfig = (await import(/*webpackIgnore: true*/ path.resolve(process.cwd(), _https))).default
httpsConfig = (await import(path.resolve(process.cwd(), _https))).default
// httpsConfig = require(path.resolve(process.cwd(), _https))
}

Expand Down
13 changes: 10 additions & 3 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ export const fileDoesExist = (path: string): Promise<boolean> => {
export const loadConfigFile = async configPath => {
let config = {}

if (os.platform() === 'win32') {
// five-server-vscode uses webpack and the code is converted to cjs
// @ts-ignore
if (typeof WEBPACK_COMMONJS2 !== 'undefined') {
delete require.cache[configPath]
config = require(configPath)
}
// on windows we need to add file:// for dynamic file imports
else if (os.platform() === 'win32') {
const p = `file://${configPath}`
config = (await import(/*webpackIgnore: true*/ p)).default
config = (await import(p)).default
} else {
config = (await import(/*webpackIgnore: true*/ configPath)).default
config = (await import(configPath)).default
}

return config
Expand Down

0 comments on commit 0e3da30

Please sign in to comment.