-
Notifications
You must be signed in to change notification settings - Fork 25
/
webpack.config.js
148 lines (131 loc) · 3.87 KB
/
webpack.config.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
const path = require("path");
const webpack = require("webpack");
const { taglib, configure } = require("@marko/compiler");
const { configBuilder } = require("@marko/build");
const nodeExternals = require("webpack-node-externals");
const { load } = require("./src/utils/language-registry");
const production = process.env.NODE_ENV === "production";
const { getServerConfig, getBrowserConfigs } = configBuilder({
entry: path.join(__dirname, "src/pages"),
production
});
const prPreview = process.env.PR_PREVIEW;
configure({
translator: require("@marko/translator-default"),
});
// globally register <code-block> so it can be used by the markdown files
taglib.register(
"code-block",
{
"<code-block>": {
transformer: require.resolve("./src/utils/code-block-transformer"),
"parse-options": {
text: true,
preserveWhitespace: true
},
}
}
);
const encodedLanguageIds = new Map();
const loadingRegistry = load({
getEncodedLanguageId(scopeName) {
let id = encodedLanguageIds.get(scopeName);
if (id) {
return id;
}
id = encodedLanguageIds.size + 1;
encodedLanguageIds.set(scopeName, id);
return id;
}
});
module.exports = [
...getBrowserConfigs(config => {
shared(config);
config.node = {
...config.node,
__dirname: true,
__filename: true
}
config.module.rules.push(
{
test: /\.wasm$/,
loader: "file-loader",
type: "javascript/auto"
},
{
test: /\.worker\.js$/,
loader: "worker-loader"
}
);
config.resolve = {
...config.resolve,
fallback: {
vm: false,
os: false,
http: false,
https: false,
},
alias: {
"@marko/compiler": path.join(__dirname, "browser-shims/compiler"),
util: require.resolve("util/"),
buffer: require.resolve("buffer"),
assert: require.resolve("assert/"),
path: require.resolve("path-browserify"),
stream: require.resolve("stream-browserify"),
crypto: require.resolve("crypto-browserify"),
fs: path.join(__dirname, "browser-shims/fs.js"),
module: path.join(__dirname, "browser-shims/module.js"),
process: path.join(__dirname, "browser-shims/process.js"),
}
};
config.plugins.push(new webpack.ProvidePlugin({
Buffer: [require.resolve("buffer"), "Buffer"],
process: path.join(__dirname, "browser-shims/process.js")
}));
config.plugins.push(new webpack.DefinePlugin({
"process.env.NODE_DEBUG": undefined,
"process.env.MARKO_DEBUG": undefined
}));
config.optimization.runtimeChunk = "single";
if (production) {
// Needed for the tryonline page.
config.optimization.moduleIds = "named";
}
return config;
}),
getServerConfig(config => {
shared(config);
config.externals = [
// Exclude node_modules, but ensure non js files are bundled.
// Eg: `.marko`, `.css`, etc.
nodeExternals({
allowlist: [/\.(?!(?:js|json)$)[^.]+$/]
})
];
return config;
})
];
function shared(config) {
config.plugins.unshift(compiler => {
// Wait for language registry to be loaded before running bundling.
// The syntax highlighter must be loaded async, but can only be
// used synchronously during the the compilation.
compiler.hooks.beforeCompile.tapPromise(
"LoadLanguageRegistry",
() => loadingRegistry
);
});
const fileLoader = config.module.rules.find(({ test }) => typeof test === "function");
const originalTest = fileLoader.test;
fileLoader.test = file => !/\.(md)$/.test(file) && originalTest(file);
if (prPreview) {
config.output.publicPath = `/website/pr-${prPreview}/assets/`;
}
config.module.rules.push({
test: /\.md$/,
use: [
"@marko/webpack/loader",
require.resolve("./src/utils/markodown-loader")
]
});
}