/
flow-and-prod-dev-entry.ts
142 lines (131 loc) Β· 4.24 KB
/
flow-and-prod-dev-entry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import path from "path";
import { Plugin } from "rollup";
import { getDevPath, getProdPath } from "../build/utils";
import { flowTemplate } from "../utils";
import { Package } from "../package";
import { FatalError } from "../errors";
import * as fs from "fs-extra";
import normalizePath from "normalize-path";
const allowedExtensionRegex = /\.([tj]sx?|json)$/;
export default function flowAndNodeDevProdEntry(
pkg: Package,
warnings: FatalError[]
): Plugin {
return {
name: "flow-and-prod-dev-entry",
load(id) {
if (id === "could-not-resolve") {
return "";
}
return null;
},
async resolveId(source, importer) {
let resolved = await this.resolve(source, importer, {
skipSelf: true,
});
if (resolved === null) {
if (!source.startsWith(".")) {
warnings.push(
new FatalError(
`"${source}" is imported ${
importer
? `by "${normalizePath(
path.relative(pkg.directory, importer!)
)}"`
: ""
} but the package is not specified in dependencies or peerDependencies`,
pkg.name
)
);
return "could-not-resolve";
}
throw new FatalError(
`Could not resolve ${source} ` +
(importer ? `from ${path.relative(pkg.directory, importer)}` : ""),
pkg.name
);
}
if (source.startsWith("\0") || resolved.id.startsWith("\0")) {
return resolved;
}
if (resolved.id.startsWith(pkg.directory)) {
if (!resolved.external && !allowedExtensionRegex.test(resolved.id)) {
warnings.push(
new FatalError(
`only .ts, .tsx, .js, .jsx, and .json files can be imported but "${source}" is imported in ${
importer
? `"${normalizePath(path.relative(pkg.directory, importer))}"`
: "a module"
}`,
pkg.name
)
);
return "could-not-resolve";
}
return resolved;
}
warnings.push(
new FatalError(
`all relative imports in a package should only import modules inside of their package directory but ${
importer
? `"${normalizePath(path.relative(pkg.directory, importer))}"`
: "a module"
} is importing "${source}"`,
pkg.name
)
);
return "could-not-resolve";
},
async generateBundle(opts, bundle) {
for (const n in bundle) {
const file = bundle[n];
if (
file.type === "asset" ||
!(file.type === "chunk" && file.isEntry) ||
file.facadeModuleId == null
) {
continue;
}
let mainFieldPath = file.fileName.replace(/\.prod\.js$/, ".js");
let relativeToSource = path.relative(
path.dirname(path.join(opts.dir!, file.fileName)),
file.facadeModuleId
);
let isEntrySourceTypeScript = /\.tsx?$/.test(file.facadeModuleId);
if (!isEntrySourceTypeScript) {
let flowMode: false | "all" | "named" = false;
let source = await fs.readFile(file.facadeModuleId, "utf8");
if (source.includes("@flow")) {
flowMode = file.exports.includes("default") ? "all" : "named";
}
if (flowMode !== false) {
let flowFileSource = flowTemplate(
flowMode === "all",
normalizePath(relativeToSource)
);
let flowFileName = mainFieldPath + ".flow";
this.emitFile({
type: "asset",
fileName: flowFileName,
source: flowFileSource,
});
}
}
let mainEntrySource = `'use strict';
if (${
// tricking static analysis is fun...
"process" + ".env.NODE_ENV"
} === "production") {
module.exports = require("./${path.basename(getProdPath(mainFieldPath))}");
} else {
module.exports = require("./${path.basename(getDevPath(mainFieldPath))}");
}\n`;
this.emitFile({
type: "asset",
fileName: mainFieldPath,
source: mainEntrySource,
});
}
},
};
}