-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
241 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable import/no-unresolved */ | ||
|
||
module.exports = require('../lib/cli'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
|
||
module.exports = require('./lib/node'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#!/usr/bin/env node | ||
|
||
/* @flow */ | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const mkdirp = require('mkdirp'); | ||
const glob = require('glob'); | ||
const commander = require('commander'); | ||
const transform = require('./transform'); | ||
|
||
commander | ||
.version(require('../package.json').version) | ||
.usage('[options] <file ...>') | ||
.option('-o, --out-dir <dir>', 'output directory for the extracted CSS files') | ||
.option('-s, --source-maps', 'generate source maps for the CSS files') | ||
.option('-r, --source-root <dir>', 'directory contaning the source JS files') | ||
.option( | ||
'-i, --insert-css-requires <dir>', | ||
'directory containing JS files to insert require statements for the CSS files' | ||
) | ||
.action((file, ...rest) => { | ||
if (typeof file !== 'string') { | ||
console.error('No files were specified to extract CSS from.\n'); | ||
commander.help(); | ||
process.exit(1); | ||
} | ||
|
||
const command = rest[rest.length - 1]; | ||
const files = [file, ...rest.slice(0, -1)]; | ||
|
||
if (!command.outDir) { | ||
console.error('--out-dir must be specified.\n'); | ||
commander.help(); | ||
process.exit(1); | ||
} | ||
|
||
if (command.insertCssRequires && !command.sourceRoot) { | ||
console.error( | ||
'--source-root must be specified when --insert-css-requires is specified.\n' | ||
); | ||
commander.help(); | ||
process.exit(1); | ||
} | ||
|
||
processFiles(files, { | ||
outDir: command.outDir, | ||
sourceMaps: command.sourceMaps, | ||
sourceRoot: command.sourceRoot, | ||
insertCssRequires: command.insertCssRequires, | ||
}); | ||
}) | ||
.parse(process.argv); | ||
|
||
type Options = { | ||
outDir: string, | ||
sourceMaps?: boolean, | ||
sourceRoot?: string, | ||
insertCssRequires?: string, | ||
}; | ||
|
||
function processFiles(files: string[], options: Options) { | ||
let count = 0; | ||
|
||
const resolvedFiles = files.reduce( | ||
(acc, pattern) => [...acc, ...glob.sync(pattern, { absolute: true })], | ||
[] | ||
); | ||
|
||
resolvedFiles.forEach(filename => { | ||
const outputFilename = resolveOutputFilename(filename, options.outDir); | ||
|
||
const { cssText, sourceMap, cssSourceMapText } = transform( | ||
fs.readFileSync(filename).toString(), | ||
{ | ||
inputFilename: filename, | ||
outputFilename, | ||
pluginOptions: {}, | ||
} | ||
); | ||
|
||
if (cssText) { | ||
mkdirp.sync(path.dirname(outputFilename)); | ||
|
||
const cssContent = | ||
options.sourceMaps && sourceMap | ||
? `${cssText}\n/*# sourceMappingURL=${outputFilename}.map */` | ||
: cssText; | ||
|
||
fs.writeFileSync(outputFilename, cssContent); | ||
|
||
if ( | ||
options.sourceMaps && | ||
sourceMap && | ||
typeof cssSourceMapText !== 'undefined' | ||
) { | ||
fs.writeFileSync(`${outputFilename}.map`, cssSourceMapText); | ||
} | ||
|
||
if (options.insertCssRequires && options.sourceRoot) { | ||
const inputFilename = path.resolve( | ||
options.insertCssRequires, | ||
path.relative(options.sourceRoot, filename) | ||
); | ||
|
||
const requireStatement = `\nrequire('${path.relative( | ||
path.dirname(inputFilename), | ||
outputFilename | ||
)}');`; | ||
|
||
const inputContent = fs.readFileSync(inputFilename, 'utf-8'); | ||
|
||
if (!inputContent.trim().endsWith(requireStatement)) { | ||
fs.writeFileSync( | ||
inputFilename, | ||
`${inputContent}\n${requireStatement}` | ||
); | ||
} | ||
} | ||
|
||
count++; | ||
} | ||
}); | ||
|
||
console.log(`Successfully extracted ${count} CSS files.`); | ||
} | ||
|
||
function resolveOutputFilename(filename: string, outDir: string) { | ||
const folderStructure = path.relative(process.cwd(), path.dirname(filename)); | ||
const outputBasename = path | ||
.basename(filename) | ||
.replace(path.extname(filename), '.css'); | ||
|
||
return path.join(outDir, folderStructure, outputBasename); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* @flow */ | ||
|
||
module.exports.transform = require('./transform'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.