Skip to content

Commit

Permalink
fix styleInject path on windows (#81)
Browse files Browse the repository at this point in the history
* fix styleInject path on windows

* fix lint

* refactor tests

* tweaks

* try bare hash

* remove hash from names in tests

* normalize path in sourcemap

* fix typo

* tweaks

* tweaks
  • Loading branch information
egoist authored Mar 2, 2018
1 parent 268ecfe commit db7db7a
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 397 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra'
import { createFilter } from 'rollup-pluginutils'
import Concat from 'concat-with-sourcemaps'
import Loaders from './loaders'
import normalizePath from './utils/normalize-path'
import humanlizePath from './utils/humanlize-path'

/**
* The options that could be `boolean` or `object`
Expand Down Expand Up @@ -89,7 +89,7 @@ export default (options = {}) => {

return {
code: res.code,
map: res.map ? JSON.parse(res.map.toString()) : { mappings: '' }
map: res.map || { mappings: '' }
}
},

Expand All @@ -105,15 +105,15 @@ export default (options = {}) => {
filepath = path.join(path.dirname(opts.file), basename + '.css')
}
}
filepath = normalizePath(filepath)
filepath = humanlizePath(filepath)
const concat = new Concat(true, filepath, '\n')
for (const res of extracted.values()) {
const relative = normalizePath(res.id)
const map = res.map ? JSON.parse(res.map.toString()) : null
const relative = humanlizePath(res.id)
const map = res.map || null
if (map) {
map.file = filepath
map.sources = map.sources.map(source =>
normalizePath(path.join(path.dirname(opts.file), source))
humanlizePath(path.join(path.dirname(opts.file), source))
)
}
concat.add(relative, res.code, map)
Expand Down
4 changes: 2 additions & 2 deletions src/less-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pify from 'pify'
import normalizePath from './utils/normalize-path'
import humanlizePath from './utils/humanlize-path'
import localRequire from './utils/local-require'

export default {
Expand All @@ -16,7 +16,7 @@ export default {

if (map) {
map = JSON.parse(map)
map.sources = map.sources.map(source => normalizePath(source))
map.sources = map.sources.map(source => humanlizePath(source))
}

return {
Expand Down
19 changes: 13 additions & 6 deletions src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import path from 'path'
import postcss from 'postcss'
import findPostcssConfig from 'postcss-load-config'
import reserved from 'reserved-words'
import normalizePath from './utils/normalize-path'
import humanlizePath from './utils/humanlize-path'
import localRequire from './utils/local-require'
import normalizePath from './utils/normalize-path'

const styleInjectPath = require.resolve('style-inject/dist/style-inject.es')
const styleInjectPath = require.resolve('style-inject/dist/style-inject.es').replace(/[\\/]+/g, '/')

function loadConfig(id, { ctx: configOptions, path: configPath }) {
const handleError = err => {
Expand Down Expand Up @@ -64,7 +65,9 @@ export default {
if (options.modules) {
plugins.push(
require('postcss-modules')({
generateScopedName: '[name]_[local]__[hash:base64:5]',
// In tests
// Skip hash in names since css content on windows and linux would differ because of `new line` (\r?\n)
generateScopedName: process.env.ROLLUP_POSTCSS_TEST ? '[name]_[local]' : '[name]_[local]__[hash:base64:5]',
...options.modules,
getJSON(filepath, json) {
modulesExported[filepath] = json
Expand Down Expand Up @@ -100,6 +103,10 @@ export default {
}

const res = await postcss(plugins).process(code, postcssOpts)
const outputMap = res.map && JSON.parse(res.map.toString())
if (outputMap && outputMap.sources) {
outputMap.sources = outputMap.sources.map(v => normalizePath(v))
}

let output = ''
let extracted
Expand All @@ -113,7 +120,7 @@ export default {
for (const name in json) {
const newName = getClassName(name)
if (name !== newName) {
console.warn(`Exported "${name}" as "${newName}" in ${normalizePath(this.id)}`)
console.warn(`Exported "${name}" as "${newName}" in ${humanlizePath(this.id)}`)
}
output += `export var ${newName} = ${JSON.stringify(
json[name]
Expand All @@ -126,7 +133,7 @@ export default {
extracted = {
id: this.id,
code: res.css,
map: res.map
map: outputMap
}
} else {
output += `var css = ${JSON.stringify(res.css)};\nexport default ${
Expand All @@ -143,7 +150,7 @@ export default {

return {
code: output,
map: res.map,
map: outputMap,
extracted
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/humanlize-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'path'

const humanlizePath = filepath => path.relative(process.cwd(),
filepath)

export default humanlizePath
7 changes: 1 addition & 6 deletions src/utils/normalize-path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import path from 'path'

const normalizePath = filepath => path.relative(process.cwd(),
filepath)

export default normalizePath
export default path => path && path.replace(/\\+/g, '/')
Loading

0 comments on commit db7db7a

Please sign in to comment.