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

Using Acorn plugins doesn't work as specified by README #13

Closed
jcowman2 opened this issue Dec 13, 2018 · 5 comments
Closed

Using Acorn plugins doesn't work as specified by README #13

jcowman2 opened this issue Dec 13, 2018 · 5 comments

Comments

@jcowman2
Copy link

Hey there, I'm having trouble using the acorn-dynamic-import plugin using the example in your README (related #11).

First off, the following line is no longer accurate:

import inject from 'acorn-dynamic-import/lib/inject'

It throws Error: Cannot find module 'acorn-dynamic-import/lib/inject', seemingly because the file was renamed to lib/walk. That can be fixed by updating the import to the following:

import inject from 'acorn-dynamic-import/lib/walk'

However, now I'm getting the following error:

(cleanup plugin) Error: Plugin 'dynamicImport' not found

After doing some digging, it looks like this is the result of a pre-existing issue with acorn: acornjs/acorn#746. The closure compiler Rollup plugin ran into the same problem (ampproject/rollup-plugin-closure-compiler#48), so maybe their solution could be useful.

Steps to Reproduce

  1. Clone the following repo (make sure branch is correct): https://github.com/regal/regal-bundler/tree/bug-repro/rollup-plugin-cleanup/1
  2. npm install
  3. npm run build

Full rollup.config.js:

import typescript from "rollup-plugin-typescript2";
import replace from "rollup-plugin-re";
import cleanup from "rollup-plugin-cleanup";
import json from "rollup-plugin-json";
import acorn from "acorn";
import inject from "acorn-dynamic-import/lib/walk";

import pkg from "./package.json";

const banner = `/**
 * Regal bundler source.
 *
 * Copyright (c) Joe Cowman
 * Licensed under MIT License (see https://github.com/regal/regal-bundler)
 */`

/** Suppress Rollup's circular import warnings for TypeScript files */
const suppressCircularImportWarnings = (message, defaultFunc) => {
    if (message.code === "CIRCULAR_DEPENDENCY") {
        return;
    }
    defaultFunc(message);
}

const tsPlugin = typescript({
    tsconfigOverride: {
        compilerOptions: { module: "esNext" }
    }
});

inject(acorn);

export default [
    {
        input: "./src/index.ts",
        output: [
            { file: pkg.main, format: "cjs", banner },
            { file: pkg.module, format: "esm", banner }
        ],
        external: Object.keys(pkg.dependencies),
        plugins: [
            replace({
                exclude: "node_modules/**",
                replaces: {
                    'import \* as cosmiconfig from "cosmiconfig";': 'import cosmiconfig from "cosmiconfig";',
                    'import \* as filenamify from "filenamify";': 'import filenamify from "filenamify";',
                }
            }),
            tsPlugin,
            json(),
            cleanup({
                extensions: [".js", ".ts"],
                comments: /^((?!(Joseph R Cowman)|tslint)[\s\S])*$/, // Removes file-header comments and tslint comments
                maxEmptyLines: 0,
                acornOptions: {
                    plugins: {
                        dynamicImport: true
                    }
                }
            })
        ],
        onwarn: suppressCircularImportWarnings
    }
]

Full error:

./src/index.ts → dist/regal-bundler.cjs.js, dist/regal-bundler.esm.js...
[!] (cleanup plugin) Error: Plugin 'dynamicImport' not found
src\index.ts
Error: Plugin 'dynamicImport' not found
    at Parser.loadPlugins (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:543:26)
    at new Parser (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:469:8)
    at Object.parse (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\node_modules\acorn\dist\acorn.js:5288:10)
    at blankComments (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:177:9)
    at resolve (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:315:14)
    at new Promise (<anonymous>)
    at cleanup (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:308:10)
    at Object.transform (C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup-plugin-cleanup\dist\rollup-plugin-cleanup.js:356:16)
    at C:\Users\Joe\Desktop\Projects\regal\regal-bundler\node_modules\rollup\dist\rollup.js:20962:25
    at <anonymous>
@aMarCruz
Copy link
Owner

@jcowman2 , I will check it this weekend,
thanks for your feedback.

@aMarCruz
Copy link
Owner

aMarCruz commented Dec 15, 2018

@jcowman2 , I'm working on this issue right now.
The current rollup 0.66.6 (branch master) is using acorn@^5.7.0 and embeding acorn-dynamic-import@^3.0.0 and acorn-import-meta@^0.2.1 so I will do it the same.

Anyway, my solution to support acorn plugins was not good 'cause it is at very low level, I think something like the rollup's "acornInjectPlugins" option is better, perhaps in automatic mode.

Also, it is possible to use Rollup's internal acorn instance to parse the code, which would solve compatibility problems (if it works for rollup, works for cleanup) ...but only in latest versions. Maybe it can be used with an option like useInternalAcorn.

In the meantime, please remove "acorn" from your devDependencies (it is already installed by rollup) and downgrade dynamic import to "^3.0.0" to use acorn-dynamic-import/lib/inject.

Note: fot this, it is better to edit the package.json, delete node_modules and the package-lock.json or yarn.lock and reinstall.

I will leave this issue open until a rn-cleanup version has been published that solves these problems, hope in a few days.

jcowman2 added a commit to regal/regal-bundler that referenced this issue Dec 16, 2018
@jcowman2
Copy link
Author

Thanks for this, @aMarCruz! Following those steps fixed the issue in my project.

I'm looking forward to seeing your permanent solution.

@aMarCruz
Copy link
Owner

aMarCruz commented Dec 27, 2018

@jcowman2 , rollup-plugin-cleanup v3.1.0 is in npm.

The solution was to write js-cleanup to replace acorn.
Details in the Changelog.

Please ping me for any issue with this.

jcowman2 added a commit to regal/regal-bundler that referenced this issue Jan 18, 2019
@jcowman2
Copy link
Author

Confirmed, everything is working in the new plugin version. Thanks again, @aMarCruz 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants