-
Notifications
You must be signed in to change notification settings - Fork 293
/
index.js
159 lines (149 loc) · 4.92 KB
/
index.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
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
const resolve = require("resolve");
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const WebpackParser = require('webpack/lib/Parser');
const webpackParse = WebpackParser.parse
// overload the webpack parser so that we can make
// acorn work with the node.js / commonjs semantics
// of being able to `return` in the top level of a
// requireable module
// https://github.com/zeit/ncc/issues/40
WebpackParser.parse = function (source, opts = {}) {
return webpackParse.call(this, source, {
...opts,
allowReturnOutsideFunction: true
});
}
const SUPPORTED_EXTENSIONS = [".js", ".mjs", ".json"];
function resolveModule(context, request, callback, forcedExternals = []) {
const resolveOptions = {
basedir: context,
preserveSymlinks: true,
extensions: SUPPORTED_EXTENSIONS
};
if (new Set(forcedExternals).has(request)) {
console.error(`ncc: Skipping bundling "${request}" per config`);
return callback(null, `commonjs ${request}`);
}
resolve(request, resolveOptions, err => {
if (err) {
console.error(
`ncc: Module directory "${context}" attempted to require "${request}" but could not be resolved, assuming external.`
);
return callback(null, `commonjs ${request}`);
}
callback();
});
}
module.exports = async (entry, { externals = [], minify = true, sourceMap = false } = {}) => {
const mfs = new MemoryFS();
const compiler = webpack({
entry,
optimization: {
nodeEnv: false,
minimize: false
},
devtool: sourceMap ? "cheap-source-map" : false,
mode: "production",
target: "node",
output: {
path: "/",
filename: "out.js",
libraryTarget: "commonjs2"
},
resolve: {
extensions: SUPPORTED_EXTENSIONS,
// webpack defaults to `module` and `main`, but that's
// not really what node.js supports, so we reset it
mainFields: ["main"]
},
// https://github.com/zeit/ncc/pull/29#pullrequestreview-177152175
node: false,
externals: (...args) => resolveModule(...[...args, externals]),
module: {
rules: [
{
parser: { amd: false }
},
{
test: /\.(js|mjs)/,
use: [{ loader: __dirname + "/asset-relocator.js" }]
}
]
},
plugins: [
{
apply(compiler) {
// override "not found" context to try built require first
compiler.hooks.compilation.tap("ncc", compilation => {
compilation.moduleTemplates.javascript.hooks.render.tap("ncc", (moduleSourcePostModule, module, options, dependencyTemplates) => {
if (module._contextDependencies &&
moduleSourcePostModule._value.match(/webpackEmptyAsyncContext|webpackEmptyContext/)) {
return moduleSourcePostModule._value.replace('var e = new Error',
`try { return require(req) }\ncatch (e) { if (e.code !== 'MODULE_NOT_FOUND') throw e }` +
`\nvar e = new Error`);
}
});
});
compiler.hooks.normalModuleFactory.tap("ncc", NormalModuleFactory => {
function handler(parser) {
parser.hooks.assign.for("require").intercept({
register: tapInfo => {
if (tapInfo.name !== "CommonJsPlugin") {
return tapInfo;
}
tapInfo.fn = () => {};
return tapInfo;
}
});
}
NormalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("ncc", handler);
NormalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("ncc", handler);
return NormalModuleFactory;
});
}
}
]
});
compiler.inputFileSystem = fs;
compiler.outputFileSystem = mfs;
compiler.resolvers.normal.fileSystem = mfs;
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) return reject(err);
if (stats.hasErrors()) {
return reject(new Error(stats.toString()));
}
const assets = Object.create(null);
getFlatFiles(mfs.data, assets);
delete assets["out.js"];
delete assets["out.js.map"];
const code = mfs.readFileSync("/out.js", "utf8");
const map = sourceMap ? mfs.readFileSync("/out.js.map", "utf8") : null;
resolve({
code,
map,
assets
});
});
});
};
// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles (mfsData, output, curBase = '') {
for (const path of Object.keys(mfsData)) {
const item = mfsData[path];
const curPath = curBase + '/' + path;
// directory
if (item[""] === true)
getFlatFiles(item, output, curPath);
// file
else if (!curPath.endsWith("/"))
output[curPath.substr(1)] = mfsData[path];
}
}