From fb02328d0bb4ecad3ea26e5e339d3a7a24fe9dee Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Tue, 27 Oct 2020 15:51:14 +0800 Subject: [PATCH 1/3] fix(babel): strip query param in extension filter --- packages/babel/package.json | 1 + packages/babel/src/index.js | 21 ++++++++----- packages/babel/src/utils.js | 11 +++++++ packages/babel/test/as-input-plugin.js | 31 +++++++++++++++++++ .../test/fixtures/with-query-and-hash/main.js | 10 ++++++ .../with-query-and-hash/module#Hash.js | 1 + .../with-query-and-hash/moduleQuery.js | 1 + .../moduleQueryAnd#Hash.js | 1 + pnpm-lock.yaml | 22 +++++++++++++ 9 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 packages/babel/test/fixtures/with-query-and-hash/main.js create mode 100644 packages/babel/test/fixtures/with-query-and-hash/module#Hash.js create mode 100644 packages/babel/test/fixtures/with-query-and-hash/moduleQuery.js create mode 100644 packages/babel/test/fixtures/with-query-and-hash/moduleQueryAnd#Hash.js diff --git a/packages/babel/package.json b/packages/babel/package.json index c86cb1152..3f0b573ee 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -62,6 +62,7 @@ "@babel/plugin-transform-runtime": "^7.10.5", "@babel/preset-env": "^7.10.4", "@rollup/plugin-json": "^4.1.0", + "@rollup/plugin-node-resolve": "^10.0.0", "@types/babel__core": "^7.1.9", "rollup": "^2.23.0", "source-map": "^0.7.3" diff --git a/packages/babel/src/index.js b/packages/babel/src/index.js index 012c12c62..25144027e 100644 --- a/packages/babel/src/index.js +++ b/packages/babel/src/index.js @@ -5,7 +5,7 @@ import { BUNDLED, HELPERS } from './constants'; import bundledHelpersPlugin from './bundledHelpersPlugin'; import preflightCheck from './preflightCheck'; import transformCode from './transformCode'; -import { addBabelPlugin, escapeRegExpCharacters, warnOnce } from './utils'; +import { addBabelPlugin, escapeRegExpCharacters, warnOnce, stripQuery } from './utils'; const unpackOptions = ({ extensions = babel.DEFAULT_EXTENSIONS, @@ -35,7 +35,7 @@ const warnAboutDeprecatedHelpersOption = ({ deprecatedOption, suggestion }) => { `\`${deprecatedOption}\` has been removed in favor a \`babelHelpers\` option. Try changing your configuration to \`${suggestion}\`. ` + `Refer to the documentation to learn more: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers` ); -} +}; const unpackInputPluginOptions = ({ skipPreflightCheck = false, ...rest }, rollupVersion) => { if ('runtimeHelpers' in rest) { @@ -110,13 +110,18 @@ function createBabelInputPluginFactory(customCallback = returnObject) { overrides ); - let babelHelpers, babelOptions, filter, skipPreflightCheck; + let babelHelpers; + let babelOptions; + let filter; + let skipPreflightCheck; return { name: 'babel', options() { - //todo: remove options hook and hoist declarations when version checks are removed - let exclude, include, extensions; + // todo: remove options hook and hoist declarations when version checks are removed + let exclude; + let include; + let extensions; ({ exclude, @@ -127,9 +132,11 @@ function createBabelInputPluginFactory(customCallback = returnObject) { ...babelOptions } = unpackInputPluginOptions(pluginOptionsWithOverrides, this.meta.rollupVersion)); - const extensionRegExp = new RegExp(`(${extensions.map(escapeRegExpCharacters).join('|')})$`); + const extensionRegExp = new RegExp( + `(${extensions.map(escapeRegExpCharacters).join('|')})$` + ); const includeExcludeFilter = createFilter(include, exclude); - filter = (id) => extensionRegExp.test(id) && includeExcludeFilter(id); + filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && includeExcludeFilter(id); return null; }, diff --git a/packages/babel/src/utils.js b/packages/babel/src/utils.js index f05c8b221..5a82d2fa0 100644 --- a/packages/babel/src/utils.js +++ b/packages/babel/src/utils.js @@ -14,3 +14,14 @@ export function warnOnce(ctx, msg) { const regExpCharactersRegExp = /[\\^$.*+?()[\]{}|]/g; export const escapeRegExpCharacters = (str) => str.replace(regExpCharactersRegExp, '\\$&'); + +export function stripQuery(id) { + // strip query params from import + const [bareId, query] = id.split('?'); + const suffix = `${query ? `?${query}` : ''}`; + return { + bareId, + query, + suffix + }; +} diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.js index 0815f4d71..ee65fbe54 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.js @@ -1,9 +1,11 @@ import * as nodePath from 'path'; +import * as fs from 'fs'; import test from 'ava'; import { rollup } from 'rollup'; import { SourceMapConsumer } from 'source-map'; import jsonPlugin from '@rollup/plugin-json'; +import nodeResolvePlugin from '@rollup/plugin-node-resolve'; import { getCode } from '../../../util/test'; @@ -287,6 +289,35 @@ test('transpiles only files with whitelisted extensions', async (t) => { t.false(code.includes('class Other '), 'should transpile .other'); }); +test('transpiles files when path contains query and hash', async (t) => { + const code = await generate( + 'fixtures/with-query-and-hash/main.js', + {}, + {}, + { + plugins: [ + babelPlugin({ babelHelpers: 'bundled' }), + // node-resolve plugin know how to resolve relative request with query + nodeResolvePlugin(), + { + load(id) { + // rollup don't know how to load module with query + // we could teach rollup to discard query while loading module + const [bareId] = id.split(`?`); + return fs.readFileSync(bareId, 'utf-8'); + } + } + ] + } + ); + t.false(code.includes('class WithQuery '), 'should transpile when path contains query'); + t.false(code.includes('class WithHash '), 'should transpile when path contains hash'); + t.false( + code.includes('class WithQueryAndHash '), + 'should transpile when path contains query and hash' + ); +}); + test('throws when trying to add babel helper unavailable in used @babel/core version', async (t) => { await t.throwsAsync( () => diff --git a/packages/babel/test/fixtures/with-query-and-hash/main.js b/packages/babel/test/fixtures/with-query-and-hash/main.js new file mode 100644 index 000000000..3158b3c38 --- /dev/null +++ b/packages/babel/test/fixtures/with-query-and-hash/main.js @@ -0,0 +1,10 @@ +/* eslint-disable import/extensions */ +/* eslint-disable import/no-unresolved */ + +// ? should be treated as special symbol for query params +export { default as WithQuery } from './moduleQuery?q=asd'; +// # should be treated as normal filename character +export { default as WithHash } from './module#Hash'; + +// So, this is an import with path "./moduleQueryAnd#Hash" and query "?q=asd#hash" +export { default as WithQueryAndHash } from './moduleQueryAnd#Hash?q=asd#hash'; diff --git a/packages/babel/test/fixtures/with-query-and-hash/module#Hash.js b/packages/babel/test/fixtures/with-query-and-hash/module#Hash.js new file mode 100644 index 000000000..8965a3c14 --- /dev/null +++ b/packages/babel/test/fixtures/with-query-and-hash/module#Hash.js @@ -0,0 +1 @@ +export default class WithHash {} diff --git a/packages/babel/test/fixtures/with-query-and-hash/moduleQuery.js b/packages/babel/test/fixtures/with-query-and-hash/moduleQuery.js new file mode 100644 index 000000000..eb2ddae8c --- /dev/null +++ b/packages/babel/test/fixtures/with-query-and-hash/moduleQuery.js @@ -0,0 +1 @@ +export default class WithQuery {} diff --git a/packages/babel/test/fixtures/with-query-and-hash/moduleQueryAnd#Hash.js b/packages/babel/test/fixtures/with-query-and-hash/moduleQueryAnd#Hash.js new file mode 100644 index 000000000..591ec31de --- /dev/null +++ b/packages/babel/test/fixtures/with-query-and-hash/moduleQueryAnd#Hash.js @@ -0,0 +1 @@ +export default class WithQueryAndHash {} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a9d401be..b77034902 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,6 +90,7 @@ importers: '@babel/plugin-transform-runtime': 7.12.1_@babel+core@7.12.3 '@babel/preset-env': 7.12.1_@babel+core@7.12.3 '@rollup/plugin-json': 4.1.0_rollup@2.32.1 + '@rollup/plugin-node-resolve': 10.0.0_rollup@2.32.1 '@types/babel__core': 7.1.10 rollup: 2.32.1 source-map: 0.7.3 @@ -102,6 +103,7 @@ importers: '@babel/plugin-transform-runtime': ^7.10.5 '@babel/preset-env': ^7.10.4 '@rollup/plugin-json': ^4.1.0 + '@rollup/plugin-node-resolve': ^10.0.0 '@rollup/pluginutils': ^3.1.0 '@types/babel__core': ^7.1.9 rollup: ^2.23.0 @@ -1668,6 +1670,22 @@ packages: rollup: ^1.20.0 || ^2.0.0 resolution: integrity: sha512-yfLbTdNS6amI/2OpmbiBoW12vngr5NW2jCJVZSBEz+H5KfUJZ2M7sDjk0U6GOOdCWFVScShte29o9NezJ53TPw== + /@rollup/plugin-node-resolve/10.0.0_rollup@2.32.1: + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.32.1 + '@types/resolve': 1.17.1 + builtin-modules: 3.1.0 + deepmerge: 4.2.2 + is-module: 1.0.0 + resolve: 1.18.1 + rollup: 2.32.1 + dev: true + engines: + node: '>= 10.0.0' + peerDependencies: + rollup: ^1.20.0||^2.0.0 + resolution: + integrity: sha512-sNijGta8fqzwA1VwUEtTvWCx2E7qC70NMsDh4ZG13byAXYigBNZMxALhKUSycBks5gupJdq0lFrKumFrRZ8H3A== /@rollup/plugin-node-resolve/8.4.0_rollup@2.32.1: dependencies: '@rollup/pluginutils': 3.1.0_rollup@2.32.1 @@ -3965,6 +3983,7 @@ packages: resolution: integrity: sha1-FQStJSMVjKpA20onh8sBQRmU6k8= /fsevents/2.1.3: + dev: true engines: node: ^8.16.0 || ^10.6.0 || >=11.0.0 optional: true @@ -4141,6 +4160,7 @@ packages: /graphql/14.7.0: dependencies: iterall: 1.3.0 + dev: true engines: node: '>= 6.x' resolution: @@ -4746,6 +4766,7 @@ packages: resolution: integrity: sha512-9tZvz7AiR3PEDNGiV9vIouQ/EAcqMXFmkcA1CDFTwOB98OZVDL0PH9glHotf5Ugp6GCOTypfzGWI/OqjWNCRUw== /iterall/1.3.0: + dev: true resolution: integrity: sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg== /js-string-escape/1.0.1: @@ -6600,6 +6621,7 @@ packages: resolution: integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== /rollup/2.32.1: + dev: true engines: node: '>=10.0.0' hasBin: true From 9f79b729865d8340048dd559eb889625788f48a5 Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Tue, 27 Oct 2020 15:51:31 +0800 Subject: [PATCH 2/3] fix(babel): fix test --- packages/babel/test/as-input-plugin.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.js index ee65fbe54..0bdd98d0e 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.js @@ -159,12 +159,14 @@ test('allows transform-runtime to be used instead of bundled helpers', async (t) code, `'use strict'; -function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } +var _classCallCheck = require('@babel/runtime/helpers/classCallCheck'); -var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck')); +function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } + +var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck); var Foo = function Foo() { - _classCallCheck(this, Foo); + _classCallCheck__default['default'](this, Foo); }; module.exports = Foo; From 96594356a02b3734cc0df884ee0e98c916d780de Mon Sep 17 00:00:00 2001 From: csr632 <632882184@qq.com> Date: Tue, 27 Oct 2020 16:01:44 +0800 Subject: [PATCH 3/3] test(babel): make test more robust --- packages/babel/test/as-input-plugin.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.js index 0bdd98d0e..14e44e118 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.js @@ -312,10 +312,10 @@ test('transpiles files when path contains query and hash', async (t) => { ] } ); - t.false(code.includes('class WithQuery '), 'should transpile when path contains query'); - t.false(code.includes('class WithHash '), 'should transpile when path contains hash'); - t.false( - code.includes('class WithQueryAndHash '), + t.true(code.includes('function WithQuery()'), 'should transpile when path contains query'); + t.true(code.includes('function WithHash()'), 'should transpile when path contains hash'); + t.true( + code.includes('function WithQueryAndHash()'), 'should transpile when path contains query and hash' ); });