Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: webpack-contrib/postcss-loader
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: bcc9ccf8b6dbe6143484853da4cac442ecf8aa58
Choose a base ref
...
head repository: webpack-contrib/postcss-loader
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6ddc2bb595fee7800908ac910d8d8fe91489e6cf
Choose a head ref
  • 12 commits
  • 16 files changed
  • 5 contributors

Commits on Nov 5, 2017

  1. docs(README): fix broken link (importLoaders) (#310)

    Jay Sherby authored and michael-ciniawsky committed Nov 5, 2017
    Copy the full SHA
    31a25e8 View commit details

Commits on Nov 24, 2017

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f33131c View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3e1c7fa View commit details
  3. Copy the full SHA
    b9c1add View commit details

Commits on Jan 3, 2018

  1. Copy the full SHA
    61ff03c View commit details
  2. Copy the full SHA
    08c063a View commit details

Commits on Feb 2, 2018

  1. Copy the full SHA
    a8921cc View commit details
  2. Copy the full SHA
    8242689 View commit details
  3. Copy the full SHA
    4e8e20c View commit details
  4. feat(index): pass AST (result.root) && Messages (result.messages)…

    … as metadata to other loaders (#322)
    michael-ciniawsky authored Feb 2, 2018
    Copy the full SHA
    56232e7 View commit details
  5. Copy the full SHA
    410bbf9 View commit details
  6. Copy the full SHA
    6ddc2bb View commit details
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js

node_js:
- "node"
- stable
- lts/*
- 6

after_success:
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,41 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="2.1.0"></a>
# [2.1.0](https://github.com/postcss/postcss-loader/compare/v2.0.10...v2.1.0) (2018-02-02)


### Bug Fixes

* **index:** continue watching after dependency `{Error}` ([#332](https://github.com/postcss/postcss-loader/issues/332)) ([a8921cc](https://github.com/postcss/postcss-loader/commit/a8921cc))


### Features

* **index:** pass AST (`result.root`) && Messages (`result.messages`) as metadata to other loaders ([#322](https://github.com/postcss/postcss-loader/issues/322)) ([56232e7](https://github.com/postcss/postcss-loader/commit/56232e7))



<a name="2.0.10"></a>
## [2.0.10](https://github.com/postcss/postcss-loader/compare/v2.0.9...v2.0.10) (2018-01-03)


### Bug Fixes

* **index:** copy loader `options` before modifying ([#326](https://github.com/postcss/postcss-loader/issues/326)) ([61ff03c](https://github.com/postcss/postcss-loader/commit/61ff03c))



<a name="2.0.9"></a>
## [2.0.9](https://github.com/postcss/postcss-loader/compare/v2.0.8...v2.0.9) (2017-11-24)


### Bug Fixes

* **index:** filter `ident` (`options.ident`) ([#315](https://github.com/postcss/postcss-loader/issues/315)) ([3e1c7fa](https://github.com/postcss/postcss-loader/commit/3e1c7fa))



<a name="2.0.8"></a>
## [2.0.8](https://github.com/postcss/postcss-loader/compare/v2.0.6...v2.0.8) (2017-10-14)

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ either add the css-loader’s [`importLoaders`] option.

or use [postcss-modules] instead of `css-loader`.

[`importLoaders`]: https://github.com/webpack-contrib/css-loader#importing-and-chained-loaders
[`importLoaders`]: https://github.com/webpack-contrib/css-loader#importloaders
[cannot be used]: https://github.com/webpack/css-loader/issues/137
[CSS Modules]: https://github.com/webpack/css-loader#css-modules
[postcss-modules]: https://github.com/outpunk/postcss-modules
28 changes: 20 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -39,8 +39,8 @@ const SyntaxError = require('./Error')
*
* @return {cb} cb Result
*/
module.exports = function loader (css, map) {
const options = loaderUtils.getOptions(this) || {}
module.exports = function loader (css, map, meta) {
const options = Object.assign({}, loaderUtils.getOptions(this))

validateOptions(require('./options.json'), options, 'PostCSS Loader')

@@ -52,11 +52,15 @@ module.exports = function loader (css, map) {
Promise.resolve().then(() => {
const length = Object.keys(options)
.filter((option) => {
// if (option === 'exec') return
if (option === 'config') return
if (option === 'sourceMap') return

return option
switch (option) {
// case 'exec':
case 'ident':
case 'config':
case 'sourceMap':
return
default:
return option
}
})
.length

@@ -156,6 +160,11 @@ module.exports = function loader (css, map) {
map.sources = map.sources.map((src) => path.resolve(src))
}

if (!meta) meta = {}

meta.ast = { 'type': 'postcss', root: result.root }
meta.messages = result.messages

if (this.loaderIndex === 0) {
/**
* @memberof loader
@@ -169,6 +178,7 @@ module.exports = function loader (css, map) {

return null
}

/**
* @memberof loader
* @callback cb
@@ -177,11 +187,13 @@ module.exports = function loader (css, map) {
* @param {String} css Result (Raw Module)
* @param {Object} map Source Map
*/
cb(null, css, map)
cb(null, css, map, meta)

return null
})
}).catch((err) => {
if (err.file) this.addDependency(err.file)

return err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err)
})
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-loader",
"version": "2.0.8",
"version": "2.1.0",
"description": "PostCSS loader for webpack",
"main": "lib/index.js",
"engines": {
@@ -13,21 +13,22 @@
"loader-utils": "^1.1.0",
"postcss": "^6.0.0",
"postcss-load-config": "^1.2.0",
"schema-utils": "^0.3.0"
"schema-utils": "^0.4.0"
},
"devDependencies": {
"coveralls": "^2.0.0",
"jest": "^21.0.0",
"jsdoc-to-markdown": "^3.0.0",
"jest": "^22.0.0",
"jsdoc-to-markdown": "^4.0.0",
"memory-fs": "^0.4.0",
"postcss-import": "^11.0.0",
"postcss-js": "^1.0.0",
"standard": "^10.0.0",
"standard-version": "^4.0.0",
"sugarss": "^1.0.0",
"util.promisify": "^1.0.0",
"webpack": "^3.0.0"
},
"scripts": {
"clean": "rm -rf dmd jest* coverage jsdoc-api test/results",
"clean": "rm -rf coverage test/outputs",
"lint": "standard",
"docs": "jsdoc2md lib/index.js > LOADER.md",
"postdocs": "npm run clean",
4 changes: 1 addition & 3 deletions test/__snapshots__/Errors.test.js.snap
Original file line number Diff line number Diff line change
@@ -12,9 +12,7 @@ exports[`Errors Syntax Error 1`] = `
`;
exports[`Errors Validation Error 1`] = `
"Validation Error
PostCSS Loader Invalid Options
"PostCSS Loader Invalid Options
options.sourceMap should be string,boolean
"
6 changes: 6 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Loader Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

exports[`Loader Watching Dependencies Error 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

exports[`Loader Watching Dependencies Error 2`] = `"throw new Error(\\"Module build failed: Syntax Error \\\\n\\\\n(1:5) Unknown word\\\\n\\\\n\\\\u001b[31m\\\\u001b[1m>\\\\u001b[22m\\\\u001b[39m\\\\u001b[90m 1 | \\\\u001b[39ma \\\\u001b[33m{\\\\u001b[39m color black \\\\u001b[33m}\\\\u001b[39m\\\\n \\\\u001b[90m | \\\\u001b[39m \\\\u001b[31m\\\\u001b[1m^\\\\u001b[22m\\\\u001b[39m\\\\n \\\\u001b[90m 2 | \\\\u001b[39m\\\\n\\");"`;
exports[`Loader Watching Dependencies Error 3`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
1 change: 1 addition & 0 deletions test/fixtures/watch/error.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { color black }
1 change: 1 addition & 0 deletions test/fixtures/watch/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a { color: black }
3 changes: 3 additions & 0 deletions test/fixtures/watch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import style from './style.css'

export default style
1 change: 1 addition & 0 deletions test/fixtures/watch/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "./import";
24 changes: 17 additions & 7 deletions test/helpers/compiler.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ module.exports = function compiler (fixture, config, options) {
output: {
path: path.resolve(
__dirname,
`../results/${config.path ? config.path : ''}`
`../outputs/${config.path ? config.path : ''}`
),
filename: '[name].bundle.js'
},
@@ -31,7 +31,7 @@ module.exports = function compiler (fixture, config, options) {
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['runtime'],
name: [ 'runtime' ],
minChunks: Infinity
})
].concat(config.plugins || [])
@@ -43,11 +43,21 @@ module.exports = function compiler (fixture, config, options) {

if (!options.emit) compiler.outputFileSystem = new MemoryFS()

return new Promise((resolve, reject) => {
return compiler.run((err, stats) => {
if (err) reject(err)
if (options.watch) {
return new Promise((resolve, reject) => {
const watcher = compiler.watch({}, (err, stats) => {
options.watch(err, stats, () => {
watcher.close(resolve)
})
})
})
} else {
return new Promise((resolve, reject) => {
return compiler.run((err, stats) => {
if (err) reject(err)

resolve(stats)
resolve(stats)
})
})
})
}
}
38 changes: 38 additions & 0 deletions test/helpers/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require('path')
const promisify = require('util.promisify')

const {
unlink: _unlink,
readFile: _readFile,
writeFile: _writeFile
} = require('fs')

const fs = {
readFile: promisify(_readFile),
writeFile: promisify(_writeFile),
unlink: promisify(_unlink)
}

function readFile (name) {
const file = path.join(__dirname, '../fixtures', name)

return fs.readFile(file)
.then((data) => data.toString())
}

function writeFile (name, data) {
const file = path.join(__dirname, '../fixtures', name)

return fs.writeFile(file, data)
}

module.exports.copyFile = function copyFile (src, dest) {
return readFile(src)
.then((data) => writeFile(dest, data))
}

module.exports.deleteFile = function deleteFile (name) {
const file = path.join(__dirname, '../fixtures', name)

return fs.unlink(file)
}
67 changes: 67 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

const webpack = require('./helpers/compiler')
const { loader } = require('./helpers/compilation')
const { copyFile, deleteFile } = require('./helpers/fs');

describe('Loader', () => {
test('Default', () => {
@@ -20,4 +21,70 @@ describe('Loader', () => {
expect(src).toMatchSnapshot()
})
})

describe('Watching', () => {
describe('Dependencies', () => {
const files = {
css: 'watch/index.css',
error: 'watch/error.css',
changed: 'watch/import.css'
}

beforeEach(() => copyFile(files.css, files.changed))

afterEach(() => deleteFile(files.changed))

test('Error', () => {
const config = {
loader: {
options: {
plugins: [
require('postcss-import')
],
}
}
}

const steps = [
(stats) => {
const { err, src } = loader(stats)

expect(src).toMatchSnapshot()
expect(err.length).toEqual(0)

return copyFile(files.error, files.changed)
},
(stats) => {
const { err, src } = loader(stats)

expect(src).toMatchSnapshot()
expect(err.length).toEqual(1)

return copyFile(files.css, files.changed)
},
(stats, close) => {
const { err, src } = loader(stats)

expect(src).toMatchSnapshot()
expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
expect(err.length).toEqual(0)

return close()
}
]

let step = 0

const options = {
watch (err, stats, close) {
steps[step](stats, close)

step++
}
}

return webpack('watch/index.js', config, options)
})
})
})
})
2 changes: 2 additions & 0 deletions test/options/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

exports[`Options Config - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Context - {Object} - with ident 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Context - {Object} 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;

exports[`Options Config - Path - {String} 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
22 changes: 22 additions & 0 deletions test/options/config.test.js
Original file line number Diff line number Diff line change
@@ -53,4 +53,26 @@ describe('Options', () => {
expect(src).toMatchSnapshot()
})
})


test('Config - Context - {Object} - with ident', () => {
const config = {
loader: {
options: {
ident: 'postcss',
config: {
path: 'test/fixtures/config/postcss.config.js',
ctx: { plugin: true }
}
}
}
}

return webpack('css/index.js', config).then((stats) => {
const src = loader(stats).src

expect(src).toEqual("module.exports = \"a { color: rgba(255, 0, 0, 1.0) }\\n\"")
expect(src).toMatchSnapshot()
})
})
})