Skip to content

Commit

Permalink
Fix acorn imports for ESM target
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 4, 2019
1 parent fbb5f9e commit 61cd049
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -95,6 +95,7 @@
"remap-istanbul": "^0.12.0",
"require-relative": "^0.8.7",
"rollup": "^0.67.4",
"rollup-plugin-alias": "^1.5.1",
"rollup-plugin-buble": "^0.19.4",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-json": "^3.1.0",
Expand Down
70 changes: 38 additions & 32 deletions rollup.config.js
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import alias from 'rollup-plugin-alias';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import resolve from 'rollup-plugin-node-resolve';
Expand Down Expand Up @@ -31,8 +32,6 @@ const banner = `/*
*/`;

const onwarn = warning => {
if (warning.pluginCode === 'ONWRITE_HOOK_DEPRECATED')
return;
// eslint-disable-next-line no-console
console.error(
'Building Rollup produced warnings that need to be resolved. ' +
Expand All @@ -41,44 +40,53 @@ const onwarn = warning => {
throw new Error(warning.message);
};

const src = path.resolve('src');
const bin = path.resolve('bin');
const expectedAcornImport = "import acorn__default, { tokTypes, Parser } from 'acorn';";
const newAcornImport =
"import * as acorn__default from 'acorn';\nimport { tokTypes, Parser } from 'acorn';";

function resolveTypescript() {
// by default, rollup-plugin-commonjs will translate require statements as default imports
// which can cause issues for secondary tools that use the ESM version of acorn
function fixAcornEsmImport() {
return {
name: 'resolve-typescript',
resolveId(importee, importer) {
// work around typescript's inability to resolve other extensions
if (~importee.indexOf('help.md')) return path.resolve('bin/src/help.md');
if (~importee.indexOf('package.json')) return path.resolve('package.json');

// bit of a hack — TypeScript only really works if it can resolve imports,
// but they misguidedly chose to reject imports with file extensions. This
// means we need to resolve them here
if (
importer &&
(importer.startsWith(src) || importer.startsWith(bin)) &&
importee[0] === '.' &&
path.extname(importee) === ''
) {
return path.resolve(path.dirname(importer), `${importee}.ts`);
name: 'fix-acorn-esm-import',
renderChunk(code, chunk, options) {
if (/esm?/.test(options.format)) {
let found = false;
const fixedCode = code.replace(expectedAcornImport, () => {
found = true;
return newAcornImport;
});
if (!found) {
this.error(
'Could not find expected acorn import, please deactive this plugin and examine generated code.'
);
}
return fixedCode;
}
}
};
}

const moduleAliases = {
resolve: ['.js', '.json', '.md'],
'acorn-dynamic-import': path.resolve('node_modules/acorn-dynamic-import/src/index.js'),
'help.md': path.resolve('bin/src/help.md'),
'package.json': path.resolve('package.json')
};

export default command => {
const nodeBuilds = [
/* Rollup core node builds */
{
input: 'src/node-entry.ts',
onwarn,
plugins: [
json(),
resolveTypescript(),
alias(moduleAliases),
resolve(),
json(),
typescript(),
commonjs()
commonjs({ include: 'node_modules/**' }),
fixAcornEsmImport()
],
// acorn needs to be external as some plugins rely on a shared acorn instance
external: ['fs', 'path', 'events', 'module', 'util', 'crypto', 'acorn'],
Expand All @@ -92,14 +100,12 @@ export default command => {
input: 'bin/src/index.ts',
onwarn,
plugins: [
string({ include: '**/*.md' }),
json(),
resolveTypescript(),
alias(moduleAliases),
resolve(),
json(),
string({ include: '**/*.md' }),
typescript(),
commonjs({
include: 'node_modules/**'
})
commonjs({ include: 'node_modules/**' })
],
external: ['fs', 'path', 'module', 'events', 'rollup', 'assert', 'os', 'util'],
output: {
Expand All @@ -123,15 +129,15 @@ export default command => {
input: 'src/browser-entry.ts',
onwarn,
plugins: [
alias(moduleAliases),
resolve({ browser: true }),
json(),
{
load: id => {
if (~id.indexOf('fs.ts')) return fs.readFileSync('browser/fs.ts', 'utf-8');
if (~id.indexOf('path.ts')) return fs.readFileSync('browser/path.ts', 'utf-8');
}
},
resolveTypescript(),
resolve({ browser: true }),
typescript(),
commonjs(),
terser({ module: true, output: { comments: 'some' } })
Expand Down

0 comments on commit 61cd049

Please sign in to comment.