Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add plugin for extracting component CSS to a separate file #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions postcss/fork-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs')
const path = require('path')

const getInputPath = (root) => root.source.input.file

module.exports = (options = { outputFolder: 'build', match: /^((?!\.(stories|test)).)*\.svelte/gi, dropExtension: true }) => {
const getOutputFile = (inputFile) => {
const fileName = path.basename(inputFile, options.dropExtension ? path.extname(inputFile) : undefined);
const relativePath = path.relative('.', path.resolve(inputFile, '..'))
return path.join(options.outputFolder, relativePath, fileName + '.css');
}

return {
postcssPlugin: 'fork-css',
OnceExit: (root) => {
const inputFile = getInputPath(root);
if (!options.match.test(inputFile)) return;

const outputFile = getOutputFile(inputFile);
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, root.toString());
}
}
}

module.exports.postcss = true;
8 changes: 7 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export default {
plugins: [
svelteTypes(),
svelte({
preprocess: sveltePreprocess(),
preprocess: sveltePreprocess({
postcss: {
plugins: [
require('./postcss/fork-css')
]
}
}),
compilerOptions: {
customElement: true
},
Expand Down