UNPKG

1.12 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7'use strict';
8
9const crypto = require('crypto');
10
11const macroCheck = new RegExp('[./]macro');
12
13module.exports = function() {
14 return {
15 // This function transforms the Babel configuration on a per-file basis
16 config(config, { source }) {
17 // Babel Macros are notoriously hard to cache, so they shouldn't be
18 // https://github.com/babel/babel/issues/8497
19 // We naively detect macros using their package suffix and add a random token
20 // to the caller, a valid option accepted by Babel, to compose a one-time
21 // cacheIdentifier for the file. We cannot tune the loader options on a per
22 // file basis.
23 if (macroCheck.test(source)) {
24 return Object.assign({}, config.options, {
25 caller: Object.assign({}, config.options.caller, {
26 craInvalidationToken: crypto.randomBytes(32).toString('hex'),
27 }),
28 });
29 }
30 return config.options;
31 },
32 };
33};