Skip to content

Commit b381aa6

Browse files
committed
integrate CLI - fixes sveltejs#1360
1 parent 1130556 commit b381aa6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+6062
-15
lines changed

cli/compile.js

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
5+
var path = require('path');
6+
var fs = require('fs');
7+
var svelte = require('../compiler/svelte.js');
8+
9+
var ansi = (strip => ({
10+
reset: strip(["\x1b[0m"]),
11+
newLine: ["\n", ""],
12+
tab: ["\t", ""],
13+
14+
black: strip(["\x1b[30m", "\x1b[39m"]),
15+
red: strip(["\x1b[31m", "\x1b[39m"]),
16+
green: strip(["\x1b[32m", "\x1b[39m"]),
17+
yellow: strip(["\x1b[33m", "\x1b[39m"]),
18+
blue: strip(["\x1b[34m", "\x1b[39m"]),
19+
magenta: strip(["\x1b[35m", "\x1b[39m"]),
20+
cyan: strip(["\x1b[36m", "\x1b[39m"]),
21+
white: strip(["\x1b[37m", "\x1b[39m"]),
22+
gray: strip(["\x1B[90m", "\x1b[39m"]),
23+
24+
bgBlack: strip(["\x1b[40m", "\x1b[49m"]),
25+
bgRed: strip(["\x1b[41m", "\x1b[49m"]),
26+
bgGreen: strip(["\x1b[42m", "\x1b[49m"]),
27+
bgYellow: strip(["\x1b[43m", "\x1b[49m"]),
28+
bgBlue: strip(["\x1b[44m", "\x1b[49m"]),
29+
bgMagenta: strip(["\x1b[45m", "\x1b[49m"]),
30+
bgCyan: strip(["\x1b[46m", "\x1b[49m"]),
31+
bgWhite: strip(["\x1b[47m", "\x1b[49m"]),
32+
33+
dim: strip(["\x1b[2m", "\x1b[22m"]),
34+
bold: strip(["\x1b[1m", "\x1b[22m"]),
35+
hidden: strip(["\x1b[8m", "\x1b[28m"]),
36+
italic: strip(["\x1b[3m", "\x1b[23m"]),
37+
underline: strip(["\x1b[4m", "\x1b[24m"]),
38+
inverse: strip(["\x1b[7m", "\x1b[27m"]),
39+
strikethrough: strip(["\x1b[9m", "\x1b[29m"])
40+
}))(
41+
ansi =>
42+
process.env.FORCE_COLOR ||
43+
process.platform === "win32" ||
44+
(process.stdout.isTTY && process.env.TERM && process.env.TERM !== "dumb")
45+
? ansi
46+
: ["", ""]
47+
);
48+
49+
var ansi$1 = /*#__PURE__*/Object.freeze({
50+
default: ansi,
51+
__moduleExports: ansi
52+
});
53+
54+
var ansi$2 = ( ansi$1 && ansi ) || ansi$1;
55+
56+
var clorox = (function Clorox(old, close) {
57+
const clorox = s => Clorox(clorox.toString(s));
58+
59+
clorox.toString = s => old + (s || "") + (close || ansi$2.reset[0]);
60+
61+
Object.keys(ansi$2).map(name => {
62+
Object.defineProperty(clorox, name, {
63+
get: () => Clorox(old + ansi$2[name][0], (close || "") + ansi$2[name][1])
64+
});
65+
});
66+
67+
return clorox
68+
})("");
69+
70+
function stderr(msg) {
71+
console.error(msg); // eslint-disable-line no-console
72+
}
73+
74+
function error(err) {
75+
stderr(`${clorox.red(err.message || err)}`);
76+
77+
if (err.frame) {
78+
stderr(err.frame); // eslint-disable-line no-console
79+
} else if (err.stack) {
80+
stderr(`${clorox.grey(err.stack)}`);
81+
}
82+
83+
process.exit(1);
84+
}
85+
86+
function mkdirp(dir) {
87+
const parent = path.dirname(dir);
88+
if (dir === parent) return;
89+
90+
mkdirp(parent);
91+
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
92+
}
93+
94+
async function compile(input, opts) {
95+
if (opts._.length > 0) {
96+
error(`Can only compile a single file or directory`);
97+
}
98+
99+
const output = opts.output;
100+
101+
const stats = fs.statSync(input);
102+
const isDir = stats.isDirectory();
103+
104+
if (isDir) {
105+
if (!output) {
106+
error(`You must specify an --output (-o) option when compiling a directory of files`);
107+
}
108+
109+
if (opts.name || opts.amdId) {
110+
error(`Cannot specify --${opts.name ? 'name' : 'amdId'} when compiling a directory`);
111+
}
112+
}
113+
114+
const globals = {};
115+
if (opts.globals) {
116+
opts.globals.split(',').forEach(pair => {
117+
const [key, value] = pair.split(':');
118+
globals[key] = value;
119+
});
120+
}
121+
122+
const options = {
123+
name: opts.name,
124+
format: opts.format,
125+
sourceMap: opts.sourcemap,
126+
globals,
127+
css: opts.css !== false,
128+
dev: opts.dev,
129+
immutable: opts.immutable,
130+
generate: opts.generate || 'dom',
131+
customElement: opts.customElement,
132+
store: opts.store
133+
};
134+
135+
if (isDir) {
136+
mkdirp(output);
137+
compileDirectory(input, output, options);
138+
} else {
139+
compileFile(input, output, options);
140+
}
141+
}
142+
143+
function compileDirectory(input, output, options) {
144+
fs.readdirSync(input).forEach(file => {
145+
const src = path.resolve(input, file);
146+
const dest = path.resolve(output, file);
147+
148+
if (path.extname(file) === '.html') {
149+
compileFile(
150+
src,
151+
dest.substring(0, dest.lastIndexOf('.html')) + '.js',
152+
options
153+
);
154+
} else {
155+
const stats = fs.statSync(src);
156+
if (stats.isDirectory()) {
157+
compileDirectory(src, dest, options);
158+
}
159+
}
160+
});
161+
}
162+
163+
let SOURCEMAPPING_URL = 'sourceMa';
164+
SOURCEMAPPING_URL += 'ppingURL';
165+
166+
function compileFile(input, output, options) {
167+
console.error(`compiling ${path.relative(process.cwd(), input)}...`); // eslint-disable-line no-console
168+
169+
options = Object.assign({}, options);
170+
if (!options.name) options.name = getName(input);
171+
172+
options.filename = input;
173+
options.outputFilename = output;
174+
175+
const { sourceMap } = options;
176+
const inline = sourceMap === 'inline';
177+
178+
let source = fs.readFileSync(input, 'utf-8');
179+
if (source[0] === 0xfeff) source = source.slice(1);
180+
181+
let compiled;
182+
183+
try {
184+
compiled = svelte.compile(source, options);
185+
} catch (err) {
186+
error(err);
187+
}
188+
189+
const { js } = compiled;
190+
191+
if (sourceMap) {
192+
js.code += `\n//# ${SOURCEMAPPING_URL}=${inline || !output
193+
? js.map.toUrl()
194+
: `${path.basename(output)}.map`}\n`;
195+
}
196+
197+
if (output) {
198+
const outputDir = path.dirname(output);
199+
mkdirp(outputDir);
200+
fs.writeFileSync(output, js.code);
201+
console.error(`wrote ${path.relative(process.cwd(), output)}`); // eslint-disable-line no-console
202+
if (sourceMap && !inline) {
203+
fs.writeFileSync(`${output}.map`, js.map);
204+
console.error(`wrote ${path.relative(process.cwd(), `${output}.map`)}`); // eslint-disable-line no-console
205+
}
206+
} else {
207+
process.stdout.write(js.code);
208+
}
209+
}
210+
211+
function getName(input) {
212+
return path.basename(input)
213+
.replace(path.extname(input), '')
214+
.replace(/[^a-zA-Z_$0-9]+/g, '_')
215+
.replace(/^_/, '')
216+
.replace(/_$/, '')
217+
.replace(/^(\d)/, '_$1');
218+
}
219+
220+
exports.compile = compile;

0 commit comments

Comments
 (0)