Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-pass transform takes no effect. #10629

Open
shrinktofit opened this issue Nov 2, 2019 · 1 comment

Comments

@shrinktofit
Copy link

@shrinktofit shrinktofit commented Nov 2, 2019

Bug Report

I'm transforming codes from CommonJS format to UMD format using babel. I break it into 2 passes:

  • Pass 1 transform CommonJS format to ES6 module format using plugin babel-plugin-transform-commonjs;
  • Pass 2 transform ES6 module format into UMD format using @babel/preset-env with options modules: "umd".

Current Behavior
The Pass2 did not transform ES6 module syntax. It keeps import/export stuffs in result.

Input Code
The following is my transform code, the test source codes are in in test repo (https://github.com/shrinktofit/cjs-umd-demo).

const babel = require('@babel/core');
const fs = require('fs');
(async () => {
	// Pass 1: transform CommonJS to ESM
    const pass1Result = await babel.transformFileAsync('source.js', {
        sourceMaps: 'inline',
        ast: true,
        code: true, // We do not generate code in pass 1
        plugins: [
            "babel-plugin-transform-commonjs",
        ],
    });
    if (!pass1Result) {
        throw new Error(`[[Transform pass 1]] Failed to transform ${fileName}`);
    }
    
    // Test only
    fs.writeFileSync("out-pass0.js", pass1Result.code);

    // Pass 2: transform es6 to es5
    const pass2Result = await babel.transformFromAstAsync(pass1Result.ast, undefined, {
        sourceMaps: 'inline',
        presets: [
            ["@babel/preset-env", {
                modules: 'umd',
            }],
        ],
    });
    if (!pass2Result) {
        throw new Error(`[[Transform pass 2]] Failed to transform ${fileName}`);
    }
    
    fs.writeFileSync("out.js", pass2Result.code);
})();

Expected behavior/code
Yeilds UMD module format code.

Babel Configuration (.babelrc, package.json, cli command)

// API

Environment

  • Babel version(s): 7.6.4
  • Node/npm version: Node 10.16.3/NPM 6.11.2
  • OS: Windows x64
  • Monorepo: [e.g. yes/no/Lerna]
  • How you are using Babel: API

Possible Solution

Additional context/Screenshots
It's strange. In my testcases only this file is incorrect. Other file seems works well.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Nov 2, 2019

Hey @shrinktofit! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.