@parcel/transformer-babel 2.16.4 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,129 +1,69 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _babelErrorUtils = require("./babelErrorUtils");
8
- function _plugin() {
9
- const data = require("@parcel/plugin");
10
- _plugin = function () {
11
- return data;
12
- };
13
- return data;
14
- }
15
- function _utils() {
16
- const data = require("@parcel/utils");
17
- _utils = function () {
18
- return data;
19
- };
20
- return data;
21
- }
22
- function _sourceMap() {
23
- const data = _interopRequireDefault(require("@parcel/source-map"));
24
- _sourceMap = function () {
25
- return data;
26
- };
27
- return data;
28
- }
29
- function _semver() {
30
- const data = _interopRequireDefault(require("semver"));
31
- _semver = function () {
32
- return data;
33
- };
34
- return data;
35
- }
36
- var _babel = _interopRequireDefault(require("./babel7"));
37
- var _config = require("./config");
38
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
39
- var _default = exports.default = new (_plugin().Transformer)({
40
- loadConfig({
41
- config,
42
- options,
43
- logger
44
- }) {
45
- return (0, _config.load)(config, options, logger);
46
- },
47
- canReuseAST({
48
- ast
49
- }) {
50
- return ast.type === 'babel' && _semver().default.satisfies(ast.version, '^7.0.0');
51
- },
52
- async transform({
53
- asset,
54
- config,
55
- logger,
56
- options,
57
- tracer
58
- }) {
59
- try {
60
- if (config !== null && config !== void 0 && config.config) {
61
- if (asset.meta.babelPlugins != null && Array.isArray(asset.meta.babelPlugins)) {
62
- await (0, _babel.default)({
63
- asset,
64
- options,
65
- logger,
66
- babelOptions: config,
67
- additionalPlugins: asset.meta.babelPlugins,
68
- tracer
69
- });
1
+ import { Transformer } from '@parcel/plugin';
2
+ import * as babel from '@babel/core';
3
+ export default new Transformer({
4
+ async transform ({ asset, config, options }) {
5
+ let babelOptions = {
6
+ filename: asset.filePath,
7
+ cwd: options.projectRoot,
8
+ sourceMaps: !!asset.target.sourceMap,
9
+ // @types/babel__core requires fields that are optional in the source map spec.
10
+ inputSourceMap: asset.getMap() ?? undefined,
11
+ // Parcel handles modules itself, so presets should leave them alone.
12
+ caller: {
13
+ name: 'parcel',
14
+ supportsStaticESM: true,
15
+ supportsDynamicImport: true,
16
+ supportsTopLevelAwait: true,
17
+ supportsExportNamespaceFrom: true
18
+ }
19
+ };
20
+ if (config != null) {
21
+ // Config from .parcelrc is used exactly as given.
22
+ babelOptions = {
23
+ ...config,
24
+ ...babelOptions,
25
+ babelrc: false,
26
+ configFile: false
27
+ };
70
28
  } else {
71
- await (0, _babel.default)({
72
- asset,
73
- options,
74
- logger,
75
- babelOptions: config,
76
- tracer
77
- });
29
+ if (!asset.isSource) {
30
+ return;
31
+ }
32
+ // Let Babel find babel.config.* and .babelrc files itself.
33
+ let partialConfig = await babel.loadPartialConfigAsync(babelOptions);
34
+ if (partialConfig == null || !partialConfig.hasFilesystemConfig()) {
35
+ return;
36
+ }
37
+ babelOptions = partialConfig.options;
38
+ }
39
+ let code = asset.getCode();
40
+ let result;
41
+ try {
42
+ result = await babel.transformAsync(code, babelOptions);
43
+ } catch (err) {
44
+ throw toDiagnosticError(err, asset.filePath);
45
+ }
46
+ // Babel returns null for files matched by an "ignore" option.
47
+ if (result?.code == null) {
48
+ return;
78
49
  }
79
- }
80
- return [asset];
81
- } catch (e) {
82
- throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
50
+ asset.setCode(result.code, result.map ?? null);
83
51
  }
84
- },
85
- async generate({
86
- asset,
87
- ast,
88
- options
89
- }) {
90
- let originalSourceMap = await asset.getMap();
91
- let sourceFileName = (0, _utils().relativeUrl)(options.projectRoot, asset.filePath);
92
- const babelCorePath = await options.packageManager.resolve('@babel/core', asset.filePath, {
93
- range: '^7.12.0',
94
- saveDev: true,
95
- shouldAutoInstall: options.shouldAutoInstall
96
- });
97
- const {
98
- default: generate
99
- } = await options.packageManager.require('@babel/generator', babelCorePath.resolved);
100
- let {
101
- code,
102
- rawMappings
103
- } = generate(ast.program, {
104
- sourceFileName,
105
- sourceMaps: !!asset.env.sourceMap,
106
- comments: true,
107
- importAttributesKeyword: 'with'
108
- });
109
- let map = new (_sourceMap().default)(options.projectRoot);
110
- if (rawMappings) {
111
- map.addIndexedMappings(rawMappings);
52
+ });
53
+ // Reshape Babel errors so they are reported with a code frame at the error location.
54
+ function toDiagnosticError(err, filePath) {
55
+ if (err?.loc == null) {
56
+ return err;
112
57
  }
113
- if (originalSourceMap) {
114
- // The babel AST already contains the correct mappings, but not the source contents.
115
- // We need to copy over the source contents from the original map.
116
- let sourcesContent = originalSourceMap.getSourcesContentMap();
117
- for (let filePath in sourcesContent) {
118
- let content = sourcesContent[filePath];
119
- if (content != null) {
120
- map.setSourceContent(filePath, content);
121
- }
122
- }
58
+ let message = String(err.message);
59
+ if (message.startsWith(filePath + ': ')) {
60
+ message = message.slice(filePath.length + 2);
123
61
  }
124
- return {
125
- content: code,
126
- map
127
- };
128
- }
129
- });
62
+ let error = new Error(message.split('\n')[0].trim());
63
+ Object.assign(error, {
64
+ filename: filePath,
65
+ line: err.loc.line,
66
+ column: err.loc.column + 1
67
+ });
68
+ return error;
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-babel",
3
- "version": "2.16.4",
3
+ "version": "3.0.0-alpha.2",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -13,27 +13,21 @@
13
13
  "type": "git",
14
14
  "url": "https://github.com/parcel-bundler/parcel.git"
15
15
  },
16
+ "type": "module",
16
17
  "main": "lib/BabelTransformer.js",
17
- "source": "src/BabelTransformer.js",
18
+ "files": [
19
+ "lib"
20
+ ],
21
+ "source": "src/BabelTransformer.ts",
18
22
  "engines": {
19
23
  "node": ">= 16.0.0",
20
- "parcel": "^2.16.4"
24
+ "parcel": "^3.0.0-0"
21
25
  },
22
26
  "dependencies": {
23
- "@parcel/diagnostic": "2.16.4",
24
- "@parcel/plugin": "2.16.4",
25
- "@parcel/source-map": "^2.1.1",
26
- "@parcel/utils": "2.16.4",
27
- "browserslist": "^4.24.5",
28
- "json5": "^2.2.3",
29
- "nullthrows": "^1.1.1",
30
- "semver": "^7.7.1"
27
+ "@babel/core": "^7.26.0",
28
+ "@parcel/plugin": "^3.0.0-alpha.2"
31
29
  },
32
30
  "devDependencies": {
33
- "@babel/core": "^7.22.11",
34
- "@babel/preset-env": "^7.22.14",
35
- "@babel/types": "^7.22.11",
36
- "@parcel/types": "2.16.4"
37
- },
38
- "gitHead": "59484858a1a0bcbb71f74088956bb437a2db6505"
39
- }
31
+ "@types/babel__core": "^7.20.5"
32
+ }
33
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-present Devon Govett
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/lib/babel7.js DELETED
@@ -1,132 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = babel7;
7
- function _assert() {
8
- const data = _interopRequireDefault(require("assert"));
9
- _assert = function () {
10
- return data;
11
- };
12
- return data;
13
- }
14
- function _path() {
15
- const data = _interopRequireDefault(require("path"));
16
- _path = function () {
17
- return data;
18
- };
19
- return data;
20
- }
21
- function _diagnostic() {
22
- const data = require("@parcel/diagnostic");
23
- _diagnostic = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _utils() {
29
- const data = require("@parcel/utils");
30
- _utils = function () {
31
- return data;
32
- };
33
- return data;
34
- }
35
- var _remapAstLocations = require("./remapAstLocations");
36
- var _package = _interopRequireDefault(require("../package.json"));
37
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
38
- const transformerVersion = _package.default.version;
39
- (0, _assert().default)(typeof transformerVersion === 'string');
40
- async function babel7(opts) {
41
- var _babelOptions$config$;
42
- let {
43
- asset,
44
- options,
45
- babelOptions,
46
- additionalPlugins = [],
47
- tracer
48
- } = opts;
49
- const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
50
- range: '^7.12.0',
51
- saveDev: true,
52
- shouldAutoInstall: options.shouldAutoInstall
53
- });
54
- let config = {
55
- ...babelOptions.config,
56
- plugins: additionalPlugins.concat(babelOptions.config.plugins),
57
- code: false,
58
- ast: true,
59
- filename: asset.filePath,
60
- babelrc: false,
61
- configFile: false,
62
- parserOpts: {
63
- ...babelOptions.config.parserOpts,
64
- sourceFilename: (0, _utils().relativeUrl)(options.projectRoot, asset.filePath),
65
- allowReturnOutsideFunction: true,
66
- strictMode: false,
67
- sourceType: 'module',
68
- plugins: [...(((_babelOptions$config$ = babelOptions.config.parserOpts) === null || _babelOptions$config$ === void 0 ? void 0 : _babelOptions$config$.plugins) ?? []), ...(babelOptions.syntaxPlugins ?? []),
69
- // Applied by preset-env
70
- 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom'
71
- // 'topLevelAwait'
72
- ]
73
- },
74
- caller: {
75
- name: 'parcel',
76
- version: transformerVersion,
77
- targets: JSON.stringify(babelOptions.targets),
78
- outputFormat: asset.env.outputFormat
79
- }
80
- };
81
- if (tracer.enabled) {
82
- config.wrapPluginVisitorMethod = (key, nodeType, fn) => {
83
- return function () {
84
- let pluginKey = key;
85
- if (pluginKey.startsWith(options.projectRoot)) {
86
- pluginKey = _path().default.relative(options.projectRoot, pluginKey);
87
- }
88
- const measurement = tracer.createMeasurement(pluginKey, nodeType, _path().default.relative(options.projectRoot, asset.filePath));
89
- fn.apply(this, arguments);
90
- measurement && measurement.end();
91
- };
92
- };
93
- }
94
- let ast = await asset.getAST();
95
- let res;
96
- if (ast) {
97
- res = await babelCore.transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
98
- } else {
99
- res = await babelCore.transformAsync(await asset.getCode(), config);
100
- if (res.ast) {
101
- let map = await asset.getMap();
102
- if (map) {
103
- (0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
104
- }
105
- }
106
- if (res.externalDependencies) {
107
- for (let f of res.externalDependencies) {
108
- if (!_path().default.isAbsolute(f)) {
109
- opts.logger.warn({
110
- message: (0, _diagnostic().md)`Ignoring non-absolute Babel external dependency: ${f}`,
111
- hints: ['Please report this to the corresponding Babel plugin and/or to Parcel.']
112
- });
113
- } else {
114
- if (await options.inputFS.exists(f)) {
115
- asset.invalidateOnFileChange(f);
116
- } else {
117
- asset.invalidateOnFileCreate({
118
- filePath: f
119
- });
120
- }
121
- }
122
- }
123
- }
124
- }
125
- if (res.ast) {
126
- asset.setAST({
127
- type: 'babel',
128
- version: '7.0.0',
129
- program: res.ast
130
- });
131
- }
132
- }
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.babelErrorEnhancer = babelErrorEnhancer;
7
- async function babelErrorEnhancer(error, asset) {
8
- if (error.loc) {
9
- let start = error.message.startsWith(asset.filePath) ? asset.filePath.length + 1 : 0;
10
- error.message = error.message.slice(start).split('\n')[0].trim();
11
- }
12
- error.source = await asset.getCode();
13
- error.filePath = asset.filePath;
14
- return error;
15
- }