Skip to content

Commit 5c143e8

Browse files
authored
fix(windows): fix component import paths on windows (#46)
fixes #43
1 parent 82ca861 commit 5c143e8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

bundler.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const fs = require('fs')
2-
const path = require('path').posix
32
const execSync = require('child_process').execSync
3+
const path = require('path')[process.platform === 'win32' ? 'win32' : 'posix']
44

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

8+
const pathCrossEnv = (path) =>
9+
process.platform !== 'win32' ? path : path.replace(/\\/g, '/')
10+
811
module.exports = function bundle (Components, out, config) {
912
if (!Components.length) {
1013
return
@@ -27,11 +30,15 @@ module.exports = function bundle (Components, out, config) {
2730
`
2831
}
2932
if (reactComponents.length) {
33+
const reactWrapperRelPath = pathCrossEnv(
34+
path.relative(absoluteOut, path.join(__dirname, REACT_WRAPPER))
35+
)
36+
3037
init = init + `
3138
import React from "react";\n
3239
import ReactDOM from "react-dom";\n
3340
34-
import ReactWrapper from '${path.relative(absoluteOut, path.join(__dirname, REACT_WRAPPER))}';\n
41+
import ReactWrapper from '${reactWrapperRelPath}';\n
3542
window.React = React;\n
3643
window.ReactDOM = ReactDOM;\n
3744
window.ReactWrapper = ReactWrapper;\n
@@ -47,8 +54,9 @@ module.exports = function bundle (Components, out, config) {
4754
if (config.betterDocs.component) {
4855
if(config.betterDocs.component.wrapper) {
4956
const absolute = path.resolve(config.betterDocs.component.wrapper)
57+
const relative = pathCrossEnv(path.relative(absoluteOut, absolute))
5058
init +=`
51-
import _CustomWrapper from '${path.relative(absoluteOut, absolute)}';\n
59+
import _CustomWrapper from '${relative}';\n
5260
window._CustomWrapper = _CustomWrapper;\n
5361
`
5462
}
@@ -57,10 +65,10 @@ module.exports = function bundle (Components, out, config) {
5765
init = `${config.betterDocs.component.entry.join('\n')}\n${init}`
5866
}
5967
}
60-
68+
6169
const entryFile = init + Components.map((c, i) => {
6270
const { displayName, filePath, type } = c.component
63-
const relativePath = path.relative(absoluteOut, filePath)
71+
const relativePath = pathCrossEnv(path.relative(absoluteOut, filePath))
6472
const name = `Component${i}`
6573
return [
6674
`import ${name} from '${relativePath}';`,
@@ -82,4 +90,4 @@ module.exports = function bundle (Components, out, config) {
8290
}
8391
throw error
8492
}
85-
}
93+
}

0 commit comments

Comments
 (0)