From ad8e97c052c14c286e47d40359fd959b6ca584e9 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 4 Jan 2019 13:10:59 +0100 Subject: [PATCH] Fix acorn imports for ESM target --- package-lock.json | 9 ++++++ package.json | 1 + rollup.config.js | 70 +++++++++++++++++++++++++---------------------- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index 431e0568df6..00465cb9e19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4477,6 +4477,15 @@ "@types/node": "*" } }, + "rollup-plugin-alias": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/rollup-plugin-alias/-/rollup-plugin-alias-1.5.1.tgz", + "integrity": "sha512-pQTYBRNfLedoVOO7AYHNegIavEIp4jKTga5jUi1r//KYgHKGWgG4qJXYhbcWKt2k1FwGlR5wCYoY+IFkme0t4A==", + "dev": true, + "requires": { + "slash": "^2.0.0" + } + }, "rollup-plugin-buble": { "version": "0.19.4", "resolved": "https://registry.npmjs.org/rollup-plugin-buble/-/rollup-plugin-buble-0.19.4.tgz", diff --git a/package.json b/package.json index 5463ebc8909..0ea01d58b84 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,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", diff --git a/rollup.config.js b/rollup.config.js index 20bc2bed2b4..6668d4aa5f4 100644 --- a/rollup.config.js +++ b/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'; @@ -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. ' + @@ -41,32 +40,40 @@ 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 */ @@ -74,11 +81,12 @@ export default command => { 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'], @@ -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: { @@ -123,6 +129,8 @@ export default command => { input: 'src/browser-entry.ts', onwarn, plugins: [ + alias(moduleAliases), + resolve({ browser: true }), json(), { load: id => { @@ -130,8 +138,6 @@ export default command => { 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' } })