-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.js
More file actions
235 lines (210 loc) · 7.99 KB
/
Copy pathplugin.js
File metadata and controls
235 lines (210 loc) · 7.99 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const solidOptionsCache = {};
function getSolidConfig(inputFile) {
const isClient = inputFile.getArch().startsWith('web');
let solidOptions = {};
// Read package.json, based on _inferFromPackageJson in
// https://github.com/meteor/meteor/blob/devel/packages/babel-compiler/babel-compiler.js
// But use root package.json over one in node_modules, to support recompiling
// an NPM package using the `solid` settings from the app.
let pkgJsonPath = inputFile.findControlFile &&
inputFile.findControlFile('package.json');
if (pkgJsonPath) {
const match = /\/node_modules\/[^/]*\/package.json$/.exec(pkgJsonPath);
if (match) {
pkgJsonPath = pkgJsonPath.slice(0, pkgJsonPath.length - match[0].length)
+ '/package.json';
// Ensure file exists, similar to InputFile's findControlFile
// https://github.com/meteor/meteor/blob/devel/tools/isobuild/compiler-plugin.js
const stat = inputFile._stat(pkgJsonPath);
if (!(stat && stat.isFile()))
pkgJsonPath = null;
}
}
if (pkgJsonPath) {
if (!Object.hasOwnProperty(solidOptionsCache, pkgJsonPath)) {
solidOptionsCache[pkgJsonPath] = JSON.parse(
inputFile.readAndWatchFile(pkgJsonPath)).solid || {};
}
solidOptions = solidOptionsCache[pkgJsonPath];
}
// Default behavior is to turn on Solid mode always.
// Two options can override this:
// * 'match' specifies one or more micromatch patterns for filenames that
// should have Solid compilation; anything else will no longer by default.
// * 'ignore' specifies one or more micromatch patterns for filenames that
// should not have Solid compilation, overriding 'match' if both specified.
let useSolid = true;
if (solidOptions.match)
useSolid = Npm.require('micromatch').isMatch(
inputFile.getPathInPackage(), solidOptions.match);
if (solidOptions.ignore)
useSolid = useSolid && !Npm.require('micromatch').isMatch(
inputFile.getPathInPackage(), solidOptions.ignore);
// Compute Solid preset
let solidPreset = null;
if (useSolid) {
if (solidOptions.ssr) {
const hydratable = solidOptions.hydratable !== false;
if (isClient)
solidPreset = ["solid", {generate: "dom", hydratable}];
else // server
solidPreset = ["solid", {generate: "ssr", hydratable}];
} else {
if (isClient)
solidPreset = ["solid"];
}
}
return {solidOptions, isClient, useSolid, solidPreset};
}
function modifyBabelConfig(babelOptions, inputFile) {
const {solidOptions, isClient, useSolid, solidPreset} =
getSolidConfig(inputFile);
// Modify babelOptions in-place.
if (solidPreset) {
if (!babelOptions.presets)
babelOptions.presets = [];
babelOptions.presets.push(solidPreset);
} else {
// Fall back to React mode, Meteor's default.
// Modify Meteor's options which are in a bundle as the first preset.
const options = babelOptions.presets[0];
// Copied from maybeAddReactPlugins from
// https://github.com/meteor/meteor/blob/devel/npm-packages/meteor-babel/options.js
// but replacing `require` with `Npm.require` for use in Meteor package.
options.presets.push(Npm.require("@babel/preset-react"));
options.plugins.push(
[Npm.require("@babel/plugin-proposal-class-properties"), {
loose: true
}]
);
// HMR enabling based on
// https://github.com/meteor/meteor/blob/devel/packages/ecmascript/plugin.js
if (inputFile.hmrAvailable() && Package.ReactFastRefresh) {
babelOptions.plugins = babelOptions.plugins || [];
babelOptions.plugins.push(...Package.ReactFastRefresh.getBabelPluginConfig());
}
}
if (solidOptions.verbose) {
console.log(inputFile.getPathInPackage() +
(inputFile.getPackageName() ?
` in package ${inputFile.getPackageName()}` : ''),
`on ${isClient ? 'client' : 'server'}`,
`using ${useSolid ? 'Solid' : 'React'}` +
(useSolid ? ` with Babel preset ${JSON.stringify(solidPreset)}` : ''));
}
}
// Tell other plugins not to use React, and to use our modifyBabelConfig.
// For example, edemaine:civet supports this protocol.
globalThis.babelFeatures = {
...globalThis.babelFeatures,
react: false,
};
const previousModifyBabelConfig = globalThis.modifyBabelConfig;
globalThis.modifyBabelConfig = function (babelOptions, inputFile) {
if (previousModifyBabelConfig) {
previousModifyBabelConfig(babelOptions, inputFile);
}
return modifyBabelConfig(babelOptions, inputFile);
};
Plugin.registerCompiler({
extensions: ['js', 'jsx', 'mjs'],
}, function () {
return new BabelCompiler({
...globalThis.babelFeatures,
react: false,
}, globalThis.modifyBabelConfig);
});
Plugin.registerCompiler({
extensions: ["ts", "tsx"],
}, function () {
return new TypeScriptCompiler({
...globalThis.babelFeatures,
react: false,
typescript: true,
}, globalThis.modifyBabelConfig);
});
// Copied from Meteor's typescript package:
// https://github.com/meteor/meteor/blob/devel/packages/typescript/plugin.js
class TypeScriptCompiler extends BabelCompiler {
processFilesForTarget(inputFiles) {
return super.processFilesForTarget(inputFiles.filter(
// TypeScript .d.ts declaration files look like .ts files, but it's
// important that we do not compile them using the TypeScript
// compiler, as it will fail with a cryptic error message.
file => ! file.getPathInPackage().endsWith(".d.ts")
));
}
}
if (Package['coffeescript-compiler']) {
class SolidCoffeeScriptCompiler extends Package['coffeescript-compiler'].CoffeeScriptCompiler {
constructor() {
super();
// Override super's babelCompiler to use our Solid compiler:
this.babelCompiler = new BabelCompiler({
...globalThis.babelFeatures,
react: false,
}, globalThis.modifyBabelConfig);
}
}
// The following code is copied from
// https://github.com/meteor/meteor/blob/devel/packages/non-core/coffeescript/compile-coffeescript.js
// copyright Meteor Software Ltd., licensed under MIT License
// Modified to use SolidCoffeeScriptCompiler over CoffeeScriptCompiler
// The CompileResult for this CachingCompiler is a {source, sourceMap} object.
class CachedCoffeeScriptCompiler extends CachingCompiler {
constructor() {
super({
compilerName: 'solid-coffeescript',
defaultCacheSize: 1024*1024*10,
});
this.coffeeScriptCompiler = new SolidCoffeeScriptCompiler();
}
getCacheKey(inputFile) {
const {useSolid, solidPreset} = getSolidConfig(inputFile);
return [
inputFile.getArch(),
inputFile.getSourceHash(),
inputFile.getDeclaredExports(),
this.coffeeScriptCompiler.getCompileOptions(inputFile),
useSolid,
solidPreset,
];
}
setDiskCacheDirectory(cacheDir) {
this.coffeeScriptCompiler.babelCompiler.setDiskCacheDirectory(cacheDir);
return super.setDiskCacheDirectory(cacheDir);
}
compileOneFileLater(inputFile, getResult) {
inputFile.addJavaScript({
path: this.coffeeScriptCompiler.outputFilePath(inputFile),
sourcePath: inputFile.getPathInPackage(),
bare: inputFile.getFileOptions().bare
}, async () => {
const result = await getResult();
return result && {
data: result.source,
sourceMap: result.sourceMap,
};
});
}
compileOneFile(inputFile) {
return this.coffeeScriptCompiler.compileOneFile(inputFile);
}
addCompileResult(inputFile, sourceWithMap) {
inputFile.addJavaScript({
path: this.coffeeScriptCompiler.outputFilePath(inputFile),
sourcePath: inputFile.getPathInPackage(),
data: sourceWithMap.source,
sourceMap: sourceWithMap.sourceMap,
bare: inputFile.getFileOptions().bare
});
}
compileResultSize(sourceWithMap) {
return sourceWithMap.source.length +
this.sourceMapSize(sourceWithMap.sourceMap);
}
}
Plugin.registerCompiler({
extensions: ['coffee', 'litcoffee', 'coffee.md']
}, () => new CachedCoffeeScriptCompiler());
}