@wyw-in-js/cli 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/esm/wyw-in-js.js +175 -0
- package/esm/wyw-in-js.js.map +1 -0
- package/lib/wyw-in-js.js +179 -0
- package/lib/wyw-in-js.js.map +1 -0
- package/package.json +46 -0
- package/types/wyw-in-js.d.ts +4 -0
- package/types/wyw-in-js.js +203 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Anton Evzhakov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/esm/wyw-in-js.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
/**
|
|
3
|
+
* This file contains a CLI for wyw-in-js.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { asyncResolveFallback } from '@wyw-in-js/shared';
|
|
9
|
+
import { createFileReporter, TransformCacheCollection, transform } from '@wyw-in-js/transform';
|
|
10
|
+
import glob from 'glob';
|
|
11
|
+
import mkdirp from 'mkdirp';
|
|
12
|
+
import normalize from 'normalize-path';
|
|
13
|
+
import yargs from 'yargs';
|
|
14
|
+
const modulesOptions = ['commonjs', 'es2015', 'es6', 'esnext', 'native'];
|
|
15
|
+
const argv = yargs.usage('Usage: $0 [options] <files ...>').option('config', {
|
|
16
|
+
alias: 'c',
|
|
17
|
+
type: 'string',
|
|
18
|
+
description: 'Path to a config file',
|
|
19
|
+
requiresArg: true,
|
|
20
|
+
coerce: path.resolve
|
|
21
|
+
}).option('out-dir', {
|
|
22
|
+
alias: 'o',
|
|
23
|
+
type: 'string',
|
|
24
|
+
description: 'Output directory for the extracted CSS files',
|
|
25
|
+
demandOption: true,
|
|
26
|
+
requiresArg: true,
|
|
27
|
+
coerce: path.resolve
|
|
28
|
+
}).option('source-maps', {
|
|
29
|
+
alias: 's',
|
|
30
|
+
type: 'boolean',
|
|
31
|
+
description: 'Generate source maps for the CSS files',
|
|
32
|
+
default: false
|
|
33
|
+
}).option('parallel', {
|
|
34
|
+
alias: 'p',
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
description: 'Run extraction in parallel',
|
|
37
|
+
default: false
|
|
38
|
+
}).option('source-root', {
|
|
39
|
+
alias: 'r',
|
|
40
|
+
type: 'string',
|
|
41
|
+
description: 'Directory containing the source JS files',
|
|
42
|
+
demandOption: true,
|
|
43
|
+
requiresArg: true,
|
|
44
|
+
coerce: path.resolve
|
|
45
|
+
}).option('insert-css-requires', {
|
|
46
|
+
alias: 'i',
|
|
47
|
+
type: 'string',
|
|
48
|
+
description: 'Directory containing JS files to insert require statements for the CSS files',
|
|
49
|
+
requiresArg: true,
|
|
50
|
+
coerce: path.resolve
|
|
51
|
+
}).option('transform', {
|
|
52
|
+
alias: 't',
|
|
53
|
+
type: 'boolean',
|
|
54
|
+
description: 'Replace template tags with evaluated values'
|
|
55
|
+
}).option('modules', {
|
|
56
|
+
alias: 'm',
|
|
57
|
+
choices: modulesOptions,
|
|
58
|
+
description: 'Specifies a type of used imports',
|
|
59
|
+
default: 'commonjs',
|
|
60
|
+
coerce: s => s.toLowerCase()
|
|
61
|
+
}).implies('insert-css-requires', 'source-root').implies('transform', 'insert-css-requires').option('ignore', {
|
|
62
|
+
alias: 'x',
|
|
63
|
+
type: 'string',
|
|
64
|
+
description: 'Pattern of files to ignore. Be sure to provide a string',
|
|
65
|
+
requiresArg: true
|
|
66
|
+
}).alias('help', 'h').alias('version', 'v').parseSync();
|
|
67
|
+
function resolveRequireInsertionFilename(filename) {
|
|
68
|
+
return filename.replace(/\.tsx?/, '.js');
|
|
69
|
+
}
|
|
70
|
+
function resolveOutputFilename(filename, outDir, sourceRoot) {
|
|
71
|
+
const outputFolder = path.relative(sourceRoot, path.dirname(filename));
|
|
72
|
+
const outputBasename = path.basename(filename).replace(path.extname(filename), '.css');
|
|
73
|
+
return path.join(outDir, outputFolder, outputBasename);
|
|
74
|
+
}
|
|
75
|
+
async function processFiles(files, options) {
|
|
76
|
+
const {
|
|
77
|
+
emitter,
|
|
78
|
+
onDone
|
|
79
|
+
} = createFileReporter();
|
|
80
|
+
const resolvedFiles = files.reduce((acc, pattern) => [...acc, ...glob.sync(pattern.toString(), {
|
|
81
|
+
absolute: true,
|
|
82
|
+
ignore: options.ignore
|
|
83
|
+
})], []);
|
|
84
|
+
const cache = new TransformCacheCollection();
|
|
85
|
+
const modifiedFiles = [];
|
|
86
|
+
const tasks = [];
|
|
87
|
+
|
|
88
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
89
|
+
for (const filename of resolvedFiles) {
|
|
90
|
+
if (fs.lstatSync(filename).isDirectory()) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const outputFilename = resolveOutputFilename(filename, options.outDir, options.sourceRoot);
|
|
94
|
+
const transformServices = {
|
|
95
|
+
options: {
|
|
96
|
+
filename,
|
|
97
|
+
outputFilename,
|
|
98
|
+
pluginOptions: {
|
|
99
|
+
configFile: options.configFile
|
|
100
|
+
},
|
|
101
|
+
root: options.sourceRoot
|
|
102
|
+
},
|
|
103
|
+
cache,
|
|
104
|
+
eventEmitter: emitter
|
|
105
|
+
};
|
|
106
|
+
tasks.push(() => transform(transformServices, fs.readFileSync(filename).toString(), asyncResolveFallback).then(({
|
|
107
|
+
code,
|
|
108
|
+
cssText,
|
|
109
|
+
sourceMap,
|
|
110
|
+
cssSourceMapText
|
|
111
|
+
}) => {
|
|
112
|
+
if (!cssText) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
mkdirp.sync(path.dirname(outputFilename));
|
|
116
|
+
const cssContent = options.sourceMaps && sourceMap ? `${cssText}\n/*# sourceMappingURL=${outputFilename}.map */` : cssText;
|
|
117
|
+
fs.writeFileSync(outputFilename, cssContent);
|
|
118
|
+
if (options.sourceMaps && sourceMap && typeof cssSourceMapText !== 'undefined') {
|
|
119
|
+
fs.writeFileSync(`${outputFilename}.map`, cssSourceMapText);
|
|
120
|
+
}
|
|
121
|
+
if (options.sourceRoot && options.insertCssRequires) {
|
|
122
|
+
const inputFilename = path.resolve(options.insertCssRequires, path.relative(options.sourceRoot, filename));
|
|
123
|
+
const relativePath = normalize(path.relative(path.dirname(inputFilename), outputFilename));
|
|
124
|
+
const pathForImport = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
125
|
+
const statement = options.modules === 'commonjs' ? `\nrequire('${pathForImport}');` : `\nimport "${pathForImport}";`;
|
|
126
|
+
const normalizedInputFilename = resolveRequireInsertionFilename(inputFilename);
|
|
127
|
+
const inputContent = options.transform ? code : fs.readFileSync(normalizedInputFilename, 'utf-8');
|
|
128
|
+
if (!inputContent.trim().endsWith(statement)) {
|
|
129
|
+
modifiedFiles.push({
|
|
130
|
+
name: normalizedInputFilename,
|
|
131
|
+
content: `${inputContent}\n${statement}\n`
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
if (options.parallel) {
|
|
139
|
+
const res = await Promise.all(tasks.map(task => task()));
|
|
140
|
+
console.log(`Successfully extracted ${res.filter(i => i).length} CSS files.`);
|
|
141
|
+
} else {
|
|
142
|
+
let count = 0;
|
|
143
|
+
for (const task of tasks) {
|
|
144
|
+
// eslint-disable-next-line no-await-in-loop
|
|
145
|
+
const res = await task();
|
|
146
|
+
if (res) {
|
|
147
|
+
count += 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
console.log(`Successfully extracted ${count} CSS files.`);
|
|
151
|
+
}
|
|
152
|
+
modifiedFiles.forEach(({
|
|
153
|
+
name,
|
|
154
|
+
content
|
|
155
|
+
}) => {
|
|
156
|
+
fs.writeFileSync(name, content);
|
|
157
|
+
});
|
|
158
|
+
cache.clear('all');
|
|
159
|
+
modifiedFiles.length = 0;
|
|
160
|
+
resolvedFiles.length = 0;
|
|
161
|
+
tasks.length = 0;
|
|
162
|
+
onDone(options.sourceRoot ?? process.cwd());
|
|
163
|
+
}
|
|
164
|
+
processFiles(argv._, {
|
|
165
|
+
configFile: argv.config,
|
|
166
|
+
ignore: argv.ignore,
|
|
167
|
+
insertCssRequires: argv['insert-css-requires'],
|
|
168
|
+
modules: argv.modules,
|
|
169
|
+
parallel: argv.parallel,
|
|
170
|
+
outDir: argv['out-dir'],
|
|
171
|
+
sourceMaps: argv['source-maps'],
|
|
172
|
+
sourceRoot: argv['source-root'],
|
|
173
|
+
transform: argv.transform
|
|
174
|
+
});
|
|
175
|
+
//# sourceMappingURL=wyw-in-js.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wyw-in-js.js","names":["fs","path","asyncResolveFallback","createFileReporter","TransformCacheCollection","transform","glob","mkdirp","normalize","yargs","modulesOptions","argv","usage","option","alias","type","description","requiresArg","coerce","resolve","demandOption","default","choices","s","toLowerCase","implies","parseSync","resolveRequireInsertionFilename","filename","replace","resolveOutputFilename","outDir","sourceRoot","outputFolder","relative","dirname","outputBasename","basename","extname","join","processFiles","files","options","emitter","onDone","resolvedFiles","reduce","acc","pattern","sync","toString","absolute","ignore","cache","modifiedFiles","tasks","lstatSync","isDirectory","outputFilename","transformServices","pluginOptions","configFile","root","eventEmitter","push","readFileSync","then","code","cssText","sourceMap","cssSourceMapText","cssContent","sourceMaps","writeFileSync","insertCssRequires","inputFilename","relativePath","pathForImport","startsWith","statement","modules","normalizedInputFilename","inputContent","trim","endsWith","name","content","parallel","res","Promise","all","map","task","console","log","filter","i","length","count","forEach","clear","process","cwd","_","config"],"sources":["../src/wyw-in-js.ts"],"sourcesContent":["/* eslint-disable no-console */\n/**\n * This file contains a CLI for wyw-in-js.\n */\n\nimport fs from 'fs';\nimport path from 'path';\n\nimport { asyncResolveFallback } from '@wyw-in-js/shared';\nimport {\n createFileReporter,\n TransformCacheCollection,\n transform,\n} from '@wyw-in-js/transform';\nimport glob from 'glob';\nimport mkdirp from 'mkdirp';\nimport normalize from 'normalize-path';\nimport yargs from 'yargs';\n\nconst modulesOptions = [\n 'commonjs',\n 'es2015',\n 'es6',\n 'esnext',\n 'native',\n] as const;\n\nconst argv = yargs\n .usage('Usage: $0 [options] <files ...>')\n .option('config', {\n alias: 'c',\n type: 'string',\n description: 'Path to a config file',\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('out-dir', {\n alias: 'o',\n type: 'string',\n description: 'Output directory for the extracted CSS files',\n demandOption: true,\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('source-maps', {\n alias: 's',\n type: 'boolean',\n description: 'Generate source maps for the CSS files',\n default: false,\n })\n .option('parallel', {\n alias: 'p',\n type: 'boolean',\n description: 'Run extraction in parallel',\n default: false,\n })\n .option('source-root', {\n alias: 'r',\n type: 'string',\n description: 'Directory containing the source JS files',\n demandOption: true,\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('insert-css-requires', {\n alias: 'i',\n type: 'string',\n description:\n 'Directory containing JS files to insert require statements for the CSS files',\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('transform', {\n alias: 't',\n type: 'boolean',\n description: 'Replace template tags with evaluated values',\n })\n .option('modules', {\n alias: 'm',\n choices: modulesOptions,\n description: 'Specifies a type of used imports',\n default: 'commonjs' as const,\n coerce: (s) => s.toLowerCase(),\n })\n .implies('insert-css-requires', 'source-root')\n .implies('transform', 'insert-css-requires')\n .option('ignore', {\n alias: 'x',\n type: 'string',\n description: 'Pattern of files to ignore. Be sure to provide a string',\n requiresArg: true,\n })\n .alias('help', 'h')\n .alias('version', 'v')\n .parseSync();\n\ntype Options = {\n configFile?: string;\n ignore?: string;\n insertCssRequires?: string;\n modules: (typeof modulesOptions)[number];\n outDir: string;\n parallel?: boolean;\n sourceMaps?: boolean;\n sourceRoot: string;\n transform?: boolean;\n};\n\nfunction resolveRequireInsertionFilename(filename: string) {\n return filename.replace(/\\.tsx?/, '.js');\n}\n\nfunction resolveOutputFilename(\n filename: string,\n outDir: string,\n sourceRoot: string\n) {\n const outputFolder = path.relative(sourceRoot, path.dirname(filename));\n const outputBasename = path\n .basename(filename)\n .replace(path.extname(filename), '.css');\n\n return path.join(outDir, outputFolder, outputBasename);\n}\n\nasync function processFiles(files: (number | string)[], options: Options) {\n const { emitter, onDone } = createFileReporter();\n\n const resolvedFiles = files.reduce(\n (acc, pattern) => [\n ...acc,\n ...glob.sync(pattern.toString(), {\n absolute: true,\n ignore: options.ignore,\n }),\n ],\n [] as string[]\n );\n const cache = new TransformCacheCollection();\n\n const modifiedFiles: { content: string; name: string }[] = [];\n\n const tasks: (() => Promise<boolean>)[] = [];\n\n // eslint-disable-next-line no-restricted-syntax\n for (const filename of resolvedFiles) {\n if (fs.lstatSync(filename).isDirectory()) {\n return;\n }\n\n const outputFilename = resolveOutputFilename(\n filename,\n options.outDir,\n options.sourceRoot\n );\n\n const transformServices = {\n options: {\n filename,\n outputFilename,\n pluginOptions: {\n configFile: options.configFile,\n },\n root: options.sourceRoot,\n },\n cache,\n eventEmitter: emitter,\n };\n\n tasks.push(() =>\n transform(\n transformServices,\n fs.readFileSync(filename).toString(),\n asyncResolveFallback\n ).then(({ code, cssText, sourceMap, cssSourceMapText }): boolean => {\n if (!cssText) {\n return false;\n }\n mkdirp.sync(path.dirname(outputFilename));\n\n const cssContent =\n options.sourceMaps && sourceMap\n ? `${cssText}\\n/*# sourceMappingURL=${outputFilename}.map */`\n : cssText;\n\n fs.writeFileSync(outputFilename, cssContent);\n\n if (\n options.sourceMaps &&\n sourceMap &&\n typeof cssSourceMapText !== 'undefined'\n ) {\n fs.writeFileSync(`${outputFilename}.map`, cssSourceMapText);\n }\n\n if (options.sourceRoot && options.insertCssRequires) {\n const inputFilename = path.resolve(\n options.insertCssRequires,\n path.relative(options.sourceRoot, filename)\n );\n\n const relativePath = normalize(\n path.relative(path.dirname(inputFilename), outputFilename)\n );\n\n const pathForImport = relativePath.startsWith('.')\n ? relativePath\n : `./${relativePath}`;\n\n const statement =\n options.modules === 'commonjs'\n ? `\\nrequire('${pathForImport}');`\n : `\\nimport \"${pathForImport}\";`;\n\n const normalizedInputFilename =\n resolveRequireInsertionFilename(inputFilename);\n\n const inputContent = options.transform\n ? code\n : fs.readFileSync(normalizedInputFilename, 'utf-8');\n\n if (!inputContent.trim().endsWith(statement)) {\n modifiedFiles.push({\n name: normalizedInputFilename,\n content: `${inputContent}\\n${statement}\\n`,\n });\n }\n }\n\n return true;\n })\n );\n }\n\n if (options.parallel) {\n const res = await Promise.all(tasks.map((task) => task()));\n console.log(\n `Successfully extracted ${res.filter((i) => i).length} CSS files.`\n );\n } else {\n let count = 0;\n for (const task of tasks) {\n // eslint-disable-next-line no-await-in-loop\n const res = await task();\n if (res) {\n count += 1;\n }\n }\n\n console.log(`Successfully extracted ${count} CSS files.`);\n }\n\n modifiedFiles.forEach(({ name, content }) => {\n fs.writeFileSync(name, content);\n });\n\n cache.clear('all');\n modifiedFiles.length = 0;\n resolvedFiles.length = 0;\n tasks.length = 0;\n\n onDone(options.sourceRoot ?? process.cwd());\n}\n\nprocessFiles(argv._, {\n configFile: argv.config,\n ignore: argv.ignore,\n insertCssRequires: argv['insert-css-requires'],\n modules: argv.modules,\n parallel: argv.parallel,\n outDir: argv['out-dir'],\n sourceMaps: argv['source-maps'],\n sourceRoot: argv['source-root'],\n transform: argv.transform,\n});\n"],"mappings":"AAAA;AACA;AACA;AACA;;AAEA,OAAOA,EAAE,MAAM,IAAI;AACnB,OAAOC,IAAI,MAAM,MAAM;AAEvB,SAASC,oBAAoB,QAAQ,mBAAmB;AACxD,SACEC,kBAAkB,EAClBC,wBAAwB,EACxBC,SAAS,QACJ,sBAAsB;AAC7B,OAAOC,IAAI,MAAM,MAAM;AACvB,OAAOC,MAAM,MAAM,QAAQ;AAC3B,OAAOC,SAAS,MAAM,gBAAgB;AACtC,OAAOC,KAAK,MAAM,OAAO;AAEzB,MAAMC,cAAc,GAAG,CACrB,UAAU,EACV,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,QAAQ,CACA;AAEV,MAAMC,IAAI,GAAGF,KAAK,CACfG,KAAK,CAAC,iCAAiC,CAAC,CACxCC,MAAM,CAAC,QAAQ,EAAE;EAChBC,KAAK,EAAE,GAAG;EACVC,IAAI,EAAE,QAAQ;EACdC,WAAW,EAAE,uBAAuB;EACpCC,WAAW,EAAE,IAAI;EACjBC,MAAM,EAAEjB,IAAI,CAACkB;AACf,CAAC,CAAC,CACDN,MAAM,CAAC,SAAS,EAAE;EACjBC,KAAK,EAAE,GAAG;EACVC,IAAI,EAAE,QAAQ;EACdC,WAAW,EAAE,8CAA8C;EAC3DI,YAAY,EAAE,IAAI;EACl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,CAAC,CAAC"}
|
package/lib/wyw-in-js.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
4
|
+
var _path = _interopRequireDefault(require("path"));
|
|
5
|
+
var _shared = require("@wyw-in-js/shared");
|
|
6
|
+
var _transform = require("@wyw-in-js/transform");
|
|
7
|
+
var _glob = _interopRequireDefault(require("glob"));
|
|
8
|
+
var _mkdirp = _interopRequireDefault(require("mkdirp"));
|
|
9
|
+
var _normalizePath = _interopRequireDefault(require("normalize-path"));
|
|
10
|
+
var _yargs = _interopRequireDefault(require("yargs"));
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/* eslint-disable no-console */
|
|
13
|
+
/**
|
|
14
|
+
* This file contains a CLI for wyw-in-js.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const modulesOptions = ['commonjs', 'es2015', 'es6', 'esnext', 'native'];
|
|
18
|
+
const argv = _yargs.default.usage('Usage: $0 [options] <files ...>').option('config', {
|
|
19
|
+
alias: 'c',
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'Path to a config file',
|
|
22
|
+
requiresArg: true,
|
|
23
|
+
coerce: _path.default.resolve
|
|
24
|
+
}).option('out-dir', {
|
|
25
|
+
alias: 'o',
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: 'Output directory for the extracted CSS files',
|
|
28
|
+
demandOption: true,
|
|
29
|
+
requiresArg: true,
|
|
30
|
+
coerce: _path.default.resolve
|
|
31
|
+
}).option('source-maps', {
|
|
32
|
+
alias: 's',
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
description: 'Generate source maps for the CSS files',
|
|
35
|
+
default: false
|
|
36
|
+
}).option('parallel', {
|
|
37
|
+
alias: 'p',
|
|
38
|
+
type: 'boolean',
|
|
39
|
+
description: 'Run extraction in parallel',
|
|
40
|
+
default: false
|
|
41
|
+
}).option('source-root', {
|
|
42
|
+
alias: 'r',
|
|
43
|
+
type: 'string',
|
|
44
|
+
description: 'Directory containing the source JS files',
|
|
45
|
+
demandOption: true,
|
|
46
|
+
requiresArg: true,
|
|
47
|
+
coerce: _path.default.resolve
|
|
48
|
+
}).option('insert-css-requires', {
|
|
49
|
+
alias: 'i',
|
|
50
|
+
type: 'string',
|
|
51
|
+
description: 'Directory containing JS files to insert require statements for the CSS files',
|
|
52
|
+
requiresArg: true,
|
|
53
|
+
coerce: _path.default.resolve
|
|
54
|
+
}).option('transform', {
|
|
55
|
+
alias: 't',
|
|
56
|
+
type: 'boolean',
|
|
57
|
+
description: 'Replace template tags with evaluated values'
|
|
58
|
+
}).option('modules', {
|
|
59
|
+
alias: 'm',
|
|
60
|
+
choices: modulesOptions,
|
|
61
|
+
description: 'Specifies a type of used imports',
|
|
62
|
+
default: 'commonjs',
|
|
63
|
+
coerce: s => s.toLowerCase()
|
|
64
|
+
}).implies('insert-css-requires', 'source-root').implies('transform', 'insert-css-requires').option('ignore', {
|
|
65
|
+
alias: 'x',
|
|
66
|
+
type: 'string',
|
|
67
|
+
description: 'Pattern of files to ignore. Be sure to provide a string',
|
|
68
|
+
requiresArg: true
|
|
69
|
+
}).alias('help', 'h').alias('version', 'v').parseSync();
|
|
70
|
+
function resolveRequireInsertionFilename(filename) {
|
|
71
|
+
return filename.replace(/\.tsx?/, '.js');
|
|
72
|
+
}
|
|
73
|
+
function resolveOutputFilename(filename, outDir, sourceRoot) {
|
|
74
|
+
const outputFolder = _path.default.relative(sourceRoot, _path.default.dirname(filename));
|
|
75
|
+
const outputBasename = _path.default.basename(filename).replace(_path.default.extname(filename), '.css');
|
|
76
|
+
return _path.default.join(outDir, outputFolder, outputBasename);
|
|
77
|
+
}
|
|
78
|
+
async function processFiles(files, options) {
|
|
79
|
+
var _options$sourceRoot;
|
|
80
|
+
const {
|
|
81
|
+
emitter,
|
|
82
|
+
onDone
|
|
83
|
+
} = (0, _transform.createFileReporter)();
|
|
84
|
+
const resolvedFiles = files.reduce((acc, pattern) => [...acc, ..._glob.default.sync(pattern.toString(), {
|
|
85
|
+
absolute: true,
|
|
86
|
+
ignore: options.ignore
|
|
87
|
+
})], []);
|
|
88
|
+
const cache = new _transform.TransformCacheCollection();
|
|
89
|
+
const modifiedFiles = [];
|
|
90
|
+
const tasks = [];
|
|
91
|
+
|
|
92
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
93
|
+
for (const filename of resolvedFiles) {
|
|
94
|
+
if (_fs.default.lstatSync(filename).isDirectory()) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const outputFilename = resolveOutputFilename(filename, options.outDir, options.sourceRoot);
|
|
98
|
+
const transformServices = {
|
|
99
|
+
options: {
|
|
100
|
+
filename,
|
|
101
|
+
outputFilename,
|
|
102
|
+
pluginOptions: {
|
|
103
|
+
configFile: options.configFile
|
|
104
|
+
},
|
|
105
|
+
root: options.sourceRoot
|
|
106
|
+
},
|
|
107
|
+
cache,
|
|
108
|
+
eventEmitter: emitter
|
|
109
|
+
};
|
|
110
|
+
tasks.push(() => (0, _transform.transform)(transformServices, _fs.default.readFileSync(filename).toString(), _shared.asyncResolveFallback).then(({
|
|
111
|
+
code,
|
|
112
|
+
cssText,
|
|
113
|
+
sourceMap,
|
|
114
|
+
cssSourceMapText
|
|
115
|
+
}) => {
|
|
116
|
+
if (!cssText) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
_mkdirp.default.sync(_path.default.dirname(outputFilename));
|
|
120
|
+
const cssContent = options.sourceMaps && sourceMap ? `${cssText}\n/*# sourceMappingURL=${outputFilename}.map */` : cssText;
|
|
121
|
+
_fs.default.writeFileSync(outputFilename, cssContent);
|
|
122
|
+
if (options.sourceMaps && sourceMap && typeof cssSourceMapText !== 'undefined') {
|
|
123
|
+
_fs.default.writeFileSync(`${outputFilename}.map`, cssSourceMapText);
|
|
124
|
+
}
|
|
125
|
+
if (options.sourceRoot && options.insertCssRequires) {
|
|
126
|
+
const inputFilename = _path.default.resolve(options.insertCssRequires, _path.default.relative(options.sourceRoot, filename));
|
|
127
|
+
const relativePath = (0, _normalizePath.default)(_path.default.relative(_path.default.dirname(inputFilename), outputFilename));
|
|
128
|
+
const pathForImport = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
129
|
+
const statement = options.modules === 'commonjs' ? `\nrequire('${pathForImport}');` : `\nimport "${pathForImport}";`;
|
|
130
|
+
const normalizedInputFilename = resolveRequireInsertionFilename(inputFilename);
|
|
131
|
+
const inputContent = options.transform ? code : _fs.default.readFileSync(normalizedInputFilename, 'utf-8');
|
|
132
|
+
if (!inputContent.trim().endsWith(statement)) {
|
|
133
|
+
modifiedFiles.push({
|
|
134
|
+
name: normalizedInputFilename,
|
|
135
|
+
content: `${inputContent}\n${statement}\n`
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
if (options.parallel) {
|
|
143
|
+
const res = await Promise.all(tasks.map(task => task()));
|
|
144
|
+
console.log(`Successfully extracted ${res.filter(i => i).length} CSS files.`);
|
|
145
|
+
} else {
|
|
146
|
+
let count = 0;
|
|
147
|
+
for (const task of tasks) {
|
|
148
|
+
// eslint-disable-next-line no-await-in-loop
|
|
149
|
+
const res = await task();
|
|
150
|
+
if (res) {
|
|
151
|
+
count += 1;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
console.log(`Successfully extracted ${count} CSS files.`);
|
|
155
|
+
}
|
|
156
|
+
modifiedFiles.forEach(({
|
|
157
|
+
name,
|
|
158
|
+
content
|
|
159
|
+
}) => {
|
|
160
|
+
_fs.default.writeFileSync(name, content);
|
|
161
|
+
});
|
|
162
|
+
cache.clear('all');
|
|
163
|
+
modifiedFiles.length = 0;
|
|
164
|
+
resolvedFiles.length = 0;
|
|
165
|
+
tasks.length = 0;
|
|
166
|
+
onDone((_options$sourceRoot = options.sourceRoot) !== null && _options$sourceRoot !== void 0 ? _options$sourceRoot : process.cwd());
|
|
167
|
+
}
|
|
168
|
+
processFiles(argv._, {
|
|
169
|
+
configFile: argv.config,
|
|
170
|
+
ignore: argv.ignore,
|
|
171
|
+
insertCssRequires: argv['insert-css-requires'],
|
|
172
|
+
modules: argv.modules,
|
|
173
|
+
parallel: argv.parallel,
|
|
174
|
+
outDir: argv['out-dir'],
|
|
175
|
+
sourceMaps: argv['source-maps'],
|
|
176
|
+
sourceRoot: argv['source-root'],
|
|
177
|
+
transform: argv.transform
|
|
178
|
+
});
|
|
179
|
+
//# sourceMappingURL=wyw-in-js.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wyw-in-js.js","names":["_fs","_interopRequireDefault","require","_path","_shared","_transform","_glob","_mkdirp","_normalizePath","_yargs","obj","__esModule","default","modulesOptions","argv","yargs","usage","option","alias","type","description","requiresArg","coerce","path","resolve","demandOption","choices","s","toLowerCase","implies","parseSync","resolveRequireInsertionFilename","filename","replace","resolveOutputFilename","outDir","sourceRoot","outputFolder","relative","dirname","outputBasename","basename","extname","join","processFiles","files","options","_options$sourceRoot","emitter","onDone","createFileReporter","resolvedFiles","reduce","acc","pattern","glob","sync","toString","absolute","ignore","cache","TransformCacheCollection","modifiedFiles","tasks","fs","lstatSync","isDirectory","outputFilename","transformServices","pluginOptions","configFile","root","eventEmitter","push","transform","readFileSync","asyncResolveFallback","then","code","cssText","sourceMap","cssSourceMapText","mkdirp","cssContent","sourceMaps","writeFileSync","insertCssRequires","inputFilename","relativePath","normalize","pathForImport","startsWith","statement","modules","normalizedInputFilename","inputContent","trim","endsWith","name","content","parallel","res","Promise","all","map","task","console","log","filter","i","length","count","forEach","clear","process","cwd","_","config"],"sources":["../src/wyw-in-js.ts"],"sourcesContent":["/* eslint-disable no-console */\n/**\n * This file contains a CLI for wyw-in-js.\n */\n\nimport fs from 'fs';\nimport path from 'path';\n\nimport { asyncResolveFallback } from '@wyw-in-js/shared';\nimport {\n createFileReporter,\n TransformCacheCollection,\n transform,\n} from '@wyw-in-js/transform';\nimport glob from 'glob';\nimport mkdirp from 'mkdirp';\nimport normalize from 'normalize-path';\nimport yargs from 'yargs';\n\nconst modulesOptions = [\n 'commonjs',\n 'es2015',\n 'es6',\n 'esnext',\n 'native',\n] as const;\n\nconst argv = yargs\n .usage('Usage: $0 [options] <files ...>')\n .option('config', {\n alias: 'c',\n type: 'string',\n description: 'Path to a config file',\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('out-dir', {\n alias: 'o',\n type: 'string',\n description: 'Output directory for the extracted CSS files',\n demandOption: true,\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('source-maps', {\n alias: 's',\n type: 'boolean',\n description: 'Generate source maps for the CSS files',\n default: false,\n })\n .option('parallel', {\n alias: 'p',\n type: 'boolean',\n description: 'Run extraction in parallel',\n default: false,\n })\n .option('source-root', {\n alias: 'r',\n type: 'string',\n description: 'Directory containing the source JS files',\n demandOption: true,\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('insert-css-requires', {\n alias: 'i',\n type: 'string',\n description:\n 'Directory containing JS files to insert require statements for the CSS files',\n requiresArg: true,\n coerce: path.resolve,\n })\n .option('transform', {\n alias: 't',\n type: 'boolean',\n description: 'Replace template tags with evaluated values',\n })\n .option('modules', {\n alias: 'm',\n choices: modulesOptions,\n description: 'Specifies a type of used imports',\n default: 'commonjs' as const,\n coerce: (s) => s.toLowerCase(),\n })\n .implies('insert-css-requires', 'source-root')\n .implies('transform', 'insert-css-requires')\n .option('ignore', {\n alias: 'x',\n type: 'string',\n description: 'Pattern of files to ignore. Be sure to provide a string',\n requiresArg: true,\n })\n .alias('help', 'h')\n .alias('version', 'v')\n .parseSync();\n\ntype Options = {\n configFile?: string;\n ignore?: string;\n insertCssRequires?: string;\n modules: (typeof modulesOptions)[number];\n outDir: string;\n parallel?: boolean;\n sourceMaps?: boolean;\n sourceRoot: string;\n transform?: boolean;\n};\n\nfunction resolveRequireInsertionFilename(filename: string) {\n return filename.replace(/\\.tsx?/, '.js');\n}\n\nfunction resolveOutputFilename(\n filename: string,\n outDir: string,\n sourceRoot: string\n) {\n const outputFolder = path.relative(sourceRoot, path.dirname(filename));\n const outputBasename = path\n .basename(filename)\n .replace(path.extname(filename), '.css');\n\n return path.join(outDir, outputFolder, outputBasename);\n}\n\nasync function processFiles(files: (number | string)[], options: Options) {\n const { emitter, onDone } = createFileReporter();\n\n const resolvedFiles = files.reduce(\n (acc, pattern) => [\n ...acc,\n ...glob.sync(pattern.toString(), {\n absolute: true,\n ignore: options.ignore,\n }),\n ],\n [] as string[]\n );\n const cache = new TransformCacheCollection();\n\n const modifiedFiles: { content: string; name: string }[] = [];\n\n const tasks: (() => Promise<boolean>)[] = [];\n\n // eslint-disable-next-line no-restricted-syntax\n for (const filename of resolvedFiles) {\n if (fs.lstatSync(filename).isDirectory()) {\n return;\n }\n\n const outputFilename = resolveOutputFilename(\n filename,\n options.outDir,\n options.sourceRoot\n );\n\n const transformServices = {\n options: {\n filename,\n outputFilename,\n pluginOptions: {\n configFile: options.configFile,\n },\n root: options.sourceRoot,\n },\n cache,\n eventEmitter: emitter,\n };\n\n tasks.push(() =>\n transform(\n transformServices,\n fs.readFileSync(filename).toString(),\n asyncResolveFallback\n ).then(({ code, cssText, sourceMap, cssSourceMapText }): boolean => {\n if (!cssText) {\n return false;\n }\n mkdirp.sync(path.dirname(outputFilename));\n\n const cssContent =\n options.sourceMaps && sourceMap\n ? `${cssText}\\n/*# sourceMappingURL=${outputFilename}.map */`\n : cssText;\n\n fs.writeFileSync(outputFilename, cssContent);\n\n if (\n options.sourceMaps &&\n sourceMap &&\n typeof cssSourceMapText !== 'undefined'\n ) {\n fs.writeFileSync(`${outputFilename}.map`, cssSourceMapText);\n }\n\n if (options.sourceRoot && options.insertCssRequires) {\n const inputFilename = path.resolve(\n options.insertCssRequires,\n path.relative(options.sourceRoot, filename)\n );\n\n const relativePath = normalize(\n path.relative(path.dirname(inputFilename), outputFilename)\n );\n\n const pathForImport = relativePath.startsWith('.')\n ? relativePath\n : `./${relativePath}`;\n\n const statement =\n options.modules === 'commonjs'\n ? `\\nrequire('${pathForImport}');`\n : `\\nimport \"${pathForImport}\";`;\n\n const normalizedInputFilename =\n resolveRequireInsertionFilename(inputFilename);\n\n const inputContent = options.transform\n ? code\n : fs.readFileSync(normalizedInputFilename, 'utf-8');\n\n if (!inputContent.trim().endsWith(statement)) {\n modifiedFiles.push({\n name: normalizedInputFilename,\n content: `${inputContent}\\n${statement}\\n`,\n });\n }\n }\n\n return true;\n })\n );\n }\n\n if (options.parallel) {\n const res = await Promise.all(tasks.map((task) => task()));\n console.log(\n `Successfully extracted ${res.filter((i) => i).length} CSS files.`\n );\n } else {\n let count = 0;\n for (const task of tasks) {\n // eslint-disable-next-line no-await-in-loop\n const res = await task();\n if (res) {\n count += 1;\n }\n }\n\n console.log(`Successfully extracted ${count} CSS files.`);\n }\n\n modifiedFiles.forEach(({ name, content }) => {\n fs.writeFileSync(name, content);\n });\n\n cache.clear('all');\n modifiedFiles.length = 0;\n resolvedFiles.length = 0;\n tasks.length = 0;\n\n onDone(options.sourceRoot ?? process.cwd());\n}\n\nprocessFiles(argv._, {\n configFile: argv.config,\n ignore: argv.ignore,\n insertCssRequires: argv['insert-css-requires'],\n modules: argv.modules,\n parallel: argv.parallel,\n outDir: argv['out-dir'],\n sourceMaps: argv['source-maps'],\n sourceRoot: argv['source-root'],\n transform: argv.transform,\n});\n"],"mappings":";;AAKA,IAAAA,GAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,KAAA,GAAAF,sBAAA,CAAAC,OAAA;AAEA,IAAAE,OAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAKA,IAAAI,KAAA,GAAAL,sBAAA,CAAAC,OAAA;AACA,IAAAK,OAAA,GAAAN,sBAAA,CAAAC,OAAA;AACA,IAAAM,cAAA,GAAAP,sBAAA,CAAAC,OAAA;AACA,IAAAO,MAAA,GAA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,CAAC;EACxB3D,aAAa,CAAC2D,MAAM,GAAG,CAAC;EACxB/C,KAAK,CAAC+C,MAAM,GAAG,CAAC;EAEhB7D,MAAM,EAAAF,mBAAA,GAACD,OAAO,CAACV,UAAU,cAAAW,mBAAA,cAAAA,mBAAA,GAAImE,OAAO,CAACC,GAAG,CAAC,CAAC,CAAC;AAC7C;AAEAvE,YAAY,CAAC9B,IAAI,CAACsG,CAAC,EAAE;EACnB9C,UAAU,EAAExD,IAAI,CAACuG,MAAM;EACvB1D,MAAM,EAAE7C,IAAI,CAAC6C,MAAM;EACnB2B,iBAAiB,EAAExE,IAAI,CAAC,qBAAqB,CAAC;EAC9C+E,OAAO,EAAE/E,IAAI,CAAC+E,OAAO;EACrBO,QAAQ,EAAEtF,IAAI,CAACsF,QAAQ;EACvBjE,MAAM,EAAErB,IAAI,CAAC,SAAS,CAAC;EACvBsE,UAAU,EAAEtE,IAAI,CAAC,aAAa,CAAC;EAC/BsB,UAAU,EAAEtB,IAAI,CAAC,aAAa,CAAC;EAC/B4D,SAAS,EAAE5D,IAAI,CAAC4D;AAClB,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wyw-in-js/cli",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"bin": {
|
|
5
|
+
"wyw-in-js": "bin/wyw-in-js.js"
|
|
6
|
+
},
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"glob": "^10.3.10",
|
|
9
|
+
"mkdirp": "^0.5.1",
|
|
10
|
+
"normalize-path": "^3.0.0",
|
|
11
|
+
"yargs": "^17.5.0",
|
|
12
|
+
"@wyw-in-js/shared": "0.2.0",
|
|
13
|
+
"@wyw-in-js/transform": "0.2.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@babel/core": "^7.23.5",
|
|
17
|
+
"@types/glob": "^7.2.0",
|
|
18
|
+
"@types/mkdirp": "^0.5.2",
|
|
19
|
+
"@types/normalize-path": "^3.0.0",
|
|
20
|
+
"@types/yargs": "^17.0.10",
|
|
21
|
+
"@wyw-in-js/babel-config": "0.2.0",
|
|
22
|
+
"@wyw-in-js/eslint-config": "0.2.0",
|
|
23
|
+
"@wyw-in-js/ts-config": "0.2.0"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=16.0.0"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"esm/",
|
|
30
|
+
"lib/",
|
|
31
|
+
"types/"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"main": "lib/wyw-in-js.js",
|
|
35
|
+
"module": "esm/wyw-in-js.js",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"types": "types/wyw-in-js.d.ts",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
42
|
+
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
43
|
+
"build:types": "tsc --project ./tsconfig.lib.json --baseUrl . --rootDir ./src",
|
|
44
|
+
"lint": "eslint --ext .js,.ts ."
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
/**
|
|
4
|
+
* This file contains a CLI for wyw-in-js.
|
|
5
|
+
*/
|
|
6
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
const fs_1 = __importDefault(require("fs"));
|
|
11
|
+
const path_1 = __importDefault(require("path"));
|
|
12
|
+
const shared_1 = require("@wyw-in-js/shared");
|
|
13
|
+
const transform_1 = require("@wyw-in-js/transform");
|
|
14
|
+
const glob_1 = __importDefault(require("glob"));
|
|
15
|
+
const mkdirp_1 = __importDefault(require("mkdirp"));
|
|
16
|
+
const normalize_path_1 = __importDefault(require("normalize-path"));
|
|
17
|
+
const yargs_1 = __importDefault(require("yargs"));
|
|
18
|
+
const modulesOptions = [
|
|
19
|
+
'commonjs',
|
|
20
|
+
'es2015',
|
|
21
|
+
'es6',
|
|
22
|
+
'esnext',
|
|
23
|
+
'native',
|
|
24
|
+
];
|
|
25
|
+
const argv = yargs_1.default
|
|
26
|
+
.usage('Usage: $0 [options] <files ...>')
|
|
27
|
+
.option('config', {
|
|
28
|
+
alias: 'c',
|
|
29
|
+
type: 'string',
|
|
30
|
+
description: 'Path to a config file',
|
|
31
|
+
requiresArg: true,
|
|
32
|
+
coerce: path_1.default.resolve,
|
|
33
|
+
})
|
|
34
|
+
.option('out-dir', {
|
|
35
|
+
alias: 'o',
|
|
36
|
+
type: 'string',
|
|
37
|
+
description: 'Output directory for the extracted CSS files',
|
|
38
|
+
demandOption: true,
|
|
39
|
+
requiresArg: true,
|
|
40
|
+
coerce: path_1.default.resolve,
|
|
41
|
+
})
|
|
42
|
+
.option('source-maps', {
|
|
43
|
+
alias: 's',
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
description: 'Generate source maps for the CSS files',
|
|
46
|
+
default: false,
|
|
47
|
+
})
|
|
48
|
+
.option('parallel', {
|
|
49
|
+
alias: 'p',
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
description: 'Run extraction in parallel',
|
|
52
|
+
default: false,
|
|
53
|
+
})
|
|
54
|
+
.option('source-root', {
|
|
55
|
+
alias: 'r',
|
|
56
|
+
type: 'string',
|
|
57
|
+
description: 'Directory containing the source JS files',
|
|
58
|
+
demandOption: true,
|
|
59
|
+
requiresArg: true,
|
|
60
|
+
coerce: path_1.default.resolve,
|
|
61
|
+
})
|
|
62
|
+
.option('insert-css-requires', {
|
|
63
|
+
alias: 'i',
|
|
64
|
+
type: 'string',
|
|
65
|
+
description: 'Directory containing JS files to insert require statements for the CSS files',
|
|
66
|
+
requiresArg: true,
|
|
67
|
+
coerce: path_1.default.resolve,
|
|
68
|
+
})
|
|
69
|
+
.option('transform', {
|
|
70
|
+
alias: 't',
|
|
71
|
+
type: 'boolean',
|
|
72
|
+
description: 'Replace template tags with evaluated values',
|
|
73
|
+
})
|
|
74
|
+
.option('modules', {
|
|
75
|
+
alias: 'm',
|
|
76
|
+
choices: modulesOptions,
|
|
77
|
+
description: 'Specifies a type of used imports',
|
|
78
|
+
default: 'commonjs',
|
|
79
|
+
coerce: (s) => s.toLowerCase(),
|
|
80
|
+
})
|
|
81
|
+
.implies('insert-css-requires', 'source-root')
|
|
82
|
+
.implies('transform', 'insert-css-requires')
|
|
83
|
+
.option('ignore', {
|
|
84
|
+
alias: 'x',
|
|
85
|
+
type: 'string',
|
|
86
|
+
description: 'Pattern of files to ignore. Be sure to provide a string',
|
|
87
|
+
requiresArg: true,
|
|
88
|
+
})
|
|
89
|
+
.alias('help', 'h')
|
|
90
|
+
.alias('version', 'v')
|
|
91
|
+
.parseSync();
|
|
92
|
+
function resolveRequireInsertionFilename(filename) {
|
|
93
|
+
return filename.replace(/\.tsx?/, '.js');
|
|
94
|
+
}
|
|
95
|
+
function resolveOutputFilename(filename, outDir, sourceRoot) {
|
|
96
|
+
const outputFolder = path_1.default.relative(sourceRoot, path_1.default.dirname(filename));
|
|
97
|
+
const outputBasename = path_1.default
|
|
98
|
+
.basename(filename)
|
|
99
|
+
.replace(path_1.default.extname(filename), '.css');
|
|
100
|
+
return path_1.default.join(outDir, outputFolder, outputBasename);
|
|
101
|
+
}
|
|
102
|
+
async function processFiles(files, options) {
|
|
103
|
+
const { emitter, onDone } = (0, transform_1.createFileReporter)();
|
|
104
|
+
const resolvedFiles = files.reduce((acc, pattern) => [
|
|
105
|
+
...acc,
|
|
106
|
+
...glob_1.default.sync(pattern.toString(), {
|
|
107
|
+
absolute: true,
|
|
108
|
+
ignore: options.ignore,
|
|
109
|
+
}),
|
|
110
|
+
], []);
|
|
111
|
+
const cache = new transform_1.TransformCacheCollection();
|
|
112
|
+
const modifiedFiles = [];
|
|
113
|
+
const tasks = [];
|
|
114
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
115
|
+
for (const filename of resolvedFiles) {
|
|
116
|
+
if (fs_1.default.lstatSync(filename).isDirectory()) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const outputFilename = resolveOutputFilename(filename, options.outDir, options.sourceRoot);
|
|
120
|
+
const transformServices = {
|
|
121
|
+
options: {
|
|
122
|
+
filename,
|
|
123
|
+
outputFilename,
|
|
124
|
+
pluginOptions: {
|
|
125
|
+
configFile: options.configFile,
|
|
126
|
+
},
|
|
127
|
+
root: options.sourceRoot,
|
|
128
|
+
},
|
|
129
|
+
cache,
|
|
130
|
+
eventEmitter: emitter,
|
|
131
|
+
};
|
|
132
|
+
tasks.push(() => (0, transform_1.transform)(transformServices, fs_1.default.readFileSync(filename).toString(), shared_1.asyncResolveFallback).then(({ code, cssText, sourceMap, cssSourceMapText }) => {
|
|
133
|
+
if (!cssText) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
mkdirp_1.default.sync(path_1.default.dirname(outputFilename));
|
|
137
|
+
const cssContent = options.sourceMaps && sourceMap
|
|
138
|
+
? `${cssText}\n/*# sourceMappingURL=${outputFilename}.map */`
|
|
139
|
+
: cssText;
|
|
140
|
+
fs_1.default.writeFileSync(outputFilename, cssContent);
|
|
141
|
+
if (options.sourceMaps &&
|
|
142
|
+
sourceMap &&
|
|
143
|
+
typeof cssSourceMapText !== 'undefined') {
|
|
144
|
+
fs_1.default.writeFileSync(`${outputFilename}.map`, cssSourceMapText);
|
|
145
|
+
}
|
|
146
|
+
if (options.sourceRoot && options.insertCssRequires) {
|
|
147
|
+
const inputFilename = path_1.default.resolve(options.insertCssRequires, path_1.default.relative(options.sourceRoot, filename));
|
|
148
|
+
const relativePath = (0, normalize_path_1.default)(path_1.default.relative(path_1.default.dirname(inputFilename), outputFilename));
|
|
149
|
+
const pathForImport = relativePath.startsWith('.')
|
|
150
|
+
? relativePath
|
|
151
|
+
: `./${relativePath}`;
|
|
152
|
+
const statement = options.modules === 'commonjs'
|
|
153
|
+
? `\nrequire('${pathForImport}');`
|
|
154
|
+
: `\nimport "${pathForImport}";`;
|
|
155
|
+
const normalizedInputFilename = resolveRequireInsertionFilename(inputFilename);
|
|
156
|
+
const inputContent = options.transform
|
|
157
|
+
? code
|
|
158
|
+
: fs_1.default.readFileSync(normalizedInputFilename, 'utf-8');
|
|
159
|
+
if (!inputContent.trim().endsWith(statement)) {
|
|
160
|
+
modifiedFiles.push({
|
|
161
|
+
name: normalizedInputFilename,
|
|
162
|
+
content: `${inputContent}\n${statement}\n`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
if (options.parallel) {
|
|
170
|
+
const res = await Promise.all(tasks.map((task) => task()));
|
|
171
|
+
console.log(`Successfully extracted ${res.filter((i) => i).length} CSS files.`);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
let count = 0;
|
|
175
|
+
for (const task of tasks) {
|
|
176
|
+
// eslint-disable-next-line no-await-in-loop
|
|
177
|
+
const res = await task();
|
|
178
|
+
if (res) {
|
|
179
|
+
count += 1;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
console.log(`Successfully extracted ${count} CSS files.`);
|
|
183
|
+
}
|
|
184
|
+
modifiedFiles.forEach(({ name, content }) => {
|
|
185
|
+
fs_1.default.writeFileSync(name, content);
|
|
186
|
+
});
|
|
187
|
+
cache.clear('all');
|
|
188
|
+
modifiedFiles.length = 0;
|
|
189
|
+
resolvedFiles.length = 0;
|
|
190
|
+
tasks.length = 0;
|
|
191
|
+
onDone(options.sourceRoot ?? process.cwd());
|
|
192
|
+
}
|
|
193
|
+
processFiles(argv._, {
|
|
194
|
+
configFile: argv.config,
|
|
195
|
+
ignore: argv.ignore,
|
|
196
|
+
insertCssRequires: argv['insert-css-requires'],
|
|
197
|
+
modules: argv.modules,
|
|
198
|
+
parallel: argv.parallel,
|
|
199
|
+
outDir: argv['out-dir'],
|
|
200
|
+
sourceMaps: argv['source-maps'],
|
|
201
|
+
sourceRoot: argv['source-root'],
|
|
202
|
+
transform: argv.transform,
|
|
203
|
+
});
|