-
-
Save eirikb/8a19f8ac82465b01bae8219c962cbede to your computer and use it in GitHub Desktop.
Global vars in parcel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async transform() { | |
if (this.options.target === 'browser') { | |
if (this.dependencies.has('fs') && FS_RE.test(this.contents)) { | |
// Check if we should ignore fs calls | |
// See https://github.com/defunctzombie/node-browser-resolve#skip | |
let pkg = await this.getPackage(); | |
let ignore = pkg && pkg.browser && pkg.browser.fs === false; | |
if (!ignore) { | |
await this.parseIfNeeded(); | |
this.traverse(fsVisitor); | |
} | |
} | |
if (GLOBAL_RE.test(this.contents)) { | |
await this.parseIfNeeded(); | |
walk.ancestor(this.ast, insertGlobals, this); | |
} | |
await babel7(this, { | |
config: { | |
plugins: [babel => ({ | |
visitor: { | |
Program(path) { | |
const declaredIds = []; | |
for (const p of path.get('body')) { | |
if (p.isVariableDeclaration()) { | |
for (const decl of p.get('declarations')) { | |
declaredIds.push(...Object.keys(decl.getBindingIdentifiers())); | |
} | |
} | |
} | |
path.pushContainer('body', babel.template.statement.ast`module.exports = [${declaredIds.join(',')}]`); | |
} | |
} | |
})] | |
} | |
}) | |
} | |
if (this.options.scopeHoist) { | |
await this.parseIfNeeded(); | |
await this.getPackage(); | |
this.traverse(hoist); | |
this.isAstDirty = true; | |
} else { | |
if (this.isES6Module) { | |
await babel7(this, { | |
internal: true, | |
config: { | |
plugins: [require('@babel/plugin-transform-modules-commonjs')] | |
} | |
}); | |
} | |
} | |
if (this.options.minify) { | |
await terser(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment