Skip to content

Commit c0ccc39

Browse files
committed
chore(build): Add guard against native build containing reference to browser-api
Signed-off-by: Marcel Klehr <[email protected]>
1 parent acce1d3 commit c0ccc39

17 files changed

+370
-293
lines changed

gulpfile.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ const js = async function() {
9595
let statsJson
9696
await new Promise((resolve) =>
9797
webpack(config, (err, stats) => {
98-
console.log(
99-
stats.toString({
100-
/* stats options */
101-
})
102-
)
98+
if (stats) {
99+
console.log(
100+
stats.toString({
101+
/* stats options */
102+
})
103+
)
104+
}
103105

104106
if (err) {
105107
console.log('Webpack', err)
@@ -110,38 +112,66 @@ const js = async function() {
110112
})
111113
)
112114
await new Promise(resolve => setTimeout(resolve, 5000))
113-
html(statsJson)
115+
statsJson.children.forEach(statsJson => html(statsJson))
116+
statsJson.children.forEach(statsJson => webpackCheck(statsJson))
117+
}
118+
119+
const webpackCheck = function(statsJson) {
120+
const browserApiModule = statsJson.modules.find(module => module.name.includes('browser-api'))
121+
if (!browserApiModule || !statsJson.entrypoints.native) {
122+
return
123+
}
124+
const nativeChunks = statsJson.entrypoints.native.chunks
125+
console.log({nativeChunks, browserApiChunks: browserApiModule.chunks})
126+
if (browserApiModule.chunks.some(chunk => nativeChunks.includes(chunk))) {
127+
throw new Error('Problem: Native chunk contains reference to browser-api')
128+
}
114129
}
115130

116131
const html = function(statsJson) {
117132
fs.mkdirSync('dist/html/', { recursive: true })
118-
let html, scripts, bgScript, addition
133+
let html, scripts, bgScript, addition, assets
119134
;['index.html', 'options.html', 'background.html', 'test.html', 'offscreen.html'].forEach(htmlFile => {
120135
switch (htmlFile) {
121136
case 'index.html':
137+
if (!statsJson.entrypoints?.native) {
138+
break
139+
}
122140
html = fs.readFileSync('html/' + htmlFile, 'utf8')
123-
scripts = statsJson.entrypoints.native.assets.map(asset => `<script src="js/${asset.name}"></script>`).join('\n')
141+
assets = statsJson.entrypoints?.native?.assets
142+
scripts = assets.map(asset => `<script src="js/${asset.name}"></script>`).join('\n')
124143
html = html.replace('{{ scripts }}', scripts)
125144
fs.writeFileSync('dist/' + htmlFile, html)
126145
break
127146
case 'options.html':
147+
if (!statsJson.entrypoints?.options) {
148+
break
149+
}
128150
html = fs.readFileSync('html/' + htmlFile, 'utf8')
129151
scripts = statsJson.entrypoints.options.assets.map(asset => `<script src="../js/${asset.name}"></script>`).join('\n')
130152
html = html.replace('{{ scripts }}', scripts)
131-
console.log(statsJson.entrypoints.options.assets)
132153
fs.writeFileSync('dist/html/' + htmlFile, html)
133154
break
134155
case 'test.html':
156+
if (!statsJson.entrypoints?.test) {
157+
break
158+
}
135159
html = fs.readFileSync('html/' + htmlFile, 'utf8')
136160
scripts = statsJson.entrypoints.test.assets.map(asset => `<script src="../js/${asset.name}"></script>`).join('\n')
137161
html = html.replace('{{ scripts }}', scripts)
138162
fs.writeFileSync('dist/html/' + htmlFile, html)
139163
break
140164
case 'offscreen.html':
165+
if (!statsJson.entrypoints?.offscreen) {
166+
break
167+
}
141168
html = fs.readFileSync('html/' + htmlFile, 'utf8')
142169
fs.writeFileSync('dist/html/' + htmlFile, html)
143170
break
144171
case 'background.html':
172+
if (!statsJson.entrypoints?.['background-script']) {
173+
break
174+
}
145175
html = fs.readFileSync('html/' + htmlFile, 'utf8')
146176
scripts = statsJson.entrypoints['background-script'].assets.map(asset => `<script src="../js/${asset.name}"></script>`).join('\n')
147177
html = html.replace('{{ scripts }}', scripts)
@@ -273,6 +303,9 @@ const watch = function() {
273303
chunks: false,
274304
colors: true
275305
}))
306+
307+
const statsJson = stats.toJson()
308+
webpackCheck(statsJson)
276309
})
277310
}
278311

0 commit comments

Comments
 (0)