1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict'
|
14 |
|
15 | const aglob = require('aglob')
|
16 | const convertSourceMap = require('convert-source-map')
|
17 | const writeout = require('writeout')
|
18 | const akvStatus = require('akv-status')
|
19 | const path = require('path')
|
20 | const filedel = require('filedel')
|
21 | const {statAsync} = require('asfs')
|
22 | const {isProduction} = require('asenv')
|
23 | const {transformFile} = require('@babel/core')
|
24 | const {DEFAULT_PRESET, DEFAULT_EXT} = require('./constants')
|
25 |
|
26 | const {replaceExt} = require('./helpers/file_helper')
|
27 | const relative = (filename) => path.relative(process.cwd(), filename)
|
28 | const mtime = (filename) => statAsync(filename).catch(() => null).then(({mtime}) => mtime)
|
29 |
|
30 |
|
31 | async function ababel (pattern, options = {}) {
|
32 | let {
|
33 | status = 'tmp/ababel.status.json',
|
34 | cwd = process.cwd(),
|
35 | out = process.cwd(),
|
36 |
|
37 | presets = DEFAULT_PRESET.split(','),
|
38 | sourceMaps = !isProduction(),
|
39 | minified = false,
|
40 | ignore = [],
|
41 | reflects = [],
|
42 | plugins = [],
|
43 | sourceRoot,
|
44 | ext = DEFAULT_EXT.split(','),
|
45 | } = options
|
46 |
|
47 | const store = akvStatus(status)
|
48 | try {
|
49 | const filenames = await aglob(pattern, {cwd, ignore})
|
50 | reflects = await aglob(reflects)
|
51 | for (const filename of filenames) {
|
52 | const src = relative(path.resolve(cwd, filename))
|
53 | const dest = relative(path.resolve(out, ext.reduce((filename, ext) => replaceExt(filename, ext, '.js'), filename)))
|
54 | if (!isProduction()) {
|
55 | const changed = await store.filterStatusUnknown([src, dest, ...reflects])
|
56 | if (changed.length === 0) {
|
57 | let srcMtime = await mtime(src)
|
58 | let destMtime = await mtime(dest)
|
59 | let skip = srcMtime && destMtime && (srcMtime <= destMtime)
|
60 | if (skip) {
|
61 | continue
|
62 | }
|
63 | }
|
64 | }
|
65 | const {code, map, ast} = await new Promise((resolve, reject) => {
|
66 | const options = {
|
67 | presets,
|
68 | minified,
|
69 | sourceMaps,
|
70 | compact: false,
|
71 | babelrc: false,
|
72 | filename: path.resolve(src),
|
73 | filenameRelative: path.relative(cwd, path.resolve(src)),
|
74 | sourceRoot: sourceRoot || path.relative(out, cwd),
|
75 | plugins: ['@babel/plugin-transform-runtime', ...plugins],
|
76 | }
|
77 | transformFile(src, options, (err, result) => err ? reject(err) : resolve(result))
|
78 | })
|
79 | try {
|
80 | if (dest !== src) {
|
81 | await filedel(dest)
|
82 | }
|
83 | } catch (err) {
|
84 |
|
85 | }
|
86 | const {skipped} = await writeout(dest, `${code}\n${convertSourceMap.fromObject(map).toComment()}`, {
|
87 | mkdirp: true,
|
88 | skipIfIdentical: true
|
89 | })
|
90 | if (!skipped) {
|
91 | console.log(`File generated: ${relative(dest)}`)
|
92 | }
|
93 |
|
94 | await store.saveStatus([src, dest, ...reflects])
|
95 | }
|
96 | } catch (err) {
|
97 | await store.destroy()
|
98 | throw err
|
99 | }
|
100 | }
|
101 |
|
102 | module.exports = ababel
|