Skip to content

Commit

Permalink
add onExtract option
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 14, 2018
1 parent 3ba8ce6 commit 25360d5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ Type: `id => void`
A function to be invoked when an import for CSS file is detected.
### onExtract
Type: `({ code, map, codeFilePath, mapFilePath }) => any`
A function to be invoked before extracting CSS file, you can make it return `false` to disable CSS extraction.
## License
MIT © [EGOIST](https://github.com/egoist)
18 changes: 15 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,26 @@ export default (options = {}) => {
code += `\n/*# sourceMappingURL=${basename}.css.map */`
}

// Release for potential next build
extracted = []

if (options.onExtract) {
const shouldExtract = await options.onExtract({
code,
map: concat.sourceMap,
codeFilePath: file,
mapFilePath: file + '.map'
})
if (shouldExtract === false) {
return
}
}

await Promise.all([
fs.writeFile(file, code, 'utf8'),
sourceMap === true &&
fs.writeFile(file + '.map', concat.sourceMap, 'utf8')
])

// Release for potential next build
extracted = []
}
}
}
7 changes: 7 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ console.log(style$1);
"
`;

exports[`onExtract 1`] = `
"'use strict';
console.log(undefined, undefined);
"
`;

exports[`postcss-config: js code 1`] = `
"'use strict';
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,18 @@ snapshot({
inject: false
}
})

test('onExtract', async () => {
const res = await write({
input: 'simple/index.js',
dirname: 'onExtract',
options: {
extract: true,
onExtract() {
return false
}
}
})
expect(await res.jsCode()).toMatchSnapshot()
expect(await res.hasCssFile()).toBe(false)
})

0 comments on commit 25360d5

Please sign in to comment.