-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrequire-json.js
31 lines (26 loc) · 1.16 KB
/
require-json.js
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
// RequireJS plugin to handle embedding JSON files (such as for string resources)
define(['require'], function (req) {
"use strict";
var api = {};
var content = {};
// Called to load a resource. This is the only mandatory API method that
// needs to be implemented for the plugin to be useful.
api.load = function (name, parentRequire, onLoad, config) {
// Do nothing outside of optimized build
if (!config.isBuild) {
onLoad();
return;
}
var fs = require.nodeRequire('fs');
var filePath = parentRequire.toUrl(name);
content[name] = fs.readFileSync(filePath, "utf-8").replace(/^\uFEFF/, '');
onLoad();
};
// Used by the optimizer to indicate when the plugin should write out
// a representation of the the resource in the optimized file.
api.write = function (pluginName, moduleName, write) {
write.asModule(pluginName + '!' + moduleName, 'define(' + content[moduleName] + ')');
};
return api;
});