Skip to content

Instantly share code, notes, and snippets.

@eirikb
Created November 16, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirikb/8a19f8ac82465b01bae8219c962cbede to your computer and use it in GitHub Desktop.
Save eirikb/8a19f8ac82465b01bae8219c962cbede to your computer and use it in GitHub Desktop.
Global vars in parcel
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