Skip to content

fix(windows): fix component import paths on windows #46

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions bundler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const fs = require('fs')
const path = require('path').posix
const execSync = require('child_process').execSync
const path = require('path')[process.platform === 'win32' ? 'win32' : 'posix']

const VUE_WRAPPER = process.env.IS_DEV ? 'src/vue-wrapper.js' : 'lib/vue-wrapper.js'
const REACT_WRAPPER = process.env.IS_DEV ? 'src/react-wrapper.jsx' : 'lib/react-wrapper.js'

const pathCrossEnv = (path) =>
process.platform !== 'win32' ? path : path.replace(/\\/g, '/')

module.exports = function bundle (Components, out, config) {
if (!Components.length) {
return
Expand All @@ -27,11 +30,15 @@ module.exports = function bundle (Components, out, config) {
`
}
if (reactComponents.length) {
const reactWrapperRelPath = pathCrossEnv(
path.relative(absoluteOut, path.join(__dirname, REACT_WRAPPER))
)

init = init + `
import React from "react";\n
import ReactDOM from "react-dom";\n

import ReactWrapper from '${path.relative(absoluteOut, path.join(__dirname, REACT_WRAPPER))}';\n
import ReactWrapper from '${reactWrapperRelPath}';\n
window.React = React;\n
window.ReactDOM = ReactDOM;\n
window.ReactWrapper = ReactWrapper;\n
Expand All @@ -47,8 +54,9 @@ module.exports = function bundle (Components, out, config) {
if (config.betterDocs.component) {
if(config.betterDocs.component.wrapper) {
const absolute = path.resolve(config.betterDocs.component.wrapper)
const relative = pathCrossEnv(path.relative(absoluteOut, absolute))
init +=`
import _CustomWrapper from '${path.relative(absoluteOut, absolute)}';\n
import _CustomWrapper from '${relative}';\n
window._CustomWrapper = _CustomWrapper;\n
`
}
Expand All @@ -57,10 +65,10 @@ module.exports = function bundle (Components, out, config) {
init = `${config.betterDocs.component.entry.join('\n')}\n${init}`
}
}

const entryFile = init + Components.map((c, i) => {
const { displayName, filePath, type } = c.component
const relativePath = path.relative(absoluteOut, filePath)
const relativePath = pathCrossEnv(path.relative(absoluteOut, filePath))
const name = `Component${i}`
return [
`import ${name} from '${relativePath}';`,
Expand All @@ -82,4 +90,4 @@ module.exports = function bundle (Components, out, config) {
}
throw error
}
}
}